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, pid1;
- int fd, status;
- int fds[2];
- if (pipe(fds) == -1) {
- return 1;
- }
- pid1 = fork();
- if (pid1 == -1) {
- return 1;
- } else if (!pid1) {
- pid1 = fork();
- if (pid1 == -1) {
- return 1;
- } else if (!pid1) {
- fd = open(argv[4], O_RDONLY);
- if (fd == -1) {
- _exit(1);
- }
- close(fds[0]);
- dup2(fd, 0);
- close(fd);
- dup2(fds[1], 1);
- close(fds[1]);
- execlp(argv[1], argv[1], NULL);
- _exit(1);
- }
- close(fds[0]);
- dup2(fds[1], 1);
- close(fds[1]);
- waitpid(pid1, &status, 0);
- if (WIFEXITED(status) && !WEXITSTATUS(status)) {
- execlp(argv[2], argv[2], NULL);
- _exit(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);
- }
- dup2(fds[0], 0);
- dup2(fd, 1);
- close(fds[0]);
- close(fds[1]);
- close(fd);
- execlp(argv[3], argv[3], NULL);
- _exit(1);
- }
- close(fds[0]);
- close(fds[1]);
- waitpid(pid, NULL, 0);
- waitpid(pid1, NULL, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement