From owner-svn-src-head@FreeBSD.ORG  Thu Apr 16 22:09:39 2015
Return-Path: <owner-svn-src-head@FreeBSD.ORG>
Delivered-To: svn-src-head@freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org
 [IPv6:2001:1900:2254:206a::19:1])
 (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))
 (No client certificate requested)
 by hub.freebsd.org (Postfix) with ESMTPS id 047FDBBE;
 Thu, 16 Apr 2015 22:09:39 +0000 (UTC)
Received: from svn.freebsd.org (svn.freebsd.org
 [IPv6:2001:1900:2254:2068::e6a:0])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
 (Client did not present a certificate)
 by mx1.freebsd.org (Postfix) with ESMTPS id D9C2B1D9F;
 Thu, 16 Apr 2015 22:09:38 +0000 (UTC)
Received: from svn.freebsd.org ([127.0.1.70])
 by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id t3GM9cYM013919;
 Thu, 16 Apr 2015 22:09:38 GMT (envelope-from allanjude@FreeBSD.org)
Received: (from allanjude@localhost)
 by svn.freebsd.org (8.14.9/8.14.9/Submit) id t3GM9cBT013917;
 Thu, 16 Apr 2015 22:09:38 GMT (envelope-from allanjude@FreeBSD.org)
Message-Id: <201504162209.t3GM9cBT013917@svn.freebsd.org>
X-Authentication-Warning: svn.freebsd.org: allanjude set sender to
 allanjude@FreeBSD.org using -f
From: Allan Jude <allanjude@FreeBSD.org>
Date: Thu, 16 Apr 2015 22:09:38 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-all@freebsd.org,
 svn-src-head@freebsd.org
Subject: svn commit: r281625 - head/usr.bin/w
X-SVN-Group: head
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: svn-src-head@freebsd.org
X-Mailman-Version: 2.1.20
Precedence: list
List-Id: SVN commit messages for the src tree for head/-current
 <svn-src-head.freebsd.org>
List-Unsubscribe: <http://lists.freebsd.org/mailman/options/svn-src-head>,
 <mailto:svn-src-head-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-head/>
List-Post: <mailto:svn-src-head@freebsd.org>
List-Help: <mailto:svn-src-head-request@freebsd.org?subject=help>
List-Subscribe: <http://lists.freebsd.org/mailman/listinfo/svn-src-head>,
 <mailto:svn-src-head-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Thu, 16 Apr 2015 22:09:39 -0000

Author: allanjude (doc committer)
Date: Thu Apr 16 22:09:37 2015
New Revision: 281625
URL: https://svnweb.freebsd.org/changeset/base/281625

Log:
  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.

Modified:
  head/usr.bin/w/Makefile
  head/usr.bin/w/w.c

Modified: head/usr.bin/w/Makefile
==============================================================================
--- head/usr.bin/w/Makefile	Thu Apr 16 22:06:48 2015	(r281624)
+++ head/usr.bin/w/Makefile	Thu Apr 16 22:09:37 2015	(r281625)
@@ -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

Modified: head/usr.bin/w/w.c
==============================================================================
--- head/usr.bin/w/w.c	Thu Apr 16 22:06:48 2015	(r281624)
+++ head/usr.bin/w/w.c	Thu Apr 16 22:09:37 2015	(r281625)
@@ -54,8 +54,10 @@ static const char sccsid[] = "@(#)w.c	8.
 #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 */