Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. struct stringlist *scan;
  2. int fd[2];
  3.  
  4. /*
  5.      SCHEME:
  6.         STDIN --> O --> O --> O --> STDOUT
  7. */
  8. int eval(tree,inport,outport)
  9.      evptr tree;
  10.      int inport, outport;
  11. {
  12.  
  13.   if (tree->tag == PIPETAG) {
  14.     pipe(fd);
  15.    
  16.     eval(tree->son.left, 0, 1);
  17.     eval(tree->son.right, 1, 0);
  18.    
  19.   } else if (tree->tag == LEAF) {
  20.  
  21.     char **commands;
  22.     int i=0;
  23.  
  24.     commands = (char**) malloc(10 * sizeof(char*));        
  25.     if (commands == NULL)
  26.       exit(-1);
  27.  
  28.     for (scan = tree->leaf.data; scan != NULL; scan = scan->next) {
  29.       commands[i] = scan->fname;
  30.       i++;
  31.     }
  32.     commands[i] = NULL;
  33.  
  34.     //======================================================
  35.    
  36.     pid_t pid;
  37.  
  38.     pid = sfork();
  39.     if (pid == 0) {
  40.       printf("inport: %d outport: %d\n", inport, outport);
  41.      
  42.       if (inport == 0) {
  43.         dup2(fd[WRITE_END], STDOUT_FILENO);
  44.       } else if (inport != 0) {
  45.         dup2(inport, STDIN_FILENO);
  46.         //dup2(fd[WRITE_END], STDOUT_FILENO);
  47.       }
  48.  
  49.       execvp(commands[0], commands);    
  50.       exit(1);
  51.     }
  52.  
  53.     else {
  54.       //close(fd[READ_END]);
  55.       //close(fd[WRITE_END]);
  56.       free(commands);
  57.       wait(NULL);
  58.     }  
  59.   } else if (tree->tag == NULLTAG) {
  60.    
  61.   } else if (tree->tag == EXITAG) {
  62.     exit(0);
  63.   } else {
  64.     exit(1);
  65.   }
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement