• 简单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;
    }
  • 相关阅读:
    Recommended Books for Algo Trading in 2020
    Market Making is simpler than you think!
    Top Crypto Market Makers of 2020
    Top Crypto Market Makers, Rated and Reviewed
    爬取伯乐在线文章(五)itemloader
    爬取伯乐在线文章(四)将爬取结果保存到MySQL
    爬取伯乐在线文章(三)爬取所有页面的文章
    爬取伯乐在线文章(二)通过xpath提取源文件中需要的内容
    爬取伯乐在线文章(一)
    爬虫去重策略
  • 原文地址:https://www.cnblogs.com/qlky/p/6890346.html
Copyright © 2020-2023  润新知