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