Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/wait.h>
  6. #include <sys/types.h>
  7.  
  8. int
  9. main(int argc, char *argv[])
  10. {
  11. int fdp[2];
  12. pipe(fdp);
  13. int status;
  14. if (fork() == 0) {
  15. pid_t pid;
  16. if ((pid = fork()) == 0) {
  17. int in = open(argv[4], O_RDONLY);
  18. close(fdp[0]);
  19. dup2(in, 0);
  20. dup2(fdp[1], 1);
  21. close(in);
  22. execlp(argv[1], argv[1], NULL);
  23. _exit(1);
  24. }
  25. waitpid(pid, &status, 0);
  26. if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  27. if (fork() == 0) {
  28. close(fdp[0]);
  29. dup2(fdp[1], 1);
  30. execlp(argv[2], argv[2], NULL);
  31. _exit(1);
  32. }
  33. }
  34. _exit(0);
  35. }
  36. close(fdp[1]);
  37. if (fork() == 0) {
  38. int out = open(argv[5], O_WRONLY | O_CREAT | O_APPEND, 0660);
  39. dup2(fdp[0], 0);
  40. dup2(out, 1);
  41. close(out);
  42. execlp(argv[3], argv[3], NULL);
  43. _exit(1);
  44. }
  45. close(fdp[0]);
  46. while (wait(NULL) != -1) {}
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement