Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7.  
  8. int n;
  9. char buff[100], buff2[100], filename[100];
  10. int pipechild[2], pipeparent[2];
  11. int childpid;
  12.  
  13. int main(){
  14. if(pipe(pipechild) < 0)
  15. perror("Blad otwierania pipe_c\n");
  16. if(pipe(pipeparent) < 0)
  17. perror("Blad otwierania pipe_s\n");
  18.  
  19. if((childpid = fork()) == -1){
  20. perror("Blad fork\n");
  21. } else {
  22. if(childpid == 0){
  23. close(pipechild[1]);
  24. close(pipeparent[0]);
  25.  
  26. if((n = read(pipechild[0], buff, sizeof(buff))) <= 0)
  27. perror("Blad odczytu pipechild[0]\n");
  28.  
  29. FILE *file = fopen(buff, "r");
  30.  
  31. if(file == NULL){
  32. if(write(pipeparent[1], "Blad otwarcia pliku\n", 20) != 20)
  33. perror("Blad zapisu pipserver[1]\n");
  34. } else {
  35. while(fgets(buff, sizeof(buff), file) != NULL)
  36. write(pipeparent[1], buff, sizeof(buff));
  37. }
  38.  
  39. fclose(file);
  40.  
  41. close(pipechild[0]);
  42. close(pipeparent[1]);
  43. } else {
  44. close(pipechild[0]);
  45. close(pipeparent[1]);
  46.  
  47. printf("Podaj nazwe pliku: ");
  48. scanf("%s", filename);
  49.  
  50. if(write(pipechild[1], filename, sizeof(filename)) != sizeof(filename))
  51. perror("Blad zapisu pipechild[1]\n");
  52.  
  53. while(read(pipeparent[0], buff2, sizeof(buff2)) > 0)
  54. write(1, buff2, strlen(buff2));
  55.  
  56. wait(NULL);
  57. close(pipechild[1]);
  58. close(pipeparent[0]);
  59. }
  60. }
  61.  
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement