-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecute.c
63 lines (57 loc) · 1.05 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "main.h"
/**
*execute - execute a program with the given filename and arguments
*@filename: the name of the program to execute
*@exec_arg: array of commands from command line
*
*Return: no return
*/
void execute(char *filename, char **exec_arg)
{
pid_t child_pid;
int status = 0;
child_pid = fork();
if (child_pid == 0)
{
if (execve(filename, exec_arg, environ) == -1)
{
if (access(filename, X_OK) != 0)
{
print_error_exec();
errno = 127;
free_contents(exec_arg);
free(filename);
exit(errno);
}
}
free(filename);
}
else
{
waitpid(child_pid, &status, 0);
if (WIFEXITED(status))
{
exit_status = WEXITSTATUS(status);
errno = exit_status;
}
}
}
/**
*exec_from_path - execute a filname considering its path
* @exec_arg: array of commands from command line
*
*Return: no return
*/
void exec_from_path(char **exec_arg)
{
char *filename;
filename = concat_path(exec_arg);
if (access(exec_arg[0], F_OK) == 0)
{
execute(exec_arg[0], exec_arg);
}
else
{
execute(filename, exec_arg);
}
}