The setbuf
command sets one or more environmental variables, then attempts in an attempt to cause athe command given as a positional parameter to use a different library. The functions in this library then act upon one or more of these environmental variables to change the buffering. SIP prevents commands stored in protected directories from being able to use libraries other than the ones specified when the command was created.
What is happening here is that grep
is using the functions in libstdbuf.so
. These functions are using the variable _STDBUF_O
to determine that no buffer is to be used.
To see which additional environmental variables are being passed to grep
in the above example, I created the file name myenv.c
containing the following source code.
#include <stdio.h>
int main(int argc, char *argv[], char *envp[]) {
while (*envp) printf("%s\n",*envp++);
return 0;
}
I used the gcc
command shown below to create the executable command myenv
. This myenv
command prints out the environmental variables.
gcc myenv.c -o myenv
To see which additional environmental variables are being passed, I entered the following command. The output is also shown below.
% diff <(./myenv) <(stdbuf -o0 ./myenv)
0a1,2
> DYLD_INSERT_LIBRARIES=/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so
> _STDBUF_O=0
24c26,27
< _=/Users/davidanderson/myenv/./myenv
---
> _=/usr/local/bin/stdbuf
> DYLD_FORCE_FLAT_NAMESPACE=y
This output shows DYLD_FORCE_FLAT_NAMESPACE
as an additional environmental variable, which I did not include in the above example. I do not think this variable is needed for macOS Monterey and newer versions of macOS, since the variable is not defined in the output of the man dyld
command. However, I did find the DYLD_FORCE_FLAT_NAMESPACE
variable defined in the man dyld
command output for macOS Catalina and macOS High Sierra. So older versions of macOS and OS X may need to have this variable defined.
References
- The
setbuf.c
source code on GitHub
- The man page for
dyld(1)