-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmybash.c
More file actions
91 lines (74 loc) · 1.78 KB
/
mybash.c
File metadata and controls
91 lines (74 loc) · 1.78 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
const char* PROMPT = "mybash> ";
int isWhitespace(const char c) {
return c == ' ' || c == '\t' || c == '\n';
}
void readCommand(char* args[50], int* argsCount, int* backgroundMode) {
printf("%s", PROMPT);
fflush(stdout);
*argsCount = 0;
*backgroundMode = 0;
char cmd[4096];
int bytesRead = read(0, cmd, 4096);
int i;
for (i = 0; i < bytesRead; i++) {
if (!isWhitespace(cmd[i])) {
if (i == 0 || isWhitespace(cmd[i - 1])) {
args[(*argsCount)++] = cmd + i;
}
}
if (i > 0 && isWhitespace(cmd[i - 1])) {
cmd[i - 1] = '\0';
}
}
cmd[bytesRead - 1] = '\0';
args[*argsCount] = NULL;
if (!strcmp(args[(*argsCount) - 1], "&")) {
*backgroundMode = 1;
(*argsCount)--;
args[*argsCount] = NULL;
}
}
int executeCommand(char* args[50], int argsCount, int backgroundMode) {
int pid = fork();
if (pid == -1) {
printf("Could not fork\n");
return -1;
}
if (!pid) {
// child
int result = execvp(args[0], args);
printf("Command %s not found\n", args[0]);
exit(result);
}
else {
// parent
if (!backgroundMode) {
int status;
wait(&status);
// if (status == 0) {
// printf("Command %s finished with status %d\n", args[0], status);
// }
return status;
}
}
}
int main() {
while (1) {
char* args[50];
int argsCount, backgroundMode;
readCommand(args, &argsCount, &backgroundMode);
if (argsCount == 1 && !strcmp(args[0], "bye")) {
printf("bye\n");
return 0;
}
if (argsCount > 0) {
executeCommand(args, argsCount, backgroundMode);
}
}
}