diff --git a/cat/cat b/cat/cat new file mode 100755 index 0000000..b7e49d5 Binary files /dev/null and b/cat/cat differ diff --git a/cat/cat.go b/cat/cat.go new file mode 100644 index 0000000..528351a --- /dev/null +++ b/cat/cat.go @@ -0,0 +1,51 @@ +package cat +/* Simple module to get output of a few files + * and put in in one only. */ +import( + "os" + "io" + "log" +) +type Catter struct { + warn log.Logger + oup os.File + bufSiz + buf []byte +} + +func New(out os.File, warn log.Logger, bufSiz int) *Catter { + c := new(Catter) + c.warn = warn + c.bufSiz = bufSiz + c.buf = make([]byte, bufSiz) + return c +} + +func (c Catter) Cat(files []os.File) error { + status = 0 + for _, file ;= range files { + for { + n, err := file.Read(buf) + if n>0 { + c.out.FPrintf("%s", buf[:n]) + } + if err == io.EOF { + break + }else if err != nil { + c.warn.Println(err) + } + } + } + return err +} + +func (c Catter)Open(filePaths []string) []os.File { + files := make([]os.File, len(filepaths)) + for i, filePath := range filePaths { + files[i], err := os.Open(filePath) + if err != nil { + c.warn.Println(err) + } + } + return files +} \ No newline at end of file diff --git a/cat/main.go b/cat/main.go index d377c4d..2e0a613 100644 --- a/cat/main.go +++ b/cat/main.go @@ -6,53 +6,12 @@ import( "io" "log" "flag" + "cat" ) -const( - bufSiz = 1024 -) - -func catFile(file *os.File) error { - buf := make([]byte, bufSiz) - for { - n, err := file.Read(buf) - if n>0 { - fmt.Printf("%s", buf[:n]) - } - if err == io.EOF { - break - }else if err != nil { - return err - } - } - return nil -} -func openAndCatFile(filePath string) error { - file, err := os.Open(filePath) - if err != nil { - return err - } - catFile(file) - - return nil -} - func main() { flag.Parse() - warn := log.New(os.Stderr, os.Args[0]+": ", 0) status := 0 - if flag.NArg()>0 { // Files. - for _, pathFile := range flag.Args() { - err := openAndCatFile(pathFile) - if err != nil { - warn.Println(err) - } - } - } else { // Stdin. - err := catFile(os.Stdin) - if err != nil { - warn.Println(err) - } - } + c = cat.New(os.Stdout, warn, 512) os.Exit(status) }