-
Notifications
You must be signed in to change notification settings - Fork 11
/
searchb.go
65 lines (59 loc) · 1.12 KB
/
searchb.go
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
// 2018, Georg Sauthoff <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
// How to build:
//
// sudo dnf install golang-github-golang-sys-devel
// export GOPATH=$HOME/go:/usr/share/gocode
// mkdir build
// cd build
// go build -o searchb-go ../searchb.go
package main
import (
"golang.org/x/sys/unix"
"bytes"
"fmt"
"os"
)
func mmap(name string) ([]byte, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
st, err := f.Stat()
if err != nil {
f.Close()
return nil, err
}
if st.Size() == 0 {
f.Close()
return nil, nil // nil is also the empty byte slice
}
m, err := unix.Mmap(int(f.Fd()), 0, int(st.Size()), unix.PROT_READ,
unix.MAP_SHARED)
if err != nil {
f.Close()
return nil, err
}
err = f.Close()
if err != nil {
unix.Munmap(m)
return nil, err
}
return m, err
}
func main() {
q, err := mmap(os.Args[1])
if err != nil {
panic(err.Error())
}
t, err := mmap(os.Args[2])
if err != nil {
panic(err.Error())
}
i := bytes.Index(t, q)
if i == -1 {
os.Exit(1)
}
fmt.Println(i)
os.Exit(0)
}