-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.c
More file actions
68 lines (64 loc) · 1.34 KB
/
execute.c
File metadata and controls
68 lines (64 loc) · 1.34 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
#include "headers.h"
#include "execute.h"
void fg(char ** args) {
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0) {
if (execvp(args[0], args) < 0) {
printf("Error: No such command\n");
}
exit(0);
}
else if (pid < 0) {
printf("Error: No such command");
return;
}
else {
strcpy(running.name, args[0]);
running.pid = pid;
int status;
waitpid(pid, &status, WUNTRACED);
fflush(stdout);
}
}
void bg (char ** command) {
int fd, status, f;
f = fork();
if (f == 0) {
setpgid(0, 0);
fd = execvp(command[0], command);
if (fd == -1) {
perror("Error");
return;
}
fflush(stdout);
exit(0);
}
else if (f > 0) {
jobinfo[jobs].pid = f;
strcpy(jobinfo[jobs].name, command[0]);
jobs++;
}
else {
perror("Error");
}
}
void execute (char * commands) {
char temp[10000];
strcpy(temp, commands);
int i = 0;
char * commandslist[50];
while (commands) {
commandslist[i] = commands;
i++;
commands = strtok(NULL, " ");
}
commandslist[i] = NULL;
if (strcmp(commandslist[i - 1], "&") == 0) {
bg(commandslist);
}
else {
fg(commandslist);
}
}