Advertisement
wowonline

Untitled

Nov 28th, 2021
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <sys/wait.h>
  4.  
  5. int
  6. main(int argc, char *argv[])
  7. {
  8.     pid_t pid;
  9.     int fd, status;
  10.     int fds[2];
  11.     if (pipe(fds) == -1) {
  12.         return 1;
  13.     }
  14.  
  15.     pid = fork();
  16.     if (pid == -1) {
  17.         return 1;
  18.     } else if (!pid) {
  19.         if (close(fds[0]) == -1) {
  20.             _exit(1);
  21.         }
  22.         fd = open(argv[4], O_RDONLY);
  23.         if (fd == -1) {
  24.             _exit(1);
  25.         }
  26.         if (dup2(fd, 0) == -1) {
  27.             _exit(1);
  28.         }
  29.         if (dup2(fds[1], 1) == -1) {
  30.             _exit(1);
  31.         }
  32.         if (close(fds[1]) == -1) {
  33.             _exit(1);
  34.         }
  35.         if (close(fd) == -1) {
  36.             _exit(1);
  37.         }
  38.         execlp(argv[1], argv[1], NULL);
  39.         _exit(1);
  40.     }
  41.     if (wait(&status) == -1) {
  42.         return 1;
  43.     }
  44.    
  45.     if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  46.         pid = fork();
  47.         if (pid == -1) {
  48.             return 1;
  49.         } else if (!pid) {
  50.             if (close(fds[0]) == -1) {
  51.                 _exit(1);
  52.             }
  53.             if (dup2(fds[1], 1) == -1) {
  54.                 _exit(1);
  55.             }
  56.             if (close(fds[1]) == -1) {
  57.                 _exit(1);
  58.             }
  59.             execlp(argv[2], argv[2], NULL);
  60.             _exit(1);
  61.         }
  62.         if (wait(NULL) == -1) {
  63.             return 1;
  64.         }
  65.     }
  66.  
  67.     pid = fork();
  68.     if (pid == -1) {
  69.         return 1;
  70.     } else if (!pid) {
  71.         fd = open(argv[5], O_CREAT | O_WRONLY | O_APPEND, 0777);
  72.         if (fd == -1) {
  73.             _exit(1);
  74.         }
  75.         if (dup2(fds[0], 0) == -1) {
  76.             _exit(1);
  77.         }
  78.         if (dup2(fds[1], 1) == -1) {
  79.             _exit(1);
  80.         }
  81.         if (close(fds[0]) == -1) {
  82.             return 1;
  83.         }
  84.         if (close(fds[1]) == -1) {
  85.             return 1;
  86.         }
  87.         execlp(argv[3], argv[3], NULL);
  88.         _exit(1);
  89.     }
  90.  
  91.     if (close(fds[0]) == -1) {
  92.         return 1;
  93.     }
  94.     if (close(fds[1]) == -1) {
  95.         return 1;
  96.     }
  97.     if (wait(NULL) == -1) {
  98.         return 1;
  99.     }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement