-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecVP.c
More file actions
56 lines (55 loc) · 1.23 KB
/
execVP.c
File metadata and controls
56 lines (55 loc) · 1.23 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
#include "./include/headers.h"
void executeChildProcess(char **commandArray)
{
int check = execvp(commandArray[0], commandArray);
if (check == -1)
{
fprintf(stderr,RED"%s is invalid command!\n"RESET, commandArray[0]);
}
return;
}
void execVP(char* com,int bg)
{
int i = 0;
char **array = (char **)malloc(sizeof(char *) * 100);
for (int i = 0; i < 100; i++)
array[i] = (char *)malloc(sizeof(char) * 100);
char t[100];
char *token;
char *ptr_out = NULL;
char del[] = " \t\f\v\r\n";
token = NULL;
ptr_out = NULL;
strcpy(t, com);
token = __strtok_r(t, del, &ptr_out);
i = 0;
while (token != NULL)
{
strcpy(array[i++], token);
token = __strtok_r(NULL, del, &ptr_out);
}
array[i] = NULL;
int child = fork();
int status;
if (child == 0)
{
executeChildProcess(array);
exit(0);
}
else if (child < 0)
{
fprintf(stderr,RED"fork failed!\n"RESET);
}
else
{
if (bg==1)
{
printf("[%d]\n", child);
insertcommandinrecord(child, array[0]);
}
else
waitpid(child, &status, 0);
}
destroystringlist(array, 100);
return;
}