//enter cmd
//analyse cmd
//exec cmd
mark here
#include
#include
#include
#include
#include
#include
#define DEBUG #define TRUE ((int)1) #define FALSE ((int)0) #define MAX 40 extern char** environ; /**************** STATIC FUNCTION ******/ static void fs_exev(char*); static char* fs_cmd_analyse( char *buf, char **argv ); static int fs_cmd_check(char **argv); /************MAIN FUNCTION*******************/ int main() { char cmd[100]; while(1) { printf("$:"); fgets(cmd,MAX,stdin); fs_exev(cmd); } return 0; } /*********************************/ /****** execute the cmd ******/ /*********************************/ static void fs_exev( char* cmd ) { //1)analyse cmd //2)check the cmd is avraible //3)use fork and execve function to create a new process char *argv[100]; char buf[100]; strcpy(buf,cmd); (void)fs_cmd_analyse(buf,argv); if( (pid = fork()) == 0 ) { if( execve(argv[0],&argv[1],environ) < 0) fprintf(stderr,":execve :%s\n",strerror(errno)); } int status; waitpid(pid,&status,0); } /*********************************************/ /*** anaylse the cmd,create a aviraible cmd***/ /*********************************************/ static char* fs_cmd_analyse(char *buf, char **argv) { char* t_cmd; char* t_cmd_head; int count; int endflag = 0; int argv_count; int start_flag = TRUE; argv_count = 0; t_cmd = buf; t_cmd_head = buf; count = 1; while( !endflag ) { if( (*t_cmd == '\0') ) { endflag = 1; } if( ((*t_cmd == ' ')||(*t_cmd == '\0') ) ||( *t_cmd == '\n') ) { if(start_flag == TRUE) { argv[argv_count] = t_cmd_head; argv[argv_count][count] = '\0'; printf("argv[%d] = %s\n",argv_count,argv[argv_count]); argv_count++; start_flag = FALSE; } if( ( *(t_cmd+1) != ' ' ) &&( *(t_cmd+1) != '\n') &&( *(t_cmd+1) != '\0') ) { t_cmd_head = t_cmd+1; count = 1; start_flag = TRUE; } } if( (*(t_cmd+1) !=' ') && ( *(t_cmd+1) != '\n') ) count++; t_cmd++; } argv[argv_count] = NULL; return *argv; } /*********************************************/ /**** check the cmd is ok or not**************/ /*********************************************/ static int fs_cmd_check(char **argv) { int status; return status; }
这里只是最简单的雏形,当然要完善的有很多,又时间再来改把。
不知道为什么一定要刷行一下才能看到代码