Advertisement
Guest User

Untitled

a guest
Jun 4th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <sys/stat.h>
  4. #include <sys/mman.h>
  5. #include <unistd.h>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     if (argc < 2) {
  10.         printf("no file\n");
  11.         return 1;
  12.     }
  13.  
  14.     int fd = open(argv[1], O_RDONLY);
  15.  
  16.     if (fd < 0) {
  17.         perror("file open error");
  18.         return 1;
  19.     }
  20.  
  21.     struct stat s;
  22.     if (fstat(fd, &s) < 0) {
  23.         perror("cannot stat file");
  24.         return 1;
  25.     }
  26.  
  27.     long a = 0;
  28.    
  29.     void *addr = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  30.  
  31.     if (addr == MAP_FAILED) {
  32.         perror("cannot map file");
  33.         return 1;
  34.     }
  35.  
  36.     for (off_t i = 0; i < s.st_size; ++i) {
  37.         if (((char *)addr)[i] == 'a')
  38.             ++a;
  39.     }
  40.  
  41.     printf("Result: %ld\n", a);
  42.  
  43.     munmap(addr, s.st_size);
  44.     close(fd);
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement