SHARE
TWEET

Untitled

a guest Dec 9th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <inttypes.h>
  8. #include <sched.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <stdbool.h>
  12.  
  13. int main(int argc, char* argv[]) {
  14.     int cmd_num = 1;
  15.     int in_pair[2];
  16.     int out_pair[2];
  17.     pipe(in_pair);
  18.     pipe(out_pair);
  19.     while (cmd_num != argc) {
  20.         pid_t pid = fork();
  21.         if (pid == 0) {
  22.             if (cmd_num != 1) {
  23.                 dup2(in_pair[0], 0);
  24.                 close(in_pair[0]);
  25.                 close(in_pair[1]);
  26.             }
  27.             if (cmd_num != argc - 1) {
  28.                 dup2(out_pair[1], 1);
  29.                 close(out_pair[1]);
  30.             }
  31.             execlp(argv[cmd_num], argv[cmd_num], NULL);
  32.             perror("failed to execlp");
  33.             exit(1);
  34.         } else {
  35.             close(in_pair[0]);
  36.             close(in_pair[1]);
  37.             int child_state;
  38.             waitpid(pid, &child_state, 0);
  39.  
  40.             if (cmd_num == argc - 1) {
  41.                 return 0;
  42.             }
  43.             in_pair[0] = out_pair[0];
  44.             in_pair[1] = out_pair[1];
  45.             pipe(out_pair);
  46.             ++cmd_num;
  47.         }
  48.     }
  49.     return 0;
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top