編程實現linux下的shell

來源:互聯網
上載者:User

標籤:style   blog   color   io   for   ar   2014   div   

/*************************************************************************    > File Name: Kris_shell.c    > Author: KrisChou    > Mail:[email protected]     > Created Time: Thu 21 Aug 2014 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>#define PROMPT ">>" #define ARG_CNT 32void command_to_arglist(char** arglist,char *cmd_line);
int main(int argc, char *argv[]){    char cmd_line[1024];    char *arglist[ARG_CNT];    while(memset(cmd_line,0,1024), printf("%s",PROMPT), fgets(cmd_line,1024,stdin))    {        if(cmd_line[0] != ‘\n‘)        {            cmd_line[strlen(cmd_line) - 1] = ‘\0‘; //將讀進來的分行符號變成‘\0‘            command_to_arglist(arglist,cmd_line);            if(fork() > 0)            {                wait(NULL);            }else            {                execvp(arglist[0],arglist);                exit(0);            }        }    }    return 0;}static int my_isspace(char c){    if(c == ‘ ‘|| c == ‘\n‘ || c == ‘\t‘ || c ==‘\v‘)        return 1;    else        return 0;}
static void command_to_arglist(char** arglist,char *cmd_line){    int cnt = 0;    int bg,end;    bg = 0;    while(cmd_line[bg] != ‘\0‘)    {        while(my_isspace(cmd_line[bg]))        {            bg++;        }        if(cmd_line[bg] == ‘\0‘)        {            break;        }        end = bg;        while(cmd_line[end] != ‘\0‘ && !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;    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.