summaryrefslogtreecommitdiffstats
path: root/usr.bin/w
diff options
context:
space:
mode:
authorallanjude <allanjude@FreeBSD.org>2015-04-16 22:09:37 +0000
committerallanjude <allanjude@FreeBSD.org>2015-04-16 22:09:37 +0000
commit3530f6e66c24a067b18d2f54cf0192345da67ca4 (patch)
tree58dfdcc492bcaf1f81f2c894f30075be48819acd /usr.bin/w
parentd3795456a8c2fcaadd3457ba209db76843c3b178 (diff)
downloadFreeBSD-src-3530f6e66c24a067b18d2f54cf0192345da67ca4.zip
FreeBSD-src-3530f6e66c24a067b18d2f54cf0192345da67ca4.tar.gz
Fix libxo output from uptime command
the libxo output for uptime returned multiple 'uptime' keys, one each for number of days, hours, and minutes of uptime. This is invalid JSON. This patch makes the output the raw number of seconds, as well as adding keys for the individual unit values A string of the original output from the plain-text uptime command is also added Differential Revision: https://reviews.freebsd.org/D2063 Reviewed by: jmg Approved by: marcel Sponsored by: ScaleEngine Inc.
Diffstat (limited to 'usr.bin/w')
-rw-r--r--usr.bin/w/Makefile2
-rw-r--r--usr.bin/w/w.c24
2 files changed, 18 insertions, 8 deletions
diff --git a/usr.bin/w/Makefile b/usr.bin/w/Makefile
index cb7a063..0c51463 100644
--- a/usr.bin/w/Makefile
+++ b/usr.bin/w/Makefile
@@ -4,7 +4,7 @@
PROG= w
SRCS= fmt.c pr_time.c proc_compare.c w.c
MAN= w.1 uptime.1
-LIBADD= kvm util xo
+LIBADD= kvm sbuf util xo
#BINGRP= kmem
#BINMODE=2555
LINKS= ${BINDIR}/w ${BINDIR}/uptime
diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c
index f21b976..f94d8a6 100644
--- a/usr.bin/w/w.c
+++ b/usr.bin/w/w.c
@@ -54,8 +54,10 @@ static const char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/ioctl.h>
+#include <sys/sbuf.h>
#include <sys/socket.h>
#include <sys/tty.h>
+#include <sys/types.h>
#include <machine/cpu.h>
#include <netinet/in.h>
@@ -472,7 +474,9 @@ pr_header(time_t *nowp, int nusers)
struct timespec tp;
int days, hrs, i, mins, secs;
char buf[256];
+ struct sbuf *upbuf;
+ upbuf = sbuf_new_auto();
/*
* Print time of day.
*/
@@ -493,21 +497,27 @@ pr_header(time_t *nowp, int nusers)
mins = uptime / 60;
secs = uptime % 60;
xo_emit(" up");
- xo_attr("seconds", "%lu", (unsigned long) tp.tv_sec);
+ xo_emit("{e:uptime/%lu}", (unsigned long) tp.tv_sec);
+ xo_emit("{e:days/%d}{e:hours/%d}{e:minutes/%d}{e:seconds/%d}", days, hrs, mins, secs);
+
if (days > 0)
- xo_emit(" {:uptime/%d day%s},",
+ sbuf_printf(upbuf, " %d day%s,",
days, days > 1 ? "s" : "");
if (hrs > 0 && mins > 0)
- xo_emit(" {:uptime/%2d:%02d},", hrs, mins);
+ sbuf_printf(upbuf, " %2d:%02d,", hrs, mins);
else if (hrs > 0)
- xo_emit(" {:uptime/%d hr%s},",
+ sbuf_printf(upbuf, " %d hr%s,",
hrs, hrs > 1 ? "s" : "");
else if (mins > 0)
- xo_emit(" {:uptime/%d min%s},",
+ sbuf_printf(upbuf, " %d min%s,",
mins, mins > 1 ? "s" : "");
- else
- xo_emit(" {:uptime/%d sec%s},",
+ else
+ sbuf_printf(upbuf, " %d sec%s,",
secs, secs > 1 ? "s" : "");
+ if (sbuf_finish(upbuf) != 0)
+ xo_err(1, "Could not generate output");
+ xo_emit("{:uptime-human/%s}", sbuf_data(upbuf));
+ sbuf_delete(upbuf);
}
/* Print number of users logged in to system */
OpenPOWER on IntegriCloud