forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsh.c
49 lines (45 loc) · 707 Bytes
/
sh.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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
#include "fcntl.h"
char *args[100];
void parse(char buf[]);
int
main(void)
{
char buf[128];
int pid;
while(1){
puts("$ ");
gets(buf, sizeof(buf));
if(buf[0] == '\0')
continue;
pid = fork();
if(pid == 0){
parse(buf);
exec(buf, args);
printf(1, "%s: not found\n", buf);
exit();
}
if(pid > 0)
wait();
}
}
void
parse(char buf[])
{
int j = 1;
int i;
args[0] = buf;
for (i = 0; buf[i] != '\0'; i++) {
if (buf[i] == ' ') {
buf[i] = '\0';
args[j++] = buf + i+1;
if (j >= 100) {
printf(2, "too many args\n");
exit();
}
}
}
}