[PATCH 3/3] touch: add --time=what option

Xabier Oneca -- xOneca xoneca at gmail.com
Mon Apr 12 21:31:44 UTC 2021


This option is the long version of -a and -m.

function                                             old     new   delta
touch_main                                           484     553     +69
.rodata                                           103649  103681     +32
static.touch_longopts                                 49      56      +7
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 108/0)             Total: 108 bytes

Signed-off-by: Xabier Oneca <xoneca at gmail.com>
---
 coreutils/touch.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/coreutils/touch.c b/coreutils/touch.c
index 6a4ffb400..9156c8cf1 100644
--- a/coreutils/touch.c
+++ b/coreutils/touch.c
@@ -76,7 +76,9 @@
  * -t STAMP
  *      use [[CC]YY]MMDDhhmm[.ss] instead of current time
  * --time=WORD
- *      change the specified time: WORD is access, atime, or use
+ *      change the specified time:
+ *        WORD is access, atime, or use: equivalent to -a
+ *        WORD is modify or mtime: equivalent to -m
  */

 #include "libbb.h"
@@ -104,12 +106,14 @@ int touch_main(int argc UNUSED_PARAM, char **argv)
         "no-create\0"         No_argument       "c"
         IF_FEATURE_TOUCH_SUSV3("reference\0"        Required_argument "r")
         IF_FEATURE_TOUCH_SUSV3("date\0"             Required_argument "d")
+        IF_FEATURE_TOUCH_SUSV3("time\0"             Required_argument "\x7f")
         IF_FEATURE_TOUCH_NODEREF("no-dereference\0" No_argument "h")
     ;
 #endif
 #if ENABLE_FEATURE_TOUCH_SUSV3
     char *reference_file = NULL;
     char *date_str = NULL;
+    IF_LONG_OPTS(char *time_arg = NULL;)
     /* timebuf[0] is atime, timebuf[1] is mtime */
     struct timespec timebuf[2];
     //timebuf[0].tv_sec  = timebuf[1].tv_sec = 0; /* -- needed on
Linux <=v2.6.26? */
@@ -136,6 +140,7 @@ int touch_main(int argc UNUSED_PARAM, char **argv)
         , &reference_file
         , &date_str
         , &date_str
+        IF_LONG_OPTS(, &time_arg)
 #endif
     );

@@ -144,6 +149,22 @@ int touch_main(int argc UNUSED_PARAM, char **argv)
         bb_show_usage();
     }

+#if ENABLE_FEATURE_TOUCH_SUSV3 && ENABLE_LONG_OPTS
+    if (time_arg) {
+        static const char *atime_values ALIGN1 = "access\0atime\0use\0";
+        static const char *mtime_values ALIGN1 = "modify\0mtime\0";
+
+        if (index_in_substrings(atime_values, time_arg) >= 0) {
+            opts |= OPT_a;
+        } else if (index_in_substrings(mtime_values, time_arg) >= 0) {
+            opts |= OPT_m;
+        } else {
+            //bb_error_msg("Invalid value: %s", time_arg);
+            bb_show_usage();
+        }
+    }
+#endif
+
     if (reference_file) {
         struct stat stbuf;
         xstat(reference_file, &stbuf);
--
2.30.2
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0003-touch-add-time-what-option.patch
Type: application/x-patch
Size: 2850 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20210412/a4151121/attachment.bin>


More information about the busybox mailing list