-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexecute.c
50 lines (47 loc) · 982 Bytes
/
execute.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "shell.h"
/**
* execute - Execute a command with arguments.
* @argv: An array of strings containing the command and its arguments.
*
* Return: The exit status of the executed command.
*/
int execute(char **argv)
{
pid_t id;
int status = 0;
char *cmd_path, *envp[2];
if (argv == NULL || *argv == NULL)
return (status);
if (check_for_builtin(argv))
return (status);
id = fork();
if (id < 0)
{
_puterror("fork");
return (1);
}
if (id == -1)
perror(argv[0]), free_tokens(argv), free_last_input();
if (id == 0)
{
envp[0] = get_path();
envp[1] = NULL;
cmd_path = NULL;
if (argv[0][0] != '/')
cmd_path = find_in_path(argv[0]);
if (cmd_path == NULL)
cmd_path = argv[0];
if (execve(cmd_path, argv, envp) == -1)
{
perror(argv[0]), free_tokens(argv), free_last_input();
exit(EXIT_FAILURE);
}
}
else
{
do {
waitpid(id, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return (status);
}