summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormjl <mjl@ueber.net>2012-08-19 16:51:01 +0200
committermjl <mjl@ueber.net>2012-08-19 16:51:01 +0200
commitb234cf29e92f836482244a7f51ae3fff1db10f90 (patch)
tree296205fdc99e0a246ec4dd6f33ac99c9b0aa2cb3
parentc0c2ae42f9d1810faacec27fe95546bfddc4ae56 (diff)
add cmp
-rw-r--r--cmp/cmp.go98
1 files changed, 98 insertions, 0 deletions
diff --git a/cmp/cmp.go b/cmp/cmp.go
new file mode 100644
index 0000000..23b0fa1
--- /dev/null
+++ b/cmp/cmp.go
@@ -0,0 +1,98 @@
+package main
+
+import (
+ "bufio"
+ "flag"
+ "fmt"
+ "io"
+ "os"
+ "strconv"
+)
+
+var lflag = flag.Bool("l", false, "do not print and character")
+var sflag = flag.Bool("s", false, "silent, do not print errors")
+var Lflag = flag.Bool("L", false, "print line number of difference")
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: cmp [-lsL] file1 file2 [offset1 [offset2]]\n")
+ os.Exit(2)
+}
+
+func error(s string) {
+ if !*sflag {
+ fmt.Fprint(os.Stderr, s, "\n")
+ }
+ os.Exit(1)
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ if len(args) < 2 || len(args) > 4 {
+ usage()
+ }
+ f1, err := os.Open(args[0])
+ if err != nil {
+ error(fmt.Sprintf("open %s: %s", args[0], err))
+ }
+ f2, err := os.Open(args[1])
+ if err != nil {
+ error(fmt.Sprintf("open %s: %s", args[1], err))
+ }
+
+ xseek := func(f *os.File, fn string, os string) {
+ o, err := strconv.ParseInt(os, 0, 64)
+ if err != nil {
+ error(fmt.Sprintf("bad offset %s: %s", os, err))
+ }
+ _, err = f.Seek(o, 0)
+ if err != nil {
+ error(fmt.Sprintf("seek %s %s: %s", fn, os, err))
+ }
+ }
+
+ if len(args) >= 3 {
+ xseek(f1, args[0], args[2])
+ }
+ if len(args) >= 4 {
+ xseek(f2, args[1], args[2])
+ }
+
+ r1 := bufio.NewReader(f1)
+ r2 := bufio.NewReader(f2)
+ nc := 1
+ l := 1
+ for {
+ b1, err1 := r1.ReadByte()
+ b2, err2 := r2.ReadByte()
+ if err1 == io.EOF && err2 == io.EOF {
+ break
+ }
+ if err1 != nil {
+ error(fmt.Sprintf("%s: read: %s", args[0], err1.Error()))
+ }
+ if err2 != nil {
+ error(fmt.Sprintf("%s: read: %s", args[1], err2.Error()))
+ }
+ if b1 != b2 {
+ if *sflag {
+ os.Exit(1)
+ }
+ if !*lflag {
+ fmt.Printf("%s %s differ: char %d", args[0], args[1], nc)
+ if !*Lflag {
+ fmt.Printf(" line %d", l)
+ }
+ fmt.Print("\n")
+ os.Exit(1)
+ }
+ fmt.Printf("%6d 0x%.2x 0x%.2x\n", nc, b1, b2)
+ }
+ nc++
+ if b1 == '\n' {
+ l++
+ }
+ }
+ os.Exit(0)
+}