-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforkloop.c
228 lines (213 loc) · 6.75 KB
/
forkloop.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
void parse(char *cmdline, struct command *cmd) {
printf("\nparsing %s\n", cmdline);
// inilize
cmd->argc=0;
printf("is this readonly\n");
char *tok;
// check allspace
if (allspace(cmdline)==1) {
// cmd->argv[cmd->argc] = NULL; // mark the end
} else {
// sep line via space
while (strstr(cmdline, " ")) {
tok = strsep(&cmdline, " ");
printf("tok:%s|\n", tok);
if(strcmp(tok, "")!=0 && strcmp(tok, "\t")!=0) {
cmd->argv[cmd->argc] = tok;
cmd->argc++;
} else {
// printf("isspace\n");
}
}
// add last one
tok = cmdline;
cmdline = "";
if(strcmp(tok, "")!=0 && strcmp(tok, "\t")!=0) {
cmd->argv[cmd->argc] = tok;
cmd->argc++;
} else {
// printf("isspace\n");
}
cmd->argv[cmd->argc] = NULL; // mark the end
// check info
if (cmd->argc!=0) {
printf("cmd->argc: %d | ", cmd->argc);
printf("cmd->argv: ");
for (int i=0; i<cmd->argc; i++) printf("%s, ", cmd->argv[i]);
printf("\n");
} else {
// printf("cmd->argc: %d\n", cmd->argc);
}
}
}
int translate(struct command *cmd) {
// check if cmd->argv[0]) is a system call
// if not then print error message
// if yes concatenate it with the path
// printf("syscall check for %s\n", cmd->argv[0]);
for (int i=0; i<pathsize; i++) {
char *buf;
buf = (char *)malloc(MAXBUFR * sizeof(char));
strcpy(buf, "");
strcat(buf, path[i]); // /bin
strcat(buf, "/"); // /bin/
strcat(buf, cmd->argv[0]); // /bin/ls
if (access(buf, X_OK)!=-1) { // path[i] work
cmd->alterpath = (char *)malloc(PATHBUFR * sizeof(char));
cmd->alterpath = buf; // replace i cmd->argv[0]
return 1;
}
}
// printf("not a system call\n");
return 0;
}
char *args[3];
if(strcmp(cmd->argv[rdrc], ">")==0) { // rdrc = index of ">" in cmd->argv
close(STDOUT_FILENO);
open(cmd->argv[rdrc+1], O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU); // rdrc+1 = index of file
args[0] = cmd->alterpath;
if (rdrc==1) args[1] = NULL;
else if (rdrc==2) {
args[1] = cmd->argv[1];
args[2] = NULL;
}
while (looptime>0) {
childPid = fork();
if (childPid<0) {
write(STDERR_FILENO, error_message, strlen(error_message));
}
else if (childPid==0) {
// execv(args[0], args);
runCommand(loop, isSyscall, rdrc);
}
else { // parent where the shell continues
printf("Child in background [%d]\n",childPid);
wait(&childPid);
}
looptime--;
}
while (looptime>0) {
childPid = fork();
if (childPid<0) {
write(STDERR_FILENO, error_message, strlen(error_message));
}
else if (childPid==0) {
// execv(args[0], args);
runCommand(loop, isSyscall, rdrc);
}
else { // parent where the shell continues
printf("Child in background [%d]\n",childPid);
wait(&childPid);
}
// do the runCommand?
if (isSyscall==1) {
childPid = fork();
// printf("\nchildPid: %d\n", childPid);
if (childPid<0) {
write(STDERR_FILENO, error_message, strlen(error_message));
}
else if (childPid==0) { // child is here
// check if redirection
if (rdrc!=0) { // rdrc=index of ">" in cmd->argv
close(STDOUT_FILENO);
open(loop->argv[rdrc+1], O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU);
char * args[3];
args[0] = loop->argv[0];
args[1] = loop->argv[1];
args[2] = NULL;
if (execv(args[0], args) < 0) {
printf("%s: Command not found\n", args[0]);
write(STDERR_FILENO, error_message, strlen(error_message));
exit(1);
}
}else {
if (execv(loop->argv[0], loop->argv) < 0) {
printf("%s: Command not found\n", cmd->argv[0]);
write(STDERR_FILENO, error_message, strlen(error_message));
exit(1);
}
}
}
else { // parent where the shell continues
printf("Child in background [%d]\n",childPid);
wait(&childPid);
}
}
looptime--;
printf("looptime remains: %d\n", looptime);
}
// check
// printf("argc: %d\n", argc);
// for (int i=0; i<argc; i++) {
// // strcat(loopbuf, cmd->argv[i]);
// printf("args[%d]: %s\n", i, args[i]);
// // if (i!=cmd->argc-1) strcat(loopbuf, " ");
// }
args[argc] = NULL;
// // reconstruct cmd with space
// char *loopbuf;
// loopbuf = (char *)malloc(MAXBUFR * sizeof(char));
// // printf("%d\n", strlen(loopbuf));
// // for (int i=0; i <100; i++) loopbuf[i] = '\0';
// // printf("container: %s\n", loopbuf);
// strcpy(loopbuf, "");
// for (int i=2; i<cmd->argc; i++) {
// strcat(loopbuf, cmd->argv[i]);
// if (i!=cmd->argc-1) strcat(loopbuf, " ");
// }
// printf("container: %s\n", loopbuf);
// // create another sturct to rec
// struct command *loop;
// parse(loopbuf, loop);
// printf("manully parse without struct\n");
// no need to parse becasue argv exists
// isSyscall = translate(loop);
// printf("manully translate without struct\n");
// no need to translate since argv[0] must be syscall
// parse and save
// loopargc = cmd->argc-2; // waive 0 1
// char* loopargs[loopargc];
// printf("cmd->argc: %d, loopargc: %d\n", cmd->argc, loopargc);
// for (int i=0; i<loopargc; i++) {
// loopargs[i] = cmd->argv[i+2]; // echo, hello, $loop
// }
// loopargs[loopargc] = NULL;
// loop->argc = cmd->argc-2; // waive 0 1
// for(int i = 0; i < loop->argc; i++) {
// loop->argv[i] = cmd->argv[i+2];
// }
// loop->argv[loop->argc] = NULL;
// // check
// printf("argc: %d\n", loop->argc);
// for (int i=0; i<loop->argc; i++) {
// printf("argv[%d]: %s\n", i,loop->argv[i]);
// }
// // create a struct
// struct command *loop;
// loop->argc = loopargc;
// // loop->argv=loopargs; will not work
// // copy the array
// // loop->argv = malloc(47 * sizeof(char*));
// for(int i = 0; i < loopargc; i++) {
// loop->argv[i] = loopargs[i];
// }
// // check
// printf("looptime: %d\n", loop->argc);
// for (int i=0; i<loop->argc; i++) {
// printf("argv[%d]: %s\n", i,loop->argv[i]);
// }
strcpy(buf, "");
for (int i=0; i<loopargc; i++) {
strcat(buf, loopargs[i]);
strcat(buf, " ");
}
buf[strlen(buf)-1] = '\0'; // remove " " from input
// printf("%s\n", buf);
// printf("\n");
// isSyscall = translate(loop);
// rdrc = checkredirection(loop);
while (looptime>0){
system(buf);
// system("echo hello");
looptime--;
}