Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <time.h>
- #include <sys/stat.h>
- #include <stdint.h>
- #include <string.h>
- void print_substr_idx(const char *str, const char *substr) {
- char * match = strstr(str, substr);
- while (match != NULL) {
- printf("%ld\n", (uint64_t)match - (uint64_t)str);
- match = strstr(match + 1, substr);
- }
- }
- int main(int argc, char **argv) {
- if (argc != 3) return 1;
- char *fname = argv[1];
- char *str = argv[2];
- int fd = open(fname, O_RDONLY);
- if (fd == -1) {
- perror("failed to open file");
- return 2;
- }
- FILE * fp = fdopen(fd, "r");
- fseek(fp, 0, SEEK_END);
- int fsize = ftell(fp);
- char * ptr = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0);
- if ((int64_t)ptr == -1) {
- perror("mmap failed");
- return 3;
- }
- print_substr_idx(ptr, str);
- munmap(ptr, fsize);
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement