1 #include<stdio.h>
2 #include<unistd.h>
3 #include<stdlib.h>
4 #include<string.h>
5 int main(int argc, char* argv[])
6 {
7 while(1){
8 printf("[user@host path]$ ");
9 fflush(stdout);
10 char buf[1024] = {
0};
11 fgets(buf, 1023, stdin);
12 printf("cmd:[%s]\n", buf);
13 }
14 return 0;
15 }
12 buf[strlen(buf) - 1] = '\0';
13 int myargc = 0;
14 char *ptr = buf, *myargv[32] = {
NULL};
15 while(*ptr != '\0'){
16 if(*ptr != ' '){
17 myargv[myargc] = ptr;
18 myargc++;
19 while(*ptr != '\0' && *ptr != ' ') ptr++;
20 *ptr = '\0';
21 }
22 ptr++;
23 }
24 myargv[myargc] = NULL;
25 int i = 0;
26 for(i = 0;i < myargc;i++){
27 printf("[%s]\n", myargv[i]);
28 }
26 pid_t pid = fork();
27 if(pid < 0){
28 perror("fork error");
29 continue;
30 }else if(pid == 0){
31 execvp(myargv[0], myargv);
32 perror("execvp error");
33 exit(-1);
34 }
35 wait(NULL);
但此时切换路径时切换不了的(cd无效), 所以此时只能调用函数改变当前的工作路径
26 if(strcmp("cd", myargv[0]) == 0){
27 chdir(myargv[1]);
28 continue;
29 }
20 int direct_flag = 0;//0-没有重定向,1-清空重定向,2-追加重定向
21 char *ptr = buf;
22 char *redirect_file = NULL;
23 while(*ptr != '\0'){
24 if(*ptr == '>'){
25 direct_flag = 1;
26 *ptr = '\0';
27 ptr++;
28 if(*ptr == '\0'){
29 direct_flag = 2;
30 *ptr = '\0';
31 ptr++;
32 }
33 while(*ptr != '\0' && *ptr == ' ') ptr++;
34 redirect_file = ptr;
35 while(*ptr != '\0' && *ptr != ' ') ptr++;
36 *ptr = '\0';
37 }
38 ptr++;
39 }
40 ptr = buf;
62 if(direct_flag == 1){
63 int fd;
64 fd = open(redirect_file, O_CREAT|O_RDWR|O_TRUNC, 0664);
65 dup2(fd, 1);
66 }else if(direct_flag == 2){
67 int fd;
68 fd = open(redirect_file, O_CREAT|O_RDWR|O_APPEND, 0664);
69 dup2(fd, 1);
70 }
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<unistd.h>
4 #include<string.h>
5 #include<ctype.h>
6 #include<fcntl.h>
7 #include<sys/stat.h>
8 #include<sys/wait.h>
9 int main(int argc,char *argv[])
10 {
11 while(1){
12 printf("[user@host path]$ ");
13 fflush(stdout);//手动刷新缓冲区
14 char buf[1024] = {
0};
15 fgets(buf, 1023, stdin);
16 buf[strlen(buf) - 1] = '\0';
17
18
19
20 int direct_flag = 0;//0-没有重定向,1-清空重定向,2-追加重定向
21 char *ptr = buf;
22 char *redirect_file = NULL;
23 while(*ptr != '\0'){
24 if(*ptr == '>'){
25 direct_flag = 1;
26 *ptr = '\0';
27 ptr++;
28 if(*ptr == '\0'){
29 direct_flag = 2;
30 *ptr = '\0';
31 ptr++;
32 }
33 while(*ptr != '\0' && *ptr == ' ') ptr++;
34 redirect_file = ptr;
35 while(*ptr != '\0' && *ptr != ' ') ptr++;
36 *ptr = '\0';
37 }
38 ptr++;
39 }
40 ptr = buf;
41 char *myargv[32] = {
NULL};
42 int myargc = 0;
43 while(*ptr != '\0'){
44 if(*ptr != ' '){
45 myargv[myargc] = ptr;
46 myargc++;
47 while(*ptr != '\0' && *ptr != ' ') ptr++;
48 *ptr = '\0';
49 }
50 ptr++;
51 }
52 myargv[myargc] = NULL;
53 if(strcmp("cd", myargv[0]) == 0){
54 chdir(myargv[1]);
55 continue;
56 }
57 pid_t pid = fork();
58 if(pid < 0){
59 perror("fork error");
60 continue;
61 }else if(pid == 0){
62 if(direct_flag == 1){
63 int fd;
64 fd = open(redirect_file, O_CREAT|O_RDWR|O_TRUNC, 0664);
65 dup2(fd, 1);
66 }else if(direct_flag == 2){
67 int fd;
68 fd = open(redirect_file, O_CREAT|O_RDWR|O_APPEND, 0664);
69 dup2(fd, 1);
70 }
71 execvp(myargv[0],myargv);
72 perror("execvp error");
73 exit(-1);
74 }
75 wait(NULL);
76 }
77 return 0;
78 }