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