[PATCH] touch: add config option to set microsecond timestamps

Peter D urmum69 at snopyta.org
Thu Apr 8 04:07:43 UTC 2021


This patch adds a config option which makes touch set timestamps to the
current microsecond, instead of the current second.

Signed-off-by: Peter D <urmum69 at snopyta.org>
---
This patch should be applied on top of Xabier Oneca's patches to add the
-a and -m options to busybox

 coreutils/touch.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/coreutils/touch.c b/coreutils/touch.c
index e80720982..ac9e233ad 100644
--- a/coreutils/touch.c
+++ b/coreutils/touch.c
@@ -33,6 +33,13 @@
 //config:	depends on TOUCH
 //config:	help
 //config:	Enable touch to use a reference file or a given date/time argument.
+//config:
+//config:config FEATURE_TOUCH_MICROSECONDS
+//config:	bool "Add support for setting microsecond timestamps"
+//config:	default y
+//config:	depends on TOUCH
+//config:	help
+//config:	Enable touch to set timestamps to the current microsecond
 
 //applet:IF_TOUCH(APPLET_NOFORK(touch, touch, BB_DIR_BIN, BB_SUID_DROP, touch))
 
@@ -142,10 +149,11 @@ int touch_main(int argc UNUSED_PARAM, char **argv)
 		xstat(reference_file, &stbuf);
 		timebuf[0].tv_sec = stbuf.st_atime;
 		timebuf[1].tv_sec = stbuf.st_mtime;
-		/* Can use .st_mtim.tv_nsec
-		 * (or is it .st_mtimensec?? see date.c)
-		 * to set microseconds too.
-		 */
+#if ENABLE_FEATURE_TOUCH_MICROSECONDS
+		/* divide nanoseconds by 1000 to get microseconds */
+		timebuf[0].tv_usec = stbuf.st_atim.tv_nsec / 1000;
+		timebuf[1].tv_usec = stbuf.st_mtim.tv_nsec / 1000;
+#endif
 		newtime = timebuf;
 	}
 
@@ -190,8 +198,14 @@ int touch_main(int argc UNUSED_PARAM, char **argv)
 				 */
 				if (!(opts & OPT_a))
 					timebuf[0].tv_sec = stbuf.st_atime;
+#if ENABLE_FEATURE_TOUCH_MICROSECONDS
+					timebuf[0].tv_usec = stbuf.st_atim.tv_nsec / 1000;
+#endif
 				if (!(opts & OPT_m))
 					timebuf[1].tv_sec = stbuf.st_mtime;
+#if ENABLE_FEATURE_TOUCH_MICROSECONDS
+					timebuf[1].tv_usec = stbuf.st_mtim.tv_nsec / 1000;
+#endif
 			}
 		}
 
-- 
2.31.1



More information about the busybox mailing list