Advertisement
wowonline

Untitled

Nov 28th, 2021
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 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.         pid = fork();
  20.         if (pid == -1) {
  21.             return 1;
  22.         } else if (!pid) {
  23.             if (close(fds[0]) == -1) {
  24.                 _exit(1);
  25.             }
  26.             fd = open(argv[4], O_RDONLY);
  27.             if (fd == -1) {
  28.                 _exit(1);
  29.             }
  30.             if (dup2(fd, 0) == -1) {
  31.                 _exit(1);
  32.             }
  33.             if (dup2(fds[1], 1) == -1) {
  34.                 _exit(1);
  35.             }
  36.             if (close(fds[1]) == -1) {
  37.                 _exit(1);
  38.             }
  39.             if (close(fd) == -1) {
  40.                 _exit(1);
  41.             }
  42.             execlp(argv[1], argv[1], NULL);
  43.             _exit(1);
  44.         }
  45.         if (wait(&status) == -1) {
  46.             return 1;
  47.         }
  48.        
  49.         if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  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.     wait(NULL);
  68.  
  69.     pid = fork();
  70.     if (pid == -1) {
  71.         return 1;
  72.     } else if (!pid) {
  73.         fd = open(argv[5], O_CREAT | O_WRONLY | O_APPEND, 0777);
  74.         if (fd == -1) {
  75.             _exit(1);
  76.         }
  77.         if (dup2(fds[0], 0) == -1) {
  78.             _exit(1);
  79.         }
  80.         if (dup2(fds[1], 1) == -1) {
  81.             _exit(1);
  82.         }
  83.         if (close(fds[0]) == -1) {
  84.             return 1;
  85.         }
  86.         if (close(fds[1]) == -1) {
  87.             return 1;
  88.         }
  89.         execlp(argv[3], argv[3], NULL);
  90.         _exit(1);
  91.     }
  92.  
  93.     if (close(fds[0]) == -1) {
  94.         return 1;
  95.     }
  96.     if (close(fds[1]) == -1) {
  97.         return 1;
  98.     }
  99.     if (wait(NULL) == -1) {
  100.         return 1;
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement