Path expansion in execve()

107 Views Asked by At

I followed what was said in a different thread.

I have used execve() with an environment variable PATH, set it to /bin, in order to be able to call commands without using the absolute path. But the function execve() still doesn't recognize the commands such as ls. Only /bin/ls works. What could be the issue?

char *envList[3] = {"HOME=/root", "PATH=/bin", NULL };
execve(command[0], command, envList);
1

There are 1 best solutions below

0
Marco Bonelli On

That's not how the execve syscall works. It will never look at PATH or any other environment variables to search for the executable to execute. You will have to provide a valid absolute or relative path.

Furthermore, setting the environment only affects the new program that is executed, not the current one. So even using the execvpe() library function that emulates the path resolution of a shell looking at PATH, it will look at the current value of the environment variable PATH, not the one being passed as environment to the new process.