Advertisement
Guest User

Untitled

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