在UNIX中使用C列出环境变量

时间:2021-01-03 23:03:29

Is there a way to enumerate environment variables and retrieve values using C?

有没有办法枚举环境变量并使用C检索值?

3 个解决方案

#1


28  

Take a look at the environ global variable.

看一下environ全局变量。

extern char **environ;

It might be defined in unistd.h (take a look at the environ (5) man page above).

它可能在unistd.h中定义(请参阅上面的environ(5)手册页)。

Here's a little code demo I wrote:

这是我写的一个小代码演示:

#include <stdio.h>
extern char **environ;

int main()
{
    for (char **env = environ; *env; ++env)
        printf("%s\n", *env);
}

Here's how to use it:

以下是如何使用它:

matt@stanley:~/Desktop$ make enumenv CFLAGS=-std=c99
cc -std=c99    enumenv.c   -o enumenv
matt@stanley:~/Desktop$ ./enumenv 
ORBIT_SOCKETDIR=/tmp/orbit-matt
SSH_AGENT_PID=1474
TERM=xterm
SHELL=/bin/bash
... (so forth)

#2


9  

The environment information can be passed as an extra parameter to main. I don't know if it is compliant or not, but it definitely works (tested on Ubuntu). Just define the extra argument and its an array of char pointers terminated by a NULL pointer. The following will print out the lot.

环境信息可以作为额外参数传递给main。我不知道它是否合规,但它确实有效(在Ubuntu上测试)。只需定义额外参数及其由NULL指针终止的char指针数组。以下将打印出该批次。

#include <stdio>

int main(int argc, char *argv[], char *envp[])
{
  int index = 0;
  while (envp[index])
    printf("%s\n", envp[index++];
}

#3


2  

There is a demo in the book "The Linux Programming Interface" at page 127.

第127页的“Linux编程接口”一书中有一个演示。

Listing 6-3: Displaying the process environment ––––––––––––––––––––––––––––––––––––––––––––––––proc/display_env.c

清单6-3:显示进程环境----------------------------------------- ------- PROC / display_env.c

#include "tlpi_hdr.h"

extern char **environ;

int
main(int argc, char *argv[])
{
    char **ep;
    for (ep = environ; *ep != NULL; ep++)
        puts(*ep);
    exit(EXIT_SUCCESS);
}

#1


28  

Take a look at the environ global variable.

看一下environ全局变量。

extern char **environ;

It might be defined in unistd.h (take a look at the environ (5) man page above).

它可能在unistd.h中定义(请参阅上面的environ(5)手册页)。

Here's a little code demo I wrote:

这是我写的一个小代码演示:

#include <stdio.h>
extern char **environ;

int main()
{
    for (char **env = environ; *env; ++env)
        printf("%s\n", *env);
}

Here's how to use it:

以下是如何使用它:

matt@stanley:~/Desktop$ make enumenv CFLAGS=-std=c99
cc -std=c99    enumenv.c   -o enumenv
matt@stanley:~/Desktop$ ./enumenv 
ORBIT_SOCKETDIR=/tmp/orbit-matt
SSH_AGENT_PID=1474
TERM=xterm
SHELL=/bin/bash
... (so forth)

#2


9  

The environment information can be passed as an extra parameter to main. I don't know if it is compliant or not, but it definitely works (tested on Ubuntu). Just define the extra argument and its an array of char pointers terminated by a NULL pointer. The following will print out the lot.

环境信息可以作为额外参数传递给main。我不知道它是否合规,但它确实有效(在Ubuntu上测试)。只需定义额外参数及其由NULL指针终止的char指针数组。以下将打印出该批次。

#include <stdio>

int main(int argc, char *argv[], char *envp[])
{
  int index = 0;
  while (envp[index])
    printf("%s\n", envp[index++];
}

#3


2  

There is a demo in the book "The Linux Programming Interface" at page 127.

第127页的“Linux编程接口”一书中有一个演示。

Listing 6-3: Displaying the process environment ––––––––––––––––––––––––––––––––––––––––––––––––proc/display_env.c

清单6-3:显示进程环境----------------------------------------- ------- PROC / display_env.c

#include "tlpi_hdr.h"

extern char **environ;

int
main(int argc, char *argv[])
{
    char **ep;
    for (ep = environ; *ep != NULL; ep++)
        puts(*ep);
    exit(EXIT_SUCCESS);
}