-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute_cmd.c
More file actions
36 lines (31 loc) · 816 Bytes
/
execute_cmd.c
File metadata and controls
36 lines (31 loc) · 816 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
#include "main.h"
/**
* execute_cmd - Execute the command
*/
void execute_cmd(tokenized_data data, list_path *head)
{
pid_t pid;
int status;
char *path;
pid = fork();
if (pid < 0) {
perror("fork");
} else if (pid == 0) {
/* Child Process: Attempt execution */
path = _which(data.tokens_array[0], head);
if (path) {
data.tokens_array[0] = path;
if (execve(data.tokens_array[0], data.tokens_array, environ) == -1) {
perror("execve");
}
free(path);
exit(EXIT_FAILURE);
} else {
perror("Command not found");
exit(EXIT_FAILURE);
}
} else {
/* Parent Process: Wait for child */
waitpid(pid, &status, 0);
}
}