-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
157 lines (156 loc) · 5.45 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "lib.h"
int main() {
while (1)
{
int chusr_flag = 0;
char command[10], file_name[100];
char username[10], password[10];
user_index = -1;
user_num = 0;
initialize_user();
while (1) {
printf("user name: ");
scanf("%s", username);
if (!strcmp(username, "quit"))
return 0;
printf("password: ");
scanf("%s", password);
if (login(username, password)) {
strcpy(current_disk, User[user_index].disk_name);
strcpy(current_user, username);
strcpy(current_path, "[");
strcat(current_path, current_user);
strcat(current_path, "@ext2 /");
printf("User %s sign in!\n", username);
break;
}
else {
printf("Wrong user name or password !\n");
printf("You can input 'quit' to exit.\n");
}
}
while(getchar() != '\n');
initialize_memory();
check_disk();
while (1) {
int write_ret = -1;
printf("%s]$ ", current_path);
char c = getchar();
if (c == '\n')
continue;
scanf("%s", command);
// 将c放到command中,因为command中
int len = strlen(command);
for (int i = len; i >= 0; i--) {
command[i + 1] = command[i];
}
command[0] = c;
// printf("command: %s\n", command);
if (!strcmp(command, "cd")) { //进入当前目录下
scanf("%s", file_name);
cd(file_name);
}
else if (!strcmp(command, "mkdir")) { //创建目录
scanf("%s", file_name);
mkdir(file_name, 2);
}
else if (!strcmp(command, "create")) { //创建文件
scanf("%s", file_name);
if (strlen(file_name)>8)
{
printf("The name of the file is too long");
}
create(file_name, 1); //1表示创建普通文件
}
else if (!strcmp(command, "rmdir")) { //删除空目录
scanf("%s", file_name);
rmdir(file_name);
}
else if (!strcmp(command, "rm")) { //删除文件或目录,不提示
scanf("%s", file_name);
del(file_name);
}
else if (!strcmp(command, "open")) { //打开一个文件
scanf("%s", file_name);
open_file(file_name);
}
else if (!strcmp(command, "close")) { //关闭一个文件
scanf("%s", file_name);
close_file(file_name);
}
else if (!strcmp(command, "read")) { //读一个文件
scanf("%s", file_name);
read_file(file_name);
}
else if (!strcmp(command, "write")) { //写一个文件
scanf("%s", file_name);
while(getchar() != '\n');
write_ret = write_file(file_name);
}
else if (!strcmp(command, "ls")) { //显示当前目录
ls();
}
else if (!strcmp(command, "format")) { //格式化硬盘
printf("\"Format\" means erase all the data in the Disk\n");
printf("Are you sure? (yes/no):\n");
fflush(stdin);
char temp[10];
scanf("%s", temp);
if (!strcmp(temp, "yes")) {
format();
printf("Format successfully!\n");
}
else {
printf("Format was canceled\n");
}
}
else if (!strcmp(command, "ckdisk")) { //检查硬盘
check_disk();
}
else if (!strcmp(command, "help") || !strcmp(command, "h")) { //查看帮助
help();
}
else if (!strcmp(command, "quit") || !strcmp(command, "exit")) { //退出系统
printf("System exit successfully!\n");
break;
}
else if (!strcmp(command, "chmod")) { //修改权限
scanf("%s", file_name);
unsigned short mode;
scanf("%hd", &mode);
chmod(file_name, mode);
}
else if (!strcmp(command, "mkusr")) { //创建用户
mkusr();
}
else if (!strcmp(command, "rmusr")) { //删除用户
rmusr();
}
else if (!strcmp(command, "chusrname")) { //修改用户名
chusrname();
}
else if (!strcmp(command, "chusrpwd")) { //修改用户密码
chusrpwd();
}
else if (!strcmp(command, "chusr")){ //切换用户
chusr_flag = 1;
break;
}
else {
printf("No this Command, use \"help\" to see the command list!\n");
//help();
}
if (strcmp(command, "write") || write_ret != 0) {
while(getchar() != '\n');
}
}
if (chusr_flag)
continue;
else
break;
}
return 0;
}