All pastes #944898 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-03-16 15:27 UTC
rendered paste body
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void) {
   pid_t childpid;

   int fd[2]; //file descriptor1

   if (pipe(fd)==-1) {
     printf(" pipe failed \n" );
   }

   childpid = fork();

   if (childpid==0) {                                  /* ls is the child */
     if (dup2(fd[1] , 1) == -1) {
       perror("Failed to redirect stdout of ls");
     }
     else {
       if (execl("/bin/cat", "cat", "sortthis.txt", NULL) == -1) {
         perror("catting test failed");
       }
     }
     return 1;
   }

   if (dup2(fd[0], 0) == -1)                /* word count is the parent */
       perror("Failed to redirect stdin of sort");
   else {
     if (execl("/bin/sort", "sort", "-r", NULL) == -1) {
       perror("could not sort anything");
     }
   }
   return 1;
}

//Sorthis.txt is really some numbers, one on each line 
//5
//3
//1
//2
//4