-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.c
More file actions
46 lines (43 loc) · 851 Bytes
/
execute.c
File metadata and controls
46 lines (43 loc) · 851 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
#include "shell.h"
/**
* cmd_exec - cmd_exec a command with its entire path variables.
* @data: a pointer to the program's data
* Return: If sucess returns zero, otherwise, return -1.
*/
int cmd_exec(data_of_program *data)
{
int retval = 0, status;
pid_t pidd;
retval = lst_builtins(data);
if (retval != -1)
return (retval);
retval = find_program(data);
if (retval)
{
return (retval);
}
else
{
pidd = fork();
if (pidd == -1)
{
perror(data->command_name);
exit(EXIT_FAILURE);
}
if (pidd == 0)
{
retval = execve(data->tokens[0], data->tokens, data->env);
if (retval == -1)
perror(data->command_name), exit(EXIT_FAILURE);
}
else
{
wait(&status);
if (WIFEXITED(status))
errno = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
errno = 128 + WTERMSIG(status);
}
}
return (0);
}