-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_execute.c
54 lines (49 loc) · 803 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
51
52
53
54
#include "main.h"
/**
* _execute - executes a command
* @cmd: command to execute
* @argv: array of arguments
* @idx: index of command
* Return: 0 on success, 127 on failure
*
*/
int _execute(char **cmd, char **argv, int idx)
{
pid_t cld;
int wstatus;
char *fullCmd;
fullCmd = get_path(cmd[0]);
if (!fullCmd)
{
my_error(argv[0], cmd[0], idx);
free_str(cmd);
return (127);
}
cld = fork();
if (cld == -1)
{
perror("fork");
return (-1);
}
if (cld == 0)
{
if (execve(fullCmd, cmd, environ) == -1)
{
free(fullCmd), fullCmd = NULL;
free_str(cmd);
exit(1);
}
}
else
{
if (waitpid(cld, &wstatus, 0) == -1)
{
perror("waitpid");
free_str(cmd);
return (-1);
}
free_str(cmd);
free(fullCmd), fullCmd = NULL;
}
return (WEXITSTATUS(wstatus));
}