Home | History | Annotate | Line # | Download | only in ps
print.c revision 1.134
      1 /*	$NetBSD: print.c,v 1.134 2021/04/06 13:35:52 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Simon Burge.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1990, 1993, 1994
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  */
     60 
     61 #include <sys/cdefs.h>
     62 #ifndef lint
     63 #if 0
     64 static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
     65 #else
     66 __RCSID("$NetBSD: print.c,v 1.134 2021/04/06 13:35:52 christos Exp $");
     67 #endif
     68 #endif /* not lint */
     69 
     70 #include <sys/param.h>
     71 #include <sys/time.h>
     72 #include <sys/resource.h>
     73 #include <sys/lwp.h>
     74 #include <sys/proc.h>
     75 #include <sys/stat.h>
     76 #include <sys/ucred.h>
     77 #include <sys/sysctl.h>
     78 
     79 #include <err.h>
     80 #include <grp.h>
     81 #include <kvm.h>
     82 #include <math.h>
     83 #include <nlist.h>
     84 #include <pwd.h>
     85 #include <stddef.h>
     86 #include <stdio.h>
     87 #include <stdlib.h>
     88 #include <string.h>
     89 #include <time.h>
     90 #include <tzfile.h>
     91 #include <unistd.h>
     92 
     93 #include "ps.h"
     94 
     95 static char *cmdpart(char *);
     96 static void  printval(void *, VAR *, enum mode);
     97 static int   titlecmp(char *, char **);
     98 
     99 static void  doubleprintorsetwidth(VAR *, double, int, enum mode);
    100 static void  intprintorsetwidth(VAR *, int, enum mode);
    101 static void  strprintorsetwidth(VAR *, const char *, enum mode);
    102 
    103 static time_t now;
    104 
    105 #define	min(a,b)	((a) <= (b) ? (a) : (b))
    106 
    107 /* pre-NetBSD 5.x support. */
    108 #ifndef LSDEAD
    109 #define LSDEAD 6
    110 #endif
    111 
    112 static void __attribute__((__format__(__strftime__, 3, 0)))
    113 safe_strftime(char *buf, size_t bufsiz, const char *fmt,
    114     const struct tm *tp)
    115 {
    116 	if (tp == NULL || strftime(buf, bufsiz, fmt, tp) == 0)
    117 		strlcpy(buf, "-", sizeof(buf));
    118 }
    119 
    120 static int
    121 iwidth(u_int64_t v)
    122 {
    123 	u_int64_t nlim, lim;
    124 	int w = 1;
    125 
    126 	for (lim = 10; v >= lim; lim = nlim) {
    127 		nlim = lim * 10;
    128 		w++;
    129 		if (nlim < lim)
    130 			break;
    131 	}
    132 	return w;
    133 }
    134 
    135 static char *
    136 cmdpart(char *arg0)
    137 {
    138 	char *cp;
    139 
    140 	return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
    141 }
    142 
    143 void
    144 printheader(void)
    145 {
    146 	int len;
    147 	VAR *v;
    148 	struct varent *vent;
    149 	static int firsttime = 1;
    150 	static int noheader = 0;
    151 
    152 	/*
    153 	 * If all the columns have user-specified null headers,
    154 	 * don't print the blank header line at all.
    155 	 */
    156 	if (firsttime) {
    157 		SIMPLEQ_FOREACH(vent, &displaylist, next) {
    158 			if (vent->var->header[0])
    159 				break;
    160 		}
    161 		if (vent == NULL) {
    162 			noheader = 1;
    163 			firsttime = 0;
    164 		}
    165 
    166 	}
    167 	if (noheader)
    168 		return;
    169 
    170 	SIMPLEQ_FOREACH(vent, &displaylist, next) {
    171 		v = vent->var;
    172 		if (firsttime) {
    173 			len = strlen(v->header);
    174 			if (len > v->width)
    175 				v->width = len;
    176 			totwidth += v->width + 1;	/* +1 for space */
    177 		}
    178 		if (v->flag & LJUST) {
    179 			if (SIMPLEQ_NEXT(vent, next) == NULL)	/* last one */
    180 				(void)printf("%s", v->header);
    181 			else
    182 				(void)printf("%-*s", v->width,
    183 				    v->header);
    184 		} else
    185 			(void)printf("%*s", v->width, v->header);
    186 		if (SIMPLEQ_NEXT(vent, next) != NULL)
    187 			(void)putchar(' ');
    188 	}
    189 	(void)putchar('\n');
    190 	if (firsttime) {
    191 		firsttime = 0;
    192 		totwidth--;	/* take off last space */
    193 	}
    194 }
    195 
    196 /*
    197  * Return 1 if the command name in the argument vector (u-area) does
    198  * not match the command name (p_comm)
    199  */
    200 static int
    201 titlecmp(char *name, char **argv)
    202 {
    203 	char *title;
    204 	int namelen;
    205 
    206 
    207 	/* no argument vector == no match; system processes/threads do that */
    208 	if (argv == 0 || argv[0] == 0)
    209 		return (1);
    210 
    211 	title = cmdpart(argv[0]);
    212 
    213 	/* the basename matches */
    214 	if (!strcmp(name, title))
    215 		return (0);
    216 
    217 	/* handle login shells, by skipping the leading - */
    218 	if (title[0] == '-' && !strcmp(name, title + 1))
    219 		return (0);
    220 
    221 	namelen = strlen(name);
    222 
    223 	/* handle daemons that report activity as daemonname: activity */
    224 	if (argv[1] == 0 &&
    225 	    !strncmp(name, title, namelen) &&
    226 	    title[namelen + 0] == ':' &&
    227 	    title[namelen + 1] == ' ')
    228 		return (0);
    229 
    230 	return (1);
    231 }
    232 
    233 static void
    234 doubleprintorsetwidth(VAR *v, double val, int prec, enum mode mode)
    235 {
    236 	int fmtlen;
    237 
    238 	if (mode == WIDTHMODE) {
    239 		if (val < 0.0 && val < v->longestnd) {
    240 			fmtlen = (int)log10(-val) + prec + 2;
    241 			v->longestnd = val;
    242 			if (fmtlen > v->width)
    243 				v->width = fmtlen;
    244 		} else if (val > 0.0 && val > v->longestpd) {
    245 			fmtlen = (int)log10(val) + prec + 1;
    246 			v->longestpd = val;
    247 			if (fmtlen > v->width)
    248 				v->width = fmtlen;
    249 		}
    250 	} else {
    251 		(void)printf("%*.*f", v->width, prec, val);
    252 	}
    253 }
    254 
    255 static void
    256 intprintorsetwidth(VAR *v, int val, enum mode mode)
    257 {
    258 	int fmtlen;
    259 
    260 	if (mode == WIDTHMODE) {
    261 		if (val < 0 && val < v->longestn) {
    262 			v->longestn = val;
    263 			fmtlen = iwidth(-val) + 1;
    264 			if (fmtlen > v->width)
    265 				v->width = fmtlen;
    266 		} else if (val > 0 && val > v->longestp) {
    267 			v->longestp = val;
    268 			fmtlen = iwidth(val);
    269 			if (fmtlen > v->width)
    270 				v->width = fmtlen;
    271 		}
    272 	} else
    273 		(void)printf("%*d", v->width, val);
    274 }
    275 
    276 static void
    277 strprintorsetwidth(VAR *v, const char *str, enum mode mode)
    278 {
    279 	int len;
    280 
    281 	if (mode == WIDTHMODE) {
    282 		len = strlen(str);
    283 		if (len > v->width)
    284 			v->width = len;
    285 	} else {
    286 		if (v->flag & LJUST)
    287 			(void)printf("%-*.*s", v->width, v->width, str);
    288 		else
    289 			(void)printf("%*.*s", v->width, v->width, str);
    290 	}
    291 }
    292 
    293 void
    294 command(struct pinfo *pi, VARENT *ve, enum mode mode)
    295 {
    296 	struct kinfo_proc2 *ki = pi->ki;
    297 	VAR *v;
    298 	int left;
    299 	char **argv, **p, *name;
    300 
    301 	if (mode == WIDTHMODE)
    302 		return;
    303 
    304 	v = ve->var;
    305 	if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
    306 		if (SIMPLEQ_NEXT(ve, next) == NULL) {
    307 			left = termwidth - (totwidth - v->width);
    308 			if (left < 1) /* already wrapped, just use std width */
    309 				left = v->width;
    310 		} else
    311 			left = v->width;
    312 	} else
    313 		left = -1;
    314 	if (needenv && kd) {
    315 		argv = kvm_getenvv2(kd, ki, termwidth);
    316 		if ((p = argv) != NULL) {
    317 			while (*p) {
    318 				fmt_puts(*p, &left);
    319 				p++;
    320 				fmt_putc(' ', &left);
    321 			}
    322 		}
    323 	}
    324 	if (needcomm) {
    325 		if (pi->prefix)
    326 			(void)fmt_puts(pi->prefix, &left);
    327 		name = ki->p_comm;
    328 		if (!commandonly) {
    329 			argv = kvm_getargv2(kd, ki, termwidth);
    330 			if ((p = argv) != NULL) {
    331 				while (*p) {
    332 					fmt_puts(*p, &left);
    333 					p++;
    334 					fmt_putc(' ', &left);
    335 					if (v->flag & ARGV0)
    336 						break;
    337 				}
    338 				if (!(v->flag & ARGV0) &&
    339 				    titlecmp(name, argv)) {
    340 					/*
    341 					 * append the real command name within
    342 					 * parentheses, if the command name
    343 					 * does not match the one in the
    344 					 * argument vector
    345 					 */
    346 					fmt_putc('(', &left);
    347 					fmt_puts(name, &left);
    348 					fmt_putc(')', &left);
    349 				}
    350 			} else {
    351 				/*
    352 				 * Commands that don't set an argv vector
    353 				 * are printed with square brackets if they
    354 				 * are system commands.  Otherwise they are
    355 				 * printed within parentheses.
    356 				 */
    357 				if (ki->p_flag & P_SYSTEM) {
    358 					fmt_putc('[', &left);
    359 					fmt_puts(name, &left);
    360 					fmt_putc(']', &left);
    361 				} else {
    362 					fmt_putc('(', &left);
    363 					fmt_puts(name, &left);
    364 					fmt_putc(')', &left);
    365 				}
    366 			}
    367 		} else {
    368 			fmt_puts(name, &left);
    369 		}
    370 	}
    371 	if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
    372 		(void)printf("%*s", left, "");
    373 }
    374 
    375 void
    376 groups(struct pinfo *pi, VARENT *ve, enum mode mode)
    377 {
    378 	struct kinfo_proc2 *ki = pi->ki;
    379 	VAR *v;
    380 	int left, i;
    381 	char buf[16], *p;
    382 
    383 	if (mode == WIDTHMODE)
    384 		return;
    385 
    386 	v = ve->var;
    387 	if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
    388 		if (SIMPLEQ_NEXT(ve, next) == NULL) {
    389 			left = termwidth - (totwidth - v->width);
    390 			if (left < 1) /* already wrapped, just use std width */
    391 				left = v->width;
    392 		} else
    393 			left = v->width;
    394 	} else
    395 		left = -1;
    396 
    397 	if (ki->p_ngroups == 0)
    398 		fmt_putc('-', &left);
    399 
    400 	for (i = 0; i < ki->p_ngroups; i++) {
    401 		(void)snprintf(buf, sizeof(buf), "%d", ki->p_groups[i]);
    402 		if (i)
    403 			fmt_putc(' ', &left);
    404 		for (p = &buf[0]; *p; p++)
    405 			fmt_putc(*p, &left);
    406 	}
    407 
    408 	if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
    409 		(void)printf("%*s", left, "");
    410 }
    411 
    412 void
    413 groupnames(struct pinfo *pi, VARENT *ve, enum mode mode)
    414 {
    415 	struct kinfo_proc2 *ki = pi->ki;
    416 	VAR *v;
    417 	int left, i;
    418 	const char *p;
    419 
    420 	if (mode == WIDTHMODE)
    421 		return;
    422 
    423 	v = ve->var;
    424 	if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
    425 		if (SIMPLEQ_NEXT(ve, next) == NULL) {
    426 			left = termwidth - (totwidth - v->width);
    427 			if (left < 1) /* already wrapped, just use std width */
    428 				left = v->width;
    429 		} else
    430 			left = v->width;
    431 	} else
    432 		left = -1;
    433 
    434 	if (ki->p_ngroups == 0)
    435 		fmt_putc('-', &left);
    436 
    437 	for (i = 0; i < ki->p_ngroups; i++) {
    438 		if (i)
    439 			fmt_putc(' ', &left);
    440 		for (p = group_from_gid(ki->p_groups[i], 0); *p; p++)
    441 			fmt_putc(*p, &left);
    442 	}
    443 
    444 	if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
    445 		(void)printf("%*s", left, "");
    446 }
    447 
    448 void
    449 ucomm(struct pinfo *pi, VARENT *ve, enum mode mode)
    450 {
    451 	struct kinfo_proc2 *k = pi->ki;
    452 	char buf[MAXPATHLEN], *p;
    453 	VAR *v;
    454 
    455 	v = ve->var;
    456 	if (pi->prefix)
    457 		snprintf(p = buf, sizeof(buf), "%s%s", pi->prefix, k->p_comm);
    458 	else
    459 		p = k->p_comm;
    460 	strprintorsetwidth(v, p, mode);
    461 }
    462 
    463 void
    464 emul(struct pinfo *pi, VARENT *ve, enum mode mode)
    465 {
    466 	struct kinfo_proc2 *k = pi->ki;
    467 	VAR *v;
    468 
    469 	v = ve->var;
    470 	strprintorsetwidth(v, k->p_ename, mode);
    471 }
    472 
    473 void
    474 logname(struct pinfo *pi, VARENT *ve, enum mode mode)
    475 {
    476 	struct kinfo_proc2 *k = pi->ki;
    477 	VAR *v;
    478 
    479 	v = ve->var;
    480 	strprintorsetwidth(v, k->p_login, mode);
    481 }
    482 
    483 void
    484 state(struct pinfo *pi, VARENT *ve, enum mode mode)
    485 {
    486 	struct kinfo_proc2 *k = pi->ki;
    487 	int flag, is_zombie;
    488 	char *cp;
    489 	VAR *v;
    490 	char buf[16];
    491 
    492 	is_zombie = 0;
    493 	v = ve->var;
    494 	flag = k->p_flag;
    495 	cp = buf;
    496 
    497 	/*
    498 	 * NOTE: There are historical letters, which are no longer used:
    499 	 *
    500 	 * - W: indicated that process is swapped out.
    501 	 * - L: indicated non-zero l_holdcnt (i.e. that process was
    502 	 *   prevented from swapping-out.
    503 	 *
    504 	 * These letters should not be used for new states to avoid
    505 	 * conflicts with old applications which might depend on them.
    506 	 */
    507 	switch (k->p_stat) {
    508 
    509 	case LSSTOP:
    510 		*cp = 'T';
    511 		break;
    512 
    513 	case LSSLEEP:
    514 		if (flag & L_SINTR)	/* interruptable (long) */
    515 			*cp = (int)k->p_slptime >= maxslp ? 'I' : 'S';
    516 		else
    517 			*cp = 'D';
    518 		break;
    519 
    520 	case LSRUN:
    521 	case LSIDL:
    522 		*cp = 'R';
    523 		break;
    524 
    525 	case LSONPROC:
    526 		*cp = 'O';
    527 		break;
    528 
    529 	case LSZOMB:
    530 		*cp = 'Z';
    531 		is_zombie = 1;
    532 		break;
    533 
    534 	case LSSUSPENDED:
    535 		*cp = 'U';
    536 		break;
    537 
    538 	default:
    539 		*cp = '?';
    540 	}
    541 	cp++;
    542 	if (k->p_nice < NZERO)
    543 		*cp++ = '<';
    544 	else if (k->p_nice > NZERO)
    545 		*cp++ = 'N';
    546 	if (flag & P_TRACED)
    547 		*cp++ = 'X';
    548 	if (flag & P_WEXIT && !is_zombie)
    549 		*cp++ = 'E';
    550 	if (flag & P_PPWAIT)
    551 		*cp++ = 'V';
    552 	if (flag & P_SYSTEM)
    553 		*cp++ = 'K';
    554 	if (k->p_eflag & EPROC_SLEADER)
    555 		*cp++ = 's';
    556 	if (flag & P_SA)
    557 		*cp++ = 'a';
    558 	else if (k->p_nlwps > 1)
    559 		*cp++ = 'l';
    560 	if ((flag & P_CONTROLT) && k->p__pgid == k->p_tpgid)
    561 		*cp++ = '+';
    562 	*cp = '\0';
    563 	strprintorsetwidth(v, buf, mode);
    564 }
    565 
    566 void
    567 lstate(struct pinfo *pi, VARENT *ve, enum mode mode)
    568 {
    569 	struct kinfo_lwp *k = pi->li;
    570 	int flag;
    571 	char *cp;
    572 	VAR *v;
    573 	char buf[16];
    574 
    575 	v = ve->var;
    576 	flag = k->l_flag;
    577 	cp = buf;
    578 
    579 	switch (k->l_stat) {
    580 
    581 	case LSSTOP:
    582 		*cp = 'T';
    583 		break;
    584 
    585 	case LSSLEEP:
    586 		if (flag & L_SINTR)	/* interruptible (long) */
    587 			*cp = (int)k->l_slptime >= maxslp ? 'I' : 'S';
    588 		else
    589 			*cp = 'D';
    590 		break;
    591 
    592 	case LSRUN:
    593 	case LSIDL:
    594 		*cp = 'R';
    595 		break;
    596 
    597 	case LSONPROC:
    598 		*cp = 'O';
    599 		break;
    600 
    601 	case LSZOMB:
    602 	case LSDEAD:
    603 		*cp = 'Z';
    604 		break;
    605 
    606 	case LSSUSPENDED:
    607 		*cp = 'U';
    608 		break;
    609 
    610 	default:
    611 		*cp = '?';
    612 	}
    613 	cp++;
    614 	if (flag & L_SYSTEM)
    615 		*cp++ = 'K';
    616 	if (flag & L_SA)
    617 		*cp++ = 'a';
    618 	if (flag & L_DETACHED)
    619 		*cp++ = '-';
    620 	*cp = '\0';
    621 	strprintorsetwidth(v, buf, mode);
    622 }
    623 
    624 void
    625 pnice(struct pinfo *pi, VARENT *ve, enum mode mode)
    626 {
    627 	struct kinfo_proc2 *k = pi->ki;
    628 	VAR *v;
    629 
    630 	v = ve->var;
    631 	intprintorsetwidth(v, k->p_nice - NZERO, mode);
    632 }
    633 
    634 void
    635 pri(struct pinfo *pi, VARENT *ve, enum mode mode)
    636 {
    637 	struct kinfo_lwp *l = pi->li;
    638 	VAR *v;
    639 
    640 	v = ve->var;
    641 	intprintorsetwidth(v, l->l_priority, mode);
    642 }
    643 
    644 void
    645 usrname(struct pinfo *pi, VARENT *ve, enum mode mode)
    646 {
    647 	struct kinfo_proc2 *k = pi->ki;
    648 	VAR *v;
    649 
    650 	v = ve->var;
    651 	strprintorsetwidth(v, user_from_uid(k->p_uid, 0), mode);
    652 }
    653 
    654 void
    655 runame(struct pinfo *pi, VARENT *ve, enum mode mode)
    656 {
    657 	struct kinfo_proc2 *k = pi->ki;
    658 	VAR *v;
    659 
    660 	v = ve->var;
    661 	strprintorsetwidth(v, user_from_uid(k->p_ruid, 0), mode);
    662 }
    663 
    664 void
    665 svuname(struct pinfo *pi, VARENT *ve, enum mode mode)
    666 {
    667 	struct kinfo_proc2 *k = pi->ki;
    668 	VAR *v;
    669 
    670 	v = ve->var;
    671 	strprintorsetwidth(v, user_from_uid(k->p_svuid, 0), mode);
    672 }
    673 
    674 void
    675 gname(struct pinfo *pi, VARENT *ve, enum mode mode)
    676 {
    677 	struct kinfo_proc2 *k = pi->ki;
    678 	VAR *v;
    679 
    680 	v = ve->var;
    681 	strprintorsetwidth(v, group_from_gid(k->p_gid, 0), mode);
    682 }
    683 
    684 void
    685 rgname(struct pinfo *pi, VARENT *ve, enum mode mode)
    686 {
    687 	struct kinfo_proc2 *k = pi->ki;
    688 	VAR *v;
    689 
    690 	v = ve->var;
    691 	strprintorsetwidth(v, group_from_gid(k->p_rgid, 0), mode);
    692 }
    693 
    694 void
    695 svgname(struct pinfo *pi, VARENT *ve, enum mode mode)
    696 {
    697 	struct kinfo_proc2 *k = pi->ki;
    698 	VAR *v;
    699 
    700 	v = ve->var;
    701 	strprintorsetwidth(v, group_from_gid(k->p_svgid, 0), mode);
    702 }
    703 
    704 void
    705 tdev(struct pinfo *pi, VARENT *ve, enum mode mode)
    706 {
    707 	struct kinfo_proc2 *k = pi->ki;
    708 	VAR *v;
    709 	dev_t dev;
    710 	char buff[16];
    711 
    712 	v = ve->var;
    713 	dev = k->p_tdev;
    714 	if (dev == NODEV) {
    715 		if (mode == PRINTMODE)
    716 			(void)printf("%*s", v->width, "?");
    717 		else
    718 			if (v->width < 2)
    719 				v->width = 2;
    720 	} else {
    721 		(void)snprintf(buff, sizeof(buff),
    722 		    "%lld/%lld", (long long)major(dev), (long long)minor(dev));
    723 		strprintorsetwidth(v, buff, mode);
    724 	}
    725 }
    726 
    727 void
    728 tname(struct pinfo *pi, VARENT *ve, enum mode mode)
    729 {
    730 	struct kinfo_proc2 *k = pi->ki;
    731 	VAR *v;
    732 	dev_t dev;
    733 	const char *ttname;
    734 	int noctty;
    735 
    736 	v = ve->var;
    737 	dev = k->p_tdev;
    738 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
    739 		if (mode == PRINTMODE)
    740 			(void)printf("%-*s", v->width, "?");
    741 		else
    742 			if (v->width < 2)
    743 				v->width = 2;
    744 	} else {
    745 		noctty = !(k->p_eflag & EPROC_CTTY) ? 1 : 0;
    746 		if (mode == WIDTHMODE) {
    747 			int fmtlen;
    748 
    749 			fmtlen = strlen(ttname) + noctty;
    750 			if (v->width < fmtlen)
    751 				v->width = fmtlen;
    752 		} else {
    753 			if (noctty)
    754 				(void)printf("%-*s-", v->width - 1, ttname);
    755 			else
    756 				(void)printf("%-*s", v->width, ttname);
    757 		}
    758 	}
    759 }
    760 
    761 void
    762 longtname(struct pinfo *pi, VARENT *ve, enum mode mode)
    763 {
    764 	struct kinfo_proc2 *k = pi->ki;
    765 	VAR *v;
    766 	dev_t dev;
    767 	const char *ttname;
    768 
    769 	v = ve->var;
    770 	dev = k->p_tdev;
    771 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
    772 		if (mode == PRINTMODE)
    773 			(void)printf("%-*s", v->width, "?");
    774 		else
    775 			if (v->width < 2)
    776 				v->width = 2;
    777 	} else {
    778 		strprintorsetwidth(v, ttname, mode);
    779 	}
    780 }
    781 
    782 void
    783 started(struct pinfo *pi, VARENT *ve, enum mode mode)
    784 {
    785 	struct kinfo_proc2 *k = pi->ki;
    786 	VAR *v;
    787 	time_t startt;
    788 	struct tm *tp;
    789 	char buf[100], *cp;
    790 
    791 	v = ve->var;
    792 	if (!k->p_uvalid) {
    793 		if (mode == PRINTMODE)
    794 			(void)printf("%*s", v->width, "-");
    795 		return;
    796 	}
    797 
    798 	startt = k->p_ustart_sec;
    799 	tp = localtime(&startt);
    800 	if (now == 0)
    801 		(void)time(&now);
    802 	if (now - k->p_ustart_sec < SECSPERDAY)
    803 		/* I *hate* SCCS... */
    804 		safe_strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
    805 	else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY)
    806 		/* I *hate* SCCS... */
    807 		safe_strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
    808 	else
    809 		safe_strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
    810 	/* %e and %l can start with a space. */
    811 	cp = buf;
    812 	if (*cp == ' ')
    813 		cp++;
    814 	strprintorsetwidth(v, cp, mode);
    815 }
    816 
    817 void
    818 lstarted(struct pinfo *pi, VARENT *ve, enum mode mode)
    819 {
    820 	struct kinfo_proc2 *k = pi->ki;
    821 	VAR *v;
    822 	time_t startt;
    823 	char buf[100];
    824 
    825 	v = ve->var;
    826 	startt = k->p_ustart_sec;
    827 
    828 	if (mode == WIDTHMODE) {
    829 		/*
    830 		 * We only need to set the width once, as we assume
    831 		 * that all times are the same length.  We do need to
    832 		 * check against the header length as well, as "no
    833 		 * header" mode for this variable will set the field
    834 		 * width to the length of the header anyway (ref: the
    835 		 * P1003.1-2004 comment in findvar()).
    836 		 *
    837 		 * XXX: The hardcoded "STARTED" string.  Better or
    838 		 * worse than a "<= 7" or some other arbitary number?
    839 		 */
    840 		if (v->width > (int)sizeof("STARTED") - 1) {
    841 			return;
    842 		}
    843 	} else {
    844 		if (!k->p_uvalid) {
    845 			(void)printf("%*s", v->width, "-");
    846 			return;
    847 		}
    848 	}
    849 	safe_strftime(buf, sizeof(buf) - 1, "%c", localtime(&startt));
    850 	strprintorsetwidth(v, buf, mode);
    851 }
    852 
    853 void
    854 elapsed(struct pinfo *pi, VARENT *ve, enum mode mode)
    855 {
    856 	struct kinfo_proc2 *k = pi->ki;
    857 	VAR *v;
    858 	int32_t origseconds, secs, mins, hours, days;
    859 	int fmtlen, printed_something;
    860 
    861 	v = ve->var;
    862 	if (k->p_uvalid == 0) {
    863 		origseconds = 0;
    864 	} else {
    865 		if (now == 0)
    866 			(void)time(&now);
    867 		origseconds = now - k->p_ustart_sec;
    868 		if (origseconds < 0) {
    869 			/*
    870 			 * Don't try to be fancy if the machine's
    871 			 * clock has been rewound to before the
    872 			 * process "started".
    873 			 */
    874 			origseconds = 0;
    875 		}
    876 	}
    877 
    878 	secs = origseconds;
    879 	mins = secs / SECSPERMIN;
    880 	secs %= SECSPERMIN;
    881 	hours = mins / MINSPERHOUR;
    882 	mins %= MINSPERHOUR;
    883 	days = hours / HOURSPERDAY;
    884 	hours %= HOURSPERDAY;
    885 
    886 	if (mode == WIDTHMODE) {
    887 		if (origseconds == 0)
    888 			/* non-zero so fmtlen is calculated at least once */
    889 			origseconds = 1;
    890 
    891 		if (origseconds > v->longestp) {
    892 			v->longestp = origseconds;
    893 
    894 			if (days > 0) {
    895 				/* +9 for "-hh:mm:ss" */
    896 				fmtlen = iwidth(days) + 9;
    897 			} else if (hours > 0) {
    898 				/* +6 for "mm:ss" */
    899 				fmtlen = iwidth(hours) + 6;
    900 			} else {
    901 				/* +3 for ":ss" */
    902 				fmtlen = iwidth(mins) + 3;
    903 			}
    904 
    905 			if (fmtlen > v->width)
    906 				v->width = fmtlen;
    907 		}
    908 	} else {
    909 		printed_something = 0;
    910 		fmtlen = v->width;
    911 
    912 		if (days > 0) {
    913 			(void)printf("%*d", fmtlen - 9, days);
    914 			printed_something = 1;
    915 		} else if (fmtlen > 9) {
    916 			(void)printf("%*s", fmtlen - 9, "");
    917 		}
    918 		if (fmtlen > 9)
    919 			fmtlen = 9;
    920 
    921 		if (printed_something) {
    922 			(void)printf("-%.*d", fmtlen - 7, hours);
    923 			printed_something = 1;
    924 		} else if (hours > 0) {
    925 			(void)printf("%*d", fmtlen - 6, hours);
    926 			printed_something = 1;
    927 		} else if (fmtlen > 6) {
    928 			(void)printf("%*s", fmtlen - 6, "");
    929 		}
    930 		if (fmtlen > 6)
    931 			fmtlen = 6;
    932 
    933 		/* Don't need to set fmtlen or printed_something any more... */
    934 		if (printed_something) {
    935 			(void)printf(":%.*d", fmtlen - 4, mins);
    936 		} else if (mins > 0) {
    937 			(void)printf("%*d", fmtlen - 3, mins);
    938 		} else if (fmtlen > 3) {
    939 			(void)printf("%*s", fmtlen - 3, "0");
    940 		}
    941 
    942 		(void)printf(":%.2d", secs);
    943 	}
    944 }
    945 
    946 void
    947 wchan(struct pinfo *pi, VARENT *ve, enum mode mode)
    948 {
    949 	struct kinfo_lwp *l = pi->li;
    950 	VAR *v;
    951 
    952 	v = ve->var;
    953 	if (l->l_wmesg[0]) {
    954 		strprintorsetwidth(v, l->l_wmesg, mode);
    955 		v->width = min(v->width, KI_WMESGLEN);
    956 	} else {
    957 		if (mode == PRINTMODE)
    958 			(void)printf("%-*s", v->width, "-");
    959 	}
    960 }
    961 
    962 #define	pgtok(a)        (((a)*(size_t)getpagesize())/1024)
    963 
    964 void
    965 vsize(struct pinfo *pi, VARENT *ve, enum mode mode)
    966 {
    967 	struct kinfo_proc2 *k = pi->ki;
    968 	VAR *v;
    969 
    970 	v = ve->var;
    971 	intprintorsetwidth(v, pgtok(k->p_vm_msize), mode);
    972 }
    973 
    974 void
    975 rssize(struct pinfo *pi, VARENT *ve, enum mode mode)
    976 {
    977 	struct kinfo_proc2 *k = pi->ki;
    978 	VAR *v;
    979 
    980 	v = ve->var;
    981 	/* XXX don't have info about shared */
    982 	intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
    983 }
    984 
    985 void
    986 p_rssize(struct pinfo *pi, VARENT *ve, enum mode mode)	/* doesn't account for text */
    987 {
    988 	struct kinfo_proc2 *k = pi->ki;
    989 	VAR *v;
    990 
    991 	v = ve->var;
    992 	intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
    993 }
    994 
    995 void
    996 cpuid(struct pinfo *pi, VARENT *ve, enum mode mode)
    997 {
    998 	struct kinfo_lwp *l = pi->li;
    999 	VAR *v;
   1000 
   1001 	v = ve->var;
   1002 	intprintorsetwidth(v, l->l_cpuid, mode);
   1003 }
   1004 
   1005 static void
   1006 cputime1(int32_t secs, int32_t psecs, VAR *v, enum mode mode)
   1007 {
   1008 	int fmtlen;
   1009 
   1010 	/*
   1011 	 * round and scale to 100's
   1012 	 */
   1013 	psecs = (psecs + 5000) / 10000;
   1014 	secs += psecs / 100;
   1015 	psecs = psecs % 100;
   1016 
   1017 	if (mode == WIDTHMODE) {
   1018 		/*
   1019 		 * Ugg, this is the only field where a value of 0 is longer
   1020 		 * than the column title.
   1021 		 * Use SECSPERMIN, because secs is divided by that when
   1022 		 * passed to iwidth().
   1023 		 */
   1024 		if (secs == 0)
   1025 			secs = SECSPERMIN;
   1026 
   1027 		if (secs > v->longestp) {
   1028 			v->longestp = secs;
   1029 			/* "+6" for the ":%02ld.%02ld" in the printf() below */
   1030 			fmtlen = iwidth(secs / SECSPERMIN) + 6;
   1031 			if (fmtlen > v->width)
   1032 				v->width = fmtlen;
   1033 		}
   1034 	} else {
   1035 		(void)printf("%*ld:%02ld.%02ld", v->width - 6,
   1036 		    (long)(secs / SECSPERMIN), (long)(secs % SECSPERMIN),
   1037 		    (long)psecs);
   1038 	}
   1039 }
   1040 
   1041 void
   1042 cputime(struct pinfo *pi, VARENT *ve, enum mode mode)
   1043 {
   1044 	struct kinfo_proc2 *k = pi->ki;
   1045 	VAR *v;
   1046 	int32_t secs;
   1047 	int32_t psecs;	/* "parts" of a second. first micro, then centi */
   1048 
   1049 	v = ve->var;
   1050 
   1051 	/*
   1052 	 * This counts time spent handling interrupts.  We could
   1053 	 * fix this, but it is not 100% trivial (and interrupt
   1054 	 * time fractions only work on the sparc anyway).	XXX
   1055 	 */
   1056 	secs = k->p_rtime_sec;
   1057 	psecs = k->p_rtime_usec;
   1058 	if (sumrusage) {
   1059 		secs += k->p_uctime_sec;
   1060 		psecs += k->p_uctime_usec;
   1061 	}
   1062 
   1063 	cputime1(secs, psecs, v, mode);
   1064 }
   1065 
   1066 void
   1067 lcputime(struct pinfo *pi, VARENT *ve, enum mode mode)
   1068 {
   1069 	struct kinfo_lwp *l = pi->li;
   1070 	VAR *v;
   1071 	int32_t secs;
   1072 	int32_t psecs;	/* "parts" of a second. first micro, then centi */
   1073 
   1074 	v = ve->var;
   1075 
   1076 	secs = l->l_rtime_sec;
   1077 	psecs = l->l_rtime_usec;
   1078 
   1079 	cputime1(secs, psecs, v, mode);
   1080 }
   1081 
   1082 void
   1083 pcpu(struct pinfo *pi, VARENT *ve, enum mode mode)
   1084 {
   1085 	VAR *v;
   1086 	double dbl;
   1087 
   1088 	v = ve->var;
   1089 	dbl = pi->pcpu;
   1090 	doubleprintorsetwidth(v, dbl, (dbl >= 99.95) ? 0 : 1, mode);
   1091 }
   1092 
   1093 double
   1094 getpmem(const struct kinfo_proc2 *k)
   1095 {
   1096 	double fracmem;
   1097 	int szptudot;
   1098 
   1099 	if (!nlistread)
   1100 		donlist();
   1101 
   1102 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
   1103 	szptudot = uspace/getpagesize();
   1104 	/* XXX don't have info about shared */
   1105 	fracmem = ((float)k->p_vm_rssize + szptudot)/mempages;
   1106 	return (100.0 * fracmem);
   1107 }
   1108 
   1109 void
   1110 pmem(struct pinfo *pi, VARENT *ve, enum mode mode)
   1111 {
   1112 	struct kinfo_proc2 *k = pi->ki;
   1113 	VAR *v;
   1114 
   1115 	v = ve->var;
   1116 	doubleprintorsetwidth(v, getpmem(k), 1, mode);
   1117 }
   1118 
   1119 void
   1120 pagein(struct pinfo *pi, VARENT *ve, enum mode mode)
   1121 {
   1122 	struct kinfo_proc2 *k = pi->ki;
   1123 	VAR *v;
   1124 
   1125 	v = ve->var;
   1126 	intprintorsetwidth(v, k->p_uvalid ? k->p_uru_majflt : 0, mode);
   1127 }
   1128 
   1129 void
   1130 maxrss(struct pinfo *pi, VARENT *ve, enum mode mode)
   1131 {
   1132 	VAR *v;
   1133 
   1134 	v = ve->var;
   1135 	/* No need to check width! */
   1136 	if (mode == PRINTMODE)
   1137 		(void)printf("%*s", v->width, "-");
   1138 }
   1139 
   1140 void
   1141 tsize(struct pinfo *pi, VARENT *ve, enum mode mode)
   1142 {
   1143 	struct kinfo_proc2 *k = pi->ki;
   1144 	VAR *v;
   1145 
   1146 	v = ve->var;
   1147 	intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode);
   1148 }
   1149 
   1150 /*
   1151  * Generic output routines.  Print fields from various prototype
   1152  * structures.
   1153  */
   1154 static void
   1155 printval(void *bp, VAR *v, enum mode mode)
   1156 {
   1157 	static char ofmt[32] = "%";
   1158 	int width, vok, fmtlen;
   1159 	const char *fcp;
   1160 	char *cp;
   1161 	int64_t val;
   1162 	u_int64_t uval;
   1163 
   1164 	val = 0;	/* XXXGCC -Wuninitialized [hpcarm] */
   1165 	uval = 0;	/* XXXGCC -Wuninitialized [hpcarm] */
   1166 
   1167 	/*
   1168 	 * Note that the "INF127" check is nonsensical for types
   1169 	 * that are or can be signed.
   1170 	 */
   1171 #define	GET(type)		(*(type *)bp)
   1172 #define	CHK_INF127(n)		(((n) > 127) && (v->flag & INF127) ? 127 : (n))
   1173 
   1174 #define	VSIGN	1
   1175 #define	VUNSIGN	2
   1176 #define	VPTR	3
   1177 
   1178 	if (mode == WIDTHMODE) {
   1179 		vok = 0;
   1180 		switch (v->type) {
   1181 		case CHAR:
   1182 			val = GET(char);
   1183 			vok = VSIGN;
   1184 			break;
   1185 		case UCHAR:
   1186 			uval = CHK_INF127(GET(u_char));
   1187 			vok = VUNSIGN;
   1188 			break;
   1189 		case SHORT:
   1190 			val = GET(short);
   1191 			vok = VSIGN;
   1192 			break;
   1193 		case USHORT:
   1194 			uval = CHK_INF127(GET(u_short));
   1195 			vok = VUNSIGN;
   1196 			break;
   1197 		case INT32:
   1198 			val = GET(int32_t);
   1199 			vok = VSIGN;
   1200 			break;
   1201 		case INT:
   1202 			val = GET(int);
   1203 			vok = VSIGN;
   1204 			break;
   1205 		case UINT:
   1206 		case UINT32:
   1207 			uval = CHK_INF127(GET(u_int));
   1208 			vok = VUNSIGN;
   1209 			break;
   1210 		case LONG:
   1211 			val = GET(long);
   1212 			vok = VSIGN;
   1213 			break;
   1214 		case ULONG:
   1215 			uval = CHK_INF127(GET(u_long));
   1216 			vok = VUNSIGN;
   1217 			break;
   1218 		case KPTR:
   1219 			uval = GET(u_int64_t);
   1220 			vok = VPTR;
   1221 			break;
   1222 		case KPTR24:
   1223 			uval = GET(u_int64_t);
   1224 			uval &= 0xffffff;
   1225 			vok = VPTR;
   1226 			break;
   1227 		case INT64:
   1228 			val = GET(int64_t);
   1229 			vok = VSIGN;
   1230 			break;
   1231 		case UINT64:
   1232 			uval = CHK_INF127(GET(u_int64_t));
   1233 			vok = VUNSIGN;
   1234 			break;
   1235 
   1236 		case SIGLIST:
   1237 		default:
   1238 			/* nothing... */;
   1239 		}
   1240 		switch (vok) {
   1241 		case VSIGN:
   1242 			if (val < 0 && val < v->longestn) {
   1243 				v->longestn = val;
   1244 				fmtlen = iwidth(-val) + 1;
   1245 				if (fmtlen > v->width)
   1246 					v->width = fmtlen;
   1247 			} else if (val > 0 && val > v->longestp) {
   1248 				v->longestp = val;
   1249 				fmtlen = iwidth(val);
   1250 				if (fmtlen > v->width)
   1251 					v->width = fmtlen;
   1252 			}
   1253 			return;
   1254 		case VUNSIGN:
   1255 			if (uval > v->longestu) {
   1256 				v->longestu = uval;
   1257 				v->width = iwidth(uval);
   1258 			}
   1259 			return;
   1260 		case VPTR:
   1261 			fmtlen = 0;
   1262 			while (uval > 0) {
   1263 				uval >>= 4;
   1264 				fmtlen++;
   1265 			}
   1266 			if (fmtlen > v->width)
   1267 				v->width = fmtlen;
   1268 			return;
   1269 		}
   1270 	}
   1271 
   1272 	width = v->width;
   1273 	cp = ofmt + 1;
   1274 	fcp = v->fmt;
   1275 	if (v->flag & LJUST)
   1276 		*cp++ = '-';
   1277 	*cp++ = '*';
   1278 	while ((*cp++ = *fcp++) != '\0')
   1279 		continue;
   1280 
   1281 	switch (v->type) {
   1282 	case CHAR:
   1283 		(void)printf(ofmt, width, GET(char));
   1284 		return;
   1285 	case UCHAR:
   1286 		(void)printf(ofmt, width, CHK_INF127(GET(u_char)));
   1287 		return;
   1288 	case SHORT:
   1289 		(void)printf(ofmt, width, GET(short));
   1290 		return;
   1291 	case USHORT:
   1292 		(void)printf(ofmt, width, CHK_INF127(GET(u_short)));
   1293 		return;
   1294 	case INT:
   1295 		(void)printf(ofmt, width, GET(int));
   1296 		return;
   1297 	case UINT:
   1298 		(void)printf(ofmt, width, CHK_INF127(GET(u_int)));
   1299 		return;
   1300 	case LONG:
   1301 		(void)printf(ofmt, width, GET(long));
   1302 		return;
   1303 	case ULONG:
   1304 		(void)printf(ofmt, width, CHK_INF127(GET(u_long)));
   1305 		return;
   1306 	case KPTR:
   1307 		(void)printf(ofmt, width, GET(u_int64_t));
   1308 		return;
   1309 	case KPTR24:
   1310 		(void)printf(ofmt, width, GET(u_int64_t) & 0xffffff);
   1311 		return;
   1312 	case INT32:
   1313 		(void)printf(ofmt, width, GET(int32_t));
   1314 		return;
   1315 	case UINT32:
   1316 		(void)printf(ofmt, width, CHK_INF127(GET(u_int32_t)));
   1317 		return;
   1318 	case SIGLIST:
   1319 		{
   1320 			sigset_t *s = (sigset_t *)(void *)bp;
   1321 			size_t i;
   1322 #define	SIGSETSIZE	(sizeof(s->__bits) / sizeof(s->__bits[0]))
   1323 			char buf[SIGSETSIZE * 8 + 1];
   1324 
   1325 			for (i = 0; i < SIGSETSIZE; i++)
   1326 				(void)snprintf(&buf[i * 8], 9, "%.8x",
   1327 				    s->__bits[(SIGSETSIZE - 1) - i]);
   1328 
   1329 			/* Skip leading zeroes */
   1330 			for (i = 0; buf[i] == '0'; i++)
   1331 				continue;
   1332 
   1333 			if (buf[i] == '\0')
   1334 				i--;
   1335 			strprintorsetwidth(v, buf + i, mode);
   1336 #undef SIGSETSIZE
   1337 		}
   1338 		return;
   1339 	case INT64:
   1340 		(void)printf(ofmt, width, GET(int64_t));
   1341 		return;
   1342 	case UINT64:
   1343 		(void)printf(ofmt, width, CHK_INF127(GET(u_int64_t)));
   1344 		return;
   1345 	default:
   1346 		errx(EXIT_FAILURE, "unknown type %d", v->type);
   1347 	}
   1348 #undef GET
   1349 #undef CHK_INF127
   1350 }
   1351 
   1352 void
   1353 pvar(struct pinfo *pi, VARENT *ve, enum mode mode)
   1354 {
   1355 	VAR *v = ve->var;
   1356 	char *b = (v->flag & LWP) ? (char *)pi->li : (char *)pi->ki;
   1357 
   1358 	if ((v->flag & UAREA) && !pi->ki->p_uvalid) {
   1359 		if (mode == PRINTMODE)
   1360 			(void)printf("%*s", v->width, "-");
   1361 		return;
   1362 	}
   1363 
   1364 	(void)printval(b + v->off, v, mode);
   1365 }
   1366 
   1367 void
   1368 putimeval(struct pinfo *pi, VARENT *ve, enum mode mode)
   1369 {
   1370 	VAR *v = ve->var;
   1371 	struct kinfo_proc2 *k = pi->ki;
   1372 	char *b = (v->flag & LWP) ? (char *)pi->li : (char *)pi->ki;
   1373 	ulong secs = *(uint32_t *)(b + v->off);
   1374 	ulong usec = *(uint32_t *)(b + v->off + sizeof (uint32_t));
   1375 	int fmtlen;
   1376 
   1377 	if (!k->p_uvalid) {
   1378 		if (mode == PRINTMODE)
   1379 			(void)printf("%*s", v->width, "-");
   1380 		return;
   1381 	}
   1382 
   1383 	if (mode == WIDTHMODE) {
   1384 		if (secs == 0)
   1385 			/* non-zero so fmtlen is calculated at least once */
   1386 			secs = 1;
   1387 		if (secs > v->longestu) {
   1388 			v->longestu = secs;
   1389 			if (secs <= 999)
   1390 				/* sss.ssssss */
   1391 				fmtlen = iwidth(secs) + 6 + 1;
   1392 			else
   1393 				/* hh:mm:ss.ss */
   1394 				fmtlen = iwidth((secs + 1) / SECSPERHOUR)
   1395 					+ 2 + 1 + 2 + 1 + 2 + 1;
   1396 			if (fmtlen > v->width)
   1397 				v->width = fmtlen;
   1398 		}
   1399 		return;
   1400 	}
   1401 
   1402 	if (secs < 999)
   1403 		(void)printf( "%*lu.%.6lu", v->width - 6 - 1, secs, usec);
   1404 	else {
   1405 		uint h, m;
   1406 		usec += 5000;
   1407 		if (usec >= 1000000) {
   1408 			usec -= 1000000;
   1409 			secs++;
   1410 		}
   1411 		m = secs / SECSPERMIN;
   1412 		secs -= m * SECSPERMIN;
   1413 		h = m / MINSPERHOUR;
   1414 		m -= h * MINSPERHOUR;
   1415 		(void)printf( "%*u:%.2u:%.2lu.%.2lu", v->width - 9, h, m, secs,
   1416 		    usec / 10000u );
   1417 	}
   1418 }
   1419 
   1420 void
   1421 lname(struct pinfo *pi, VARENT *ve, enum mode mode)
   1422 {
   1423 	struct kinfo_lwp *l = pi->li;
   1424 	VAR *v;
   1425 
   1426 	v = ve->var;
   1427 	if (l->l_name[0] != '\0') {
   1428 		strprintorsetwidth(v, l->l_name, mode);
   1429 		v->width = min(v->width, KI_LNAMELEN);
   1430 	} else {
   1431 		if (mode == PRINTMODE)
   1432 			(void)printf("%-*s", v->width, "-");
   1433 	}
   1434 }
   1435