10 #define BLOCKSIZE 64*1024
13 static int usage(const char *a0
) {
14 dprintf(2, "usage: %s file term\n"
15 "search for term in file and print the offset if found\n\n"
16 "optimized for big block reads\n"
17 "designed to find strings of accidentally "
18 "deleted files in blockdevices.\n"
23 static off_t offset
= 0;
24 static int sigc
, neednl
;
25 static void sigh(int nsig
) {
27 dprintf(2, "\rcurrent offset: 0x%llx, elapsed %d", offset
, sigc
*ATIME
);
32 int main(int argc
, char **argv
) {
33 if(argc
!= 3) return usage(argv
[0]);
34 const char* file
= argv
[1], *term
= argv
[2];
35 unsigned char *dblbuf
= calloc(1, BLOCKSIZE
* 2);
36 unsigned char *readbuf
= dblbuf
+ BLOCKSIZE
;
37 size_t termlen
= strlen(term
);
38 unsigned char *searchbuf
= readbuf
- termlen
;
39 unsigned char *copybuf
= readbuf
+ BLOCKSIZE
- termlen
;
40 int fd
= open(file
, O_RDONLY
), success
=0;
45 signal(SIGALRM
, sigh
);
48 ssize_t n
= read(fd
, readbuf
, BLOCKSIZE
);
50 void* res
= memmem(searchbuf
, BLOCKSIZE
+termlen
, term
, termlen
);
52 if(neednl
) dprintf(2, "\n");
54 dprintf(1, "bingo: 0x%llx\n", (unsigned long long) offset
+ ((uintptr_t) res
- (uintptr_t)readbuf
));
58 memcpy(searchbuf
, copybuf
, termlen
);