zoukankan      html  css  js  c++  java
  • 简单shell实现

    http://blog.csdn.net/lishuhuakai/article/details/11928055

    #include <stdio.h>
    #include <unistd.h>
    #include <wait.h>
    #define MAXLINE 80
    
    void setup (char inputBuffer[], char *argv[])
    {
    
        int i, j;
        char *p;
        i = 0;
        for (p = inputBuffer; ;p++)/*将字符命令一段一段拆分开来,如输入ls -l,则argv[0]= "ls",argv[1]="-l",argv[2]=NULL*/
        {
            argv[i++] = p;
            while (*p != ' ' && *p != '')
                p++;
            if (*p == '')
            {
                break;
            }
            *p = '';
           }
        argv[i] = NULL;/*最后一个参数须用空指针NULL作结束*/
        execvp (argv[0], argv);/*执行命令,argv[0]放着执行的命令,其余的argv放着参数*/
    }
    
    
    
    int main()
    {
    
        char inputBuffer[MAXLINE];
        char *argv[MAXLINE / 2 - 1];
    
        while (1)
        {
            printf ("COMMAND->");
              gets (inputBuffer);/*输入命令*/
            setup (inputBuffer, argv);
                    printf ("扑街!");
        }
        return 0;
    }
    #include <stdio.h>
    #include <unistd.h>
    //#define SIZE 20
    #define MAXLINE 80
    char *p;
    
    void setup (char inputBuffer[], char *argv[])
    {
        int i, j;
        i = 0;
        for (p = inputBuffer; ;p++)/*一个一个地将输入字符分开*/
        {
            argv[i++] = p;
            while (*p != ' ' && *p != '')
                p++;
            if (*p == '')
            {
                break;
            }
            *p = '';
           }
        argv[i] = NULL;
        pid_t pid
        pid = fork();/*新建一个子进程*/
        if (pid == 0)/*对于子进程,执行输入的命令*/
        {
         execvp (argv[0], argv);
         printf ("command not found
    ");/*如果execvp执行失败的话,会返回-1,也就是这句话会被执行*/
        }
    
        else if( pid > 0 )/*对于父进程,等待*/
        {
         wait(NULL);/*父进程等待子进程执行完*/
        }
    
         else /*否则的话,就出错了*/
         {
          printf ("fork error
    ");
         }
    }
    
    int main()
    {
        char inputBuffer[MAXLINE];
        char *argv[MAXLINE / 2 - 1];
    
        while (1)
        {
            printf ("COMMAND->");
              gets (inputBuffer);
            /*关于scanf和gets,scanf对于输入的字符遇到空格或回车就结束了,而gets对于输入的空格也接受*/
            setup (inputBuffer, argv);
    
        }
        return 0;
    }
    #include <stdio.h>
    #include <unistd.h>
    #define MAXLINE 100
    char *p;
    char buffer[1024];
    
    void setup (char inputBuffer[], char *argv[])
    {
        int i=0,j;
        for (p = inputBuffer; ;p++)
        {
            argv[i++] = p;
            while (*p != ' ' && *p != '') p++;
            if (*p == '') break;
            *p = '';
        }
        
        argv[i] = NULL;
        int pid;
        pid = fork();/*新建一个子进程*/
        if (pid == 0)/*对于子进程,执行输入的命令*/
        {
            execvp (argv[0], argv);
            printf ("command not found
    ");
        }
        else if( pid > 0 ) wait(NULL);
        else printf ("fork error
    ");
    }
    
    void getLoc()
    {
        //获取当前的工作目录,注意:长度必须大于工作目录的长度加一  
        char *p = getcwd(buffer , 40);  
        char *dir = NULL;  
      
        printf("buffer:%s   p:%s size:%d  
    " , buffer , p , strlen(buffer));  
        //获取当前工作目录的名字  
        dir = (char *)get_current_dir_name();  
        printf("dir:%s 
    " , dir);  
      
        char *twd = NULL ;   
              
        twd = getwd(buffer);  
          
        //printf("buffer:%s   twd:%s 
    " , buffer , twd);
    }
    
    int main()
    {
        char inputBuffer[MAXLINE];
        char *argv[MAXLINE/2 - 1];
        while (1)
        {
            getLoc();
            printf ("%s->",buffer);
            gets (inputBuffer);
            setup (inputBuffer, argv);
        }
        return 0;
    }
  • 相关阅读:
    [Python自学] PyQT5-Web控件、与JavaScript交互
    [Python自学] PyQT5-选项卡窗口、堆栈窗口、停靠窗口、子窗口
    [Python自学] PyQT5-窗口风格、窗口样式、GIF动画、窗口透明
    [Python自学] PyQT5-子线程更新UI数据、信号槽自动绑定、lambda传参、partial传参、覆盖槽函数
    [Python自学] PyQT5-信号与槽
    [Python自学] PyQT5-菜单栏、工具栏、状态栏
    [Python自学] PyQT5-控件拖拽、剪切板
    [Python自学] PyQT5-各种QDialog对话框
    [Python自学] PyQT5-QSpinBox、QSlider控件
    Linux操作系统分析 | 课程学习总结报告
  • 原文地址:https://www.cnblogs.com/qlky/p/6890346.html
Copyright © 2011-2022 走看看