-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.c
More file actions
51 lines (49 loc) · 695 Bytes
/
exec.c
File metadata and controls
51 lines (49 loc) · 695 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 "myshell.h"
/**
* cmd_excuter - Execute cmd
* @cmd: command to be executed
* @argv: command line argument
* Return: exist status
*/
int cmd_excuter(char **cmd, char *argv)
{
char *cm;
int i, status;
pid_t pid = fork();
if (pid == -1)
{
perror(argv);
exit(EXIT_FAILURE);
}
if (pid == 0)
{
cm = cmd[0];
if(cm == NULL)
{
for (i = 0; cmd[i]; i++)
free(cmd[i]);
free(cmd);
cmd = NULL;
perror(argv);
exit(EXIT_FAILURE);
}
if (execve(cm, cmd, environ) == -1)
{
for (i = 0; cmd[i]; i++)
free(cmd[i]);
free(cmd);
cmd = NULL;
perror(argv);
exit(EXIT_FAILURE);
}
}
else
{
waitpid(pid, &status, 0);
for (i = 0; cmd[i]; i++)
free(cmd[i]);
free(cmd);
cmd = NULL;
}
return (WEXITSTATUS(status));
}