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";
|
2009-01-09 16:16:31 -07:00
|
|
|
"os";
|
2008-09-15 12:48:37 -06:00
|
|
|
)
|
|
|
|
|
2009-11-07 19:05:30 -07:00
|
|
|
var rot13Flag = flag.Bool("rot13", false, "rot13 the input")
|
2008-09-15 12:48:37 -06:00
|
|
|
|
2008-09-16 12:00:11 -06:00
|
|
|
func rot13(b byte) byte {
|
2008-09-15 12:48:37 -06:00
|
|
|
if 'a' <= b && b <= 'z' {
|
2009-10-13 13:37:04 -06:00
|
|
|
b = 'a' + ((b - 'a') + 13) % 26;
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
if 'A' <= b && b <= 'Z' {
|
2009-10-13 13:37:04 -06:00
|
|
|
b = 'A' + ((b - 'A') + 13) % 26
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-10-13 13:37:04 -06:00
|
|
|
return b
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:54:07 -07:00
|
|
|
type reader interface {
|
2009-04-17 01:08:24 -06:00
|
|
|
Read(b []byte) (ret int, err os.Error);
|
2009-01-09 16:16:31 -07:00
|
|
|
String() string;
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:54:07 -07:00
|
|
|
type rotate13 struct {
|
2009-10-13 13:37:04 -06:00
|
|
|
source reader;
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:54:07 -07:00
|
|
|
func newRotate13(source reader) *rotate13 {
|
2009-10-13 13:37:04 -06:00
|
|
|
return &rotate13{source}
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
|
2009-04-17 01:08:24 -06:00
|
|
|
func (r13 *rotate13) Read(b []byte) (ret int, err os.Error) {
|
2008-09-15 12:48:37 -06:00
|
|
|
r, e := r13.source.Read(b);
|
2009-01-09 16:16:31 -07:00
|
|
|
for i := 0; i < r; i++ {
|
2009-10-13 13:37:04 -06:00
|
|
|
b[i] = rot13(b[i])
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-10-13 13:37:04 -06:00
|
|
|
return r, e
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:54:07 -07:00
|
|
|
func (r13 *rotate13) String() string {
|
2009-10-13 13:37:04 -06:00
|
|
|
return r13.source.String()
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-01-20 20:32:36 -07:00
|
|
|
// end of rotate13 implementation
|
2008-09-15 12:48:37 -06:00
|
|
|
|
2009-01-15 18:54:07 -07:00
|
|
|
func cat(r reader) {
|
2008-09-15 12:48:37 -06:00
|
|
|
const NBUF = 512;
|
|
|
|
var buf [NBUF]byte;
|
|
|
|
|
2009-11-07 19:05:30 -07:00
|
|
|
if *rot13Flag {
|
2009-10-13 13:37:04 -06:00
|
|
|
r = newRotate13(r)
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
for {
|
2009-04-15 21:27:45 -06:00
|
|
|
switch nr, er := r.Read(&buf); {
|
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", r.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
|
|
|
nw, ew := file.Stdout.Write(buf[0:nr]);
|
2008-09-15 12:48:37 -06:00
|
|
|
if nw != nr {
|
2009-11-01 21:47:03 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", r.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
|
|
|
}
|
|
|
|
}
|