Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/dmesg add -TT printing ISO 8601 duration



details:   https://anonhg.NetBSD.org/src/rev/57acb47ee4f5
branches:  trunk
changeset: 433526:57acb47ee4f5
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Sep 19 00:15:05 2018 +0000

description:
add -TT printing ISO 8601 duration

diffstat:

 sbin/dmesg/dmesg.8 |   7 ++-
 sbin/dmesg/dmesg.c |  94 +++++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 87 insertions(+), 14 deletions(-)

diffs (173 lines):

diff -r 989c051fe351 -r 57acb47ee4f5 sbin/dmesg/dmesg.8
--- a/sbin/dmesg/dmesg.8        Tue Sep 18 22:51:00 2018 +0000
+++ b/sbin/dmesg/dmesg.8        Wed Sep 19 00:15:05 2018 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: dmesg.8,v 1.21 2018/04/14 01:34:47 kre Exp $
+.\"    $NetBSD: dmesg.8,v 1.22 2018/09/19 00:15:05 christos Exp $
 .\"
 .\" Copyright (c) 1980, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)dmesg.8    8.1 (Berkeley) 6/5/93
 .\"
-.Dd April 10, 2018
+.Dd September 18, 2018
 .Dt DMESG 8
 .Os
 .Sh NAME
@@ -58,8 +58,9 @@
 Extract the name list from the specified system instead of the default
 ``/netbsd''.
 .It Fl T
-Format uptime timestamps in a human readable form (using
+Format uptime timestamps in a human readable form.  (using
 .Xr ctime 3 ) .
+Repeating this option, prints the uptime in ISO 8601 duration form.
 .It Fl t
 Quiet printing, don't print timestamps.
 .El
diff -r 989c051fe351 -r 57acb47ee4f5 sbin/dmesg/dmesg.c
--- a/sbin/dmesg/dmesg.c        Tue Sep 18 22:51:00 2018 +0000
+++ b/sbin/dmesg/dmesg.c        Wed Sep 19 00:15:05 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dmesg.c,v 1.33 2018/04/14 01:37:34 kre Exp $   */
+/*     $NetBSD: dmesg.c,v 1.34 2018/09/19 00:15:05 christos Exp $      */
 /*-
  * Copyright (c) 1991, 1993
  *     The Regents of the University of California.  All rights reserved.
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)dmesg.c    8.1 (Berkeley) 6/5/93";
 #else
-__RCSID("$NetBSD: dmesg.c,v 1.33 2018/04/14 01:37:34 kre Exp $");
+__RCSID("$NetBSD: dmesg.c,v 1.34 2018/09/19 00:15:05 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -47,6 +47,7 @@
 #include <sys/sysctl.h>
 
 #include <err.h>
+#include <ctype.h>
 #include <fcntl.h>
 #include <time.h>
 #include <kvm.h>
@@ -68,6 +69,76 @@
 
 #define        KREAD(addr, var) \
        kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
+
+static const char *
+fmtydhmsf(char *b, size_t l, time_t t, long nsec)
+{
+       time_t s, m, h, d, M, y;
+       int z;
+       size_t o;
+
+       s = t % 60;
+       t /= 60;
+
+       m = t % 60;
+       t /= 60;
+
+       h = t % 24;
+       t /= 24;
+
+       d = t % 30;
+       t /= 30;
+
+       M = t % 12;
+       t /= 12;
+
+       y = t;
+
+       z = 0;
+       o = 0;
+
+#define APPENDFMT(fmt, ...)  \
+    do { \
+           z = snprintf(b + o, l - o, fmt, __VA_ARGS__); \
+           if (z == -1) \
+                   return b; \
+           o += (size_t)z; \
+           if (o >= l) \
+                   return b; \
+    } while (/*CONSTCOND*/0)
+
+#define APPEND(a) \
+    do if (a) \
+    APPENDFMT("%jd%c", (intmax_t)a, toupper((unsigned char)__STRING(a)[0])); \
+    while (/*CONSTCOND*/0)
+#define APPENDS(a, s) \
+    APPENDFMT("%jd.%ld%c", (intmax_t)a, s, \
+       toupper((unsigned char)__STRING(a)[0]))
+
+       APPENDFMT("%s", "P");
+       APPEND(y);
+       APPEND(M);
+       APPEND(d);
+       APPENDFMT("%s", "T");
+       APPEND(h);
+       APPEND(m);
+       if (nsec)
+               nsec = nsec / 1000000;
+       if (nsec)
+               APPENDS(s, nsec);
+       else
+               APPEND(s);
+       return b;
+}
+
+static void
+pnsec(long nsec, long fsec, int scale)
+{
+       if (scale > 6)
+               printf("%6.6ld", (nsec + 499) / 1000);
+       else
+               printf("%*.*ld%.*s", scale, scale, fsec, 6 - scale, "000000");
+}
 #endif
 
 int
@@ -116,7 +187,7 @@
                        quiet = 1;
                        break;
                case 'T':
-                       humantime = 1;
+                       humantime++;
                        break;
                case '?':
                default:
@@ -257,7 +328,7 @@
                                        nsec *= 10;
                                if (!quiet || deltas)
                                        printf("[");
-                               if (humantime) {
+                               if (humantime == 1) {
                                        time_t t;
                                        struct tm tm;
 
@@ -268,14 +339,15 @@
                                                     &tm);
                                                printf("%s", tbuf);
                                        }
+                               } else if (humantime > 1) {
+                                       const char *fp = fmtydhmsf(tbuf,
+                                           sizeof(tbuf), sec, fsec);
+                                       if (fp) {
+                                               printf("%s", fp);
+                                       }
                                } else if (!quiet) {
-                                       if (scale > 6)
-                                               printf("% 5jd.%6.6ld",
-                                                   sec, (nsec + 499) / 1000);
-                                       else
-                                               printf("% 5jd.%*.*ld%.*s",
-                                                   sec, scale, scale, fsec,
-                                                   6 - scale, "000000");
+                                       printf(" %5jd.", sec);
+                                       pnsec(nsec, fsec, scale);
                                }
                                if (deltas) {
                                        struct timespec nt = { sec, nsec };



Home | Main Index | Thread Index | Old Index