c - execvpe 的可移植替代品

标签 c linux libc

execvpe 函数是一个 GNU 扩展。如果我希望我的代码可移植到非 gnu libc,如何将 execvpe 调用更改为可移植?

execvp 不会设置新程序的环境,并且 execle poth 不允许传递数组作为参数,并且不会尝试解析来自 PATH 的可执行文件。

最佳答案

直接更改环境

the POSIX exec() documentation (加粗我的):

... In addition, the following variable, which must be declared by the user if it is to be used directly:

extern char **environ;

is initialized as a pointer to an array of character pointers to the environment strings. The argv and environ arrays are each terminated by a null pointer. The null pointer terminating the argv array is not counted in argc.

Applications can change the entire environment in a single operation by assigning the environ variable to point to an array of character pointers to the new environment strings. ...

所以而不是

exec...e( ...., myNewEnv );

你可以做到

extern char **environ;

   .
   .
   .

environ = myNewEnv;
exec...(...);

关于c - execvpe 的可移植替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67633176/

相关文章:

python - 启动程序不会启动应用程序

rust - C 结构到 Rust 的错误映射

linux - uclinux - 与 libc.so.0 库的链接

c - shm_unlink 说 "no such file or directory"

python - 为 Python 查找最长重复字符串的有效方法(来自 Programming Pearls)

c - Linux 内核 I/O

linux - 以另一个用户身份执行脚本而不是 root

linux - Libc的静态链接

c - C中数字字符的数值

C:让用户一个字符一个字符地填充一个char数组。然后打印出来