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.
The execvp library function mimics the shell search inside
$PATHand then callexecve.If you want on the contrary to execute a program whose file path is known to you (e.g.
/usr/bin/mail) then just callexecvewith the complete path as the first filename argument.