-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsearchb.c
67 lines (63 loc) · 1.17 KB
/
searchb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 2018, Georg Sauthoff <mail@gms.tf>
// SPDX-License-Identifier: GPL-3.0-or-later
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
static void *mmap_file(const char *filename, size_t *n)
{
static unsigned char empty[0];
int fd = open(filename, O_RDONLY);
if (fd == -1)
return 0;
struct stat st;
int r = fstat(fd, &st);
if (r == -1) {
close(fd);
return 0;
}
*n = st.st_size;
if (!*n) {
close(fd);
return empty;
}
void *p = mmap(0, *n, PROT_READ, MAP_SHARED, fd, 0);
if (p == (void*)-1) {
close(fd);
return 0;
}
r = close(fd);
if (r == -1) {
munmap(p, *n);
return 0;
}
return p;
}
int main(int argc, char **argv)
{
if (argc < 3) {
fprintf(stderr, "call: %s queryfile file\n", argv[0]);
return 1;
}
size_t m;
const char *q = mmap_file(argv[1], &m);
if (!q) {
perror(0);
return 1;
}
size_t n;
const char *t = mmap_file(argv[2], &n);
if (!q) {
perror(0);
return 1;
}
const char *p = memmem(t, n, q, m);
if (!p)
return 1;
printf("%zd\n", p-t);
return 0;
}