-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_cmd.c
More file actions
77 lines (72 loc) · 1.51 KB
/
Copy pathexec_cmd.c
File metadata and controls
77 lines (72 loc) · 1.51 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "main.h"
void run_execve(char **tokens, char **argv, char **env);
/**
* exec_cmd - Execute command
* @tokens: tokenize command
* @argv: argument vector
* @line: string from getline
* @env: environment variable
*
* Return: No return value
*/
void exec_cmd(char **tokens, char **argv, char *line, char **env)
{
if (_strcmp(tokens[0], "exit") == 0 || _strcmp(tokens[0], "cd") == 0 ||
_strcmp(tokens[0], "help") == 0 || _strcmp(tokens[0], "env") == 0)
exec_builtin(tokens, argv, line);
else
{
free(line);
run_execve(tokens, argv, env);
}
}
/**
* run_execve - Run the execution with the execve
* @tokens: the tokenize command
* @argv: argument vector
* @env: environment variable
*
* Return: no return.
*/
void run_execve(char **tokens, char **argv, char **env)
{
pid_t pid;
char *prog_path;
int status;
UNUSED(argv);
prog_path = proc_path(tokens, _getpath("PATH"));
/*comment_handler(prog_path);*/
if (prog_path != NULL)
{
pid = fork();
signal(SIGINT, signal_handler2);
if (pid < 0)
{
free(prog_path), free_token_array(tokens);
perror(argv[0]);
exit(0);
}
if (pid == 0)
{
if (execve(prog_path, tokens, env) == -1)
{
free(prog_path), free_token_array(tokens);
perror(argv[0]);
exit(0);
}
}
else if (pid > 0)
{
do {
signal(SIGINT, signal_handler);
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
}
else
{
free(prog_path);
perror(argv[0]);
}
free(prog_path), free_token_array(tokens);
}