Calculate field widths on the fly so that all columns line up nicely

and make more effective use of screen real estate when some columns
(eg USER and VSZ) didn't need the full default width.
This commit is contained in:
simonb 2000-06-07 04:57:59 +00:00
parent 8183434ff7
commit a98dd470c6
5 changed files with 549 additions and 251 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.18 2000/06/02 03:39:02 simonb Exp $ */
/* $NetBSD: extern.h,v 1.19 2000/06/07 04:57:59 simonb Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@ -50,42 +50,42 @@ extern VAR var[];
extern VARENT *vhead;
__BEGIN_DECLS
void command __P((struct kinfo_proc2 *, VARENT *));
void cputime __P((struct kinfo_proc2 *, VARENT *));
void command __P((struct kinfo_proc2 *, VARENT *, int));
void cputime __P((struct kinfo_proc2 *, VARENT *, int));
int donlist __P((void));
int donlist_sysctl __P((void));
void fmt_puts __P((char *, int *));
void fmt_putc __P((int, int *));
double getpcpu __P((struct kinfo_proc2 *));
double getpmem __P((struct kinfo_proc2 *));
void logname __P((struct kinfo_proc2 *, VARENT *));
void longtname __P((struct kinfo_proc2 *, VARENT *));
void lstarted __P((struct kinfo_proc2 *, VARENT *));
void maxrss __P((struct kinfo_proc2 *, VARENT *));
void logname __P((struct kinfo_proc2 *, VARENT *, int));
void longtname __P((struct kinfo_proc2 *, VARENT *, int));
void lstarted __P((struct kinfo_proc2 *, VARENT *, int));
void maxrss __P((struct kinfo_proc2 *, VARENT *, int));
void nlisterr __P((struct nlist *));
void p_rssize __P((struct kinfo_proc2 *, VARENT *));
void pagein __P((struct kinfo_proc2 *, VARENT *));
void p_rssize __P((struct kinfo_proc2 *, VARENT *, int));
void pagein __P((struct kinfo_proc2 *, VARENT *, int));
void parsefmt __P((char *));
void pcpu __P((struct kinfo_proc2 *, VARENT *));
void pmem __P((struct kinfo_proc2 *, VARENT *));
void pnice __P((struct kinfo_proc2 *, VARENT *));
void pri __P((struct kinfo_proc2 *, VARENT *));
void pcpu __P((struct kinfo_proc2 *, VARENT *, int));
void pmem __P((struct kinfo_proc2 *, VARENT *, int));
void pnice __P((struct kinfo_proc2 *, VARENT *, int));
void pri __P((struct kinfo_proc2 *, VARENT *, int));
void printheader __P((void));
struct kinfo_proc2
*getkinfo_procfs __P((int, int, int*));
*getkinfo_procfs __P((int, int, int *));
char **procfs_getargv __P((const struct kinfo_proc2 *, int));
void pvar __P((struct kinfo_proc2 *, VARENT *));
void rssize __P((struct kinfo_proc2 *, VARENT *));
void runame __P((struct kinfo_proc2 *, VARENT *));
void pvar __P((struct kinfo_proc2 *, VARENT *, int));
void rssize __P((struct kinfo_proc2 *, VARENT *, int));
void runame __P((struct kinfo_proc2 *, VARENT *, int));
void showkey __P((void));
void started __P((struct kinfo_proc2 *, VARENT *));
void state __P((struct kinfo_proc2 *, VARENT *));
void tdev __P((struct kinfo_proc2 *, VARENT *));
void tname __P((struct kinfo_proc2 *, VARENT *));
void tsize __P((struct kinfo_proc2 *, VARENT *));
void ucomm __P((struct kinfo_proc2 *, VARENT *));
void uname __P((struct kinfo_proc2 *, VARENT *));
void uvar __P((struct kinfo_proc2 *, VARENT *));
void vsize __P((struct kinfo_proc2 *, VARENT *));
void wchan __P((struct kinfo_proc2 *, VARENT *));
void started __P((struct kinfo_proc2 *, VARENT *, int));
void state __P((struct kinfo_proc2 *, VARENT *, int));
void tdev __P((struct kinfo_proc2 *, VARENT *, int));
void tname __P((struct kinfo_proc2 *, VARENT *, int));
void tsize __P((struct kinfo_proc2 *, VARENT *, int));
void ucomm __P((struct kinfo_proc2 *, VARENT *, int));
void uname __P((struct kinfo_proc2 *, VARENT *, int));
void uvar __P((struct kinfo_proc2 *, VARENT *, int));
void vsize __P((struct kinfo_proc2 *, VARENT *, int));
void wchan __P((struct kinfo_proc2 *, VARENT *, int));
__END_DECLS

View File

@ -1,4 +1,4 @@
/* $NetBSD: keyword.c,v 1.24 2000/06/02 03:39:02 simonb Exp $ */
/* $NetBSD: keyword.c,v 1.25 2000/06/07 04:57:59 simonb Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@ -38,7 +38,7 @@
#if 0
static char sccsid[] = "@(#)keyword.c 8.5 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: keyword.c,v 1.24 2000/06/02 03:39:02 simonb Exp $");
__RCSID("$NetBSD: keyword.c,v 1.25 2000/06/07 04:57:59 simonb Exp $");
#endif
#endif /* not lint */
@ -63,126 +63,118 @@ __RCSID("$NetBSD: keyword.c,v 1.24 2000/06/02 03:39:02 simonb Exp $");
static VAR *findvar __P((char *));
static int vcmp __P((const void *, const void *));
#define SIGWIDTH (((_NSIG / 32) + 1) * 8)
#ifdef NOTINUSE
int utime(), stime(), ixrss(), idrss(), isrss();
{{"utime"}, "UTIME", USER, utime, 4},
{{"stime"}, "STIME", USER, stime, 4},
{{"ixrss"}, "IXRSS", USER, ixrss, 4},
{{"idrss"}, "IDRSS", USER, idrss, 4},
{{"isrss"}, "ISRSS", USER, isrss, 4},
{"stime", "STIME", NULL, 0, cputime}, /* XXX add stime, utime ... */
{"utime", "UTIME", NULL, 0, cputime}, /* ... display to cputime() */
{"idrss", "IDRSS", NULL, 0, pvar, 0, POFF(p_uru_idrss), ULONG, "d"},
{"isrss", "ISRSS", NULL, 0, pvar, 0, POFF(p_uru_isrss), ULONG, "d"},
{"ixrss", "IXRSS", NULL, 0, pvar, 0, POFF(p_uru_ixrss), ULONG, "d"},
#endif
/* Compute offset in common structures. */
#define POFF(x) offsetof(struct kinfo_proc2, x)
#define UIDFMT "u"
#define UIDLEN 5
#define UID(n1, n2, fn, off) \
{ n1, n2, NULL, 0, fn, UIDLEN, off, UINT32, UIDFMT }
{ n1, n2, NULL, 0, fn, 0, off, UINT32, UIDFMT }
#define GID(n1, n2, fn, off) UID(n1, n2, fn, off)
#define PIDFMT "d"
#define PIDLEN 5
#define PID(n1, n2, fn, off) \
{ n1, n2, NULL, 0, fn, PIDLEN, off, INT32, PIDFMT }
#define USERLEN 8
{ n1, n2, NULL, 0, fn, 0, off, INT32, PIDFMT }
VAR var[] = {
{"%cpu", "%CPU", NULL, 0, pcpu, 4},
{"%mem", "%MEM", NULL, 0, pmem, 4},
{"acflag", "ACFLG", NULL, 0, pvar, 3, POFF(p_acflag), USHORT, "x"},
{"%cpu", "%CPU", NULL, 0, pcpu},
{"%mem", "%MEM", NULL, 0, pmem},
{"acflag", "ACFLG", NULL, 0, pvar, 0, POFF(p_acflag), USHORT, "x"},
{"acflg", "", "acflag"},
{"blocked", "", "sigmask"},
{"caught", "", "sigcatch"},
{"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
{"cpu", "CPU", NULL, 0, pvar, 3, POFF(p_estcpu), UINT, "d"},
{"command", "COMMAND", NULL, COMM|LJUST, command},
{"cpu", "CPU", NULL, 0, pvar, 0, POFF(p_estcpu), UINT, "d"},
{"cputime", "", "time"},
{"f", "F", NULL, 0, pvar, 7, POFF(p_flag), INT, "x"},
{"f", "F", NULL, 0, pvar, 0, POFF(p_flag), INT, "x"},
{"flags", "", "f"},
{"holdcnt", "HOLDCNT", NULL, 0, pvar, 8, POFF(p_holdcnt), INT, "d"},
{"holdcnt", "HOLDCNT", NULL, 0, pvar, 0, POFF(p_holdcnt), INT, "d"},
{"ignored", "", "sigignore"},
{"inblk", "INBLK", NULL, USER, pvar, 4, POFF(p_uru_inblock), LONG, "d"},
{"inblk", "INBLK", NULL, 0, pvar, 0, POFF(p_uru_inblock), ULONG, "d"},
{"inblock", "", "inblk"},
{"jobc", "JOBC", NULL, 0, pvar, 4, POFF(p_jobc), SHORT, "d"},
{"ktrace", "KTRACE", NULL, 0, pvar, 8, POFF(p_traceflag), INT, "x"},
{"jobc", "JOBC", NULL, 0, pvar, 0, POFF(p_jobc), SHORT, "d"},
{"ktrace", "KTRACE", NULL, 0, pvar, 0, POFF(p_traceflag), INT, "x"},
/* XXX */
{"ktracep", "KTRACEP", NULL, 0, pvar, 8, POFF(p_tracep), KPTR, "x"},
{"lim", "LIM", NULL, 0, maxrss, 5},
{"login", "LOGIN", NULL, LJUST, logname, MAXLOGNAME},
{"ktracep", "KTRACEP", NULL, 0, pvar, 0, POFF(p_tracep), KPTR, "x"},
{"lim", "LIM", NULL, 0, maxrss},
{"login", "LOGIN", NULL, LJUST, logname},
{"logname", "", "login"},
{"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28},
{"majflt", "MAJFLT", NULL, USER, pvar, 4, POFF(p_uru_majflt), LONG, "d"},
{"minflt", "MINFLT", NULL, USER, pvar, 4, POFF(p_uru_minflt), LONG, "d"},
{"msgrcv", "MSGRCV", NULL, USER, pvar, 4, POFF(p_uru_msgrcv), LONG, "d"},
{"msgsnd", "MSGSND", NULL, USER, pvar, 4, POFF(p_uru_msgsnd), LONG, "d"},
{"lstart", "STARTED", NULL, LJUST, lstarted},
{"majflt", "MAJFLT", NULL, 0, pvar, 0, POFF(p_uru_majflt), ULONG, "d"},
{"minflt", "MINFLT", NULL, 0, pvar, 0, POFF(p_uru_minflt), ULONG, "d"},
{"msgrcv", "MSGRCV", NULL, 0, pvar, 0, POFF(p_uru_msgrcv), ULONG, "d"},
{"msgsnd", "MSGSND", NULL, 0, pvar, 0, POFF(p_uru_msgsnd), ULONG, "d"},
{"ni", "", "nice"},
{"nice", "NI", NULL, 0, pnice, 3},
{"nivcsw", "NIVCSW", NULL, USER, pvar, 5, POFF(p_uru_nivcsw), LONG, "d"},
{"nice", "NI", NULL, 0, pnice},
{"nivcsw", "NIVCSW", NULL, 0, pvar, 0, POFF(p_uru_nivcsw), ULONG, "d"},
{"nsignals", "", "nsigs"},
{"nsigs", "NSIGS", NULL, USER, pvar, 4, POFF(p_uru_nsignals), LONG, "d"},
{"nswap", "NSWAP", NULL, USER, pvar, 4, POFF(p_uru_nswap), LONG, "d"},
{"nvcsw", "NVCSW", NULL, USER, pvar, 5, POFF(p_uru_nvcsw), LONG, "d"},
{"nsigs", "NSIGS", NULL, 0, pvar, 0, POFF(p_uru_nsignals), ULONG, "d"},
{"nswap", "NSWAP", NULL, 0, pvar, 0, POFF(p_uru_nswap), ULONG, "d"},
{"nvcsw", "NVCSW", NULL, 0, pvar, 0, POFF(p_uru_nvcsw), ULONG, "d"},
/* XXX */
{"nwchan", "WCHAN", NULL, 0, pvar, 6, POFF(p_wchan), KPTR, "x"},
{"oublk", "OUBLK", NULL, USER, pvar, 4, POFF(p_uru_oublock), LONG, "d"},
{"nwchan", "WCHAN", NULL, 0, pvar, 0, POFF(p_wchan), KPTR, "x"},
{"oublk", "OUBLK", NULL, 0, pvar, 0, POFF(p_uru_oublock), ULONG, "d"},
{"oublock", "", "oublk"},
/* XXX */
{"p_ru", "P_RU", NULL, 0, pvar, 6, POFF(p_ru), KPTR, "x"},
{"p_ru", "P_RU", NULL, 0, pvar, 0, POFF(p_ru), KPTR, "x"},
/* XXX */
{"paddr", "PADDR", NULL, 0, pvar, 6, POFF(p_paddr), KPTR, "x"},
{"pagein", "PAGEIN", NULL, USER, pagein, 6},
{"paddr", "PADDR", NULL, 0, pvar, 0, POFF(p_paddr), KPTR, "x"},
{"pagein", "PAGEIN", NULL, 0, pagein},
{"pcpu", "", "%cpu"},
{"pending", "", "sig"},
PID("pgid", "PGID", pvar, POFF(p__pgid)),
PID("pid", "PID", pvar, POFF(p_pid)),
{"pmem", "", "%mem"},
PID("ppid", "PPID", pvar, POFF(p_ppid)),
{"pri", "PRI", NULL, 0, pri, 3},
{"re", "RE", NULL, INF127, pvar, 3, POFF(p_swtime), UINT, "d"},
{"pri", "PRI", NULL, 0, pri},
{"re", "RE", NULL, INF127, pvar, 0, POFF(p_swtime), UINT, "d"},
GID("rgid", "RGID", pvar, POFF(p_rgid)),
/* XXX */
{"rlink", "RLINK", NULL, 0, pvar, 8, POFF(p_back), KPTR, "x"},
{"rss", "RSS", NULL, 0, p_rssize, 4},
{"rlink", "RLINK", NULL, 0, pvar, 0, POFF(p_back), KPTR, "x"},
{"rss", "RSS", NULL, 0, p_rssize},
{"rssize", "", "rsz"},
{"rsz", "RSZ", NULL, 0, rssize, 4},
{"rsz", "RSZ", NULL, 0, rssize},
UID("ruid", "RUID", pvar, POFF(p_ruid)),
{"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
{"sess", "SESS", NULL, 0, pvar, 6, POFF(p_sess), KPTR24, "x"},
{"ruser", "RUSER", NULL, LJUST, runame},
{"sess", "SESS", NULL, 0, pvar, 0, POFF(p_sess), KPTR24, "x"},
PID("sid", "SID", pvar, POFF(p_sid)),
{"sig", "PENDING",
NULL, 0, pvar, SIGWIDTH, POFF(p_siglist), SIGLIST, "s"},
NULL, 0, pvar, 0, POFF(p_siglist), SIGLIST, "s"},
{"sigcatch", "CAUGHT",
NULL, 0, pvar, SIGWIDTH, POFF(p_sigcatch), SIGLIST, "s"},
NULL, 0, pvar, 0, POFF(p_sigcatch), SIGLIST, "s"},
{"sigignore", "IGNORED",
NULL, 0, pvar, SIGWIDTH, POFF(p_sigignore), SIGLIST, "s"},
NULL, 0, pvar, 0, POFF(p_sigignore), SIGLIST, "s"},
{"sigmask", "BLOCKED",
NULL, 0, pvar, SIGWIDTH, POFF(p_sigmask), SIGLIST, "s"},
{"sl", "SL", NULL, INF127, pvar, 3, POFF(p_slptime), UINT, "d"},
{"start", "STARTED", NULL, LJUST|USER, started, 8},
NULL, 0, pvar, 0, POFF(p_sigmask), SIGLIST, "s"},
{"sl", "SL", NULL, INF127, pvar, 0, POFF(p_slptime), UINT, "d"},
{"start", "STARTED", NULL, 0, started},
{"stat", "", "state"},
{"state", "STAT", NULL, 0, state, 4},
{"state", "STAT", NULL, LJUST, state},
GID("svgid", "SVGID", pvar, POFF(p_gid)),
UID("svuid", "SVUID", pvar, POFF(p_uid)),
{"tdev", "TDEV", NULL, 0, tdev, 4},
{"time", "TIME", NULL, USER, cputime, 9},
{"tdev", "TDEV", NULL, 0, tdev},
{"time", "TIME", NULL, 0, cputime},
PID("tpgid", "TGPID", pvar, POFF(p_tpgid)),
{"tsess", "TSESS", NULL, 0, pvar, 6, POFF(p_tsess), KPTR, "x"},
{"tsiz", "TSIZ", NULL, 0, tsize, 4},
{"tt", "TT", NULL, LJUST, tname, 3},
{"tty", "TTY", NULL, LJUST, longtname, 8},
{"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN},
{"tsess", "TSESS", NULL, 0, pvar, 0, POFF(p_tsess), KPTR, "x"},
{"tsiz", "TSIZ", NULL, 0, tsize},
{"tt", "TT", NULL, LJUST, tname},
{"tty", "TTY", NULL, LJUST, longtname},
{"ucomm", "UCOMM", NULL, LJUST, ucomm},
UID("uid", "UID", pvar, POFF(p_uid)),
{"upr", "UPR", NULL, 0, pvar, 3, POFF(p_usrpri), UCHAR, "d"},
{"user", "USER", NULL, LJUST, uname, USERLEN},
{"upr", "UPR", NULL, 0, pvar, 0, POFF(p_usrpri), UCHAR, "d"},
{"user", "USER", NULL, LJUST, uname},
{"usrpri", "", "upr"},
{"vsize", "", "vsz"},
{"vsz", "VSZ", NULL, 0, vsize, 5},
{"wchan", "WCHAN", NULL, LJUST, wchan, 6},
{"xstat", "XSTAT", NULL, 0, pvar, 4, POFF(p_xstat), USHORT, "x"},
{"vsz", "VSZ", NULL, 0, vsize},
{"wchan", "WCHAN", NULL, LJUST, wchan},
{"xstat", "XSTAT", NULL, 0, pvar, 0, POFF(p_xstat), USHORT, "x"},
{""},
};

View File

@ -1,4 +1,4 @@
/* $NetBSD: print.c,v 1.52 2000/06/02 03:39:02 simonb Exp $ */
/* $NetBSD: print.c,v 1.53 2000/06/07 04:58:00 simonb Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@ -38,7 +38,7 @@
#if 0
static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
#else
__RCSID("$NetBSD: print.c,v 1.52 2000/06/02 03:39:02 simonb Exp $");
__RCSID("$NetBSD: print.c,v 1.53 2000/06/07 04:58:00 simonb Exp $");
#endif
#endif /* not lint */
@ -68,9 +68,13 @@ __RCSID("$NetBSD: print.c,v 1.52 2000/06/02 03:39:02 simonb Exp $");
#include "ps.h"
static char *cmdpart __P((char *));
static void printval __P((char *, VAR *));
static void printval __P((void *, VAR *, int));
static int titlecmp __P((char *, char **));
static void doubleprintorsetwidth __P((VAR *, double, int, int));
static void intprintorsetwidth __P((VAR *, int, int));
static void strprintorsetwidth __P((VAR *, const char *, int));
#define min(a,b) ((a) <= (b) ? (a) : (b))
#define max(a,b) ((a) >= (b) ? (a) : (b))
@ -86,22 +90,33 @@ cmdpart(arg0)
void
printheader()
{
int len;
VAR *v;
struct varent *vent;
static int firsttime = 1;
for (vent = vhead; vent; vent = vent->next) {
v = vent->var;
if (firsttime && vent->next != NULL) {
len = strlen(v->header);
if (len > v->width)
v->width = len;
totwidth += v->width + 1; /* +1 for space */
}
if (v->flag & LJUST) {
if (vent->next == NULL) /* last one */
(void)printf("%s", v->header);
else
(void)printf("%-*s", v->width, v->header);
(void)printf("%-*s", v->width,
v->header);
} else
(void)printf("%*s", v->width, v->header);
if (vent->next != NULL)
(void)putchar(' ');
}
(void)putchar('\n');
if (firsttime)
firsttime = 0;
}
static int
@ -134,19 +149,94 @@ titlecmp(name, argv)
return (1);
}
static void
doubleprintorsetwidth(v, val, prec, mode)
VAR *v;
double val;
int prec;
int mode;
{
int fmtlen;
if (mode == WIDTHMODE) {
if (val < 0.0 && val < v->longestnd) {
fmtlen = (int)log10(-val) + prec + 2;
v->longestnd = val;
if (fmtlen > v->width)
v->width = fmtlen;
} else if (val > 0.0 && val > v->longestpd) {
fmtlen = (int)log10(val) + prec + 1;
v->longestpd = val;
if (fmtlen > v->width)
v->width = fmtlen;
}
} else {
printf("%*.*f", v->width, prec, val);
}
}
static void
intprintorsetwidth(v, val, mode)
VAR *v;
int val;
int mode;
{
int fmtlen;
if (mode == WIDTHMODE) {
if (val < 0 && val < v->longestn) {
fmtlen = (int)log10((double)-val) + 2;
v->longestn = val;
if (fmtlen > v->width)
v->width = fmtlen;
} else if (val > 0 && val > v->longestp) {
fmtlen = (int)log10((double)val) + 1;
v->longestp = val;
if (fmtlen > v->width)
v->width = fmtlen;
}
} else
printf("%*d", v->width, val);
}
static void
strprintorsetwidth(v, str, mode)
VAR *v;
const char *str;
{
int len;
if (mode == WIDTHMODE) {
len = strlen(str);
if (len > v->width)
v->width = len;
} else {
if (v->flag & LJUST)
printf("%-*.*s", v->width, v->width, str);
else
printf("%*.*s", v->width, v->width, str);
}
}
void
command(ki, ve)
command(ki, ve, mode)
struct kinfo_proc2 *ki;
VARENT *ve;
int mode;
{
VAR *v;
int left;
char **argv, **p, *name;
v = ve->var;
if (mode == WIDTHMODE) {
v->width = 0;
return;
}
v = ve->var;
if (ve->next != NULL || termwidth != UNLIMITED) {
if (ve->next == NULL) {
left = termwidth - (totwidth - v->width);
left = termwidth - totwidth;
if (left < 1) /* already wrapped, just use std width */
left = v->width;
} else
@ -196,35 +286,34 @@ command(ki, ve)
}
void
ucomm(k, ve)
ucomm(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%-*s", v->width, k->p_comm);
strprintorsetwidth(v, k->p_comm, mode);
}
void
logname(k, ve)
logname(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
int n;
v = ve->var;
n = min(v->width, MAXLOGNAME);
(void)printf("%-*.*s", n, n, k->p_login);
if (v->width > n)
(void)printf("%*s", v->width - n, "");
strprintorsetwidth(v, k->p_login, mode);
}
void
state(k, ve)
state(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
int flag, is_zombie;
char *cp;
@ -285,59 +374,62 @@ state(k, ve)
if ((flag & P_CONTROLT) && k->p__pgid == k->p_tpgid)
*cp++ = '+';
*cp = '\0';
(void)printf("%-*s", v->width, buf);
strprintorsetwidth(v, buf, mode);
}
void
pnice(k, ve)
pnice(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%*d", v->width, k->p_nice - NZERO);
intprintorsetwidth(v, k->p_nice - NZERO, mode);
}
void
pri(k, ve)
pri(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%*d", v->width, k->p_priority - PZERO);
intprintorsetwidth(v, k->p_priority - PZERO, mode);
}
void
uname(k, ve)
uname(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%-*s",
(int)v->width, user_from_uid(k->p_uid, 0));
strprintorsetwidth(v, user_from_uid(k->p_uid, 0), mode);
}
void
runame(k, ve)
runame(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%-*s",
(int)v->width, user_from_uid(k->p_ruid, 0));
strprintorsetwidth(v, user_from_uid(k->p_ruid, 0), mode);
}
void
tdev(k, ve)
tdev(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
dev_t dev;
@ -345,41 +437,65 @@ tdev(k, ve)
v = ve->var;
dev = k->p_tdev;
if (dev == NODEV)
(void)printf("%*s", v->width, "??");
else {
if (dev == NODEV) {
/*
* Minimum width is width of header - we don't
* need to check it every time.
*/
if (mode == PRINTMODE)
(void)printf("%*s", v->width, "??");
} else {
(void)snprintf(buff, sizeof(buff),
"%d/%d", major(dev), minor(dev));
(void)printf("%*s", v->width, buff);
strprintorsetwidth(v, buff, mode);
}
}
void
tname(k, ve)
tname(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
dev_t dev;
const char *ttname;
int noctty;
v = ve->var;
dev = k->p_tdev;
if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
(void)printf("%-*s", v->width, "??");
else {
if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
/*
* Minimum width is width of header - we don't
* need to check it every time.
*/
if (mode == PRINTMODE)
(void)printf("%-*s", v->width, "??");
} else {
if (strncmp(ttname, "tty", 3) == 0 ||
strncmp(ttname, "dty", 3) == 0)
ttname += 3;
(void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
k->p_eflag & EPROC_CTTY ? ' ' : '-');
noctty = !(k->p_eflag & EPROC_CTTY) ? 1 : 0;
if (mode == WIDTHMODE) {
int fmtlen;
fmtlen = strlen(ttname) + noctty;
if (v->width < fmtlen)
fmtlen = v->width;
} else {
if (noctty)
printf("%-*s-", v->width - 1, ttname);
else
printf("%-*s", v->width, ttname);
}
}
}
void
longtname(k, ve)
longtname(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
dev_t dev;
@ -387,26 +503,43 @@ longtname(k, ve)
v = ve->var;
dev = k->p_tdev;
if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
(void)printf("%-*s", v->width, "??");
else
(void)printf("%-*s", v->width, ttname);
if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
/*
* Minimum width is width of header - we don't
* need to check it every time.
*/
if (mode == PRINTMODE)
(void)printf("%-*s", v->width, "??");
}
else {
strprintorsetwidth(v, ttname, mode);
}
}
void
started(k, ve)
started(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
static time_t now;
time_t startt;
struct tm *tp;
char buf[100];
char buf[100], *cp;
/*
* XXX: The maximum width of this field is the same as the header
* "STARTED" for locales that have 3 letter abbreviated month
* names and 2 letter am/pm descriptions.
*/
if (mode == WIDTHMODE)
return;
v = ve->var;
if (!k->p_uvalid) {
(void)printf("%-*s", v->width, "-");
if (mode == PRINTMODE)
(void)printf("%*s", v->width, "-");
return;
}
@ -414,23 +547,26 @@ started(k, ve)
tp = localtime(&startt);
if (!now)
(void)time(&now);
if (now - k->p_ustart_sec < 24 * SECSPERHOUR) {
if (now - k->p_ustart_sec < SECSPERDAY)
/* I *hate* SCCS... */
static char fmt[] = __CONCAT("%l:%", "M%p");
(void)strftime(buf, sizeof(buf) - 1, fmt, tp);
} else if (now - k->p_ustart_sec < 7 * SECSPERDAY) {
(void)strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY)
/* I *hate* SCCS... */
static char fmt[] = __CONCAT("%a%", "I%p");
(void)strftime(buf, sizeof(buf) - 1, fmt, tp);
} else
(void)strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
else
(void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
(void)printf("%-*s", v->width, buf);
/* %e and %l can start with a space. */
cp = buf;
if (*cp == ' ')
cp++;
strprintorsetwidth(v, cp, mode);
}
void
lstarted(k, ve)
lstarted(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
time_t startt;
@ -438,83 +574,103 @@ lstarted(k, ve)
v = ve->var;
if (!k->p_uvalid) {
(void)printf("%-*s", v->width, "-");
/*
* Minimum width is less than header - we don't
* need to check it every time.
*/
if (mode == PRINTMODE)
(void)printf("%*s", v->width, "-");
return;
}
startt = k->p_ustart_sec;
(void)strftime(buf, sizeof(buf) -1, "%c",
localtime(&startt));
(void)printf("%-*s", v->width, buf);
/* assume all times are the same length */
if (mode != WIDTHMODE || v->width == 0) {
(void)strftime(buf, sizeof(buf) -1, "%c",
localtime(&startt));
strprintorsetwidth(v, buf, mode);
}
}
void
wchan(k, ve)
wchan(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
int n;
char *buf;
v = ve->var;
if (k->p_wchan) {
if (k->p_wmesg) {
n = min(v->width, WMESGLEN);
(void)printf("%-*.*s", n, n, k->p_wmesg);
if (v->width > n)
(void)printf("%*s", v->width - n, "");
} else
(void)printf("%-*lx", v->width,
(long)k->p_wchan);
} else
(void)printf("%-*s", v->width, "-");
strprintorsetwidth(v, k->p_wmesg, mode);
v->width = min(v->width, WMESGLEN);
} else {
(void)asprintf(&buf, "%-*llx", v->width,
(long long)k->p_wchan);
if (buf == NULL)
err(1, "%s", "");
strprintorsetwidth(v, buf, mode);
v->width = min(v->width, WMESGLEN);
free(buf);
}
} else {
if (mode == PRINTMODE)
(void)printf("%-*s", v->width, "-");
}
}
#define pgtok(a) (((a)*getpagesize())/1024)
void
vsize(k, ve)
vsize(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%*d", v->width,
pgtok(k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize));
intprintorsetwidth(v,
pgtok(k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize), mode);
}
void
rssize(k, ve)
rssize(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
/* XXX don't have info about shared */
(void)printf("%*d", v->width, pgtok(k->p_vm_rssize));
intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
}
void
p_rssize(k, ve) /* doesn't account for text */
p_rssize(k, ve, mode) /* doesn't account for text */
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%*d", v->width, pgtok(k->p_vm_rssize));
intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
}
void
cputime(k, ve)
cputime(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
long secs;
long psecs; /* "parts" of a second. first micro, then centi */
char obuff[128];
int fmtlen;
v = ve->var;
if (P_ZOMBIE(k) || k->p_uvalid == 0) {
@ -539,9 +695,26 @@ cputime(k, ve)
secs += psecs / 100;
psecs = psecs % 100;
}
(void)snprintf(obuff, sizeof(obuff),
"%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
(void)printf("%*s", v->width, obuff);
if (mode == WIDTHMODE) {
/*
* Ugg, this is the only field where a value of 0 longer
* than the column title, and log10(0) isn't good enough.
* Use SECSPERMIN, because secs is divided by that when
* passed to log10().
*/
if (secs == 0 && v->longestp == 0)
secs = SECSPERMIN;
if (secs > v->longestp) {
/* "+6" for the "%02ld.%02ld" in the printf() below */
fmtlen = (int)log10((double)secs / SECSPERMIN) + 1 + 6;
v->longestp = secs;
if (fmtlen > v->width)
v->width = fmtlen;
}
} else {
printf("%*ld:%02ld.%02ld", v->width - 6, secs / SECSPERMIN,
secs % SECSPERMIN, psecs);
}
}
double
@ -568,14 +741,15 @@ getpcpu(k)
}
void
pcpu(k, ve)
pcpu(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%*.1f", v->width, getpcpu(k));
doubleprintorsetwidth(v, getpcpu(k), 1, mode);
}
double
@ -601,48 +775,53 @@ getpmem(k)
}
void
pmem(k, ve)
pmem(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%*.1f", v->width, getpmem(k));
doubleprintorsetwidth(v, getpmem(k), 1, mode);
}
void
pagein(k, ve)
pagein(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%*lld", v->width,
k->p_uvalid ? (long long)k->p_uru_majflt : 0);
intprintorsetwidth(v, k->p_uvalid ? k->p_uru_majflt : 0, mode);
}
void
maxrss(k, ve)
maxrss(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%*s", v->width, "-");
/* No need to check width! */
if (mode == PRINTMODE)
(void)printf("%*s", v->width, "-");
}
void
tsize(k, ve)
tsize(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
(void)printf("%*d", v->width, pgtok(k->p_vm_tsize));
intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode);
}
/*
@ -650,21 +829,17 @@ tsize(k, ve)
* structures.
*/
static void
printval(bp, v)
char *bp;
printval(bp, v, mode)
void *bp;
VAR *v;
int mode;
{
static char ofmt[32] = "%";
char *fcp, *cp;
int width, vok, fmtlen;
char *fcp, *cp, *obuf;
enum type type;
cp = ofmt + 1;
fcp = v->fmt;
if (v->flag & LJUST)
*cp++ = '-';
*cp++ = '*';
while ((*cp++ = *fcp++) != '\0')
continue;
long long val;
unsigned long long uval;
/*
* Note that the "INF127" check is nonsensical for types
@ -673,6 +848,103 @@ printval(bp, v)
#define GET(type) (*(type *)bp)
#define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
#define VSIGN 1
#define VUNSIGN 2
#define VPTR 3
if (mode == WIDTHMODE) {
vok = 0;
switch (v->type) {
case CHAR:
val = GET(char);
vok = VSIGN;
break;
case UCHAR:
uval = CHK_INF127(GET(u_char));
vok = VUNSIGN;
break;
case SHORT:
val = GET(short);
vok = VSIGN;
break;
case USHORT:
uval = CHK_INF127(GET(u_short));
vok = VUNSIGN;
break;
case INT32:
val = GET(int32_t);
vok = VSIGN;
break;
case INT:
val = GET(int);
vok = VSIGN;
break;
case UINT:
case UINT32:
uval = CHK_INF127(GET(u_int));
vok = VUNSIGN;
break;
case LONG:
val = GET(long);
vok = VSIGN;
break;
case ULONG:
uval = CHK_INF127(GET(u_long));
vok = VUNSIGN;
break;
case KPTR:
uval = GET(u_long);
vok = VPTR;
break;
case KPTR24:
uval = GET(u_long) & 0xffffff;
vok = VPTR;
break;
default:
/* nothing... */;
}
switch (vok) {
case VSIGN:
if (val < 0 && val < v->longestn) {
fmtlen = (int)log10((double)-val) + 2;
v->longestn = val;
if (fmtlen > v->width)
v->width = fmtlen;
} else if (val > 0 && val > v->longestp) {
fmtlen = (int)log10((double)val) + 1;
v->longestp = val;
if (fmtlen > v->width)
v->width = fmtlen;
}
return;
case VUNSIGN:
if (uval > v->longestu) {
fmtlen = (int)log10((double)uval) + 1;
v->longestu = uval;
v->width = fmtlen;
}
return;
case VPTR:
fmtlen = 0;
while (uval > 0) {
uval >>= 4;
fmtlen++;
}
if (fmtlen > v->width)
v->width = fmtlen;
return;
}
}
width = v->width;
cp = ofmt + 1;
fcp = v->fmt;
if (v->flag & LJUST)
*cp++ = '-';
*cp++ = '*';
while ((*cp++ = *fcp++) != '\0')
continue;
switch (v->type) {
case INT32:
if (sizeof(int32_t) == sizeof(int))
@ -697,35 +969,35 @@ printval(bp, v)
switch (type) {
case CHAR:
(void)printf(ofmt, v->width, GET(char));
break;
(void)printf(ofmt, width, GET(char));
return;
case UCHAR:
(void)printf(ofmt, v->width, CHK_INF127(GET(u_char)));
break;
(void)printf(ofmt, width, CHK_INF127(GET(u_char)));
return;
case SHORT:
(void)printf(ofmt, v->width, GET(short));
break;
(void)printf(ofmt, width, GET(short));
return;
case USHORT:
(void)printf(ofmt, v->width, CHK_INF127(GET(u_short)));
break;
(void)printf(ofmt, width, CHK_INF127(GET(u_short)));
return;
case INT:
(void)printf(ofmt, v->width, GET(int));
break;
(void)printf(ofmt, width, GET(int));
return;
case UINT:
(void)printf(ofmt, v->width, CHK_INF127(GET(u_int)));
break;
(void)printf(ofmt, width, CHK_INF127(GET(u_int)));
return;
case LONG:
(void)printf(ofmt, v->width, GET(long));
break;
(void)printf(ofmt, width, GET(long));
return;
case ULONG:
(void)printf(ofmt, v->width, CHK_INF127(GET(u_long)));
break;
(void)printf(ofmt, width, CHK_INF127(GET(u_long)));
return;
case KPTR:
(void)printf(ofmt, v->width, GET(u_long));
break;
(void)printf(ofmt, width, GET(u_long));
return;
case KPTR24:
(void)printf(ofmt, v->width, GET(u_long) & 0xffffff);
break;
(void)printf(ofmt, width, GET(u_long) & 0xffffff);
return;
case SIGLIST:
{
sigset_t *s = (sigset_t *)(void *)bp;
@ -744,23 +1016,38 @@ printval(bp, v)
if (buf[i] == '\0')
i--;
(void)printf(ofmt, v->width, &buf[i]);
(void)asprintf(&obuf, ofmt, width, &buf[i]);
}
break;
default:
errx(1, "unknown type %d", v->type);
}
if (obuf == NULL)
err(1, "%s", "");
if (mode == WIDTHMODE) {
/* Skip leading spaces. */
cp = strrchr(obuf, ' ');
if (cp == NULL)
cp = obuf;
else
cp++; /* skip last space */
}
else
cp = obuf;
strprintorsetwidth(v, cp, mode);
free(obuf);
#undef GET
#undef CHK_INF127
}
void
pvar(k, ve)
pvar(k, ve, mode)
struct kinfo_proc2 *k;
VARENT *ve;
int mode;
{
VAR *v;
v = ve->var;
printval((char *)k + v->off, v);
printval((char *)k + v->off, v, mode);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: ps.c,v 1.38 2000/05/26 03:04:28 simonb Exp $ */
/* $NetBSD: ps.c,v 1.39 2000/06/07 04:58:01 simonb Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: ps.c,v 1.38 2000/05/26 03:04:28 simonb Exp $");
__RCSID("$NetBSD: ps.c,v 1.39 2000/06/07 04:58:01 simonb Exp $");
#endif
#endif /* not lint */
@ -78,7 +78,7 @@ struct varent *vhead, *vtail;
int eval; /* exit value */
int rawcpu; /* -C */
int sumrusage; /* -S */
int dontuseprocfs=0; /* -K */
int dontuseprocfs; /* -K */
int termwidth; /* width of screen (0 == infinity) */
int totwidth; /* calculated width of requested variables */
@ -113,7 +113,7 @@ main(argc, argv)
struct varent *vent;
struct winsize ws;
int ch, flag, i, fmt, lineno, nentries;
int prtheader, wflag, what, xflg;
int prtheader, wflag, what, xflg, mode;
char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX];
char *ttname;
@ -132,6 +132,7 @@ main(argc, argv)
what = KERN_PROC_UID;
flag = myuid = getuid();
memf = nlistf = swapf = NULL;
mode = PRINTMODE;
while ((ch = getopt(argc, argv,
"acCeghjKLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1)
switch((char)ch) {
@ -316,8 +317,7 @@ main(argc, argv)
parsefmt(dfmt);
/*
* scan requested variables, noting what structures are needed,
* and adjusting header widths as appropiate.
* scan requested variables, noting what structures are needed.
*/
scanvars();
@ -347,19 +347,38 @@ main(argc, argv)
"valid data for all fields.\n");
use_procfs = 1;
}
/*
* print header
*/
printheader();
if (nentries == 0)
if (nentries == 0) {
printheader();
exit(0);
}
/*
* sort proc list
*/
qsort(kinfo, nentries, sizeof(struct kinfo_proc2), pscomp);
/*
* for each proc, call each variable output function.
* For each proc, call each variable output function in
* "setwidth" mode to determine the widest element of
* the column.
*/
if (mode == PRINTMODE)
for (i = 0; i < nentries; i++) {
struct kinfo_proc2 *ki = &kinfo[i];
if (xflg == 0 && (ki->p_tdev == NODEV ||
(ki->p_flag & P_CONTROLT) == 0))
continue;
for (vent = vhead; vent; vent = vent->next)
(vent->var->oproc)(ki, vent, WIDTHMODE);
}
/*
* Print header - AFTER determining process field widths.
* printheader() also adds up the total width of all
* fields the first time it's called.
*/
printheader();
/*
* For each proc, call each variable output function in
* print mode.
*/
for (i = lineno = 0; i < nentries; i++) {
struct kinfo_proc2 *ki = &kinfo[i];
@ -368,7 +387,7 @@ main(argc, argv)
(ki->p_flag & P_CONTROLT ) == 0))
continue;
for (vent = vhead; vent; vent = vent->next) {
(vent->var->oproc)(ki, vent);
(vent->var->oproc)(ki, vent, mode);
if (vent->next != NULL)
(void)putchar(' ');
}
@ -397,18 +416,12 @@ scanvars()
{
struct varent *vent;
VAR *v;
int i;
for (vent = vhead; vent; vent = vent->next) {
v = vent->var;
i = strlen(v->header);
if (v->width < i)
v->width = i;
totwidth += v->width + 1; /* +1 for space */
if (v->flag & COMM)
needcomm = 1;
}
totwidth--;
}
static int

View File

@ -1,4 +1,4 @@
/* $NetBSD: ps.h,v 1.16 2000/05/26 03:04:28 simonb Exp $ */
/* $NetBSD: ps.h,v 1.17 2000/06/07 04:58:02 simonb Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -36,6 +36,10 @@
*/
#define UNLIMITED 0 /* unlimited terminal width */
#define PRINTMODE 0 /* print values */
#define WIDTHMODE 1 /* determine width of column */
enum type {
CHAR, UCHAR, SHORT, USHORT, INT, UINT, LONG, ULONG, KPTR, KPTR24,
INT32, UINT32, SIGLIST
@ -53,11 +57,10 @@ typedef struct var {
char *alias; /* aliases */
#define COMM 0x01 /* needs exec arguments and environment (XXX) */
#define LJUST 0x02 /* left adjust on output (trailing blanks) */
#define USER 0x04 /* needs user structure */
#define INF127 0x08 /* 127 = infinity: if > 127, print 127. */
#define INF127 0x04 /* 127 = infinity: if > 127, print 127. */
u_int flag;
/* output routine */
void (*oproc) __P((struct kinfo_proc2 *, struct varent *));
void (*oproc) __P((struct kinfo_proc2 *, struct varent *, int));
short width; /* printing width */
/*
* The following (optional) elements are hooks for passing information
@ -67,9 +70,12 @@ typedef struct var {
int off; /* offset in structure */
enum type type; /* type of element */
char *fmt; /* printf format */
/*
* glue to link selected fields together
*/
/* current longest element */
int64_t longestp; /* longest positive signed value */
int64_t longestn; /* longest negative signed value */
u_int64_t longestu; /* longest unsigned value */
double longestpd; /* longest positive double */
double longestnd; /* longest negative double */
} VAR;
#include "extern.h"