#define _GNU_SOURCE
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
long filesize(FILE * const F) {
assert(!fseek(F, 0, SEEK_END));
const long sz = ftell(F);
assert(sz > 0);
return sz;
}
int main(int argc, char **argv) {
assert(argc > 1);
FILE * const haystack_file = fopen(argv[1], "r");
assert(haystack_file);
const long haystacklen = filesize(haystack_file);
void * const haystack = mmap(NULL, haystacklen, PROT_READ, MAP_PRIVATE, fileno(haystack_file), 0);
assert(haystack);
char filename[0x400];
FILE *needle_file;
void *needle, *result;
long needlelen;
size_t fnlen;
while (fgets(filename, sizeof(filename), stdin)) {
fnlen = strlen(filename) - 1;
assert(filename[fnlen] == '\n');
filename[fnlen] = '\0';
needle_file = fopen(filename, "r");
assert(needle_file);
needlelen = filesize(needle_file);
needle = mmap(NULL, needlelen, PROT_READ, MAP_PRIVATE, fileno(needle_file), 0);
assert(needle);
result = memmem(haystack, haystacklen, needle, needlelen);
munmap(needle, needlelen);
fclose(needle_file);
if (result) {
printf("%llu\t%s\n", (unsigned long long)(result - haystack), filename);
} else {
printf("-1\t%s\n", filename);
}
fflush(stdout);
}
}