Facebook
From Vlad, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 151
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <unistd.h>
  6. #include <sys/mman.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <limits.h> // necesar pentru PATH_MAX
  11.  
  12. #define SECTOR_SIZE 4096
  13.  
  14. char buffer[SECTOR_SIZE], comanda[10];
  15. int bytes_read;
  16.  
  17. int main(int argc, char **argv)
  18. {
  19.     struct stat sb;
  20.  
  21.     long PageSize = sysconf(_SC_PAGE_SIZE);
  22.  
  23.     if (PageSize == -1)
  24.     {
  25.         perror("Eroare la sysconf");
  26.         exit(1);
  27.     }
  28.  
  29.     if (argc < 3)
  30.     {
  31.         printf("Optiuni insuficiente.");
  32.         exit(1);
  33.     }
  34.  
  35.     int f1 = open(argv[1], O_RDONLY);
  36.  
  37.     if (f1 < 0)
  38.     {
  39.         perror("Error on opening file");
  40.         exit(1);
  41.     }
  42.  
  43.     int f2 = open(argv[2], O_RDWR);
  44.  
  45.     if (f2 > 0)
  46.     {
  47.         printf("Doriti sa suprascrieti continutul ? da/nu \n");
  48.         scanf("%10s", &comanda);
  49.         if (strcmp(comanda, "da") != 0)
  50.         {
  51.             printf("\nNu ai acordat permisiunea \n");
  52.             exit(5);
  53.         }
  54.     }
  55.     else
  56.     {
  57.         f2 = open(argv[2], O_RDWR | O_CREAT);
  58.         chmod(argv[2], S_IRUSR | S_IWUSR);
  59.     }
  60.  
  61.     if (fstat(f1, &sb) == -1)
  62.     {
  63.         perror("Eroare la fstat");
  64.         exit(4);
  65.     }
  66.  
  67.     int offset = 0;
  68.     int length = sb.st_size;
  69.  
  70.     if (ftruncate(f2, length) == -1)
  71.         {
  72.             perror("Eroare la trunchiere");
  73.             exit(5);
  74.         }
  75.  
  76.     while (length > PageSize)
  77.     {
  78.         afiseaza(f1, f2, PageSize, offset);
  79.         length -= PageSize;
  80.         offset += PageSize;
  81.     }
  82.     afiseaza(f1, f2, length, offset);
  83.  
  84.     if (close(f1) == -1 || close(f2) == -1)
  85.     {
  86.         perror("Error on closing files");
  87.         exit(9);
  88.     }
  89.  
  90.     return 0;
  91. }
  92.  
  93. void afiseaza(int f1, int f2, int length, int offset)
  94. {
  95.     char *map_addr1 = mmap(NULL, length, PROT_READ, MAP_SHARED, f1, offset);
  96.     char *map_addr2 = mmap(NULL, length, PROT_WRITE, MAP_SHARED, f2, offset);
  97.  
  98.     if (map_addr1 == MAP_FAILED || map_addr2 == MAP_FAILED)
  99.     {
  100.         perror("Eroare la mmap.");
  101.         exit(3);
  102.     }
  103.  
  104.     for (int i = 0; i < length; i++)
  105.         if(map_addr1[i]>='a' && map_addr1[i]<='z')
  106.             map_addr2[offset+i]=map_addr1[i]-'a'+'A';
  107.         else map_addr2[offset+i]=map_addr1[i];
  108.  
  109.     if (-1 == msync(map_addr2, length, MS_SYNC))
  110.     {
  111.         perror("Erroar la msync");
  112.         exit(1);
  113.     }
  114.  
  115.     if (munmap(map_addr1, length) == -1 || munmap(map_addr2, length) == -1)
  116.     {
  117.         perror("Erroare la munmap.");
  118.         exit(2);
  119.     }
  120. }