Commits

Miki Tebeka committed b554cd8

cat

  • Participants
  • Parent commits dd6acc5

Comments (0)

Files changed (2)

 *.6
 *.out
 avg64
+cat
 fact
 fib
 fizzbuzz
+package main
+
+import (
+	"bufio"
+	"flag"
+	"fmt"
+	"os"
+)
+
+func open(filename string) *bufio.Reader {
+	var file *os.File
+
+	if filename == "-" {
+		file = os.Stdin
+	} else {
+		var err os.Error
+		file, err = os.Open(filename)
+		if err != nil {
+			panic(fmt.Sprintf("error: can't open %s", filename))
+		}
+	}
+
+	return bufio.NewReader(file)
+}
+
+var lineNums = flag.Bool("n", false, "number lines")
+
+func main() {
+	flag.Parse()
+
+	var files []string
+
+	if flag.NArg() == 0 {
+		files = []string{"-"}
+	} else {
+		files = flag.Args()
+	}
+
+	fmt.Println(files)
+
+	lnum := uint64(0)
+	for _, filename := range(files) {
+		file := open(filename)
+		for {
+			lnum ++
+			buf, err := file.ReadBytes('\n')
+			if err == os.EOF {
+				break
+			}
+			if err != nil {
+				panic(fmt.Sprintf("%s error - %v", filename, err))
+			}
+			if (*lineNums) {
+				fmt.Printf("%5d %s", lnum, buf)
+			} else {
+				fmt.Printf("%s", buf)
+			}
+		}
+	}
+}