All pastes #944935 Raw Edit

Something

public text v1 · immutable
·published 2008-03-16 16:02 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 ((close(fd[0]) == -1) || (close(fd[1]) == -1)) {
       perror("failed at closing");
     }
     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 ((close(fd[0]) == -1) || (close(fd[1]) == -1)) {
     perror("failed at closing");
   }
   else {
     if (execl("usr/bin/sort", "sort", "-r", NULL) == -1) {
       perror("could not sort anything");
     }
   }
   return 1;
}