Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <inttypes.h>
- #include <stdint.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- void swap(int* x, int* y) {
- int z = (*x);
- (*x) = (*y);
- (*y) = z;
- }
- int main(int argc, char* argv[]) {
- int pipefd[2];
- int pipefd2[2] = {11111, 22901};
- pid_t pid = 0;
- for (int i = 1; i < argc; i++) {
- char* command = argv[i];
- if (i != argc - 1) pipe(pipefd);
- pid = fork();
- if (pid == 0) {
- if (i != 1) {
- close(pipefd2[1]);
- dup2(pipefd2[0], 0);
- close(pipefd2[0]);
- }
- if (i != argc - 1) {
- close(pipefd[0]);
- dup2(pipefd[1], 1);
- close(pipefd[1]);
- }
- execlp(command, command, NULL);
- exit(1);
- }
- else {
- swap(pipefd, pipefd2);
- swap(pipefd + 1, pipefd2 + 1);
- close(pipefd[0]);
- close(pipefd[1]);
- }
- wait(NULL);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement