-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_exec.c
More file actions
69 lines (65 loc) · 1.13 KB
/
shell_exec.c
File metadata and controls
69 lines (65 loc) · 1.13 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
#include "shell.h"
/**
* struct builtin - yeah
* struct builtin builtins - yeah
* Description: funcion selecter
*/
struct builtin builtins[] = {
/*{"help", _help},*/
{"/bin/exit", My_exit},
/*{"cd", _cd},*/
};
/**
* num_of_builtins - number of builtins
* Return: integer
*/
int num_of_builtins(void)
{
return (sizeof(builtins) / sizeof(struct builtin));
}
/**
* shell_exec - executes commands
* @args: command and arguments
*/
void shell_exec(char **args)
{
pid_t child_pid;
int i;
/*for (i = 0; i < num_of_builtins(); i++)
{
if (strcmp(args[0], builtins[i].name) == 0)
{
builtins[i].func(args);
return;
}
}*/
if (access(args[0], F_OK) == -1)
{
for (i = 0; i < num_of_builtins(); i++)
{
if (strcmp(args[0], builtins[i].name) == 0)
{
builtins[i].func(args);
return;
}
}
perror(args[0]);
return;
}
child_pid = fork();
if (child_pid == 0)
{
execve(args[0], args, environ);
perror("Error");
exit(1);
}
else if (child_pid > 0)
{
int status;
do {
waitpid(child_pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
else
perror("Error");
}