-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
59 lines (49 loc) · 983 Bytes
/
main.go
File metadata and controls
59 lines (49 loc) · 983 Bytes
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
// cat - concatenate and print files.
package main
import (
"solod.dev/so/flag"
"solod.dev/so/fmt"
"solod.dev/so/io"
"solod.dev/so/os"
)
func main() {
flag.Parse()
args := flag.Args()
exitCode := 0
if len(args) == 0 {
if cat(os.Stdin) != nil {
exitCode = 1
}
os.Exit(exitCode)
}
for _, fname := range args {
if fname == "-" {
if cat(os.Stdin) != nil {
writeErr("-", "Error reading standard input")
exitCode = 1
}
continue
}
f, err := os.Open(fname)
if err != nil {
writeErr(fname, "No such file or directory")
exitCode = 1
continue
}
if cat(&f) != nil {
writeErr(fname, "Error reading file")
exitCode = 1
}
f.Close()
}
os.Exit(exitCode)
}
// cat writes all data from r to standard output.
func cat(r io.Reader) error {
_, err := io.Copy(os.Stdout, r)
return err
}
// writeErr writes an error message to standard error.
func writeErr(name string, msg string) {
fmt.Printf("cat: %s: %s\n", name, msg)
}