execvpe 的便携替代品

Portable alternative to execvpe

execvpe 函数是 GNU 扩展。如果我希望我的代码可移植到非 gnu libcs​​,我怎样才能将 execvpe 调用更改为可移植?

execvp 不设置新程序的环境,并且 execle poth 不允许将数组作为参数传递并且不尝试从 [= 解析可执行文件14=].

直接改environ

Per 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...(...);