Advertisement
wowonline

Untitled

Nov 28th, 2021
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 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, pid1;
  9.     int fd, status;
  10.     int fds[2];
  11.     if (pipe(fds) == -1) {
  12.         return 1;
  13.     }
  14.  
  15.     pid1 = fork();
  16.     if (pid1 == -1) {
  17.         return 1;
  18.     } else if (!pid1) {
  19.         pid1 = fork();
  20.         if (pid1 == -1) {
  21.             return 1;
  22.         } else if (!pid1) {
  23.             fd = open(argv[4], O_RDONLY);
  24.             if (fd == -1) {
  25.                 _exit(1);
  26.             }
  27.             close(fds[0]);
  28.             dup2(fd, 0);
  29.             close(fd);
  30.             dup2(fds[1], 1);
  31.             close(fds[1]);
  32.             execlp(argv[1], argv[1], NULL);
  33.             _exit(1);
  34.         }
  35.  
  36.         close(fds[0]);
  37.         dup2(fds[1], 1);
  38.         close(fds[1]);
  39.         waitpid(pid1, &status, 0);
  40.         if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  41.             execlp(argv[2], argv[2], NULL);
  42.             _exit(1);
  43.         }
  44.     }
  45.  
  46.     pid = fork();
  47.     if (pid == -1) {
  48.         return 1;
  49.     } else if (!pid) {
  50.         fd = open(argv[5], O_CREAT | O_WRONLY | O_APPEND, 0777);
  51.         if (fd == -1) {
  52.             _exit(1);
  53.         }
  54.         dup2(fds[0], 0);
  55.         dup2(fd, 1);
  56.         close(fds[0]);
  57.         close(fds[1]);
  58.         close(fd);
  59.         execlp(argv[3], argv[3], NULL);
  60.         _exit(1);
  61.     }
  62.  
  63.     close(fds[0]);
  64.     close(fds[1]);
  65.     waitpid(pid, NULL, 0);
  66.     waitpid(pid1, NULL, 0);
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement