自己写一个简单的unix外壳

//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; } 
         
       
      
      
     
     
    
    
   
   

这里只是最简单的雏形,当然要完善的有很多,又时间再来改把。大笑

不知道为什么一定要刷行一下才能看到代码疑问


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值