[英]How can I implement "export" command in C?
I am writing a project that implementing "shell" with some commands and I am struggling with the "export" command.我正在编写一个用一些命令实现“shell”的项目,而我正在努力使用“export”命令。 When I use export command it prints some variables.当我使用导出命令时,它会打印一些变量。 But I don know how can get that info's with C.但我不知道如何使用 C 获取该信息。
The portable POSIX way to traverse the environment is through the global variable environ
, which works like argv
but is for environment variables.遍历环境的可移植 POSIX 方法是通过全局变量environ
,它的工作方式与argv
类似,但用于环境变量。 It stores strings of the form foo=bar
, where foo
is an environment variable and bar
is its assigned value.它存储foo=bar
形式的字符串,其中foo
是一个环境变量,而bar
是它的赋值。
In general, you should not modify the strings nor what strings are pointed to, hence I've const
ified the declaration below, but conventionally it's typed as char**
.一般来说,您不应该修改字符串或字符串所指向的内容,因此我已经对下面的声明进行了const
化,但通常它的类型为char**
。
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
extern const char *const *const environ;
int main(void) {
for(size_t i = 0; environ[i]; i++) {
if(puts(environ[i]) == EOF) {
perror("Failed to print environment variable");
exit(EXIT_FAILURE);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.