-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
245 lines (212 loc) · 5.03 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*****************
shell内部函数声明
******************/
void cshell_loop(void);
char *cshell_read_line(void);
char **cshell_split_line(char*);
int cshell_launch(char **args);
int cshell_num_builtins();
int cshell_execute(char **args);
/*********************
内置shell命令函数声明
**********************/
int cshell_cd(char **args);
int cshell_help(char **args);
int cshell_exit(char **args);
/************
内置命令列表
************/
char *builtin_str[] = {
"cd",
"help",
"exit"
};
/****************
内置命令对应函数 (将builtin_func声明为返回int的函数指针数组(指向char的指针))
****************/
int (*builtin_func[])(char **) = {
&cshell_cd,
&cshell_help,
&cshell_exit
};
int main(int argc, char** argv)
{
cshell_loop();
return EXIT_SUCCESS;
}
void cshell_loop(void)
{
char *line;
char **args;
int status;
do{
printf(">> ");
line = cshell_read_line();
args = cshell_split_line(line);
status = cshell_execute(args);
free(line);
free(args);
} while (status);
}
char *cshell_read_line(void)
{
#ifdef CSHELL_USE_STD_GETLINE
char *line = NULL;
ssize_t bufsize = 0;
if (getline(&line, &bufsize, stdin) == -1)
{
if (feof(stdin))
{
exit(EXIT_SUCCESS);
} else {
perror("cshell: getline\n");
exit(EXIT_FAILURE);
}
// return line; //mistake
}
return line;
#else
#define CSHELL_RL_BUFSIZE 1024
int bufsize = CSHELL_RL_BUFSIZE;
int position = 0;
char *buffer = malloc(sizeof(char) * bufsize);
int c;
if (!buffer)
{
fprintf(stderr, "cshell: allocation error\n");
exit(EXIT_FAILURE);
}
while (1)
{
// 读取一个字符
c = getchar();
// 如果命中EOF, 将它替换为空字符串并且返回
if (c == EOF || c == '\n')
{
buffer[position] = '\0';
return buffer;
} else{
buffer[position] = c;
}
position++;
// 如果超过缓冲区, 重新分配
if (position >= bufsize)
{
bufsize += CSHELL_RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer)
{
fprintf(stderr, "cshell: allocation error\n");
exit(EXIT_FAILURE);
}
}
}
#endif
}
#define CSHELL_TOK_BUFSIZE 64
#define CSHELL_TOK_DELIM " \t\r\n\a"
char **cshell_split_line(char *line)
{
int bufsize = CSHELL_TOK_BUFSIZE, position = 0;
char **tokens = malloc(bufsize * sizeof(char*));
char *token, **tokens_backup;
if (!tokens)
{
fprintf(stderr, "cshell: allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(line, CSHELL_TOK_DELIM);
while (token != NULL)
{
tokens[position] = token;
position++;
if (position >= bufsize){
bufsize += CSHELL_TOK_BUFSIZE;
tokens_backup = tokens;
tokens = realloc(tokens, bufsize * sizeof(char*));
if (!tokens) {
free(tokens_backup);
fprintf(stderr, "cshell: allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, CSHELL_TOK_DELIM);
}
tokens[position] = NULL;
return tokens;
}
int cshell_launch(char **args)
{
pid_t pid;
int status;
pid = fork();
if (pid == 0){
// 子进程执行
if (execvp(args[0], args) == -1) {
perror("cshell");
}
exit(EXIT_FAILURE);
} else if (pid < 0){
// fork Error
perror("cshell");
} else {
// 父进程执行
do {
waitpid(pid, &status, WUNTRACED);
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
int cshell_num_builtins()
{
return sizeof(builtin_str) / sizeof(char *);
}
int cshell_cd(char **args)
{
if (args[1] == NULL)
{
fprintf(stderr, "cshell: expected argument to \"cd\"\n");
} else {
if (chdir(args[1]) != 0)
{
perror("cshell");
}
}
return 1;
}
int cshell_help(char **args)
{
int i;
printf("ForkShannon's cshell\n");
printf("Type program names an arguments, and hit enter.\n");
printf("The following are built in:\n");
for ( i = 0; i < cshell_num_builtins(); i++)
{
printf(" %s\n", builtin_str[i]);
}
printf("Use the man command for information on other programs.\n");
return 1;
}
int cshell_exit(char **args)
{
return EXIT_SUCCESS;
}
int cshell_execute(char **args)
{
int i;
if(args[0] == NULL) {
return 1;
}
for (i = 0; i < cshell_num_builtins(); i++) {
if (strcmp(args[0], builtin_str[i]) == 0) {
return (*builtin_func[i]) (args);
}
}
return cshell_launch(args);
}