-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfillfile.go
53 lines (46 loc) · 870 Bytes
/
fillfile.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
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
)
func main() {
log.SetFlags(0)
log.SetPrefix("fillfile: ")
flag.Usage = usage
flag.Parse()
var args []string
switch flag.NArg() {
case 4:
args = flag.Args()
case 3:
args = append(flag.Args(), "0")
args[1], args[3] = args[3], args[1]
default:
usage()
}
offset, _ := strconv.ParseInt(args[1], 0, 64)
value, _ := strconv.ParseInt(args[2], 0, 64)
length, _ := strconv.ParseInt(args[3], 0, 64)
buf, err := os.ReadFile(flag.Arg(0))
ck(err)
for i := offset; i < offset+length; i++ {
if i < 0 || i >= int64(len(buf)) {
break
}
buf[i] = byte(value)
}
ck(os.WriteFile(flag.Arg(0), buf, 644))
}
func ck(err error) {
if err != nil {
log.Fatal(err)
}
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: fillfile file [offset] value length")
flag.PrintDefaults()
os.Exit(2)
}