In a C
program, I need to find the OSTYPE
during runtime, on the basis of which I will do some operations.
Here is the code
#include <stdlib.h>
#include <string.h>
int main () {
const char * ostype = getenv("OSTYPE");
if (strcasecmp(ostype, /* insert os name */) == 0) ...
return 0;
}
But getenv
returns NULL
(and there is segmentation fault). When I do a echo $OSTYPE
in the terminal it prints darwin15
. But when I do env | grep OSTYPE
nothing gets printed, which means it is not in the list of environment variables. To make it work on my local machine I can go to the .bash_profile
and export the OSTYPE
manually but that doesn't solve the problem if I want to run a generated executable on a new machine.
Why is OSTYPE
available while running terminal, but apparently not there in the list of environment variables. How to get around this ?