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
andenviron
arrays are each terminated by a null pointer. The null pointer terminating theargv
array is not counted inargc
.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/