-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsh.c
171 lines (140 loc) · 3 KB
/
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
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
#include "type.h"
#include "ucode.c"
#define N_BUF 128
char cmdLine[N_BUF];
char* sh_cmd[]={"?", "help", "pwd", "cd", "logout", 0};
char* cmdToks[20];
void menu()
{
printf("[============== MENU ==============]\n");
printf("[ ? help pwd cd logout ]\n");
printf("[ cat grep ]\n");
printf("[============= FSHELL =============]\n");
}
int find_shcmd(char* name)
{
int i=0;
char* p = sh_cmd[0];
while(p) {
if(0 == strcmp(p, name))
return i;
i++;
p = sh_cmd[i];
}
return -1;
}
void do_shcmd(int cmd)
{
char buf[N_BUF];
switch(cmd)
{
case 0:
case 1: menu(); break;
case 2: getcwd(buf); printf("%s\n", buf); break;
case 3: chdir(cmdToks[1]); break;
case 4: exit(0);
}
}
void tok_cmd(char* line) //sh_token
{
int i = 1;
cmdToks[0] = strtok(line, " ");
while(cmdToks[i-1] != 0) {
cmdToks[i++] = strtok(0, " ");
//i++;
}
}
void strnclr(char* str, int n)
{
int i;
for(i=0; i<n; i++)
*(str+i) = 0;
}
int main(int argc, char* argv[])
{
int pid, cid, me, status, mode;
int pd[2];
int i, j, r;
char tmp[N_BUF];
char* cp;
STAT s;
me = getpid();
while(1) {
printf("FSHELL command ? ");
gets(cmdLine);
if(0 == cmdLine[0])
continue;
strcpy(tmp, cmdLine);
tok_cmd(tmp);
strnclr(cmdLine, 64);
// check if shell commmand, if so, execute, and continue
r = find_shcmd(cmdToks[0]);
if(r >= 0) {
do_shcmd(r);
continue;
}
pid = fork();
if(0==pid) { // child
cp = cmdLine;
for(i=0; 0 !=cmdToks[i]; i++) {
switch(cmdToks[i][0]) {
case '>':
// >> append
if('>' == cmdToks[i][1]) {
mode = O_APPEND;
}
// > overwrite
else {
mode = O_WRONLY;
creat(cmdToks[i+1]);
}
close(STDOUT);
if(open(cmdToks[++i], mode) != STDOUT) {
write(STDERR, "could not open file\n\r", 21);
exit(0);
}
break;
// end case >
case '<':
stat(cmdToks[i+1], &s);
if(!s.st_size) {
printf("%s does not exist\n");
exit(0);
}
close(STDIN);
open(cmdToks[++i], O_RDONLY);
break;
// end case <
case '|': // pipe output to program
pipe(pd);
pid = fork();
if(pid) { // parent
cmdToks[i+1] = '\0';
close(pd[0]);
close(STDOUT);
dup2(pd[1], STDOUT);
}
else { // child
strnclr(cmdLine, 64);
cp = cmdLine;
close(pd[1]);
close(STDIN);
dup2(pd[0], STDIN);
}
break;
// end case |
default: // copy args to cmdLine
strcpy(cp, cmdToks[i]);
cp += strlen(cmdToks[i]);
*(cp++) = ' ';
} // end switch
} // end for
exec(cmdLine);
printf("exec() failed\n");
exit(1);
} // end if(0==pid)
else { // parent
pid = wait(&status);
}
} // end while(1)
}