1. 程式人生 > >手把手教你編寫一個具有基本功能的shell(已開源)

手把手教你編寫一個具有基本功能的shell(已開源)

/*read command line until EOF*/while(read(stdin,buffer,numchars)){    /*parse command line*/    if(/* command line contains & */)        amper = 1;    else        amper = 0;    /* for commands not part of the shell command language */    if(fork() == 0)    {        /* redirection of IO?*/        if(/* redirect output 
*/)        {            fd = creat(newfile,fmask);            close(stdout);            dup(fd);            close(fd);            /* stdout is now redirected */        }        if(/* piping */)        {            pipe(fildes);            if(fork() == 0)            {            /* first component of command line
*/                close(stdout);                dup(fildes[1]);                close(fildes[1]);                close(fildes[0]);                /* stdout now goes to pipe */                /* child process does command */                execlp(command1,command1,0);            }            /* 2nd command component of command line
*/            close(stdin);            dup(fildes[0]);            close(fildes[0]);            close(fildes[1]);            /* standard input now comes from pipe */        }        execve(command2,command2,0);    }    /* parent continues over here ...    /* waits for child to exit if required    */   if(amper == 0)        retid = wait(&status);}