标签:oid linu col stdout title .com nbsp null 返回
编写思路:
标*暂未实现
1 #include <sys/types.h> 2 #include <sys/wait.h> 3 #include <unistd.h> 4 #include <signal.h> 5 #include <stdio.h> 6 #include <string.h> 7 #include <stdlib.h> 8 #include "mysh.h" 9 10 void initShell() 11 { 12 while (1) { 13 prompt(); 14 fflush(stdout); 15 fflush(stdin); 16 char buf[1024]; 17 memset(buf, 0, sizeof(buf)); 18 ssize_t size = read(0, buf, sizeof(buf) - 1); 19 if (size > 0) { 20 buf[size - 1] = ‘\0‘; // del ‘\n‘ 21 } 22 char *argv[64] = {0}; 23 parse(buf, argv); 24 25 int pid = fork(); 26 if (pid == -1) { 27 perror("fork"); 28 exit(-1); 29 } 30 if (pid == 0) { 31 execvp(argv[0], argv); 32 } else { 33 waitpid(-1, NULL, 0); 34 } 35 } 36 } 37 38 void prompt() 39 { 40 printf("begin type cmd: "); 41 } 42 43 void parse(char *buf, char *argv[]) 44 { 45 char *p = buf; 46 int n = 0; 47 argv[n++] = p; 48 while (*p != ‘\0‘) { 49 if (*p == ‘ ‘) { 50 *p = ‘\0‘; 51 ++p; 52 argv[n++] = p; 53 } else { 54 ++p; 55 } 56 } 57 argv[n] = NULL; 58 }
标签:oid linu col stdout title .com nbsp null 返回
原文地址:https://www.cnblogs.com/wangyubjhd/p/10281516.html