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