Reputation: 53
I'm currently working on a program, and I would like to print special output if an environment variable is set.
For example, suppose I want environment variable "DEBUG"
.
In my bash command prompt, I set DEBUG
by typing the command:
DEBUG=
Then in my C program, I can verify this environment variable is set by printing out all the content of char **environ
. DEBUG
does show up in this environment printout.
However, I don't know how to retrieve this environment variable for conditional checking. I've tried using the function getenv like so:
getenv("DEBUG")
If I were to try to print out this output like below I get a seg fault:
printf("get env: %s\n", getenv("DEBUG"));
I even tried this on a known environment variable like "HOME"
:
printf("get env: %s\n", getenv("HOME"));
which still produces a seg fault.
Does any one have any experience checking if an environment variable is set from a C program? I'm having issues even pulling a single environment variable which is preventing me from doing so.
Upvotes: 3
Views: 20817
Reputation: 301
code snippet :
if(NULL == getenv("TIME_ELAPSED"))
{
putenv("TIME_ELAPSED=1");
}
we have to take care of error handling for putenv also .Sometimes it returns ENOMEM Insufficient space to allocate new environment.
Upvotes: 1
Reputation: 241721
You need to make sure that getenv
(and printf
) are correctly declared.
For getenv
, you need:
#include <stdlib.h>
If you don't declare it, segfaults are likely when you call it. If you were to get that far, trying to use the value it returns would probably also segfault.
Undeclared functions are handled as though they were declared to accept either integer or double arguments (depending on what is provided) and as though they return integers. If int
is the same size as a pointer, that might work, but in the common case where pointers are 64 bits but ints are only 32, passing a pointer as though it were an integer will result in half of its bits being dropped on the floor, making it pretty much unusable as a pointer.
Always specify -Wall
when you compile your code, and make sure you pay attention to the warnings. They are important.
Upvotes: 3
Reputation: 53006
It's because you are not including stdlib.h
and the compiler is assuming getenv()
returns int
.
You have two options, you can declare getenv()
like
char *getenv(const char *);
or include stdlib.h
, and the same applies for printf()
but in that case the header is stdio.h
.
You should enable compiler warning, on linux gcc
and clang
both support -Wall -Wextra -Werror
, the most important one, -Werror
will prevent compilation in this case.
Upvotes: 3
Reputation: 44033
getenv
returns NULL
when the environment variable for which it is asked is not set. Your check could thus simply be
if(getenv("DEBUG")) {
// DEBUG is set
} else {
// DEBUG is not set
}
Note that there is a difference between shell and environment variables; if you want a variable to show up in the environment of a shell's subprocess, you have to export
it in the shell:
export DEBUG=some_value
or
DEBUG=some_value
export DEBUG
It is not enough to just say DEBUG=some_value
.
Upvotes: 8