we can get the environment variable in C like this:
extern char **environ;
int main(int argc, char *argv[])
{
int count = 0;
printf("\n");
while(environ[count] != NULL)
{
printf("[%s] :: ", environ[count]);
count++;
}
return 0;
}
but where is the defination of environ? I can't find that in unistd.h. and how does it work?
extern char **environ; environ is a pointer to a NULL-terminated array of pointers to null-terminated character strings. Each string has the form "name=value" to indicate the name of an environment variable and its current value.
environ is defined as a global variable in the Glibc source file posix/environ.
The variable environ points to an array of pointers to strings called the "environment". The last pointer in this array has the value NULL. This array of strings is made available to the process by the execve(2) call when a new program is started.
environ
is defined as a global variable in the Glibc source file posix/environ.c
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With