-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.c
More file actions
51 lines (47 loc) · 773 Bytes
/
execute.c
File metadata and controls
51 lines (47 loc) · 773 Bytes
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
51
#include "shell.h"
/**
* execute - A function that executes a command.
* @av: argument it takes
*
* Return: (-1)
*/
int execute(char *av[])
{
pid_t forkRV;
int status;
if (av == NULL || av[0] == NULL || av[0][0] == '\0')
{
printf("Invalid command.\n");
return (-1);
}
forkRV = fork();
if (forkRV < 0)
{
perror("Fork failed");
return (-1);
}
if (forkRV == 0)
{
if (execvp(av[0], av) == -1)
{
perror("Error");
free_tokens(av);
exit(EXIT_FAILURE);
}
}
else
{
do {
if (waitpid(forkRV, &status, WUNTRACED) == -1)
{
perror("Waitpid failed"); /* waitPID failed */
return (-1);
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
if (WIFEXITED(status))
{
return (WEXITSTATUS(status));
}
}
return (0);
}