-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.c
More file actions
50 lines (46 loc) · 884 Bytes
/
exec.c
File metadata and controls
50 lines (46 loc) · 884 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
#include "main.h"
/**
* execute - execute entered command
* @tokens: commands with it's arguments
* @argv: program arguments
* @env: environment variables
* Return: 1 if success
*/
int execute(char **tokens, char *argv[], char **env)
{
pid_t pid;
int status;
int (*builtin_func)(char **, char **, char **);
if (tokens)
{
builtin_func = is_builtin(tokens[0]);
if (builtin_func)
{
status = exec_builtin(builtin_func, tokens, env, argv);
return (status);
}
pid = fork();
if (pid == 0)
{
if (execve(tokens[0], tokens, env) == -1)
{
print_error(argv[0], NULL);
perror(tokens[0]);
exit(127);
}
}
else if (pid < 0)
{
print_error(argv[0], NULL);
perror(tokens[0]);
exit(127);
}
else
{
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
}
return (status);
}