首页 > 代码库 > 编程实现linux下的shell
编程实现linux下的shell
/************************************************************************* > File Name: Kris_shell.c > Author: KrisChou > Mail:zhoujx0219@163.com > Created Time: Thu 21 Aug 2014 04:31:55 PM CST ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <sys/wait.h>#define PROMPT ">>" #define ARG_CNT 32void command_to_arglist(char** arglist,char *cmd_line);
int main(int argc, char *argv[]){ char cmd_line[1024]; char *arglist[ARG_CNT]; while(memset(cmd_line,0,1024), printf("%s",PROMPT), fgets(cmd_line,1024,stdin)) { if(cmd_line[0] != ‘\n‘) { cmd_line[strlen(cmd_line) - 1] = ‘\0‘; //将读进来的换行符变成‘\0‘ command_to_arglist(arglist,cmd_line); if(fork() > 0) { wait(NULL); }else { execvp(arglist[0],arglist); exit(0); } } } return 0;}static int my_isspace(char c){ if(c == ‘ ‘|| c == ‘\n‘ || c == ‘\t‘ || c ==‘\v‘) return 1; else return 0;}
static void command_to_arglist(char** arglist,char *cmd_line){ int cnt = 0; int bg,end; bg = 0; while(cmd_line[bg] != ‘\0‘) { while(my_isspace(cmd_line[bg])) { bg++; } if(cmd_line[bg] == ‘\0‘) { break; } end = bg; while(cmd_line[end] != ‘\0‘ && !my_isspace(cmd_line[end])) { end++; } arglist[cnt] = (char*)calloc(1, (end-bg+1)); strncpy(arglist[cnt],cmd_line+bg,(end-bg)); cnt++; bg = end; } arglist[cnt] = NULL; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。