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-01-09 16:16:31 -07:00
|
|
|
"fd";
|
|
|
|
"flag";
|
2008-09-15 12:48:37 -06:00
|
|
|
)
|
|
|
|
|
2009-01-09 16:16:31 -07:00
|
|
|
func cat(file *fd.FD) {
|
2008-09-15 12:48:37 -06:00
|
|
|
const NBUF = 512;
|
|
|
|
var buf [NBUF]byte;
|
|
|
|
for {
|
2009-01-09 16:16:31 -07:00
|
|
|
switch nr, er := file.Read(buf); true {
|
2008-09-15 12:48:37 -06:00
|
|
|
case nr < 0:
|
2009-01-09 16:16:31 -07:00
|
|
|
print("error reading from ", file.String(), ": ", er.String(), "\n");
|
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-01-09 16:16:31 -07:00
|
|
|
if nw, ew := fd.Stdout.Write(buf[0:nr]); nw != nr {
|
|
|
|
print("error writing from ", file.String(), ": ", ew.String(), "\n");
|
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 {
|
|
|
|
cat(fd.Stdin);
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-01-09 16:16:31 -07:00
|
|
|
for i := 0; i < flag.NArg(); i++ {
|
|
|
|
file, err := fd.Open(flag.Arg(i), 0, 0);
|
|
|
|
if file == nil {
|
|
|
|
print("can't open ", flag.Arg(i), ": error ", err, "\n");
|
2009-01-16 15:58:14 -07:00
|
|
|
sys.Exit(1);
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-01-09 16:16:31 -07:00
|
|
|
cat(file);
|
|
|
|
file.Close();
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
}
|