Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <sys/mman.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <fcntl.h>
  9.  
  10. int main(int argc, char** argv) {
  11.         const char* fn = argv[1];
  12.         const char* pattern = argv[2];
  13.         int fd = open(fn, O_RDONLY | O_CREAT);
  14.         //FILE* file = fdopen(fd, "r");
  15.         //lseek(fd, 0, SEEK_END);
  16.         long size = lseek(fd, 0, SEEK_END) + 1;
  17.         char* text = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
  18.         char* cur_found_substring = strstr(text, pattern);
  19.         if ((int64_t)text == -1) {
  20.                 return 1;
  21.         }
  22.         while (cur_found_substring != NULL && (uint64_t)(cur_found_substring - text + 1) < size && cur_found_substring + 1 < text + size) {
  23.                 printf("%llu\n", (uint64_t)(cur_found_substring - text));
  24.                 cur_found_substring = strstr(cur_found_substring + 1, pattern);
  25.         }
  26.         munmap(text, size);
  27.         close(fd);
  28.         return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement