Convert to use libxo.

Obtained from:  Phil Shafer <phil@juniper.net>
Sponsored by:   Juniper Networks, Inc.
This commit is contained in:
Marcel Moolenaar 2014-11-05 23:54:33 +00:00
parent 982247d2cf
commit 76c0abf129
3 changed files with 82 additions and 37 deletions

View File

@ -4,8 +4,8 @@
PROG= w
SRCS= fmt.c pr_time.c proc_compare.c w.c
MAN= w.1 uptime.1
DPADD= ${LIBKVM} ${LIBUTIL}
LDADD= -lkvm -lutil
DPADD= ${LIBKVM} ${LIBUTIL} ${LIBXO}
LDADD= -lkvm -lutil -lxo
#BINGRP= kmem
#BINMODE=2555
LINKS= ${BINDIR}/w ${BINDIR}/uptime

View File

@ -41,6 +41,7 @@ static const char sccsid[] = "@(#)pr_time.c 8.2 (Berkeley) 4/4/94";
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <libxo/xo.h>
#include "extern.h"
@ -82,12 +83,14 @@ pr_attime(time_t *started, time_t *now)
(void)wcsftime(buf, sizeof(buf), fmt, &tp);
len = wcslen(buf);
width = wcswidth(buf, len);
xo_attr("since", "%lu", (unsigned long) *started);
xo_attr("delta", "%lu", (unsigned long) diff);
if (len == width)
(void)wprintf(L"%-7.7ls", buf);
xo_emit("{:login-time/%-7.7ls/%ls}", buf);
else if (width < 7)
(void)wprintf(L"%ls%.*s", buf, 7 - width, " ");
xo_emit("{:login-time/%ls}%.*s", buf, 7 - width, " ");
else {
(void)wprintf(L"%ls", buf);
xo_emit("{:login-time/%ls}", buf);
offset = width - 7;
}
return (offset);
@ -104,7 +107,7 @@ pr_idle(time_t idle)
/* If idle more than 36 hours, print as a number of days. */
if (idle >= 36 * 3600) {
int days = idle / 86400;
(void)printf(" %dday%s ", days, days > 1 ? "s" : " " );
xo_emit(" {:idle/%dday%s} ", days, days > 1 ? "s" : " " );
if (days >= 100)
return (2);
if (days >= 10)
@ -113,15 +116,15 @@ pr_idle(time_t idle)
/* If idle more than an hour, print as HH:MM. */
else if (idle >= 3600)
(void)printf(" %2d:%02d ",
xo_emit(" {:idle/%2d:%02d/} ",
(int)(idle / 3600), (int)((idle % 3600) / 60));
else if (idle / 60 == 0)
(void)printf(" - ");
xo_emit(" - ");
/* Else print the minutes idle. */
else
(void)printf(" %2d ", (int)(idle / 60));
xo_emit(" {:idle/%2d} ", (int)(idle / 60));
return (0); /* not idle longer than 9 days */
}

View File

@ -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);
}