All pastes #729894 Raw Edit

Someone

public text v1 · immutable
·published 2007-10-08 15:38 UTC
rendered paste body
#include "header.h"

int main(int argc, char *argv[])
{
  /* initialization variables */
  struct args these_args;
  int fd_in;
  int fd_out;
  int pfd[2];
  int status;
  char *num_of_lines;
  these_args = parse_input(argc, argv);
  fd_in = these_args.input_fd;
  fd_out = these_args.output_fd;
  num_of_lines = these_args.num_of_lines;
  if(fd_in != 0)
  {
    close(INPUT);
    dup(fd_in);
  }
  if(fd_out != 1)
  {
    close(OUTPUT);
    dup(fd_out);
  }
  pipe(pfd);
  if(fork() != 0)
  {
    if(fork() != 0) /* parent */
    {
      close(pfd[1]);
      close(pfd[2]);
      wait(&status);
      wait(&status);
    }
    else  /* child2 */
    {
      /* setup for writing */
      close(pfd[0]);
      close(OUTPUT);
      if(dup(pfd[1]) == -1)
      {
      	perror("dup2 failed");
      }
      close(pfd[1]); 
      execl("convert", "convert", 0);
    }
  }
  else { /* child1 */
    /* setup for reading */
    close(pfd[1]);
    close(INPUT);
    if(dup(pfd[0]) == -1)
    {
      perror("dup2 failed");
    }
    close(pfd[0]); 
    /* get stdin, which should be output from child2 */
    if(strcmp(num_of_lines, "") == 0)
    {
      execl("count", "count", 0);
    }
    else
    {
      execl("count", "count", num_of_lines , 0); 
    }
  }
  close(fd_in);
  close(fd_out);
}