Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <string.h>
  8. #include <dirent.h>
  9. #include <sys/types.h>
  10. #include <sys/wait.h>
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. int pfd[2];
  15. int pfd2[2];
  16. int pid;
  17.  
  18. if(pipe(pfd)<0)
  19. {
  20. printf("Error creating the pipe");
  21. exit(1);
  22. }
  23.  
  24. if(pipe(pfd2)<0)
  25. {
  26. printf("Error creating the pipe");
  27. exit(1);
  28. }
  29.  
  30. if((pid=fork())<0)
  31. {
  32. printf("Error fork");
  33. exit(1);
  34. }
  35.  
  36. if(pid>0)
  37. {
  38.  
  39. int pid2;
  40. if((pid2=fork())<0)
  41. {
  42. printf("Error fork");
  43. exit(1);
  44. }
  45. if(pid2>0)
  46. {
  47. int pid3;
  48. if((pid3=fork())<0)
  49. {
  50. printf("Error fork");
  51. exit(1);
  52. }
  53. if(pid3>0){
  54.  
  55. close(pfd2[0]);
  56. close(pfd[0]);
  57. char c;
  58. int n;
  59. int fd = open(argv[1], O_RDONLY);
  60. if (fd < 0)
  61. {
  62. printf("Error opening the file\n");
  63. exit(1);
  64. }
  65.  
  66. while ((n = read(fd, &c, 1)) > 0)
  67. {
  68. int i = c - '0';
  69. if(i>=0&&i<=9)
  70. {
  71. if(i%2!=0)
  72. {
  73. write(pfd2[1], &c, 1);
  74. }
  75. else
  76. {
  77. write(pfd[1], &c, 1);
  78. }
  79. }
  80. }
  81.  
  82. close(pfd2[1]);
  83. close(pfd[1]);
  84.  
  85.  
  86. int status;
  87. if ( waitpid(pid, &status, 0) == -1 ) {
  88. perror("waitpid failed");
  89. return EXIT_FAILURE;
  90. }
  91.  
  92. if ( WIFEXITED(status) ) {
  93. const int es = WEXITSTATUS(status);
  94. printf("Exit status for %d(odd digits): %d\n", pid, es);
  95. }
  96.  
  97. if ( waitpid(pid2, &status, 0) == -1 ) {
  98. perror("waitpid failed");
  99. return EXIT_FAILURE;
  100. }
  101.  
  102. if ( WIFEXITED(status) ) {
  103. const int es = WEXITSTATUS(status);
  104. printf("Exit status for %d(even digits): %d\n", pid2, es);
  105. }
  106.  
  107. exit(0);
  108. }
  109. else
  110. {
  111. char * const ptr[]={"/bin/sh","-c","ls -l" ,NULL};
  112. execv("/bin/sh",ptr);
  113. exit(0);
  114. }
  115.  
  116. }
  117. else
  118. {
  119. close(pfd[1]);
  120. close(pfd2[1]);
  121. close(pfd2[0]);
  122.  
  123. char c;
  124. int t, evenCount=0;
  125.  
  126. while((t = read(pfd[0], &c, 1)) > 0)
  127. {
  128. evenCount++;
  129. }
  130.  
  131. //printf("%d even digit(s)\n", evenCount);
  132.  
  133. close(pfd[0]);
  134. exit(evenCount);
  135. }
  136.  
  137.  
  138. }
  139. else
  140. {
  141. close(pfd2[1]);
  142. close(pfd[1]);
  143. close(pfd[0]);
  144.  
  145. char c;
  146. int r, count=0;
  147.  
  148. while((r = read(pfd2[0], &c, 1))>0)
  149. {
  150. count++;
  151. }
  152.  
  153. //printf("%d odd digit(s)\n", count);
  154.  
  155. close(pfd2[0]);
  156. exit(count);
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement