Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/wait.h>
- int
- main(int argc, char *argv[])
- {
- pid_t pid;
- int fd, status;
- int fds[2];
- if (pipe(fds) == -1) {
- return 1;
- }
- pid = fork();
- if (pid == -1) {
- return 1;
- } else if (!pid) {
- if (close(fds[0]) == -1) {
- _exit(1);
- }
- fd = open(argv[4], O_RDONLY);
- if (fd == -1) {
- _exit(1);
- }
- if (dup2(fd, 0) == -1) {
- _exit(1);
- }
- if (dup2(fds[1], 1) == -1) {
- _exit(1);
- }
- execlp(argv[1], argv[1], NULL);
- close(fds[1]);
- _exit(1);
- }
- if (wait(&status) == -1) {
- return 1;
- }
- if (WIFEXITED(status) && !WEXITSTATUS(status)) {
- pid = fork();
- if (pid == -1) {
- return 1;
- } else if (!pid) {
- if (close(fds[0]) == -1) {
- _exit(1);
- }
- if (dup2(fds[1], 1) == -1) {
- _exit(1);
- }
- execlp(argv[2], argv[2], NULL);
- close(fds[1]);
- _exit(1);
- }
- if (wait(NULL) == -1) {
- return 1;
- }
- }
- pid = fork();
- if (pid == -1) {
- return 1;
- } else if (!pid) {
- fd = open(argv[5], O_CREAT | O_WRONLY | O_APPEND, 0777);
- if (fd == -1) {
- _exit(1);
- }
- if (dup2(fds[0], 0) == -1) {
- _exit(1);
- }
- if (dup2(fds[1], 1) == -1) {
- _exit(1);
- }
- execlp(argv[3], argv[3], NULL);
- close(fds[0]);
- close(fds[1]);
- _exit(1);
- }
- if (wait(NULL) == -1) {
- return 1;
- }
- if (close(fds[0]) == -1) {
- return 1;
- }
- if (close(fds[1]) == -1) {
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement