Advertisement
Guest User

Untitled

a guest
May 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <inttypes.h>
  7. #include <stdint.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10.  
  11. void swap(int* x, int* y) {
  12. int z = (*x);
  13. (*x) = (*y);
  14. (*y) = z;
  15. }
  16.  
  17. int main(int argc, char* argv[]) {
  18. int pipefd[2];
  19. int pipefd2[2] = {11111, 22901};
  20.  
  21. pid_t pid = 0;
  22. for (int i = 1; i < argc; i++) {
  23. char* command = argv[i];
  24. if (i != argc - 1) pipe(pipefd);
  25. pid = fork();
  26. if (pid == 0) {
  27. if (i != 1) {
  28. close(pipefd2[1]);
  29. dup2(pipefd2[0], 0);
  30. close(pipefd2[0]);
  31. }
  32. if (i != argc - 1) {
  33. close(pipefd[0]);
  34. dup2(pipefd[1], 1);
  35. close(pipefd[1]);
  36. }
  37. execlp(command, command, NULL);
  38. exit(1);
  39. }
  40. else {
  41. swap(pipefd, pipefd2);
  42. swap(pipefd + 1, pipefd2 + 1);
  43. close(pipefd[0]);
  44. close(pipefd[1]);
  45. }
  46. wait(NULL);
  47. }
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement