Advertisement
Guest User

Untitled

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