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-03-16 23:53:23 -06:00
|
|
|
"file";
|
2009-01-09 16:16:31 -07:00
|
|
|
"flag";
|
2009-03-18 15:09:16 -06:00
|
|
|
"fmt";
|
|
|
|
"os";
|
2008-09-15 12:48:37 -06:00
|
|
|
)
|
|
|
|
|
2009-03-16 23:53:23 -06:00
|
|
|
func cat(f *file.File) {
|
2008-09-15 12:48:37 -06:00
|
|
|
const NBUF = 512;
|
|
|
|
var buf [NBUF]byte;
|
|
|
|
for {
|
2009-03-16 23:53:23 -06:00
|
|
|
switch nr, er := f.Read(buf); true {
|
2008-09-15 12:48:37 -06:00
|
|
|
case nr < 0:
|
2009-03-18 15:09:16 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "error reading from %s: %s\n", f.String(), er.String());
|
2009-01-16 15:58:14 -07:00
|
|
|
sys.Exit(1);
|
2008-09-15 12:48:37 -06:00
|
|
|
case nr == 0: // EOF
|
|
|
|
return;
|
|
|
|
case nr > 0:
|
2009-03-16 23:53:23 -06:00
|
|
|
if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr {
|
2009-03-18 15:09:16 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "error writing from %s: %s\n", f.String(), ew.String());
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2009-01-09 16:16:31 -07:00
|
|
|
flag.Parse(); // Scans the arg list and sets up flags
|
|
|
|
if flag.NArg() == 0 {
|
2009-03-16 23:53:23 -06: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++ {
|
2009-03-16 23:53:23 -06:00
|
|
|
f, err := file.Open(flag.Arg(i), 0, 0);
|
|
|
|
if f == nil {
|
2009-03-18 15:09:16 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "can't open %s: error %s\n", flag.Arg(i), err);
|
2009-01-16 15:58:14 -07:00
|
|
|
sys.Exit(1);
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-03-16 23:53:23 -06:00
|
|
|
cat(f);
|
|
|
|
f.Close();
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
}
|