-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzerocheck.go
65 lines (55 loc) · 880 Bytes
/
zerocheck.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
package main
import (
"flag"
"fmt"
"io"
"os"
)
func main() {
flag.Usage = usage
flag.Parse()
status := 0
for _, name := range flag.Args() {
z, err := check(name)
if err != nil {
fmt.Fprintln(os.Stderr, "zerocheck:", err)
status = 1
continue
}
if z {
fmt.Println(name)
} else {
status = 1
}
}
os.Exit(status)
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: file ...")
os.Exit(2)
}
func check(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()
var b [8192]byte
for {
n, err := f.Read(b[:])
if err != nil && err != io.EOF {
return false, err
}
if n > 0 {
for i := range b[:n] {
if b[i] != 0 {
return false, nil
}
}
}
if err == io.EOF {
break
}
}
return true, nil
}