-
Notifications
You must be signed in to change notification settings - Fork 0
/
My_shell.c
264 lines (236 loc) · 8.31 KB
/
My_shell.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <termios.h>
#include <fcntl.h>
#include <signal.h>
#define MAX_COMMAND_LENGTH 100
#define MAX_ARGS 10
#define MAX_ARGUMENTS 100
enum RedirectType {
NO_REDIRECT,
OUTPUT_REDIRECT,
INPUT_REDIRECT,
APPEND_REDIRECT
};
#define COLOR_GREEN "\033[0;32m"
#define COLOR_RESET "\033[0m"
void echo(const char *message) {
printf("%s\n", message);
}
void change_directory(const char *path) {
if (path == NULL || strcmp(path, "") == 0) {
path = getenv("HOME");
}
if (strcmp(path, "-") == 0 || strcmp(path, " ") == 0) {
const char *previous_directory = getenv("OLDPWD");
if (previous_directory == NULL) {
fprintf(stderr, "找不到上次所在的目录\n");
return;
}
if (chdir(previous_directory) != 0) {
perror("chdir");
return;
}
} else {
if (chdir(path) != 0) {
perror("chdir");
return;
}
}
char current_directory[4096];
if (getcwd(current_directory, sizeof(current_directory)) == NULL) {
perror("getcwd");
return;
}
if (setenv("OLDPWD", getenv("PWD"), 1) != 0) {
perror("setenv");
return;
}
if (setenv("PWD", current_directory, 1) != 0) {
perror("setenv");
return;
}
}
void execute_command(char *command, char *args[MAX_ARGS], enum RedirectType redirect_type, char *output_file, int run_in_background, char *input_file) {
if (strcmp(command, "cd") == 0) {
change_directory(args[1]); // 直接调用 change_directory
return;
} else if (strcmp(command, "exit") == 0) {
exit(0); // 退出 shell
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// 子进程中的代码
if (run_in_background) {
setpgid(0, 0);
}
if (redirect_type == OUTPUT_REDIRECT || redirect_type == APPEND_REDIRECT) {
int fd;
if (redirect_type == OUTPUT_REDIRECT) {
fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
} else { // APPEND_REDIRECT
fd = open(output_file, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
}
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
if (dup2(fd, STDOUT_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
close(fd);
}
if (input_file != NULL) {
int fd_in = open(input_file, O_RDONLY);
if (fd_in == -1) {
perror("open");
exit(EXIT_FAILURE);
}
if (dup2(fd_in, STDIN_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
close(fd_in);
}
execvp(command, args);
perror("");
exit(EXIT_FAILURE);
} else {
if (!run_in_background) {
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
int exit_status = WEXITSTATUS(status);
printf("子进程退出,退出状态码:%d\n", exit_status);
} else if (WIFSIGNALED(status)) {
int term_signal = WTERMSIG(status);
printf("子进程被信号终止,信号编号:%d\n", term_signal);
}
}
}
}
void sigint_handler(int signum) {
// 空函数,不执行任何操作
}
int main() {
while (1) {
signal(SIGINT, SIG_IGN);
struct termios term;//存储终端的相关信息
tcgetattr(STDIN_FILENO, &term);//获取相关信息,并存入结构体
term.c_cc[VEOF] = _POSIX_VDISABLE;//禁止使用EOF
tcsetattr(STDIN_FILENO, TCSANOW, &term);//将设置存入终端
char command[MAX_COMMAND_LENGTH];
enum RedirectType redirect_type = NO_REDIRECT;
char input_file[4096];
char output_file[4096];
int run_in_background = 0;
char current_directory[4096];
if (getcwd(current_directory, sizeof(current_directory)) == NULL) {
perror("getcwd");
exit(EXIT_FAILURE);
}
char *username = getenv("USER");
printf(COLOR_GREEN "%s@%s:" COLOR_RESET "%s$ ", username, getenv("HOSTNAME"), current_directory);
fgets(command, sizeof(command), stdin);
command[strcspn(command, "\n")] = '\0';
if (strcmp(command, "") == 0 || strspn(command, " \t") == strlen(command)) {
continue;
}
char *token = strtok(command, "|");
char *pipe_commands[MAX_ARGS];
int num_pipes = 0;
while (token != NULL && num_pipes < MAX_ARGS) {
pipe_commands[num_pipes++] = token;
token = strtok(NULL, "|");
}
int fd_in = 0;
for (int i = 0; i < num_pipes; i++) {
char *trimmed_token = strtok(pipe_commands[i], " \t");
if (strcmp(trimmed_token, "") == 0) {
continue;
}
char *args[MAX_ARGS];
int arg_index = 0;
while (trimmed_token != NULL && arg_index < MAX_ARGS - 1) {
if (strcmp(trimmed_token, ">") == 0 || strcmp(trimmed_token, "<") == 0 || strcmp(trimmed_token, ">>") == 0) {
redirect_type = strcmp(trimmed_token, ">") == 0 ? OUTPUT_REDIRECT : strcmp(trimmed_token, ">>") == 0 ? APPEND_REDIRECT : INPUT_REDIRECT;
trimmed_token = strtok(NULL, " \t");
if (redirect_type == INPUT_REDIRECT) {
strcpy(input_file, trimmed_token);
} else {
strcpy(output_file, trimmed_token);
}
break;
} else if (strcmp(trimmed_token, "&") == 0) {
run_in_background = 1;
break;
}
args[arg_index++] = trimmed_token;
trimmed_token = strtok(NULL, " \t");
}
args[arg_index] = NULL;
if (strcmp(args[0], "cd") == 0) {
change_directory(args[1]);
continue;
} else if (strcmp(args[0], "exit") == 0) {
exit(0);
}
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
dup2(fd_in, 0);
if (i < num_pipes - 1) {
dup2(pipefd[1], 1);
} else {
if (redirect_type == OUTPUT_REDIRECT || redirect_type == APPEND_REDIRECT) {
int fd;
if (redirect_type == OUTPUT_REDIRECT) {
fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
} else { // APPEND_REDIRECT
fd = open(output_file, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
}
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
dup2(fd, STDOUT_FILENO);
close(fd);
}
}
if (redirect_type == INPUT_REDIRECT) {
int fd_in = open(input_file, O_RDONLY);
if (fd_in == -1) {
perror("open");
exit(EXIT_FAILURE);
}
dup2(fd_in, STDIN_FILENO);
close(fd_in);
}
close(pipefd[0]);
execvp(args[0], args);
perror("execvp");
exit(EXIT_FAILURE);
} else {
wait(NULL);
close(pipefd[1]);
fd_in = pipefd[0];
}
}
}
return 0;
}