added -E switch
[tridge/junkcode.git] / memfind.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <sys/mman.h>
4 #include <sys/stat.h>
5
6 static void *map_file(char *fname, size_t *size)
7 {
8         int fd = open(fname, O_RDONLY);
9         struct stat st;
10         void *p;
11
12         fstat(fd, &st);
13         p = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
14         close(fd);
15
16         *size = st.st_size;
17         return p;
18 }
19
20 int main(int argc, char *argv[])
21 {
22         size_t size1, size2, ofs1=0;
23         char *p1, *p2;
24
25         p1 = map_file(argv[1], &size1);
26         p2 = map_file(argv[2], &size2);
27         
28         while (ofs1 < size1) {
29                 int n = 32;
30                 char *q, *p = memmem(p2, size2, p1+ofs1, 32);
31                 if (!p) {
32                         printf("data at %d not found!\n", ofs1);
33                         exit(1);
34                 }
35                 
36                 while (p[n] == p1[ofs1+n]) n++;
37                 printf("found 0x%x bytes at 0x%x (to 0x%x)\n",
38                        n, (int)(p-p2), (int)(n+(p-p2)));
39                 ofs1 += n;
40         }
41 }