Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <stdlib.h>
- #include <sys/mman.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <fcntl.h>
- int main(int argc, char** argv) {
- const char* fn = argv[1];
- const char* pattern = argv[2];
- int fd = open(fn, O_RDONLY | O_CREAT);
- //FILE* file = fdopen(fd, "r");
- //lseek(fd, 0, SEEK_END);
- long size = lseek(fd, 0, SEEK_END) + 1;
- char* text = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
- char* cur_found_substring = strstr(text, pattern);
- if ((int64_t)text == -1) {
- return 1;
- }
- while (cur_found_substring != NULL && (uint64_t)(cur_found_substring - text + 1) < size && cur_found_substring + 1 < text + size) {
- printf("%llu\n", (uint64_t)(cur_found_substring - text));
- cur_found_substring = strstr(cur_found_substring + 1, pattern);
- }
- munmap(text, size);
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement