summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUriel <u@berlinblue.org>2012-08-19 17:13:09 +0200
committerUriel <u@berlinblue.org>2012-08-19 17:13:09 +0200
commit6e9eaed6b2db772f5343f7b1105142d553c4562c (patch)
tree8217197ad6d6c8b16018b58421a5ceb535496d31
parent3f5c2846770f4cac966b8f7c9fee43eb5218cbfa (diff)
Initial touch(1) version.
-rw-r--r--touch/main.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/touch/main.go b/touch/main.go
new file mode 100644
index 0000000..ed28213
--- /dev/null
+++ b/touch/main.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+ "os"
+ "flag"
+ "fmt"
+ "time"
+)
+
+var dontCreate = flag.Bool("c", false, "If the file doesn't exist, don't create it.")
+var newt = flag.Int64("t", time.Now().Unix(), "Set the modified time.")
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+
+ var t time.Time
+ if newt != nil {
+ t = time.Unix(*newt, 0)
+ } else {
+ t = time.Now()
+ }
+
+ for _, a := range flag.Args() {
+ err := os.Chtimes(a, t, t) // ??? We set the atime too
+
+ if os.IsNotExist(err) {
+ if *dontCreate {
+ fmt.Fprintf(os.Stderr, "touch: %s\n", err)
+ os.Exit(2)
+ }
+ f, err := os.Create(a)
+ if err != nil {
+ f.Close()
+ }
+
+ if err == nil && newt != nil {
+ err = os.Chtimes(a, t, t)
+ }
+
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "touch: %s\n", err)
+ os.Exit(2)
+ }
+ }
+ }
+}
+
+func usage() {
+ fmt.Print("usage: touch [-c] [-t time] files...")
+ flag.PrintDefaults()
+ os.Exit(2)
+}