#include
#include
#include
#include
#include
int main() { char cmd[128]; while(1){ printf("[lx@bogon class4]# "); fflush(stdout); ssize_t _s = read(0,cmd,sizeof(cmd)-1); if(_s > 0){ cmd[_s-1] = '\0'; }else{ perror("read"); return 1; } // printf("%s\n",cmd); char *_argv[32]; _argv[0] = cmd; int i = 1; char *start = cmd; while(*start){ if(isspace(*start)){ *start = '\0'; start++; _argv[i] = start; i++; // printf("i= %d\n",i); continue; } start++; } // printf("NULL : i = %d\n",i); _argv[i] = NULL; pid_t id = fork(); if(id < 0){ perror("fork"); }else if( id == 0){ execvp(_argv[0],_argv); exit(1); }else{ int status = 0; pid_t ret = waitpid(id,&status,0); if(ret > 0 && WIFEXITED(status)){ }else{ perror("waitpid"); } } } return 0; }
自己编写shell
最新推荐文章于 2024-06-21 18:05:40 发布