简体   繁体   English

如何检查是否在C程序中设置了环境变量

[英]How to check if environment variable is set from C program

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" . 例如,假设我想要环境变量"DEBUG"

In my bash command prompt, I set DEBUG by typing the command: DEBUG= 在我的bash命令提示符中,通过键入以下命令来设置DEBUGDEBUG=

Then in my C program, I can verify this environment variable is set by printing out all the content of char **environ . 然后在我的C程序中,我可以通过打印出char **environ所有内容来验证是否设置了此环境变量。 DEBUG does show up in this environment printout. DEBUG确实显示在此环境的打印输出中。

However, I don't know how to retrieve this environment variable for conditional checking. 但是,我不知道如何检索此环境变量以进行条件检查。 I've tried using the function getenv like so: 我试过像这样使用功能getenv:

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" : 我什至在类似于"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? 是否有人有经验检查是否从C程序设置了环境变量? I'm having issues even pulling a single environment variable which is preventing me from doing so. 我什至在拉单个环境变量时遇到问题,这使我无法这样做。

getenv returns NULL when the environment variable for which it is asked is not set. 如果未设置要查询的环境变量,则getenv返回NULL 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; 请注意,shell和环境变量之间存在差异。 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到外壳程序中:

export DEBUG=some_value

or 要么

DEBUG=some_value
export DEBUG

It is not enough to just say DEBUG=some_value . 仅仅说DEBUG=some_value是不够的。

You need to make sure that getenv (and printf ) are correctly declared. 您需要确保正确声明了getenv (和printf )。

For getenv , you need: 对于getenv ,您需要:

#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. 如果int与指针的大小相同,则可能会起作用,但是在通常情况下指针为64位而int只有32位的情况下,将指针当作整数来传递将导致其一半的位被丢弃地板,使其几乎无法用作指针。

Always specify -Wall when you compile your code, and make sure you pay attention to the warnings. 编译代码时,请始终指定-Wall ,并确保注意警告。 They are important. 它们很重要。

It's because you are not including stdlib.h and the compiler is assuming getenv() returns int . 这是因为您不包括stdlib.h ,并且编译器假定getenv()返回int

You have two options, you can declare getenv() like 您有两个选择,可以像这样声明getenv()

char *getenv(const char *);

or include stdlib.h , and the same applies for printf() but in that case the header is stdio.h . 或包含stdlib.h ,这同样适用于printf()但在这种情况下,标头为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. 您应该启用编译器警告,在linux上gccclang都支持-Wall -Wextra -Werror ,最重要的一个是-Werror在这种情况下将阻止编译。

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. 我们还必须注意putenv的错误处理。有时它会返回ENOMEM空间不足,无法分配新环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM