Facebook
From Vlad, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 165
  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. void scrieFisier(char *map_addr, int length);
  15. void recursive(int f, int length, int offset, int PageSize);
  16.  
  17. int main(int argc, char **argv)
  18. {
  19.     if (argc < 2)
  20.     {
  21.         printf("Introdu numele fisierului.\n");
  22.         exit(1);
  23.     }
  24.  
  25.     for (int i = 1; i < argc; i++)
  26.         if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
  27.         {
  28.             system("rev -h");
  29.             exit(1);
  30.         }
  31.     for (int i = 1; i < argc; i++)
  32.         if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
  33.         {
  34.             printf("1.0.0\n");
  35.             exit(1);
  36.         }
  37.  
  38.     for (int i = 1; i < argc; i++)
  39.     {
  40.         int f = open(argv[i], O_RDONLY);
  41.  
  42.         if (f == -1)
  43.         {
  44.             perror("A aparut o eroare cand am incercat sa deschide fisierul");
  45.             continue;
  46.         }
  47.  
  48.         long PageSize = sysconf(_SC_PAGE_SIZE);
  49.         struct stat sb;
  50.  
  51.         if (PageSize == -1)
  52.         {
  53.             perror("Eroare la sysconf");
  54.             exit(1);
  55.         }
  56.  
  57.         if (fstat(f, &sb) == -1)
  58.         {
  59.             perror("Eroare la fstat");
  60.             exit(2);
  61.         }
  62.  
  63.         recursive(f, sb.st_size, 0, PageSize);
  64.     }
  65. }
  66.  
  67. void reverse(char *sir)
  68. {
  69.     int lg = strlen(sir) - 1;
  70.     for (int i = 0; i <= lg / 2; i++)
  71.     {
  72.         char aux = sir[i];
  73.         sir[i] = sir[lg - i];
  74.         sir[lg - i] = aux;
  75.     }
  76. }
  77.  
  78. void scrieFisier(char *map_addr, int length)
  79. {
  80.     char aux[SECTOR_SIZE] = {0};
  81.     for (int i = 0; i < length; i++)
  82.         if (map_addr[i] != '\n')
  83.         {
  84.             int lg = strlen(aux);
  85.             aux[lg] = map_addr[i];
  86.             aux[lg + 1] = '\0';
  87.         }
  88.         else
  89.         {
  90.             reverse(aux);
  91.             printf("%s\n", aux);
  92.             aux[0] = '\0';
  93.         }
  94.  
  95.     if (strlen(aux) > 0)
  96.     {
  97.         reverse(aux);
  98.         printf("%s\n", aux);
  99.         aux[0] = '\0';
  100.     }
  101. }
  102.  
  103. void recursive(int f, int length, int offset, int PageSize)
  104. {
  105.     int ok = 0;
  106.     char *map_addr;
  107.     if (length > PageSize)
  108.     {
  109.         map_addr = mmap(NULL, PageSize, PROT_READ, MAP_PRIVATE, f, offset);
  110.         scrieFisier(map_addr, PageSize);
  111.         recursive(f, length - PageSize, offset + PageSize, PageSize);
  112.         if (munmap(map_addr, PageSize) == -1)
  113.         {
  114.             perror("Erroare la munmap.");
  115.             exit(4);
  116.         }
  117.         return;
  118.     }
  119.     map_addr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, f, offset);
  120.     scrieFisier(map_addr, length);
  121.     if (munmap(map_addr, length) == -1)
  122.     {
  123.         perror("Erroare la munmap.");
  124.         exit(4);
  125.     }
  126. }