Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <wait.h>
- #include <unistd.h>
- #include <fcntl.h>
- enum
- {
- MODE = 0666,
- };
- int
- main(int argc, char **argv)
- {
- int fd[2];
- pipe(fd);
- pid_t pid12 = fork();
- if (pid12 < 0) {
- _exit(0);
- } else if (!pid12) {
- if (close(fd[0]) < 0 || dup2(fd[1], 1) < 0 || close(fd[1]) < 0) {
- _exit(1);
- }
- pid_t pid1 = fork();
- if (pid1 < 0) {
- _exit(0);
- } else if (!pid1) {
- int in1 = open(argv[4], O_RDONLY);
- if (in1 < 0 || dup2(in1, 0) < 0 || close(in1) < 0) {
- _exit(1);
- }
- execlp(argv[1], argv[1], NULL);
- _exit(1);
- } else {
- int status;
- wait(&status);
- if (!WIFEXITED(status) || WEXITSTATUS(status)) {
- _exit(0);
- }
- pid_t pid2 = fork();
- if (pid2 < 0) {
- _exit(0);
- } else if (!pid2) {
- execlp(argv[2], argv[2], NULL);
- _exit(1);
- } else {
- wait(NULL);
- _exit(0);
- }
- }
- } else {
- pid_t pid3 = fork();
- if (pid3 < 0) {
- _exit(0);
- } else if (!pid3) {
- if (close(fd[1]) < 0 || dup2(fd[0], 0) < 0 || close(fd[0]) < 0) {
- _exit(1);
- }
- int out3 = open(argv[5], O_CREAT | O_APPEND | O_WRONLY, MODE);
- if (out3 < 0 || dup2(out3, 1) < 0 || close(out3) < 0) {
- _exit(0);
- }
- execlp(argv[3], argv[3], NULL);
- _exit(1);
- } else {
- close(fd[0]);
- close(fd[1]);
- while (wait(NULL) != -1);
- exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement