[PATCH v2] stat: print nanoseconds immediately after seconds

Dmitry Savelyev ds2012.65 at gmail.com
Sun Apr 23 23:46:22 UTC 2017


* util.c (sprinttime): Add an nsec parameter for printing nanoseconds
immediately after seconds.
* defs.h (sprinttime): Likewise.
* statx.c (SYS_FUNC(statx)): Use it.
* print_struct_stat.c (print_struct_stat): Likewise.
* tests/print_time.c (print_time_t_nsec): Update to test printing of
times in the new format.
---
 defs.h              |  2 +-
 print_struct_stat.c |  4 +---
 statx.c             |  5 ++---
 tests/print_time.c  | 15 +++++++++++----
 util.c              | 26 +++++++++++++++++++-------
 utime.c             |  4 ++--
 6 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/defs.h b/defs.h
index 5b81647c..223a47a8 100644
--- a/defs.h
+++ b/defs.h
@@ -547,7 +547,7 @@ extern int printargs_d(struct tcb *);
 extern void addflags(const struct xlat *, uint64_t);
 extern int printflags64(const struct xlat *, uint64_t, const char *);
 extern const char *sprintflags(const char *, const struct xlat *, uint64_t);
-extern const char *sprinttime(time_t);
+extern const char *sprinttime(time_t, unsigned long long);
 extern void print_symbolic_mode_t(unsigned int);
 extern void print_numeric_umode_t(unsigned short);
 extern void print_numeric_long_umask(unsigned long);
diff --git a/print_struct_stat.c b/print_struct_stat.c
index b5e0167e..487206ed 100644
--- a/print_struct_stat.c
+++ b/print_struct_stat.c
@@ -67,9 +67,7 @@ print_struct_stat(struct tcb *tcp, const struct strace_stat *const st)
 	if (!abbrev(tcp)) {
 #define PRINT_ST_TIME(field)				\
 	tprints(", st_" #field "=");			\
-	tprints(sprinttime(st->field));			\
-	if (st->field ## _nsec)				\
-		tprintf(".%09llu", st->field ## _nsec)
+	tprints(sprinttime(st->field, st->field ## _nsec));
 
 		PRINT_ST_TIME(atime);
 		PRINT_ST_TIME(mtime);
diff --git a/statx.c b/statx.c
index c53f29c2..e0c576ca 100644
--- a/statx.c
+++ b/statx.c
@@ -60,9 +60,8 @@ SYS_FUNC(statx)
 #define PRINT_FIELD_TIME(field)						\
 	do {								\
 		tprints(", " #field "=");				\
-		tprints(sprinttime(stx.field.sec));			\
-		if (stx.field.nsec)					\
-			tprintf(".%09" PRId32, stx.field.nsec);		\
+		tprints(sprinttime(stx.field.sec,			\
+				   zero_extend_signed_to_ull(stx.field.nsec))); \
 	} while (0)
 
 		struct_statx stx;
diff --git a/tests/print_time.c b/tests/print_time.c
index 3d5b5545..76411dd7 100644
--- a/tests/print_time.c
+++ b/tests/print_time.c
@@ -42,13 +42,20 @@ print_time_t_nsec(const time_t t, const unsigned long long nsec)
 			perror_msg_and_fail("localtime");
 		}
 
-		strftime(buf, sizeof(buf), "%FT%T%z", p);
+		strftime(buf, sizeof(buf), "%FT%T", p);
+		fputs(buf, stdout);
+
+		if (nsec) {
+			printf(".%09llu", nsec);
+		}
+
+		strftime(buf, sizeof(buf), "%z", p);
 		fputs(buf, stdout);
 	} else {
 		putchar('0');
-	}
 
-	if (nsec) {
-		printf(".%09llu", nsec);
+		if (nsec) {
+			printf("%.09llu", nsec);
+		}
 	}
 }
diff --git a/util.c b/util.c
index 31336a42..bf6439f0 100644
--- a/util.c
+++ b/util.c
@@ -545,18 +545,30 @@ printnum_addr_klong_int(struct tcb *tcp, const kernel_ulong_t addr)
 #endif /* !current_klongsize */
 
 const char *
-sprinttime(time_t t)
+sprinttime(time_t t, unsigned long long nsec)
 {
+	static char buf[sizeof(int) * 3 * 6 + sizeof(unsigned long long) * 3 +
+			sizeof("+0000")];
 	struct tm *tmp;
-	static char buf[sizeof(int) * 3 * 6 + sizeof("+0000")];
 
-	if (t == 0)
+	if (t == 0 && nsec == 0)
 		return "0";
 	tmp = localtime(&t);
-	if (tmp)
-		strftime(buf, sizeof(buf), "%FT%T%z", tmp);
-	else
-		snprintf(buf, sizeof(buf), "%lu", (unsigned long) t);
+	if (tmp) {
+		if (nsec) {
+			static char tmp_buf[sizeof(buf)];
+			strftime(tmp_buf, sizeof(tmp_buf), "%FT%T.%%09llu%z",
+				 tmp);
+			snprintf(buf, sizeof(buf), tmp_buf, nsec);
+		} else
+			strftime(buf, sizeof(buf), "%FT%T%z", tmp);
+	} else {
+		if (nsec)
+			snprintf(buf, sizeof(buf), "%lu.%09llu",
+				 (unsigned long) t, nsec);
+		else
+			snprintf(buf, sizeof(buf), "%lu", (unsigned long) t);
+	}
 
 	return buf;
 }
diff --git a/utime.c b/utime.c
index 8126b350..1bff0faa 100644
--- a/utime.c
+++ b/utime.c
@@ -15,8 +15,8 @@ SYS_FUNC(utime)
 	printpath(tcp, tcp->u_arg[0]);
 	tprints(", ");
 	if (!umove_or_printaddr(tcp, tcp->u_arg[1], &u)) {
-		tprintf("{actime=%s,", sprinttime(u.actime));
-		tprintf(" modtime=%s}", sprinttime(u.modtime));
+		tprintf("{actime=%s,", sprinttime(u.actime, 0));
+		tprintf(" modtime=%s}", sprinttime(u.modtime, 0));
 	}
 
 	return RVAL_DECODED;
-- 
2.12.2





More information about the Strace-devel mailing list