summaryrefslogtreecommitdiffstats
path: root/usr.bin/w/w.c
diff options
context:
space:
mode:
authormarcel <marcel@FreeBSD.org>2014-11-05 23:54:33 +0000
committermarcel <marcel@FreeBSD.org>2014-11-05 23:54:33 +0000
commitf0f8104fff67466ca69df4b6d363029b7629ddfe (patch)
tree559eb6a543cb848de37e2ce03c7fed99470466f2 /usr.bin/w/w.c
parentd18a2c8088ca3144e65c43a66c8990e98f95eeab (diff)
downloadFreeBSD-src-f0f8104fff67466ca69df4b6d363029b7629ddfe.zip
FreeBSD-src-f0f8104fff67466ca69df4b6d363029b7629ddfe.tar.gz
Convert to use libxo.
Obtained from: Phil Shafer <phil@juniper.net> Sponsored by: Juniper Networks, Inc.
Diffstat (limited to 'usr.bin/w/w.c')
-rw-r--r--usr.bin/w/w.c98
1 files changed, 70 insertions, 28 deletions
diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c
index ca5b79e..059f7ce 100644
--- a/usr.bin/w/w.c
+++ b/usr.bin/w/w.c
@@ -83,6 +83,7 @@ static const char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
#include <unistd.h>
#include <utmpx.h>
#include <vis.h>
+#include <libxo/xo.h>
#include "extern.h"
@@ -133,7 +134,7 @@ main(int argc, char *argv[])
struct stat *stp;
time_t touched;
int ch, i, nentries, nusers, wcmd, longidle, longattime, dropgid;
- const char *memf, *nlistf, *p;
+ const char *memf, *nlistf, *p, *save_p;
char *x_suffix;
char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
char fn[MAXHOSTNAMELEN];
@@ -143,6 +144,10 @@ main(int argc, char *argv[])
use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
use_comma = (*nl_langinfo(RADIXCHAR) != ',');
+ argc = xo_parse_args(argc, argv);
+ if (argc < 0)
+ exit(1);
+
/* Are we w(1) or uptime(1)? */
if (strcmp(basename(argv[0]), "uptime") == 0) {
wcmd = 0;
@@ -254,9 +259,12 @@ main(int argc, char *argv[])
}
endutxent();
+ xo_open_container("uptime-information");
+
if (header || wcmd == 0) {
pr_header(&now, nusers);
if (wcmd == 0) {
+ xo_close_container("uptime-information");
(void)kvm_close(kd);
exit(0);
}
@@ -268,7 +276,7 @@ main(int argc, char *argv[])
#define HEADER_WHAT "WHAT\n"
#define WUSED (W_DISPUSERSIZE + W_DISPLINESIZE + W_DISPHOSTSIZE + \
sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
- (void)printf("%-*.*s %-*.*s %-*.*s %s",
+ xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%s}",
W_DISPUSERSIZE, W_DISPUSERSIZE, HEADER_USER,
W_DISPLINESIZE, W_DISPLINESIZE, HEADER_TTY,
W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
@@ -342,6 +350,9 @@ main(int argc, char *argv[])
}
}
+ xo_open_container("user-table");
+ xo_open_list("user-entry");
+
for (ep = ehead; ep != NULL; ep = ep->next) {
struct addrinfo hints, *res;
struct sockaddr_storage ss;
@@ -351,7 +362,9 @@ main(int argc, char *argv[])
time_t t;
int isaddr;
- p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
+ xo_open_instance("user-entry");
+
+ save_p = p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
if ((x_suffix = strrchr(p, ':')) != NULL) {
if ((dot = strchr(x_suffix, '.')) != NULL &&
strchr(dot+1, '.') == NULL)
@@ -400,6 +413,9 @@ main(int argc, char *argv[])
p = buf;
}
if (dflag) {
+ xo_open_container("process-table");
+ xo_open_list("process-entry");
+
for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
const char *ptr;
@@ -407,24 +423,41 @@ main(int argc, char *argv[])
dkp->ki_comm, NULL, MAXCOMLEN);
if (ptr == NULL)
ptr = "-";
- (void)printf("\t\t%-9d %s\n",
+ xo_open_instance("process-entry");
+ xo_emit("\t\t{:process-id/%-9d/%d} {:command/%s}\n",
dkp->ki_pid, ptr);
+ xo_close_instance("process-entry");
}
+ xo_close_list("process-entry");
+ xo_close_container("process-table");
}
- (void)printf("%-*.*s %-*.*s %-*.*s ",
- W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user,
- W_DISPLINESIZE, W_DISPLINESIZE,
- *ep->utmp.ut_line ?
- (strncmp(ep->utmp.ut_line, "tty", 3) &&
- strncmp(ep->utmp.ut_line, "cua", 3) ?
- ep->utmp.ut_line : ep->utmp.ut_line + 3) : "-",
+ xo_emit("{:user/%-*.*s/%@**@s} {:tty/%-*.*s/%@**@s} ",
+ W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user,
+ W_DISPLINESIZE, W_DISPLINESIZE,
+ *ep->utmp.ut_line ?
+ (strncmp(ep->utmp.ut_line, "tty", 3) &&
+ strncmp(ep->utmp.ut_line, "cua", 3) ?
+ ep->utmp.ut_line : ep->utmp.ut_line + 3) : "-");
+
+ if (save_p && save_p != p)
+ xo_attr("address", "%s", save_p);
+ xo_emit("{:from/%-*.*s/%@**@s} ",
W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
t = ep->utmp.ut_tv.tv_sec;
longattime = pr_attime(&t, &now);
longidle = pr_idle(ep->idle);
- (void)printf("%.*s\n", argwidth - longidle - longattime,
+ xo_emit("{:command/%.*s/%@*@s}\n",
+ argwidth - longidle - longattime,
ep->args);
+
+ xo_close_instance("user-entry");
}
+
+ xo_close_list("user-entry");
+ xo_close_container("user-table");
+ xo_close_container("uptime-information");
+ xo_finish();
+
(void)kvm_close(kd);
exit(0);
}
@@ -443,7 +476,7 @@ pr_header(time_t *nowp, int nusers)
*/
if (strftime(buf, sizeof(buf),
use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0)
- (void)printf("%s ", buf);
+ xo_emit("{:time-of-day/%s} ", buf);
/*
* Print how long system has been up.
*/
@@ -457,35 +490,45 @@ pr_header(time_t *nowp, int nusers)
uptime %= 3600;
mins = uptime / 60;
secs = uptime % 60;
- (void)printf(" up");
+ xo_emit(" up");
+ xo_attr("seconds", "%lu", (unsigned long) tp.tv_sec);
if (days > 0)
- (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
+ xo_emit(" {:uptime/%d day%s},",
+ days, days > 1 ? "s" : "");
if (hrs > 0 && mins > 0)
- (void)printf(" %2d:%02d,", hrs, mins);
+ xo_emit(" {:uptime/%2d:%02d},", hrs, mins);
else if (hrs > 0)
- (void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
+ xo_emit(" {:uptime/%d hr%s},",
+ hrs, hrs > 1 ? "s" : "");
else if (mins > 0)
- (void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
+ xo_emit(" {:uptime/%d min%s},",
+ mins, mins > 1 ? "s" : "");
else
- (void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
+ xo_emit(" {:uptime/%d sec%s},",
+ secs, secs > 1 ? "s" : "");
}
/* Print number of users logged in to system */
- (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
+ xo_emit(" {:users/%d} user%s", nusers, nusers == 1 ? "" : "s");
/*
* Print 1, 5, and 15 minute load averages.
*/
if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
- (void)printf(", no load average information available\n");
+ xo_emit(", no load average information available\n");
else {
- (void)printf(", load averages:");
+ static const char *format[] = {
+ " {:load-average-1/%.2f}",
+ " {:load-average-5/%.2f}",
+ " {:load-average-15/%.2f}",
+ };
+ xo_emit(", load averages:");
for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) {
if (use_comma && i > 0)
- (void)printf(",");
- (void)printf(" %.2f", avenrun[i]);
+ xo_emit(",");
+ xo_emit(format[i], avenrun[i]);
}
- (void)printf("\n");
+ xo_emit("\n");
}
}
@@ -506,9 +549,8 @@ static void
usage(int wcmd)
{
if (wcmd)
- (void)fprintf(stderr,
- "usage: w [-dhin] [-M core] [-N system] [user ...]\n");
+ xo_error("usage: w [-dhin] [-M core] [-N system] [user ...]\n");
else
- (void)fprintf(stderr, "usage: uptime\n");
+ xo_error("usage: uptime\n");
exit(1);
}
OpenPOWER on IntegriCloud