/************************************************************************* > File name:kris_shell.c > Autho R:krischou > Mail:[email protected] > Created time:thu-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>#definePROMPT ">>"#defineARG_CNT 32voidCommand_to_arglist (Char* * arglist,Char*cmd_line);
intMainintargcChar*argv[]) { Charcmd_line[1024x768]; Char*arglist[arg_cnt]; while(Memset (Cmd_line,0,1024x768), printf ("%s", PROMPT), Fgets (Cmd_line,1024x768, stdin)) { if(cmd_line[0] !='\ n') {Cmd_line[strlen (cmd_line)-1] =' /';//turn the read-in newline character into '. 'command_to_arglist (Arglist,cmd_line); if(Fork () >0) {wait (NULL); }Else{EXECVP (arglist[0],arglist); Exit (0); } } } return 0;}Static intMy_isspace (Charc) { if(c = =' '|| c = ='\ n'|| c = ='\ t'|| c = ='\v') return 1; Else return 0;}
static voidCommand_to_arglist (Char* * arglist,Char*cmd_line) { intCNT =0; intBg,end; BG=0; while(CMD_LINE[BG]! =' /') { while(My_isspace (CMD_LINE[BG])) {BG++; } if(CMD_LINE[BG] = =' /') { Break; } End=BG; while(Cmd_line[end]! =' /'&&!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; }