Piping in my own C shell -


i've implemented beginning of c shell below. far have redirection working, , thought implement | in similar way having difficulty. can help? begin checking pipe operator, saving sa[i-1] , sa[i+1] 2 separate commands, i'm not sure how fork() , exec() after this.

int startprocess (stringarray sa) {   int pid;    int status;   int fd1;   int fd2;    int current_in;   int current_out;   int fd0;   int fd00;   int in = 0;   int out = 0;    char input[64]="";   char output[64]="";   char cmd1[64] ="";   char cmd2[64] ="";   int fd[2];   int pipe = 0;     switch( pid = fork()){  case -1://this error     perror("failure of child.");    return 1;  case 0: // child    // redirection      /* finds '<' or '>' occurs , make sa[i] = null ,       ensure command wont' read that*/      for(int i=0;sa[i]!='\0';i++)     {         if(strcmp(sa[i],"<")==0)         {                     sa[i]=null;             strcpy(input,sa[i+1]);             in=2;                    }                         if(strcmp(sa[i],">")==0)         {                   sa[i]=null;             strcpy(output,sa[i+1]);             out=2;         }      }      //if '<' char found in string inputted user     if(in)     {             // fdo file-descriptor         int fd0;         if ((fd0 = open(input, o_rdonly, 0)) < 0) {             perror("couldn't open input file");             exit(0);         }                    // dup2() copies content of fdo in input of preceeding file         dup2(fd0, 0); // stdin_fileno here can replaced 0           close(fd0); // necessary     }      //if '>' char found in string inputted user      if (out)     {          int fd00 ;         if ((fd00 = creat(output , 0644)) < 0) {             perror("couldn't open output file");             exit(0);         }                     dup2(fd00, stdout_fileno); // 1 here can replaced stdout_fileno         close(fd00);     }             execvp(sa[0], sa);           perror("execvp");           _exit(1);       printf("could not execute '%s'\n", sa[0]);   default:// parent     wait(&status);    return (status == 0) ? 0: 1;   } } 

  1. make pipe.
  2. fork().
  3. in parent set stdout file descriptor (1) input of pipe.
  4. in child set stdin file descriptor (0) output of pipe.
  5. exec() in both parent , child.

do of in child after fork(), redirection.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -