- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <limits.h> // necesar pentru PATH_MAX
- int main(int argc, char *argv[])
- {
- unsigned long no_bytes, no_words;
- unsigned int no_newlines, max_length;
- unsigned char fc, fw, fn, fL;
- unsigned char last;
- unsigned int iterator, no_chars_per_line = 0;
- unsigned char ch;
- char *nume_fisier = NULL;
- int fd;
- no_bytes = no_words = no_newlines = max_length = 0;
- last = 0;
- fc = fw = fn = fL = 0;
- for (iterator = 1; iterator < argc; iterator++)
- {
- if (0 == strcmp(argv[iterator], "-c"))
- fc = 1;
- else if (0 == strcmp(argv[iterator], "-w"))
- fw = 1;
- else if (0 == strcmp(argv[iterator], "-l"))
- fn = 1;
- else if (0 == strcmp(argv[iterator], "-L"))
- fL = 1;
- else if (!nume_fisier)
- nume_fisier = argv[iterator];
- else
- {
- printf("Programul accepta (deocamdata) un singur nume de fisier, nu mai multe!\n");
- exit(1);
- }
- }
- if ((argc == 1) || (nume_fisier == NULL))
- {
- fprintf(stderr, "Ati uitat sa specificati numele fisierului de prelucrat!\n");
- exit(2);
- }
- if (0 == fc + fw + fn + fL)
- fc = fw = fn = 1;
- fd = open(nume_fisier, O_RDONLY);
- if (-1 == fd)
- {
- perror("Eroare la deschiderea fisierului de prelucrat!\n");
- exit(3);
- }
- struct stat sb;
- if (fstat(fd, &sb) == -1)
- perror("Eroare la fstat.");
- int offset = 0;
- int length = sb.st_size;
- char *map_addr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, offset);
- if (map_addr == MAP_FAILED)
- perror("Eroare la mmap");
- for (int i = 0; i < length; i++)
- {
- char ch = *(map_addr + i);
- no_bytes++;
- if (fw)
- {
- if (isspace(ch))
- {
- if (last == 1)
- {
- no_words++;
- }
- last = 2;
- }
- else
- last = 1;
- }
- if (fn)
- {
- if (ch == '\n')
- {
- no_newlines++;
- }
- }
- if (fL)
- {
- if (ch != '\n')
- {
- if (ch == '\t')
- no_chars_per_line += 8;
- else
- no_chars_per_line++;
- }
- else
- {
- if (max_length < no_chars_per_line)
- {
- max_length = no_chars_per_line;
- }
- no_chars_per_line = 0;
- }
- }
- }
- if (fw)
- {
- if (last == 1)
- {
- no_words++;
- }
- }
- printf("Fisierul %s contine: ", nume_fisier);
- if (fc)
- printf("%lu caractere (octeti) , ", no_bytes);
- if (fw)
- printf("%lu cuvinte , ", no_words);
- if (fn)
- printf("%u linii de text (de fapt, newline-uri) , ", no_newlines);
- if (fL)
- printf("%u lungimea maxima a liniilor de text.", max_length);
- printf("\n");
- if (munmap(map_addr, length) == -1)
- perror("Erroare la munmap.");
- if(close(fd) == -1)
- perror("Eroare la inchiderea fisierului.");
- return 0;
- }