Facebook
From Cute Ibis, 6 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 291
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <wait.h>
  6. #include <string.h>
  7. #define DL_NAZWY 1024
  8. #define DL_BUFORA 10
  9.  
  10.  
  11. int main(){
  12.  
  13.  
  14.     int pipe_server_write[2];
  15.     int pipe_client_write[2];
  16.     int childpid;
  17.  
  18.     if (pipe(pipe_server_write) < 0) {
  19.         perror("Blad tworzenia PIPE serwera");
  20.         exit(0);
  21.     }
  22.  
  23.     if (pipe(pipe_client_write) < 0) {
  24.         perror("Blad tworzenia PIPE klienta");
  25.         exit(0);
  26.     }
  27.  
  28.     if ((childpid = fork()) == -1)
  29.     {
  30.         perror("nie moge forknac");
  31.         exit(1);
  32.     }
  33.     else
  34.         if (childpid ==0 ) // SERWER
  35.         {
  36.             close(pipe_server_write[0]);
  37.             close(pipe_client_write[1]);
  38.             char nazwa[DL_NAZWY];
  39.             int status=0;
  40.             int n=read(pipe_client_write[0],nazwa,sizeof(nazwa));
  41.             if (n<=0)
  42.                 perror("Blad read serwera");
  43.  
  44.             nazwa[n-1]='\0';
  45.             FILE *plik=fopen(nazwa,"r");
  46.             if(plik==NULL){
  47.                 printf("Brak pliku");
  48.                 status=-1;
  49.                 write(pipe_server_write[1],&status,sizeof(status));
  50.             } else {
  51.             char bufor[DL_BUFORA];
  52.             write(pipe_server_write[1],&status,sizeof(status));
  53.             while(fgets(bufor,sizeof(bufor),plik)!=NULL){
  54.                 write(pipe_server_write[1],bufor,sizeof(bufor));
  55.             } }            
  56.             close(pipe_server_write[1]);
  57.             close(pipe_client_write[0]);        
  58.         }
  59.         else // KLIENT
  60.         {
  61.             close(pipe_server_write[1]);
  62.             close(pipe_client_write[0]);    
  63.             char nazwa[DL_NAZWY];
  64.             printf("Podaj nazwe pliku\n");
  65.             fgets (nazwa, sizeof(nazwa), stdin);
  66.             int n=strlen(nazwa);
  67.             if(write(pipe_client_write[1],nazwa,n)!=n)
  68.                 perror("Blad write klienta");
  69.  
  70.             char bufor[DL_BUFORA];
  71.             int status;
  72.             read(pipe_server_write[0],&status,sizeof(status));
  73.             if (status==-1){
  74.                 printf("Nie moge otworzyc pliku! Prawdopodobnie podales zla nazwe!\n");
  75.  
  76.             }
  77.  
  78.             else
  79.             {
  80.                 while (read(pipe_server_write[0],bufor,sizeof(bufor))>0)
  81.                     write(1,bufor,strlen(bufor));                    
  82.             }
  83.             wait(NULL);
  84.             close(pipe_server_write[0]);
  85.             close(pipe_client_write[1]);
  86.         }
  87.  
  88.  
  89.  
  90.  
  91.     return 0;
  92. }
  93.