-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.cpp
187 lines (187 loc) · 3.68 KB
/
shell.cpp
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
#include "filesys.h"
#define CLEN 10
#define CNUM 15
//enum ctype
char commands[CNUM][CLEN]={
"exit",
"dir",
"mkdir",
"cd",
"mkfile",
"del",
"write",
"read",
"who",
"pwd",
"rename",
"password",
"adduser"
};
int getcid(char *command){
int i;
if(command == NULL)
return -1;
for(i=0;i<CNUM;i++){
if(strcmp(command,commands[i]) == 0)
return i;
}
return -1;
}
int shell(int user_id,char *str){
char seps[] =" \t\n\0";
char* token,*tstr,*buf;
char wd[2048];
unsigned short mode;
unsigned short uid, gid;
int fd;
int cid,size;
token = strtok(str,seps);
if(token == NULL)
return 1;
cid = getcid(token);
switch(cid){
case 1:
_dir();
break;
case 2:
token = strtok(NULL,seps);
if(token == NULL){
printf("mkdir命令的正确格式为mkdir dirname,请检查命令!\n");
break;
}
mkdir(token);
break;
case 3:
token = strtok(NULL,seps);
if(token == NULL){
printf("cd命令的正确格式为cd dirname,请检查命令!\n");
break;
}
chdir(token);
break;
case 4:
token = strtok(NULL,seps);
if(token == NULL){
printf("mkfile 命令的正确格式为mkfile filename [mode],请检查命令!\n");
break;
}
tstr =token;
mode = DEFAULTMODE;
token = strtok(NULL,seps);
if(token != NULL){
sscanf(token,"%o",&mode);
}
mode = mode|DIFILE|0700;
fd = creat(user_id,tstr,mode);
if(fd == -1){
printf("创建文件失败!\n");
break;
}
//printf("%d %d\n", user_id, fd);/*调试用句*/
close(user_id,fd);
break;
case 5:
token = strtok(NULL,seps);
if(token == NULL){
printf("del 命令的正确格式为del filename,请检查命令!\n");
break;
}
delete(token);
break;
case 6:
mode = WRITE;
token = strtok(NULL,seps);
tstr = token;
token = strtok(NULL,seps);
if(token == NULL){
printf("write 命令的正确格式为write filename bytes,请检查命令!\n");
break;
}
if(token[0] == '-'){
if(token[1] == 'a')
mode = FAPPEND;
}else{
sscanf(token,"%d",&size);
}
fd = open(user_id,tstr,char(mode));
if (fd == -1) {
printf("文件不存在!\n");
break;
}
//printf("%d %d\n", user_id, fd);/*调试用句*/
buf = (char*)malloc(size);
size = write(fd,buf,size);
printf("%d bytes have been written into the file %s.\n",size,tstr);
free(buf);
close(user_id,fd);
break;
case 7:
token = strtok(NULL,seps);
tstr = token;
token = strtok(NULL,seps);
if(token == NULL){
printf("read 命令的正确格式为read filename bytes,请检查命令!\n");
break;
}
sscanf(token,"%d",&size);
fd = open(user_id,tstr,READ);
if (fd == -1) {
printf("文件不存在!\n");
break;
}
//printf("%d %d\n", user_id, fd);/*调试用句*/
buf = (char*)malloc(size+1);
size = read(fd,buf,size);
printf("%d bytes have been read into buf from the file %s.\n",size,tstr);
free(buf);
close(user_id,fd);
break;
case 8:
who(user_id);
break;
case 9:
_pwd(wd);
printf("%s\n", wd);
break;
case 10:
token = strtok(NULL, seps);
tstr = token;
token = strtok(NULL, seps);
if (token == NULL) {
printf("rename 命令的正确格式为rename oldname newname,请检查命令!\n");
break;
}
_rename(tstr, token);
break;
case 11:
token = strtok(NULL, seps);
tstr = token;
token = strtok(NULL, seps);
if (token == NULL) {
printf("password 命令的正确格式为password oldpw newpw,请检查命令!\n");
break;
}
_password(user_id, tstr, token);
break;
case 12:
token = strtok(NULL, seps);
tstr = token;
token = strtok(NULL, seps);
buf = token;
token = strtok(NULL, seps);
if (token == NULL) {
printf("adduser 命令的正确格式为adduser uid gid password,请检查命令!\n");
break;
}
sscanf(tstr, "%u", &uid);
sscanf(buf, "%u", &gid);
adduser(uid, gid, token);
break;
case 0:
return 0;
default:
printf("错误:没有命令%s!\n",token);
break;
}
return 1;
}