File: catl.go 1 /* 2 The MIT License (MIT) 3 4 Copyright (c) 2026 pacman64 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights to 9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 10 of the Software, and to permit persons to whom the Software is furnished to do 11 so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in all 14 copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 SOFTWARE. 23 */ 24 25 /* 26 To compile a smaller-sized command-line app, you can use the `go` command as 27 follows: 28 29 go build -ldflags "-s -w" -trimpath catl.go 30 */ 31 32 package main 33 34 import ( 35 "bufio" 36 "errors" 37 "io" 38 "os" 39 ) 40 41 // Note: the code is avoiding using the fmt package to save hundreds of 42 // kilobytes on the resulting executable, which is a noticeable difference. 43 44 const info = ` 45 catl [options...] [file...] 46 47 48 Unlike "cat", conCATenate Lines ensures lines across inputs are never joined 49 by accident, when an input's last line doesn't end with a line-feed. 50 51 Input is assumed to be UTF-8, and all CRLF byte-pairs are turned into line 52 feeds. Leading BOM (byte-order marks) on first lines are also ignored. 53 54 All (optional) leading options start with either single or double-dash: 55 56 -h show this help message 57 -help show this help message 58 ` 59 60 // errNoMoreOutput is a dummy error whose message is ignored, and which 61 // causes the app to quit immediately and successfully 62 var errNoMoreOutput = errors.New(`no more output`) 63 64 func main() { 65 buffered := false 66 args := os.Args[1:] 67 68 if len(args) > 0 { 69 switch args[0] { 70 case `-b`, `--b`, `-buffered`, `--buffered`: 71 buffered = true 72 args = args[1:] 73 74 case `-h`, `--h`, `-help`, `--help`: 75 os.Stdout.WriteString(info[1:]) 76 return 77 } 78 } 79 80 if len(args) > 0 && args[0] == `--` { 81 args = args[1:] 82 } 83 84 liveLines := !buffered 85 if !buffered { 86 if _, err := os.Stdout.Seek(0, io.SeekCurrent); err == nil { 87 liveLines = false 88 } 89 } 90 91 if err := run(os.Stdout, args, liveLines); isActualError(err) { 92 os.Stderr.WriteString(err.Error()) 93 os.Stderr.WriteString("\n") 94 os.Exit(1) 95 } 96 } 97 98 func run(w io.Writer, args []string, live bool) error { 99 bw := bufio.NewWriter(w) 100 defer bw.Flush() 101 102 if len(args) == 0 { 103 return catl(bw, os.Stdin, live) 104 } 105 106 for _, name := range args { 107 if err := handleFile(bw, name, live); err != nil { 108 return err 109 } 110 } 111 return nil 112 } 113 114 func handleFile(w *bufio.Writer, name string, live bool) error { 115 if name == `` || name == `-` { 116 return catl(w, os.Stdin, live) 117 } 118 119 f, err := os.Open(name) 120 if err != nil { 121 return errors.New(`can't read from file named "` + name + `"`) 122 } 123 defer f.Close() 124 125 return catl(w, f, live) 126 } 127 128 // isActualError is to figure out whether not to ignore an error, and thus 129 // show it as an error message 130 func isActualError(err error) bool { 131 return err != nil && err != errNoMoreOutput 132 } 133 134 func catl(w *bufio.Writer, r io.Reader, live bool) error { 135 const gb = 1024 * 1024 * 1024 136 sc := bufio.NewScanner(r) 137 sc.Buffer(nil, 8*gb) 138 139 for i := 0; sc.Scan(); i++ { 140 s := sc.Bytes() 141 if i == 0 && len(s) > 2 && s[0] == 0xef && s[1] == 0xbb && s[2] == 0xbf { 142 s = s[3:] 143 } 144 145 w.Write(s) 146 if w.WriteByte('\n') != nil { 147 return errNoMoreOutput 148 } 149 150 if !live { 151 continue 152 } 153 154 if err := w.Flush(); err != nil { 155 return errNoMoreOutput 156 } 157 } 158 159 return sc.Err() 160 }