-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.c
127 lines (119 loc) · 2.41 KB
/
exec.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "main.h"
/**
* get_input_line - get user input from stdin
*
* @status: return value of the last command executed
*
* Return: the user input line
*/
char *get_input_line(int status)
{
char *line = NULL;
size_t bufsize = 0;
if (getline(&line, &bufsize, stdin) == -1)
{
free(line);
/*
* if the last command succeed return 0
* otherwise return 127
*/
if (status == 1)
exit(EXIT_SUCCESS);
else
exit(status);
}
return (line);
}
/**
* insertArgument - a function that fill the informations in the struct
* @dest: array
* @arg: array of args
* Return: void
*/
void insertArgument(char **dest, char *arg)
{
int i = 0;
while (*(dest + i) != NULL)
i++;
*(dest + i) = arg;
}
/**
* **build_args - a function that return args
* @cmd_line: command line caught by the getline cmd
* Return: an array of built args
*/
char **build_args(char *cmd_line)
{
int i = 0;
int token_size = TOKENS_SIZE;
char *p;
char **args = malloc(sizeof(char *) * TOKENS_SIZE);
if (args == NULL)
return (NULL);
for (i = 0; i < token_size; i++)
args[i] = NULL;
p = strtok(cmd_line, DELIMITER);
while (p != NULL)
{
insertArgument(args, p);
p = strtok(NULL, DELIMITER);
i++;
if (i > token_size)
{
args = _realloc(args, token_size, token_size * 2);
if (args == NULL)
return (NULL);
token_size *= 2;
}
}
return (args);
}
/**
* exec_cmd - a function that execute a simple command
* @args : command arguments
* @argv: list of shell arguments
* @cmd_line: command line
* @ncmd: number of the command to execute
* Return: status
*/
int exec_cmd(char **args, char **argv, char *cmd_line, unsigned int ncmd)
{
int rvalue, wstatus, builtin_status;
pid_t cpid;
char *cmd_path = NULL;
if (!args[0])
return (1);
builtin_status = get_built_in(argv, args, cmd_line, ncmd);
if (builtin_status == 0)
{
cmd_path = search_in_path(args[0]);
if (!cmd_path)
print_error(argv[0], args[0], ncmd);
else
{
cpid = fork();
if (cpid == -1)
perror(args[0]);
if (cpid == 0)
{
if (*cmd_path == '.')
rvalue = execve(args[0], args, environ);
else
rvalue = execve(cmd_path, args, environ);
if (rvalue == -1)
perror(args[0]);
}
else
{
do {
waitpid(cpid, &wstatus, WUNTRACED);
} while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
free(cmd_path);
if (WEXITSTATUS(wstatus))
return (2);
return (1);
}
}
}
return (127);
}