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-06-25 21:13:56 -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-04-15 21:27:45 -06:00
|
|
|
switch nr, er := f.Read(&buf); true {
|
2008-09-15 12:48:37 -06:00
|
|
|
case nr < 0:
|
2009-11-01 21:47:03 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", f.String(), er.String());
|
2009-05-08 16:21:41 -06:00
|
|
|
os.Exit(1);
|
2009-10-13 13:37:04 -06:00
|
|
|
case nr == 0: // EOF
|
2008-09-15 12:48:37 -06:00
|
|
|
return;
|
|
|
|
case nr > 0:
|
2009-03-16 23:53:23 -06:00
|
|
|
if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr {
|
2009-11-01 21:47:03 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", f.String(), ew.String());
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2009-10-13 13:37:04 -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-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-11-01 21:47:03 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err);
|
2009-05-08 16:21:41 -06:00
|
|
|
os.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
|
|
|
}
|
|
|
|
}
|