shell代码

代码部分

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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/limits.h>
#include <pwd.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
char *read_str(); // 打印开头,
void cdfun(int index, char a[40][40], char *cdhistory);
// void cut_str(char cut, char *a, char **str, int *index); //
bool shell(char a[40][40], int index, char *cd_history);
void exe(char *a, int num_pipe); //
void redirect(char *argv[], int argc);
void redirect_input(char *file_name);
void redirect_output(char *file_name, int index);
void home(char *a, char *home);
int main() {
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGTSTP, SIG_IGN); // 屏蔽信号
char cd_history[300];
getcwd(cd_history, sizeof(cd_history));
while (1) {
char *a = read_str(); // a为
if (a == NULL) {
free(a);
continue;
}
char *b = (char *)malloc(sizeof(char) * (strlen(a) + 1));
strcpy(b, a);
char argv[40][40]; // 分割
int argc = 0;
const char delim[] = " ";
char *token = strtok(a, delim);

while (token != NULL && argc < 40) {
strncpy(argv[argc], token, sizeof(argv[argc]) - 1);
argv[argc][sizeof(argv[argc]) - 1] = '\0'; // 确保字符串以空字符结尾
argc++;
token = strtok(NULL, delim);
}
// printf("zhel%d\n", argc);
int num_pipe = 0;
for (int i = 0; i < argc; i++) {

if (strcmp("|", argv[i]) == 0) {
num_pipe++;
}
// printf("%s\n", argv[i]);
}
if (shell(argv, argc, cd_history)) {
continue;
}
exe(b, num_pipe);
printf("\n");
free(a);
free(b);
}

return 0;
}

char *read_str() { // 打印开头
char *username = getlogin();
printf(" \033[0;36m%s\033[0m @", username); // 用户名
FILE *file = fopen("/proc/sys/kernel/hostname", "r");
if (file == NULL) {
return NULL;
} else {
char computername[257];
fgets(computername, sizeof(computername), file);
int len = strlen(computername);
computername[len - 1] = '\0';
printf(" \033[0;32m%s\033[0m ", computername); // 电脑名
}
struct passwd *pw = getpwuid(getuid());
const char *home_dir = pw->pw_dir; // 获取用户目录
char cwd[1024];

if (getcwd(cwd, sizeof(cwd)) != NULL) {
if (strcmp(pw->pw_dir, cwd) <= 0) {
home(cwd, pw->pw_dir);
}
printf("in \033[1;33m%s\033[0m ", cwd); // 黄色
}
time_t rawTime;
struct tm *localTime;
time(&rawTime);
localTime = localtime(&rawTime);
printf("[%02d:%02d:%02d] ", localTime->tm_hour, localTime->tm_min,
localTime->tm_sec);
char *a = readline("\001\n\033[1;31m$\033[0m \002");
if (a == NULL || a == "\n" || a == " ")
return NULL;
a[strlen(a)] = '\0';
add_history(a);
return a;
}
void cdfun(int index, char a[40][40], char *cdhistory) {
// printf("%s\n", cdhistory);
struct passwd *pw = getpwuid(getuid());
const char *home_dir = pw->pw_dir; // 获取用户目录
if (index > 2) {
printf("cd: 没有那个文件或目录: %s", a[1]);
return;
} else if (index == 2) {
char new_directory[256];
strcpy(new_directory, a[1]);
char h1[300];
getcwd(h1, sizeof(h1));
if (chdir(new_directory) == 0) {
strcpy(cdhistory, h1);
} else if (strcmp(new_directory, "-") == 0) {
chdir(cdhistory);
printf("%s\n", cdhistory);
strcpy(cdhistory, h1);
} else if (strcmp(new_directory, "~") == 0) {
chdir(home_dir);
strcpy(cdhistory, h1);
} else {
perror("chdir");
}
} else {
chdir(home_dir);
}
}
bool shell(char a[40][40], int index, char *cd_history) {
if (strcmp(a[0], "exit") == 0) {
exit(1);
}
if (strcmp(a[0], "cd") == 0) {
cdfun(index, a, cd_history);
return true;
}
if (index == 0) {
return true;
}
return false;
}

void exe(char *a, int num_pipe) {
int num_com = 0;
const char delim[] = "|";
// printf("a=%s\n", a);
char *token = strtok(a, delim);
char com[40][40];
char **free_1;
int free_argc;
while (token != NULL) {
// com[num_com] = (char *)malloc(strlen(token) + 1);
strcpy(com[num_com], token);
num_com++;
token = strtok(NULL, delim);
if (token == NULL) {
break;
}
} // 隔管道
if (num_com == 0) {
return;
}
int pipefd[num_com - 1][2];
for (int i = 0; i < num_com - 1; i++) { // 创建管道
if (pipe(pipefd[i]) == -1) {
perror("pipe");
exit(4);
}
}
pid_t child_pid;
for (int i = 0; i < num_com; i++) { // 进程
if ((child_pid = fork()) == -1) {
perror("fork");
exit(4);
} // fork();
if (child_pid == 0) { // 子进程里面
if (num_com != 1) {
if (i > 0) {
dup2(pipefd[i - 1][0], STDIN_FILENO);
} // 连接前一个的写入
if (i < num_com - 1) {
dup2(pipefd[i][1], STDOUT_FILENO);
} // 自己连接读
for (int i = 0; i < num_com - 1; i++) {
close(pipefd[i][1]);
close(pipefd[i][0]);
} // 关闭所有管道
} // 如果有管道
char *argv[40];
free_1 = argv;
int argc = 0;
token = strtok(com[i], " ");
while (token != NULL) {
argv[argc] = (char *)malloc(sizeof(char) * strlen(token) + 1);
strcpy(argv[argc++], token);
token = strtok(NULL, " ");
}
free_argc = argc;
// 分割了 ,要开始execvp了
argv[argc] = NULL;
redirect(argv, argc);
execvp(argv[0], argv);
perror("execvp");
exit(EXIT_FAILURE);
for (int i = 0; i < free_argc; i++) {
free(free_1[i]);
}
}

// else { // 父进程

// wait(NULL);
// printf("父进程停止wait%d\n", i);
// }
} // 循环结束
for (int i = 0; i < num_com - 1; i++) {
close(pipefd[i][1]);
close(pipefd[i][0]);
} // 关闭
for (int i = 0; i < num_com; i++) {
pid_t pid;
pid = wait(NULL);
printf(":: %d/%d\n", i, num_com - 1);
if (strstr(com[i], "&"))
printf("%d %s", pid, com[i]);
}
}
void redirect(char *argv[], int argc) {

for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "<") == 0) {
redirect_input(argv[i + 1]);
argv[i + 1] = NULL;
argv[i] = NULL;
i += 2;
continue;
}
if (strcmp(argv[i], ">") == 0) {
redirect_output(argv[i + 1], 0);
argv[i + 1] = NULL;
argv[i] = NULL;
i += 2;
continue;
}
if (strcmp(argv[i], ">>") == 0) {
redirect_output(argv[i + 1], 1);
argv[i + 1] = NULL; // argvi+1为文件
argv[i] = NULL;
i += 2;
continue;
}
}
}
void redirect_input(char *file_name) {
int fd = open(file_name, O_RDONLY);
if (dup2(fd, 0) == -1) {
perror("dup2");
exit(4);
}
}
void redirect_output(char *file_name, int index) {
int fd;
if (index == 0) {
fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0777);
} else if (index == 1) {
fd = open(file_name, O_WRONLY | O_CREAT | O_APPEND, 0644);
}
if (dup2(fd, STDOUT_FILENO) == -1) {
perror("dup2");
}
close(fd);
}
void home(char *a, char *home) { // 改home文件成~;
int lenhome = strlen(home);
int len = strlen(a);
a[0] = '~';
for (int i = 1; i < len - lenhome + 1; i++) {
a[i] = a[i + lenhome - 1];
}
a[len - lenhome + 1] = '\0';
}


shell代码
http://example.com/posts/5315.html
作者
yunjianworld
发布于
2024年3月30日
许可协议