-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
78 lines (70 loc) · 1.25 KB
/
main.c
File metadata and controls
78 lines (70 loc) · 1.25 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
/* **************************
* The main funtion of shell
* *************************/
#include "main.h"
command_type cmd[] =
{
{"exit",4},
{"cd",2},
{"ls",2},
{"date",4},
{"pwd",3},
{"cat",3},
{"less",4},
{"cp",2},
};
int main(int argc, char *argv[])
{
char buf[BUF_SIZE];
int res = 1;
int flag = 1;
int index = 0;
while(flag)
{
printf("|->");
if(NULL == fgets(buf,BUF_SIZE,stdin))
return 0;
index = sizeof(cmd)/sizeof(command_type);
while(index-- > 0)
{
res = strncmp(buf,cmd[index].str,cmd[index].size);
if(res == 0)
{
switch(index)
{
case 0: // exec exit command
flag = 0;
break;
case 1: // exec cd command
cds(buf);
break;
case 2: // exec ls command
lss(buf);
break;
case 3: // exec date command
dates(buf);
break;
case 4: // exec pwd command
pwds();
break;
case 5: // exec cat command
cats(buf);
break;
case 6: // exec less command
lesss(buf);
break;
case 7: // exec cp command
cps(buf);
break;
default:
printf("can't running this command \n");
break;
}
index = -1; // if find the command, stop finding
}
}
if(index == -1)
printf("can't find command: %s",buf);
}
return 0;
}