2008-09-15 12:48:37 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:29:53 -07:00
|
|
|
"./file"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2008-09-15 12:48:37 -06:00
|
|
|
)
|
|
|
|
|
2009-03-16 23:53:23 -06:00
|
|
|
func cat(f *file.File) {
|
2009-12-15 16:29:53 -07:00
|
|
|
const NBUF = 512
|
|
|
|
var buf [NBUF]byte
|
2008-09-15 12:48:37 -06:00
|
|
|
for {
|
2010-09-09 21:53:18 -06:00
|
|
|
switch nr, er := f.Read(buf[:]); true {
|
2008-09-15 12:48:37 -06:00
|
|
|
case nr < 0:
|
2009-12-15 16:29:53 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", f.String(), er.String())
|
|
|
|
os.Exit(1)
|
2010-09-09 21:53:18 -06:00
|
|
|
case nr == 0: // EOF
|
2009-12-15 16:29:53 -07:00
|
|
|
return
|
2008-09-15 12:48:37 -06:00
|
|
|
case nr > 0:
|
2009-03-16 23:53:23 -06:00
|
|
|
if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr {
|
2009-12-15 16:29:53 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", f.String(), ew.String())
|
2011-04-18 11:51:40 -06:00
|
|
|
os.Exit(1)
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2010-09-09 21:53:18 -06:00
|
|
|
flag.Parse() // Scans the arg list and sets up flags
|
2009-01-09 16:16:31 -07:00
|
|
|
if flag.NArg() == 0 {
|
2009-12-15 16:29:53 -07:00
|
|
|
cat(file.Stdin)
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-01-09 16:16:31 -07:00
|
|
|
for i := 0; i < flag.NArg(); i++ {
|
2011-04-18 11:51:40 -06:00
|
|
|
f, err := file.Open(flag.Arg(i))
|
2009-03-16 23:53:23 -06:00
|
|
|
if f == nil {
|
2009-12-15 16:29:53 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err)
|
|
|
|
os.Exit(1)
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-12-15 16:29:53 -07:00
|
|
|
cat(f)
|
|
|
|
f.Close()
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
}
|