Advertisement
Guest User

processline

a guest
Nov 29th, 2012
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. void processline (char *line, int inFD, int outFD, plFlags FLAGS) //, int mainargc, char **mainargv, char **shiftedMArgV)
  2. {
  3.     int    status;
  4.     char expanded_line[EXPANDEDLEN];
  5.     char **args;
  6.     char *line_itr = line;
  7.     int command_count;
  8.     int pix;
  9.     int fd[2];
  10.     int line_len = strlen(line);
  11.     plFlags pl_flags = {0U, 0U, 0U};
  12.    
  13.     if(FLAGS.EXPAND)
  14.     {
  15.         if(expand(line, expanded_line, EXPANDEDLEN)) return; //, mainargc, mainargv, shiftedMArgV)) return;
  16.         line_itr = expanded_line;
  17.         line_len = strlen(expanded_line);
  18.     }
  19.  
  20.     if((pix = findUnquotChar(line_itr, '|')))
  21.     {
  22.         line_itr[pix++] = 0;
  23.         if(pipe (fd) < 0) perror("pipe");
  24.         processline(line_itr, inFD, fd[1], pl_flags);
  25.         line_itr = &(line_itr[pix]);
  26.  
  27.         while((pix = findUnquotChar(line_itr, '|')) && pix < line_len)
  28.         {
  29.             line_itr[pix++] = 0;
  30.             //if(pipe (fd) < 0) perror("pipe");
  31.             processline(line_itr, fd[0], fd[1] pl_flags);
  32.             line_itr = &(line_itr[pix]);
  33.         }
  34.         return;
  35.     }
  36.    
  37.     command_count = arg_parse(expanded_line, &args);
  38.     /* print_args(args); */
  39.  
  40.     if(!command_count) return;
  41.  
  42.     if(!exec_if_built_in(args, outFD))
  43.     {  
  44.         /* Start a new process to do the job. */
  45.         cpid = fork();
  46.         if (cpid < 0) {
  47.             perror ("fork");
  48.             free(args);
  49.             return;
  50.         }
  51.        
  52.         /* Check for who we are! */
  53.         if (cpid == 0) {
  54.           /* We are the child! */
  55.    
  56.             /* Turning stdout into pipe output if needed */
  57.             if((dup2(outFD, 1)) < 0)
  58.             {
  59.                 perror("dup");
  60.                 return;
  61.             }
  62.            
  63.             execvp(args[0], args);
  64.             perror ("exec");
  65.             exit (127);
  66.             last_exit_status = 127;
  67.         }
  68.  
  69.         /* Have the parent wait for child to complete */
  70.         if(FLAGS.WAIT)
  71.         {
  72.             if(wait(&status) < 0) perror("wait");
  73.             last_exit_status = ((WEXITSTATUS(status) || !WIFEXITED(status)) ? 127 : 0);
  74.         }
  75.     }
  76.  
  77.     free(args);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement