Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/wait.h>
  4. #include <sys/unistd.h>
  5.  
  6. typedef struct {
  7. int in;
  8. int out;
  9. } __attribute__
  10. ((packed, aligned(sizeof(int))))
  11. pipe_fds_t;
  12.  
  13. int
  14. main (int argc, char **argv) {
  15. char **cmds = argv + 1;
  16. int cmds_amt = argc - 1, cmd_id = 0;
  17. for (; cmd_id < cmds_amt; ++cmd_id) {
  18. pipe_fds_t pipes;
  19. pipe((int *) &pipes);
  20. pid_t pid;
  21. if ((pid = fork()) < 0) {
  22. fprintf(stderr, "FORK ERROR");
  23. exit(1);
  24. } else if (!pid) {
  25. if (cmd_id != cmds_amt - 1) {
  26. dup2(pipes.out, 1);
  27. }
  28. close(pipes.in);
  29. close(pipes.out);
  30. execlp(cmds[cmd_id], cmds[cmd_id], NULL);
  31. perror("exec");
  32. exit(1);
  33. } else {
  34. dup2(pipes.in, 0);
  35. close(pipes.in);
  36. waitpid(pid, NULL, 0);
  37. close(pipes.out);
  38. }
  39. }
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement