-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsh.c
More file actions
280 lines (235 loc) · 6.67 KB
/
sh.c
File metadata and controls
280 lines (235 loc) · 6.67 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "ucode.c"
#define BLK 1024
char *name[16];
int nk;
char buf[1024];
void help()
{
print2f("~~~~~~~~~~~~~~Assistance~~~~~~~~~~~~~~\n\r");
print2f(" ls cd pwd cat cp mv ps \n\r");
print2f(" mkdir rmdir creat rm chmod more grep \n\r");
print2f(" > >> < | \n\r");
print2f("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\r");
}
int main(int argc, char *argv[]) //~~~~start here~~~~
{
int pid, status, i;
char buf[256], tempBuf[256], *cp, *cq;
while (1)
{
printf("sh %d# ", getpid()); //prints shell number
gets(buf); //gets command lines
if (buf[0] == 0) //if buf is empty repeat loop
continue;
cp = buf; //set cp to buf
while (*cp == ' ') //skip leading blanks
cp++;
cq = cp;
while (*cq) //zero our trailing blanks
cq++;
cq--;
while (*cq == ' ')
{
*cq = 0;
cq--;
}
if (strcmp(cp, "") == 0) // if nothing or a bunch of spaces
continue; // repeat the while loop
strcpy(buf, cp);
strcpy(tempBuf, buf);
nk = eatpath(tempBuf, name); //eatpath separates words and puts it in name char array, pass tempbuf so we dont fuck up buf
if (strcmp(name[0], "cd") == 0)
{
if (name[1] == 0)
chdir("/");
else
chdir(name[1]);
continue;
}
if (strcmp(name[0], "pwd") == 0)
{
pwd();
continue;
}
if (strcmp(name[0], "echo") == 0)
{
for (i = 1; i < nk; i++)
{
printf("%s ", name[i]);
}
continue;
}
/* chname must be done by sh itself */
if (strcmp(name[0], "chname") == 0)
{
chname(name[1]);
continue;
}
if (strcmp(name[0], "help") == 0)
{
help();
continue;
}
if (strcmp(name[0], "logout") == 0)
{
print2f("~GO AWAY!~\n\r");
chdir("/");
exit(0);
}
if (strcmp(name[0], "exit") == 0)
{
exit(0);
continue;
}
printf("parent sh %d: fork a child\n", getpid()); //if command is not trivial command forks a child
pid = fork(); /* sh forks child */
if (pid)
{ /* parent sh */ //parents gonna wait
printf("parent sh %d: wait for child %d to die\n", getpid(), pid);
pid = wait(&status);
printf("sh %d: child %d exit status = %x\n", getpid(), pid, status);
continue; //once parent dies continue
}
else //childs gonna go here
{
printf("child sh %d running : cmd=%s\n", getpid(), buf);
do_pipe(buf, 0); //pass commands/make sure cmd is empty
}
}
}
//scan breaks up buf = "head | tail" into "head" "tail"
int scan(char *buf, char **tail)
{
char *p;
p = buf; //point p to buf
*tail = 0; //set tail to 0
while (*p) //get to end of buf
p++;
while (p != buf && *p != '|') //while we're not at front of buf and not at | GOTO |
p--;
if (p == buf) //if at front of buf return 0 means no pipe
return 0;
*p = 0; // change | to NULL
p++; // move p right by 1
while (*p == ' ') // skip over any blanks
p++;
*tail = p; // change tail pointer to p
return 1; // head points at buf; return head
}
//does single pipes
int do_pipe(char *buf, int *cmd)
{
//pd ~ pipe itself
char *tail; //output for pipe
int pid, pd[2], hasPipe;
hasPipe = scan(buf, &tail);
if (hasPipe) //read is 0 write is 1
{
pid = fork(); //fork for writer, parent will be reader
//print2f("\n\nENTERING HASPIPE\n\n");
pipe(pd);
if (pid) //parent as pipe reader
{
close(pd[1]); //close writer for parent
dup2(pd[0], 0); //dup2 is taking control of stdout
command(tail);
}
else
{ //writer
close(pd[0]);
dup2(pd[1], 1); //dup2 is taking control of stdin
command(buf);
}
}
else
{
command(buf); //if no pipe
}
return 1;
}
/*
takes in tail and head for pipe
also does redirect
*/
int command(char *s)
{
char *name[16], tempBuf[64];
int i, j, nk, I; //, used to check if double carrot
char cmdline[64];
strcpy(tempBuf, s);
nk = eatpath(tempBuf, name); //eats tempBuf and throws it all in name
I = nk;
for (i = 0; i < nk; i++)
{
if (strcmp(name[i], "<") == 0)
{
printf("proc %d redirect input from %s\n", getpid(), name[i + 1]);
if (I > i) //if I > then index
I = i; // I = index of either < or > or >>
if (name[i + 1] == 0) //if nothing to take in from
{
print2f("invalid < attempt\n\r");
exit(1);
}
close(0);
if (open(name[i + 1], 0) < 0)
{
print2f("no such input file\n");
exit(2);
}
break;
}
}
for (i = 0; i < nk; i++)
{
if (strcmp(name[i], ">") == 0) //outputs to an existing file
{
printf("proc %d redirect outout to %s\n", getpid(), name[i + 1]);
if (I > i)
I = i; // I = index of first > or < or >>
if (name[i + 1] == 0) //if nothing to output to
{
print2f("invalid > attempt\n\r");
exit(3);
}
close(1);
open(name[i + 1], O_WRONLY | O_CREAT);
break;
}
}
for (i = 0; i < nk; i++) //append and creat if file doesnt exist yet
{
if (strcmp(name[i], ">>") == 0)
{
printf("proc %d append output to %s\n", getpid(), name[i + 1]);
if (I > i)
I = i;
if (name[i + 1] == 0)
{
print2f("invalid >>\n\r");
exit(4);
}
close(1);
//open the string after >> for append
open(name[i + 1], O_WRONLY | O_CREAT | O_APPEND);
break;
}
}
strcpy(cmdline, name[0]);
for (j = 1; j < I; j++)
{
strcat(cmdline, " ");
strcat(cmdline, name[j]);
}
if (getpid() < 9)
{
if (exec(cmdline) < 0)
exit(1);
}
while (1)
{
printf("%d : enter a key : ", getpid());
getc();
}
return 1;
}