Facebook
From Vlad, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 96
  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. int main(int argc, char *argv[])
  13. {
  14.     unsigned long no_bytes, no_words;
  15.     unsigned int no_newlines, max_length;
  16.     unsigned char fc, fw, fn, fL;
  17.  
  18.     unsigned char last;
  19.  
  20.     unsigned int iterator, no_chars_per_line = 0;
  21.     unsigned char ch;
  22.     char *nume_fisier = NULL;
  23.     int fd;
  24.  
  25.     no_bytes = no_words = no_newlines = max_length = 0;
  26.     last = 0;
  27.     fc = fw = fn = fL = 0;
  28.  
  29.     for (iterator = 1; iterator < argc; iterator++)
  30.     {
  31.         if (0 == strcmp(argv[iterator], "-c"))
  32.             fc = 1;
  33.         else if (0 == strcmp(argv[iterator], "-w"))
  34.             fw = 1;
  35.         else if (0 == strcmp(argv[iterator], "-l"))
  36.             fn = 1;
  37.         else if (0 == strcmp(argv[iterator], "-L"))
  38.             fL = 1;
  39.         else if (!nume_fisier)
  40.             nume_fisier = argv[iterator];
  41.         else
  42.         {
  43.             printf("Programul accepta (deocamdata) un singur nume de fisier, nu mai multe!\n");
  44.             exit(1);
  45.         }
  46.     }
  47.     if ((argc == 1) || (nume_fisier == NULL))
  48.     {
  49.         fprintf(stderr, "Ati uitat sa specificati numele fisierului de prelucrat!\n");
  50.         exit(2);
  51.     }
  52.  
  53.     if (0 == fc + fw + fn + fL)
  54.         fc = fw = fn = 1;
  55.  
  56.     fd = open(nume_fisier, O_RDONLY);
  57.     if (-1 == fd)
  58.     {
  59.         perror("Eroare la deschiderea fisierului de prelucrat!\n");
  60.         exit(3);
  61.     }
  62.  
  63.     struct stat sb;
  64.  
  65.     if (fstat(fd, &sb) == -1)
  66.         perror("Eroare la fstat.");
  67.  
  68.     int offset = 0;
  69.     int length = sb.st_size;
  70.  
  71.     char *map_addr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, offset);
  72.  
  73.     if (map_addr == MAP_FAILED)
  74.         perror("Eroare la mmap");
  75.  
  76.     for (int i = 0; i < length; i++)
  77.     {
  78.         char ch = *(map_addr + i);
  79.  
  80.         no_bytes++;
  81.  
  82.         if (fw)
  83.         {
  84.             if (isspace(ch))
  85.             {
  86.                 if (last == 1)
  87.                 {
  88.                     no_words++;
  89.                 }
  90.                 last = 2;
  91.             }
  92.             else
  93.                 last = 1;
  94.         }
  95.  
  96.         if (fn)
  97.         {
  98.             if (ch == '\n')
  99.             {
  100.                 no_newlines++;
  101.             }
  102.         }
  103.  
  104.         if (fL)
  105.         {
  106.             if (ch != '\n')
  107.             {
  108.                 if (ch == '\t')
  109.                     no_chars_per_line += 8;
  110.                 else
  111.                     no_chars_per_line++;
  112.             }
  113.             else
  114.             {
  115.                 if (max_length < no_chars_per_line)
  116.                 {
  117.                     max_length = no_chars_per_line;
  118.                 }
  119.                 no_chars_per_line = 0;
  120.             }
  121.         }
  122.     }
  123.  
  124.     if (fw)
  125.     {
  126.         if (last == 1)
  127.         {
  128.             no_words++;
  129.         }
  130.     }
  131.  
  132.     printf("Fisierul %s contine: ", nume_fisier);
  133.     if (fc)
  134.         printf("%lu caractere (octeti) , ", no_bytes);
  135.     if (fw)
  136.         printf("%lu cuvinte , ", no_words);
  137.     if (fn)
  138.         printf("%u linii de text (de fapt, newline-uri) , ", no_newlines);
  139.     if (fL)
  140.         printf("%u lungimea maxima a liniilor de text.", max_length);
  141.     printf("\n");
  142.  
  143.     if (munmap(map_addr, length) == -1)
  144.         perror("Erroare la munmap.");
  145.  
  146.     if(close(fd) == -1)
  147.         perror("Eroare la inchiderea fisierului.");
  148.  
  149.     return 0;
  150. }
  151.