2

I'm trying to implement some Shell commands in C but the Pipes are giving me some trouble (I kicked most code out so you guys can compile it - if needed)

I worked pretty close to Mkabs solution which he posted 2011 old post

After going through it, I thought I had it down, but its not working.

simple example: >> **ls | sort -r **

  • first child uses first pipe1 as stdout{1}
  • second child uses first pipe[0] as stdin{0}

but both exec() fails with: ENOENT, No such file or directory
It doesnt matter if I read the commands from userInput or hard code a given string, it always fails.

So the error must be in the ExecutePipe() fkt, no need to go thru the rest.

    #define _POSIX_C_SOURCE 1
    #include <alloca.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <signal.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/times.h>
    #include <sys/wait.h>


    typedef char* string;
    static int keepRunning = 1;
    char inputBuffer[512];
    string satz[256];
    int j,comCount, modePipe,mode;

    void Tokens(string);
    void ExecutePipe(void);
    int ScanInput(void);


    void Tokens(string token){
        int len;
        char* ptrC;
        j=0;
        satz[j] = strtok (inputBuffer, token); 
        while (satz[j] != NULL)
        {
            j++;
            satz[j] = strtok (NULL, token); 
        }
        comCount =j;  


        /*REPLACE NEWLINE of LAST COMMAND */
        len =strlen(satz[j-1]);
        ptrC = satz[j-1];
        for (j=0;j<len;j++){
            if (*ptrC == '\n'){
                *ptrC = '\0';
            }
            ptrC++;
        }
    }

    void ExecutePipe(void){
        int* ptrPipe;
        int i,k=0, numPipes;
        pid_t pid, status;
        int  stdin_dupfd,stdout_dupfd;
        string* ptrSatz = satz;
        modePipe=1;
        Tokens("|");
        stdin_dupfd = dup(0);
        stdout_dupfd = dup(1);

        numPipes = comCount-1;

        ptrPipe = (int*) alloca(numPipes*2);

        for(i = 0; i < numPipes; i++){
            if(pipe(ptrPipe + i*2) < 0) {
                printf("Error: pipe(%d)\n",i);
                return;
            }
        }

        while(k<comCount) {
            pid = fork();
            if(pid < 0){
                fprintf(stderr,"Error: PID.%d\n", pid);
                exit(9);
            }
            else if(pid == 0) {  /* child gets input from the previous command*/
                if(k > 0){      /*if not first command*/
                    if(dup2(ptrPipe[(k-1) * 2], 1) < 0){ /*ptr[0]. ptr[2], ptr[4], ...*/    
                        fprintf(stderr,"Error: dup2(firstCommand)\n");
                        exit(10);
                    }
                    else fprintf(stderr,"k[%d]:dupe2([%d], 0)\n",k,(k-1)*2);
                }
                if(k != (comCount-1) ){ /*if not last command*/
                    if(dup2(ptrPipe[k * 2 + 1], 1) < 0){ /*ptr[1]. ptr[3], ptr[5], ...*/    
                        fprintf(stderr,"Error: dup2(notLastCommand)\n");
                        exit(11);
                    }
                    else fprintf(stderr,"k[%d]:dupe2([%d], 1)\n",k,k*2+1);
                }
                for(i = 0; i < 2*numPipes; i++){
                    close(ptrPipe[i]);
                }
                if( execlp(*ptrSatz, *ptrSatz) < 0 ){   /*  */
                    fprintf(stderr,"Error: ");
                    fprintf(stderr,"exec(%s)(%d)\n", *ptrSatz, errno);
                    exit(12);
                }
            }

            fprintf(stderr,"[%d]: %s\n", k,*ptrSatz);
            ptrSatz++;
            k++;
        } 

        for(i = 0; i < 2 * numPipes; i++){
            close(ptrPipe[i]);
            /*printf("loop %d of %d\n ",i, 2*numPipes);*/
        }
        for(i = 0; i < numPipes + 1; i++){
            wait(&status);
        }

        dup2(stdin_dupfd, 0);
        dup2(stdout_dupfd, 1);
        close(stdin_dupfd);     
        close(stdout_dupfd);    

        inputBuffer[0] = '\0';
        modePipe=0;
    }   

    int main (void)
    {
        while (keepRunning){
            printf(">> ");
            fgets(inputBuffer, 512, stdin);   /* input buffer, max.Input(char), whereFrom?*/
            mode = ScanInput(); /*checks inputBuffer on keywords, returns int 1-13*/
            /*printf(">> mode:%d\n", mode);*/
            switch(mode){
                case 11 :
                    ExecutePipe();
                    break; 
            }
        }
        return 0;
    }

    int ScanInput(void){
        char * pch;

        pch = strstr (inputBuffer," | ");
        if (pch != NULL)
            return 11;
        else 
            return 1;
    }

Thanks!

Community
  • 1
  • 1
Yama
  • 178
  • 9

2 Answers2

0

exec can only call a single binary. You gave here a shell command. With the system() function you can call shell commands, too, but it works only because it calls a bash to interpret it.

The simplest solution were to call this line by system, and exit-ing after it, like

exec("complex|shell|command >&3");

needs to be changed to

system("complex|shell|command >&3");
exit(0);
peterh
  • 11,875
  • 18
  • 85
  • 108
  • every Child invokes only a single binary + parameters. [1]exec(ls) [2]exec(sort -r). – Yama Mar 28 '14 at 14:38
0

Had an error with my dup2 iteration variable. The expected behaviour is:

dup2 - notLast      notFirst
       1,1          [first]
       3,1          0,0
       5,1          2,0
       7,1          4,0
       ..           ..

And the notLast part incremented wrongly...

After fixing this, it ran smoothly

Yama
  • 178
  • 9