-
Notifications
You must be signed in to change notification settings - Fork 1
/
execute_command.c
188 lines (162 loc) · 3.56 KB
/
execute_command.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "unixshell.h"
/**
* handle_exec- a function that searches for and execute an executable files
*
* @cmd_arg: command line arguments
* @count: the number of processes per loop
* @filename: the name of executable files
* @env: environmental variable
*
* Return: nothing
**/
void handle_exec(char *cmd_arg[], int count, char *filename, char **env)
{
char *path = getenv("PATH");
char *saveptr;
char *path_copy = _strdup(path);
char *dir = _strtok(path_copy, ":", &saveptr);
while (dir != NULL)
{
char *full_path = malloc(_strlen(dir) + _strlen(cmd_arg[0]) + 2);
_strcpy(full_path, dir);
_strcat(full_path, "/");
_strcat(full_path, cmd_arg[0]);
if (access(full_path, X_OK) == 0)
{
execve(full_path, cmd_arg, env);
free(full_path);
break;
}
free(full_path);
dir = _strtok(NULL, ":", &saveptr);
}
free(path_copy);
if (dir == NULL)
{
msgerror(filename, count, cmd_arg);
exit(EXIT_FAILURE);
}
}
/**
* handle_exit- a function that exits the program
*
* @cmd_arg: command line arguments
* @line: the buffer
* @count: the number of processes per loop
* @filename: the name of executable files
*
* Return: nothing
**/
void handle_exit(char *cmd_arg[], char *line, int count, char *filename)
{
if (_strcmp(cmd_arg[0], "exit") == 0)
{
if (cmd_arg[1] != NULL)
{
int status = _atoi(cmd_arg[1]);
if (status != 0)
{
free(line);
exit(status);
}
else
msgerror(filename, count, cmd_arg);
}
free(line);
exit(EXIT_SUCCESS);
}
}
/**
* execute_command- a function that executes commands
*
* @line: the buffer
* @count: the number of processes per loop
* @filename: the name of executable files
* @env: environmental variable
*
* Return: nothing
**/
void execute_command(char *line, int count, char *filename, char **env)
{
char *token, *saveptr, *cmd_arg[BUFSIZ] = {NULL};
int status, i = 0;
pid_t child_pid;
token = _strtok(line, DELIM, &saveptr);
while (token != NULL)
{
cmd_arg[i++] = token;
token = _strtok(NULL, DELIM, &saveptr);
}
cmd_arg[i] = NULL;
if (i == 0)
return;
handle_exit(cmd_arg, line, count, filename);
if (_strcmp(cmd_arg[0], "env") == 0)
print_env(env);
if (_strcmp(cmd_arg[0], "cd") == 0)
{
change_dir(cmd_arg[1]);
return;
}
child_pid = fork();
if (child_pid == 0)
{
if (_strchr(cmd_arg[0], '/') == NULL)
handle_exec(cmd_arg, count, filename, env);
else if (execve(cmd_arg[0], cmd_arg, env) == -1)
{
msgerror(filename, count, cmd_arg);
exit(EXIT_FAILURE);
}
}
else if (child_pid < 0)
msgerror(filename, count, cmd_arg);
else
wait(&status);
}
/**
* _strcpy- copies the string pointed to by src,
* including the terminating null byte to the buffer pointed to by dest.
*
* @dest: destination
* @src: source
* Return: the pointer to dest
*
**/
char *_strcpy(char *dest, char *src)
{
int count = 0;
while (count >= 0)
{
*(dest + count) = *(src + count);
if (*(src + count) == '\0')
break;
count++;
}
return (dest);
}
/**
* _realloc - reallocates a memory block.
* @ptr: pointer to the memory previously allocated.
* @old_size: size, in bytes, of the allocated space of ptr.
* @new_size: new size, in bytes, of the new memory block.
*
* Return: ptr.
* if new_size == old_size, returns ptr without changes.
* if malloc fails, returns NULL.
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
if (ptr == NULL)
ptr = malloc(new_size);
if (new_size == old_size)
return (ptr);
free(ptr);
ptr = malloc(new_size);
return (ptr);
}