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