summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUriel <u@berlinblue.org>2012-08-31 22:55:17 +0200
committerUriel <u@berlinblue.org>2012-08-31 22:55:17 +0200
commit728bd91eae4a43e347e41e8ff18b5225ae26f25f (patch)
tree15d76ed0cd75027bb005d656183504f693f58b14
parent4a986cd1d0d59f6202430ba7a94bedda0637cc9b (diff)
parent0807c1c84811ebedb159ec9fbc433054916dd409 (diff)
Merge remote-tracking branch 'mjl/master'HEADmaster
Add mjl's commands.
-rw-r--r--basename/basename.go53
-rw-r--r--cat/cat.go52
-rw-r--r--cleanname/cleanname.go38
-rw-r--r--cmp/cmp.go98
-rw-r--r--date/date.go47
-rw-r--r--md5sum/md5sum.go52
-rw-r--r--sha1sum/sha1sum.go52
-rw-r--r--sleep/sleep.go37
-rw-r--r--tee/tee.go77
9 files changed, 506 insertions, 0 deletions
diff --git a/basename/basename.go b/basename/basename.go
new file mode 100644
index 0000000..4d33ed2
--- /dev/null
+++ b/basename/basename.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "strings"
+)
+
+var dflag = flag.Bool("d", false, "print directories, not file")
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: basename [-d] string [suffix]\n")
+ os.Exit(2)
+}
+
+func error(s string) {
+ fmt.Fprint(os.Stderr, s, "\n")
+ os.Exit(1)
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ if len(args) != 1 && len(args) != 2 {
+ usage()
+ }
+
+ slash := strings.LastIndex(args[0], "/")
+ pr := ""
+ if slash >= 0 {
+ pr = args[0][slash:]
+ }
+ if *dflag {
+ if pr != "" {
+ fmt.Print(args[0][:slash], "\n")
+ } else {
+ fmt.Print(".\n")
+ }
+ os.Exit(0)
+ }
+ if pr != "" {
+ pr = pr[1:]
+ } else {
+ pr = args[0]
+ }
+ if len(args) == 2 && strings.HasSuffix(pr, args[1]) {
+ pr = pr[:len(pr)-len(args[1])]
+ }
+ fmt.Print(pr, "\n")
+ os.Exit(0)
+}
diff --git a/cat/cat.go b/cat/cat.go
new file mode 100644
index 0000000..e2c926d
--- /dev/null
+++ b/cat/cat.go
@@ -0,0 +1,52 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io"
+ "os"
+)
+
+func cat(f io.Reader, s string) {
+ b := make([]byte, 8*1024)
+ for {
+ n, err := f.Read(b)
+ if n == 0 {
+ return
+ }
+ if err != nil {
+ error(fmt.Sprintf("%s: read: %s", s, err))
+ }
+ if n, err = os.Stdout.Write(b[:n]); err != nil {
+ error(fmt.Sprintf("write: %s", err))
+ }
+ }
+}
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: cat [file ...]\n")
+ flag.PrintDefaults()
+ os.Exit(2)
+}
+
+func error(s string) {
+ fmt.Fprint(os.Stderr, s, "\n")
+ os.Exit(1)
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ if len(args) == 0 {
+ cat(os.Stdin, "<stdin>")
+ } else {
+ for _, s := range args {
+ f, err := os.Open(s)
+ if err != nil {
+ error(fmt.Sprintf("open %s: %s", s, err))
+ }
+ cat(f, s)
+ }
+ }
+}
diff --git a/cleanname/cleanname.go b/cleanname/cleanname.go
new file mode 100644
index 0000000..759bdc8
--- /dev/null
+++ b/cleanname/cleanname.go
@@ -0,0 +1,38 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "path"
+ "strings"
+)
+
+var dir = flag.String("d", "", "directory to use as working directory")
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: cleanname [-d pwd] name ...\n")
+ os.Exit(2)
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ if len(args) == 0 {
+ usage()
+ }
+
+ for _, p := range args {
+ if *dir == "" && strings.HasPrefix(p, "/") {
+ fmt.Print(path.Clean(p), "\n")
+ } else {
+ xdir := *dir
+ if xdir == "" {
+ xdir = "."
+ }
+ fmt.Print(path.Clean(fmt.Sprint(xdir, "/", p)), "\n")
+ }
+ }
+ os.Exit(0)
+}
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)
+}
diff --git a/date/date.go b/date/date.go
new file mode 100644
index 0000000..d6ca038
--- /dev/null
+++ b/date/date.go
@@ -0,0 +1,47 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "strconv"
+ "time"
+)
+
+var nflag = flag.Bool("n", false, "print as number of seconds since epoch")
+var uflag = flag.Bool("u", false, "print utc")
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: date [-un] [seconds]\n")
+ os.Exit(2)
+}
+
+func error(s string) {
+ fmt.Fprint(os.Stderr, s, "\n")
+ os.Exit(1)
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ var now time.Time
+ if len(args) == 0 {
+ now = time.Now()
+ } else if len(args) != 1 {
+ usage()
+ } else {
+ s, err := strconv.ParseInt(args[0], 0, 64)
+ if err != nil {
+ error(fmt.Sprint("bad number: ", err))
+ }
+ now = time.Unix(s, 0)
+ }
+ if *nflag {
+ fmt.Printf("%d\n", now.Unix())
+ } else if *uflag {
+ fmt.Printf("%s\n", now.UTC().Format("Mon Jan 2 15:04:05 MST 2006"))
+ } else {
+ fmt.Printf("%s\n", now.Format("Mon Jan 2 15:04:05 MST 2006"))
+ }
+}
diff --git a/md5sum/md5sum.go b/md5sum/md5sum.go
new file mode 100644
index 0000000..a7beb5a
--- /dev/null
+++ b/md5sum/md5sum.go
@@ -0,0 +1,52 @@
+package main
+
+import (
+ "crypto/md5"
+ "flag"
+ "fmt"
+ "io"
+ "os"
+)
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: md5sum [file ...]\n")
+ os.Exit(2)
+}
+
+func error(s string) {
+ fmt.Fprint(os.Stderr, s, "\n")
+ os.Exit(1)
+}
+
+func sum(f *os.File, path string) {
+ h := md5.New()
+ if _, err := io.Copy(h, f); err != nil {
+ fmt.Fprintf(os.Stderr, "read %s: %s\n", path, err)
+ return
+ }
+ if path == "" {
+ fmt.Printf("%x\n", h.Sum(nil))
+ } else {
+ fmt.Printf("%x\t%s\n", h.Sum(nil), path)
+ }
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ if len(args) == 0 {
+ sum(os.Stdin, "")
+ } else {
+ for _, path := range args {
+ f, err := os.Open(path)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "open %s: %s\n", path, err)
+ continue
+ }
+ sum(f, path)
+ f.Close()
+ }
+ }
+ os.Exit(0)
+}
diff --git a/sha1sum/sha1sum.go b/sha1sum/sha1sum.go
new file mode 100644
index 0000000..d946f02
--- /dev/null
+++ b/sha1sum/sha1sum.go
@@ -0,0 +1,52 @@
+package main
+
+import (
+ "crypto/sha1"
+ "flag"
+ "fmt"
+ "io"
+ "os"
+)
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: sha1sum [file ...]\n")
+ os.Exit(2)
+}
+
+func error(s string) {
+ fmt.Fprint(os.Stderr, s, "\n")
+ os.Exit(1)
+}
+
+func sum(f *os.File, path string) {
+ h := sha1.New()
+ if _, err := io.Copy(h, f); err != nil {
+ fmt.Fprintf(os.Stderr, "read %s: %s\n", path, err)
+ return
+ }
+ if path == "" {
+ fmt.Printf("%x\n", h.Sum(nil))
+ } else {
+ fmt.Printf("%x\t%s\n", h.Sum(nil), path)
+ }
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ if len(args) == 0 {
+ sum(os.Stdin, "")
+ } else {
+ for _, path := range args {
+ f, err := os.Open(path)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "open %s: %s\n", path, err)
+ continue
+ }
+ sum(f, path)
+ f.Close()
+ }
+ }
+ os.Exit(0)
+}
diff --git a/sleep/sleep.go b/sleep/sleep.go
new file mode 100644
index 0000000..c34f54a
--- /dev/null
+++ b/sleep/sleep.go
@@ -0,0 +1,37 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "strconv"
+ "time"
+)
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: sleep seconds\n")
+ flag.PrintDefaults()
+ os.Exit(2)
+}
+
+func error(s string) {
+ fmt.Fprint(os.Stderr, s, "\n")
+ os.Exit(1)
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ if len(args) != 1 {
+ usage()
+ }
+ n, err := strconv.Atoi(args[0])
+ if err != nil {
+ usage()
+ }
+ for ; n > 0; n-- {
+ time.Sleep(1*time.Second)
+ }
+ os.Exit(0)
+}
diff --git a/tee/tee.go b/tee/tee.go
new file mode 100644
index 0000000..9d1cc97
--- /dev/null
+++ b/tee/tee.go
@@ -0,0 +1,77 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io"
+ "os"
+ "os/signal"
+)
+
+var iflag = flag.Bool("i", false, "ignore interrupts")
+var aflag = flag.Bool("a", false, "append instead of overwriting")
+var uflag = flag.Bool("u", false, "unix relic, ignored")
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: tee [-ai] [file ...]\n")
+ flag.PrintDefaults()
+ os.Exit(2)
+}
+
+func xerror(s string) {
+ fmt.Fprint(os.Stderr, s, "\n")
+ os.Exit(1)
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ if *iflag {
+ c := make(chan os.Signal)
+ signal.Notify(c, os.Interrupt)
+ }
+ args := flag.Args()
+ files := make([]*os.File, len(args)+1)
+ files[len(args)] = os.Stdout
+ for i, path := range args {
+ var err error
+ var f *os.File
+ if *aflag {
+ f, err = os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
+ } else {
+ f, err = os.Create(path)
+ }
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%s, ignoring\n", err)
+ continue
+ }
+ files[i] = f
+ }
+
+ buf := make([]byte, 8*1024)
+ for {
+ n, err := os.Stdin.Read(buf)
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ xerror(fmt.Sprintf("read: %s", err))
+ }
+ for i, f := range files {
+ if f == nil {
+ continue
+ }
+ _, err := f.Write(buf[:n])
+ if err != nil {
+ path := "stdout"
+ if i < len(args) {
+ path = args[i]
+ }
+ fmt.Fprintf(os.Stderr, "write %s: %s, dropping\n", path, err)
+ files[i] = nil
+ }
+ }
+ }
+
+ os.Exit(0)
+}