Reputation: 114
So I am working on a small project and I have this function:
void call_execve(char *cmd)
{
int i;
char bin[5];
char full_cmd[100];
strcpy(bin, "/bin/");
strcat(full_cmd, bin);
strcat(full_cmd, cmd);
if(fork() == 0) {
i = execve(full_cmd, my_argv, my_envp);
if(i < 0) {
printf("%s: %s\n", full_cmd, "command not found");
exit(1);
}
} else {
wait(NULL);
}
}
I guess my brain just isn't thinking tonight, I need to make it check the /usr/bin directory for the program or shell command.
I know I can use the $PATH variable, but I am unsure how to work with it via the C Language. It would be great if you could re-write this function to either execute the program from the /usr/bin directory or use $PATH to execute the program
Thank you in advance.
GeissT.
Upvotes: 0
Views: 93
Reputation: 1
The execvp library function mimics the shell search inside $PATH
and then call execve
.
If you want on the contrary to execute a program whose file path is known to you (e.g. /usr/bin/mail
) then just call execve
with the complete path as the first filename argument.
Upvotes: 0
Reputation: 799390
From the exec(3)
man page:
The
execlp()
,execvp()
, andexecvpe()
functions duplicate the actions of the shell in searching for an executable file if the specified file‐ name does not contain a slash (/) character. The file is sought in the colon-separated list of directory pathnames specified in thePATH
environment variable.
Upvotes: 1