-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.c
47 lines (45 loc) · 1.19 KB
/
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
#include "simpleshell.h"
/**
* execute - execute a command with its entire path variables.
* @data: a pointer to the program's data
* Return: If sucess returns zero, otherwise, return -1.
*/
int execute(data_of_program *data)
{
int retval = 0, status;
pid_t pidd;
/* check for program in built ins */
retval = builtins_list(data);
if (retval != -1)/* if program was found in built ins */
return (retval);
/* check for program file system */
retval = find_program(data);
if (retval)
{/* if program not found */
return (retval);
}
else
{/* if program was found */
pidd = fork(); /* create a child process */
if (pidd == -1)
{ /* if the fork call failed */
perror(data->command_name);
exit(EXIT_FAILURE);
}
if (pidd == 0)
{/* I am the child process, I execute the program*/
retval = execve(data->tokens[0], data->tokens, data->env);
if (retval == -1) /* if error when execve*/
perror(data->command_name), exit(EXIT_FAILURE);
}
else
{/* I am the father, I wait and check the exit status of the child */
wait(&status);
if (WIFEXITED(status))
errno = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
errno = 128 + WTERMSIG(status);
}
}
return (0);
}