-
using getenv correctly
Hi all, I am writing my first programs to gain a better understanding of environment variables.
So far I have written this - and it compiles with no errors and returns successfully returns a value for the 'PATH'. As seen below:
Code:
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *env = getenv("PATH");
if (env)
printf("\nvalue of PATH is: %s\n", env);
else
printf("\nNULL\n");
return 0;
}
However, if change 'PATH' to another environment variable as stated on this list: http://en.kioskea.net/contents/syste...ronnement.php3
such as 'TIME' , then 'NULL' is returned. Basically my question is theory only and I want to know why some environment variables are returned and others are not?. Also I have only a basic understanding of how to use the terminal correctly, so are there commands from the terminal that will show me what environment variables are running?
-
Try with TEMP or TMP
I can imagine TIME (and DATE and RANDOM) being a pseudo-variable evaluated by the shell at the moment you use it.
-
This is more simple than you think: the environment variables are strings "name=value" copied into the process stack when you run it, nothing more. You can get the entire list by walking the 'environ' variable (this is a global pointer to the array of environment variable your process got). You can also see your current env. var in a shell by typing 'env'.
from <unistd.h>:
Code:
/* NULL-terminated array of "NAME=VALUE" environment variables. */
...
extern char **environ;
...
-