diff --git a/cat/main.go b/cat/main.go index 6a157d4..b4a6b26 100644 --- a/cat/main.go +++ b/cat/main.go @@ -17,7 +17,7 @@ func Fail(err error, format string, v ...any) { type Opts struct { Number bool Squeeze bool - Filename string + Filenames []string } func parse_opts() (Opts) { @@ -32,28 +32,31 @@ func parse_opts() (Opts) { os.Exit(1) } - opts.Filename = flag.Arg(0) + opts.Filenames = flag.Args() return opts } func main() { opts := parse_opts() - in_file, err := os.ReadFile(opts.Filename) - if err != nil { Fail(err, "cannot open %s:", opts.Filename) } + for _, filename := range opts.Filenames { + in_file, err := os.ReadFile(filename) - if(opts.Number) { - count := 1 - for line := range strings.Lines(string(in_file)) { - if opts.Squeeze && len(line) <= 1 { - continue - } + if err != nil { Fail(err, "cannot open %s:", filename) } + + if(opts.Number) { + count := 1 + for line := range strings.Lines(string(in_file)) { + if opts.Squeeze && len(line) <= 1 { + continue + } - fmt.Printf("%0.4d: %s", count, line) - count++ + fmt.Printf("%0.4d: %s", count, line) + count++ + } + } else { + fmt.Print(string(in_file)) } - } else { - fmt.Print(string(in_file)) } }