Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <string.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int main(int argc, char *argv[])
- {
- int pfd[2];
- int pfd2[2];
- int pid;
- if(pipe(pfd)<0)
- {
- printf("Error creating the pipe");
- exit(1);
- }
- if(pipe(pfd2)<0)
- {
- printf("Error creating the pipe");
- exit(1);
- }
- if((pid=fork())<0)
- {
- printf("Error fork");
- exit(1);
- }
- if(pid>0)
- {
- int pid2;
- if((pid2=fork())<0)
- {
- printf("Error fork");
- exit(1);
- }
- if(pid2>0)
- {
- int pid3;
- if((pid3=fork())<0)
- {
- printf("Error fork");
- exit(1);
- }
- if(pid3>0){
- close(pfd2[0]);
- close(pfd[0]);
- char c;
- int n;
- int fd = open(argv[1], O_RDONLY);
- if (fd < 0)
- {
- printf("Error opening the file\n");
- exit(1);
- }
- while ((n = read(fd, &c, 1)) > 0)
- {
- int i = c - '0';
- if(i>=0&&i<=9)
- {
- if(i%2!=0)
- {
- write(pfd2[1], &c, 1);
- }
- else
- {
- write(pfd[1], &c, 1);
- }
- }
- }
- close(pfd2[1]);
- close(pfd[1]);
- int status;
- if ( waitpid(pid, &status, 0) == -1 ) {
- perror("waitpid failed");
- return EXIT_FAILURE;
- }
- if ( WIFEXITED(status) ) {
- const int es = WEXITSTATUS(status);
- printf("Exit status for %d(odd digits): %d\n", pid, es);
- }
- if ( waitpid(pid2, &status, 0) == -1 ) {
- perror("waitpid failed");
- return EXIT_FAILURE;
- }
- if ( WIFEXITED(status) ) {
- const int es = WEXITSTATUS(status);
- printf("Exit status for %d(even digits): %d\n", pid2, es);
- }
- exit(0);
- }
- else
- {
- char * const ptr[]={"/bin/sh","-c","ls -l" ,NULL};
- execv("/bin/sh",ptr);
- exit(0);
- }
- }
- else
- {
- close(pfd[1]);
- close(pfd2[1]);
- close(pfd2[0]);
- char c;
- int t, evenCount=0;
- while((t = read(pfd[0], &c, 1)) > 0)
- {
- evenCount++;
- }
- //printf("%d even digit(s)\n", evenCount);
- close(pfd[0]);
- exit(evenCount);
- }
- }
- else
- {
- close(pfd2[1]);
- close(pfd[1]);
- close(pfd[0]);
- char c;
- int r, count=0;
- while((r = read(pfd2[0], &c, 1))>0)
- {
- count++;
- }
- //printf("%d odd digit(s)\n", count);
- close(pfd2[0]);
- exit(count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement