Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <wait.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6.  
  7. enum
  8. {
  9.     MODE = 0666,
  10. };
  11.  
  12. int
  13. main(int argc, char **argv)
  14. {
  15.     int fd[2];
  16.     pipe(fd);
  17.  
  18.     pid_t pid12 = fork();
  19.     if (pid12 < 0) {
  20.         _exit(0);
  21.     } else if (!pid12) {
  22.         if (close(fd[0]) < 0 || dup2(fd[1], 1) < 0 || close(fd[1]) < 0) {
  23.             _exit(1);
  24.         }
  25.         pid_t pid1 = fork();
  26.         if (pid1 < 0) {
  27.             _exit(0);
  28.         } else if (!pid1) {
  29.             int in1 = open(argv[4], O_RDONLY);
  30.             if (in1 < 0 || dup2(in1, 0) < 0 || close(in1) < 0) {
  31.                 _exit(1);
  32.             }
  33.             execlp(argv[1], argv[1], NULL);
  34.             _exit(1);
  35.         } else {
  36.             int status;
  37.             wait(&status);
  38.             if (!WIFEXITED(status) || WEXITSTATUS(status)) {
  39.                 _exit(0);
  40.             }
  41.             pid_t pid2 = fork();
  42.             if (pid2 < 0) {
  43.                 _exit(0);
  44.             } else if (!pid2) {
  45.                 execlp(argv[2], argv[2], NULL);
  46.                 _exit(1);
  47.             } else {
  48.                 wait(NULL);
  49.                 _exit(0);
  50.             }
  51.         }
  52.     } else {
  53.         pid_t pid3 = fork();
  54.         if (pid3 < 0) {
  55.             _exit(0);
  56.         } else if (!pid3) {
  57.             if (close(fd[1]) < 0 || dup2(fd[0], 0) < 0 || close(fd[0]) < 0) {
  58.                 _exit(1);
  59.             }
  60.             int out3 = open(argv[5], O_CREAT | O_APPEND | O_WRONLY, MODE);
  61.             if (out3 < 0 || dup2(out3, 1) < 0 || close(out3) < 0) {
  62.                 _exit(0);
  63.             }
  64.             execlp(argv[3], argv[3], NULL);
  65.             _exit(1);
  66.         } else {
  67.             close(fd[0]);
  68.             close(fd[1]);
  69.             while (wait(NULL) != -1);
  70.             exit(0);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement