Guest User

Untitled

a guest
Aug 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. #define PERROR_AND_DIE(_x_) {perror(_x_); _exit(1);}
  5.  
  6. int main(int argc, char **argv) {
  7. int fd0[2];
  8. int fd1[2];
  9.  
  10.  
  11. if ( argc != 3 ) {
  12. fprintf(stdout, "Usage %s: \"[command 1]\" \"[command 2]\"\n", argv[0]);
  13. _exit(1);
  14. }
  15.  
  16. if ( pipe(fd0) || pipe(fd1) ) PERROR_AND_DIE("pipe")
  17.  
  18. pid_t id = fork();
  19. if ( id == -1 ) PERROR_AND_DIE("fork");
  20.  
  21. if ( id ) {
  22. if ( -1 == close(0) ) PERROR_AND_DIE("P1: close 0");
  23. if ( -1 == dup2(fd0[0], 0) ) PERROR_AND_DIE("P1: dup 0"); //Read my STDIN from this pipe
  24.  
  25. if ( -1 == close(1) ) PERROR_AND_DIE("P1: close 1");
  26. if ( -1 == dup2(fd1[1], 1) ) PERROR_AND_DIE("P1: dup 1"); //Write my STDOUT here
  27. execl("/bin/sh", "/bin/sh", "-c", argv[1], NULL);
  28. PERROR_AND_DIE("P1: exec")
  29. }
  30.  
  31. if ( -1 == close(0) ) PERROR_AND_DIE("P2: close 0");
  32. if ( -1 == dup2(fd1[0], 0) ) PERROR_AND_DIE("P2: dup 0");
  33.  
  34. if ( -1 == close(1) ) PERROR_AND_DIE("P2: close 1");
  35. if ( -1 == dup2(fd0[1], 1) ) PERROR_AND_DIE("P2: dup 1");
  36.  
  37.  
  38. execl("/bin/sh", "/bin/sh", "-c", argv[2], NULL);
  39. PERROR_AND_DIE("P2: exec")
  40. }
Add Comment
Please, Sign In to add comment