OSDN Git Service

Add special "touch -" behavior and require an argument.
authorRob Landley <rob@landley.net>
Thu, 20 Oct 2016 21:35:13 +0000 (16:35 -0500)
committerRob Landley <rob@landley.net>
Thu, 20 Oct 2016 21:35:13 +0000 (16:35 -0500)
tests/touch.test
toys/posix/touch.c

index 1b71c5e..263e9a0 100755 (executable)
@@ -32,6 +32,9 @@ testing "-t seconds" \
   "touch -t 201201231234.56 walrus && date -r walrus +%Y%m%d-%H%M%S.%N" \
   "20120123-123456.000000000\n" "" ""
 
+testing "-t -" "touch -t 200109081946.40 - > walrus && date -r walrus +%s" \
+   "1000000000\n" "" ""
+
 testing "-t nanoseconds" \
   "touch -t 201201231234.56123456789 walrus && date -r walrus +%Y%m%d-%H%M%S.%N" \
   "20120123-123456.123456789\n" "" ""
index c18087a..79eba17 100644 (file)
@@ -4,7 +4,7 @@
  *
  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html
 
-USE_TOUCH(NEWTOY(touch, "acd:mr:t:h[!dtr]", TOYFLAG_BIN))
+USE_TOUCH(NEWTOY(touch, "<1acd:mr:t:h[!dtr]", TOYFLAG_BIN))
 
 config TOUCH
   bool "touch"
@@ -115,13 +115,21 @@ void touch_main(void)
 
   // Loop through files on command line
   for (ss = toys.optargs; *ss;) {
+    char *s = *ss++;
 
-    // cheat: FLAG_h is rightmost flag, so its value is 1
-    if (!utimensat(AT_FDCWD, *ss, ts,
-        (toys.optflags & FLAG_h)*AT_SYMLINK_NOFOLLOW)) ss++;
-    else if (toys.optflags & FLAG_c) ss++;
-    else if (access(*ss, F_OK) && (-1!=(fd = open(*ss, O_CREAT, 0666))))
-      close(fd);
-    else perror_msg("'%s'", *ss++);
+    if (!strcmp(s, "-")) {
+      if (!futimens(1, ts)) continue;
+    } else {
+      // cheat: FLAG_h is rightmost flag, so its value is 1
+      if (!utimensat(AT_FDCWD, s, ts,
+          (toys.optflags & FLAG_h)*AT_SYMLINK_NOFOLLOW)) continue;
+      if (toys.optflags & FLAG_c) continue;
+      if (access(s, F_OK) && (-1!=(fd = open(s, O_CREAT, 0666)))) {
+        close(fd);
+        if (toys.optflags) ss--;
+        continue;
+      }
+    }
+    perror_msg("'%s'", s);
   }
 }