Advertisement
pochti_da

Untitled

Dec 14th, 2020
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <string.h>
  3. #include <sys/mman.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include <stdbool.h>
  10.  
  11. int
  12. main(int argc, char *argv[])
  13. {
  14.     int pip1[2];
  15.     pipe2(pip1, O_CLOEXEC);
  16.  
  17.     if (!fork()) {
  18.         dup2(pip1[1], 1);
  19.         execlp(argv[1], argv[1], NULL);
  20.     }
  21.  
  22.     int pip2[2];
  23.     pipe2(pip2, O_CLOEXEC);
  24.     if (!fork()) {
  25.         dup2(pip1[0], 0);
  26.         dup2(pip2[1], 1);
  27.  
  28.         close(pip1[0]);
  29.         close(pip1[1]);
  30.         close(pip2[0]);
  31.         close(pip2[1]);
  32.  
  33.         int cmd2_pid = fork();
  34.  
  35.         if (cmd2_pid == 0) {
  36.             int file = open(argv[5], O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
  37.             dup2(file, 2);
  38.             execlp(argv[2], argv[2], NULL);
  39.         }
  40.  
  41.         int st;
  42.         waitpid(cmd2_pid, &st, 0);
  43.  
  44.         if (!WIFEXITED(st) || WEXITSTATUS(st) != 0) {
  45.             _exit(0);
  46.         }
  47.  
  48.         if (!fork()) {
  49.             execlp(argv[3], argv[3], NULL);
  50.         }
  51.  
  52.         wait(NULL);
  53.         _exit(0);
  54.     }
  55.  
  56.     if (!fork()) {
  57.         dup2(pip2[0], 0);
  58.         execlp(argv[4], argv[4], NULL);
  59.     }
  60.  
  61.     wait(NULL);
  62.     wait(NULL);
  63.     wait(NULL);
  64.  
  65.     return 0;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement