gpt4 book ai didi

c - 如何在从该 C 程序执行 C 程序的 shell 中更改环境变量?

转载 作者:太空宇宙 更新时间:2023-11-04 02:03:46 26 4
gpt4 key购买 nike

我想在 C 程序中更改 PATH 变量的值,然后在运行该程序的 shell 中查看更改后的值。

做这样的事情,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main () {

char *path = getenv ("PATH");
printf ("%s\n\n", path);
setenv ("PATH", strcat (path, ":~/myNewPath/"), 1);
printf ("%s\n\n", path);

int pid = fork ();
if (pid == -1)
abort ();
if (pid == 0) {

} else {
// use execlp? how? source? any hints?
}

return 0;
}

如果我在 exec* 系统调用中使用 source 命令。在 shell 中向后更新此 PATH 变量的语法是什么?

最佳答案

这是不可能的。子进程无法更改其父进程的环境变量。

要了解为什么这是不可能的,请查看 execve 的签名

int execve(const char *program, char *const *argv, char *const *envp);

它与 Unix 系统上maintrue 签名配对

int main(int argc, char **argv, char **envp);

也许您开始明白,就内核而言,环境变量是第二组命令行参数。它们似乎可以通过getenvsetenv 等独立访问,并且似乎从父子继承,是由 C 库维护的一种错觉。

有关其工作原理的更多详细信息,请研究 the x86-64 ELF ABI specification, section 3.4.1 "Initial Stack and Register State"特别注意图 3.9,它显示了 execve 复制到新创建的堆栈上的数据布局。 (链接的文档特定于一种 CPU 架构,但这种工作方式在现代 Unix 中通常是一致的;细节当然会因 CPU 和 OS 的不同而有所不同。)

关于c - 如何在从该 C 程序执行 C 程序的 shell 中更改环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23183117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com