Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/wait.h>
- #include <sys/unistd.h>
- typedef struct {
- int in;
- int out;
- } __attribute__
- ((packed, aligned(sizeof(int))))
- pipe_fds_t;
- int
- main (int argc, char **argv) {
- char **cmds = argv + 1;
- int cmds_amt = argc - 1, cmd_id = 0;
- for (; cmd_id < cmds_amt; ++cmd_id) {
- pipe_fds_t pipes;
- pipe((int *) &pipes);
- pid_t pid;
- if ((pid = fork()) < 0) {
- fprintf(stderr, "FORK ERROR");
- exit(1);
- } else if (!pid) {
- if (cmd_id != cmds_amt - 1) {
- dup2(pipes.out, 1);
- }
- close(pipes.in);
- close(pipes.out);
- execlp(cmds[cmd_id], cmds[cmd_id], NULL);
- perror("exec");
- exit(1);
- } else {
- dup2(pipes.in, 0);
- close(pipes.in);
- waitpid(pid, NULL, 0);
- close(pipes.out);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement