-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat.go
66 lines (60 loc) · 1.11 KB
/
cat.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
)
func (app *applets) Applet_cat(args []string) {
var set = flag.NewFlagSet(args[0], flag.ExitOnError)
var bflag = set.Bool("b", false, "number nonblank")
var nflag = set.Bool("n", false, "number all output lines")
set.Parse(args[1:])
args = set.Args()
var curLine int
var retval int
read := func(file *os.File) {
reader := bufio.NewReader(file)
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
} else if err != nil {
retval = 1
break
}
if *nflag && (!*bflag || len(line) > 0) {
curLine++
}
if *bflag && len(line) == 0 {
// number nonblank
fmt.Println()
} else if *nflag {
// number all output lines
fmt.Printf("%6d %s\n", curLine, line)
} else {
fmt.Printf("%s\n", line)
}
}
}
if len(args) == 0 {
read(os.Stdin)
} else {
for i := range args {
if args[i] == "-" {
read(os.Stdin)
} else {
f, err := os.Open(args[i])
if err != nil {
fmt.Println(err)
retval = 1
} else {
read(f)
f.Close()
}
}
}
}
os.Exit(retval)
}