-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinfile.go
101 lines (86 loc) · 1.58 KB
/
infile.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"path/filepath"
)
var (
recursive = flag.Bool("r", false, "recursive")
status = 0
)
func main() {
log.SetPrefix("infile: ")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
if flag.NArg() < 2 {
usage()
}
args := flag.Args()
err := infile(args[0], args[1:]...)
if err != nil {
log.Fatal(err)
}
os.Exit(status)
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: infile [options] file/dir file ...")
flag.PrintDefaults()
os.Exit(2)
}
func infile(file string, cmpfiles ...string) error {
var cmp [][]byte
for _, name := range cmpfiles {
b, err := os.ReadFile(name)
if err != nil {
fmt.Fprintln(os.Stderr, err)
status = 1
cmp = append(cmp, nil)
} else {
cmp = append(cmp, b)
}
}
filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
if !*recursive {
return filepath.SkipDir
}
return nil
}
buf, err := os.ReadFile(path)
if err != nil {
fmt.Fprintln(os.Stderr, err)
status = 1
return nil
}
in(path, buf, cmpfiles, cmp)
return nil
})
return nil
}
func in(file string, buf []byte, cmpfiles []string, cmp [][]byte) {
idx := make([]int, len(cmp))
for {
eof := true
for i := 0; i < len(cmp); i++ {
if idx[i] >= len(buf) {
continue
}
n := bytes.Index(buf[idx[i]:], cmp[i])
if n < 0 || len(cmp[i]) == 0 {
l := len(cmp) - 1
cmp[i], cmp = cmp[l], cmp[:l]
idx = idx[:l]
} else {
fmt.Printf("%s %s offset %d (%#x)\n", file, cmpfiles[i], n, n)
idx[i] = n + 1
}
}
if eof {
break
}
}
}