Home | History | Annotate | Line # | Download | only in ps
print.c revision 1.98
      1 /*	$NetBSD: print.c,v 1.98 2007/02/10 18:20:12 ad 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  * 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.98 2007/02/10 18:20:12 ad 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 & KP_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 & KP_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 		*cp = 'Z';
    516 		is_zombie = 1;
    517 		break;
    518 
    519 	case LSSUSPENDED:
    520 		*cp = 'U';
    521 		break;
    522 
    523 	default:
    524 		*cp = '?';
    525 	}
    526 	cp++;
    527 	if (flag & KP_INMEM) {
    528 	} else
    529 		*cp++ = 'W';
    530 	if (k->p_nice < NZERO)
    531 		*cp++ = '<';
    532 	else if (k->p_nice > NZERO)
    533 		*cp++ = 'N';
    534 	if (flag & KP_TRACED)
    535 		*cp++ = 'X';
    536 	if (flag & KP_SYSTRACE)
    537 		*cp++ = 'x';
    538 	if (flag & KP_WEXIT && !is_zombie)
    539 		*cp++ = 'E';
    540 	if (flag & KP_PPWAIT)
    541 		*cp++ = 'V';
    542 	if (flag & KP_SYSTEM)
    543 		*cp++ = 'K';
    544 	/* system process might have this too, don't need to double up */
    545 	else if (k->p_holdcnt)
    546 		*cp++ = 'L';
    547 	if (k->p_eflag & EPROC_SLEADER)
    548 		*cp++ = 's';
    549 	if (flag & KP_SA)
    550 		*cp++ = 'a';
    551 	else if (k->p_nlwps > 1)
    552 		*cp++ = 'l';
    553 	if ((flag & KP_CONTROLT) && k->p__pgid == k->p_tpgid)
    554 		*cp++ = '+';
    555 	*cp = '\0';
    556 	strprintorsetwidth(v, buf, mode);
    557 }
    558 
    559 void
    560 lstate(void *arg, VARENT *ve, int mode)
    561 {
    562 	struct kinfo_lwp *k;
    563 	int flag, is_zombie;
    564 	char *cp;
    565 	VAR *v;
    566 	char buf[16];
    567 
    568 	k = arg;
    569 	is_zombie = 0;
    570 	v = ve->var;
    571 	flag = k->l_flag;
    572 	cp = buf;
    573 
    574 	switch (k->l_stat) {
    575 
    576 	case LSSTOP:
    577 		*cp = 'T';
    578 		break;
    579 
    580 	case LSSLEEP:
    581 		if (flag & L_SINTR)	/* interruptible (long) */
    582 			*cp = k->l_slptime >= maxslp ? 'I' : 'S';
    583 		else
    584 			*cp = 'D';
    585 		break;
    586 
    587 	case LSRUN:
    588 	case LSIDL:
    589 	case LSONPROC:
    590 		*cp = 'R';
    591 		break;
    592 
    593 	case LSZOMB:
    594 		*cp = 'Z';
    595 		is_zombie = 1;
    596 		break;
    597 
    598 	case LSSUSPENDED:
    599 		*cp = 'U';
    600 		break;
    601 
    602 	default:
    603 		*cp = '?';
    604 	}
    605 	cp++;
    606 	if (flag & L_INMEM) {
    607 	} else
    608 		*cp++ = 'W';
    609 	if (k->l_holdcnt)
    610 		*cp++ = 'L';
    611 	if (flag & KL_DETACHED)
    612 		*cp++ = '-';
    613 	*cp = '\0';
    614 	strprintorsetwidth(v, buf, mode);
    615 }
    616 
    617 void
    618 pnice(void *arg, VARENT *ve, int mode)
    619 {
    620 	struct kinfo_proc2 *k;
    621 	VAR *v;
    622 
    623 	k = arg;
    624 	v = ve->var;
    625 	intprintorsetwidth(v, k->p_nice - NZERO, mode);
    626 }
    627 
    628 void
    629 pri(void *arg, VARENT *ve, int mode)
    630 {
    631 	struct kinfo_lwp *l;
    632 	VAR *v;
    633 
    634 	l = arg;
    635 	v = ve->var;
    636 	intprintorsetwidth(v, l->l_priority - PZERO, mode);
    637 }
    638 
    639 void
    640 uname(void *arg, VARENT *ve, int mode)
    641 {
    642 	struct kinfo_proc2 *k;
    643 	VAR *v;
    644 
    645 	k = arg;
    646 	v = ve->var;
    647 	strprintorsetwidth(v, user_from_uid(k->p_uid, 0), mode);
    648 }
    649 
    650 void
    651 runame(void *arg, VARENT *ve, int mode)
    652 {
    653 	struct kinfo_proc2 *k;
    654 	VAR *v;
    655 
    656 	k = arg;
    657 	v = ve->var;
    658 	strprintorsetwidth(v, user_from_uid(k->p_ruid, 0), mode);
    659 }
    660 
    661 void
    662 svuname(void *arg, VARENT *ve, int mode)
    663 {
    664 	struct kinfo_proc2 *k;
    665 	VAR *v;
    666 
    667 	k = arg;
    668 	v = ve->var;
    669 	strprintorsetwidth(v, user_from_uid(k->p_svuid, 0), mode);
    670 }
    671 
    672 void
    673 gname(void *arg, VARENT *ve, int mode)
    674 {
    675 	struct kinfo_proc2 *k;
    676 	VAR *v;
    677 
    678 	k = arg;
    679 	v = ve->var;
    680 	strprintorsetwidth(v, group_from_gid(k->p_gid, 0), mode);
    681 }
    682 
    683 void
    684 rgname(void *arg, VARENT *ve, int mode)
    685 {
    686 	struct kinfo_proc2 *k;
    687 	VAR *v;
    688 
    689 	k = arg;
    690 	v = ve->var;
    691 	strprintorsetwidth(v, group_from_gid(k->p_rgid, 0), mode);
    692 }
    693 
    694 void
    695 svgname(void *arg, VARENT *ve, int mode)
    696 {
    697 	struct kinfo_proc2 *k;
    698 	VAR *v;
    699 
    700 	k = arg;
    701 	v = ve->var;
    702 	strprintorsetwidth(v, group_from_gid(k->p_svgid, 0), mode);
    703 }
    704 
    705 void
    706 tdev(void *arg, VARENT *ve, int mode)
    707 {
    708 	struct kinfo_proc2 *k;
    709 	VAR *v;
    710 	dev_t dev;
    711 	char buff[16];
    712 
    713 	k = arg;
    714 	v = ve->var;
    715 	dev = k->p_tdev;
    716 	if (dev == NODEV) {
    717 		if (mode == PRINTMODE)
    718 			(void)printf("%*s", v->width, "?");
    719 		else
    720 			if (v->width < 2)
    721 				v->width = 2;
    722 	} else {
    723 		(void)snprintf(buff, sizeof(buff),
    724 		    "%d/%d", major(dev), minor(dev));
    725 		strprintorsetwidth(v, buff, mode);
    726 	}
    727 }
    728 
    729 void
    730 tname(void *arg, VARENT *ve, int mode)
    731 {
    732 	struct kinfo_proc2 *k;
    733 	VAR *v;
    734 	dev_t dev;
    735 	const char *ttname;
    736 	int noctty;
    737 
    738 	k = arg;
    739 	v = ve->var;
    740 	dev = k->p_tdev;
    741 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
    742 		if (mode == PRINTMODE)
    743 			(void)printf("%-*s", v->width, "?");
    744 		else
    745 			if (v->width < 2)
    746 				v->width = 2;
    747 	} else {
    748 		noctty = !(k->p_eflag & EPROC_CTTY) ? 1 : 0;
    749 		if (mode == WIDTHMODE) {
    750 			int fmtlen;
    751 
    752 			fmtlen = strlen(ttname) + noctty;
    753 			if (v->width < fmtlen)
    754 				v->width = fmtlen;
    755 		} else {
    756 			if (noctty)
    757 				(void)printf("%-*s-", v->width - 1, ttname);
    758 			else
    759 				(void)printf("%-*s", v->width, ttname);
    760 		}
    761 	}
    762 }
    763 
    764 void
    765 longtname(void *arg, VARENT *ve, int mode)
    766 {
    767 	struct kinfo_proc2 *k;
    768 	VAR *v;
    769 	dev_t dev;
    770 	const char *ttname;
    771 
    772 	k = arg;
    773 	v = ve->var;
    774 	dev = k->p_tdev;
    775 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
    776 		if (mode == PRINTMODE)
    777 			(void)printf("%-*s", v->width, "?");
    778 		else
    779 			if (v->width < 2)
    780 				v->width = 2;
    781 	} else {
    782 		strprintorsetwidth(v, ttname, mode);
    783 	}
    784 }
    785 
    786 void
    787 started(void *arg, VARENT *ve, int mode)
    788 {
    789 	struct kinfo_proc2 *k;
    790 	VAR *v;
    791 	time_t startt;
    792 	struct tm *tp;
    793 	char buf[100], *cp;
    794 
    795 	k = arg;
    796 	v = ve->var;
    797 	if (!k->p_uvalid) {
    798 		if (mode == PRINTMODE)
    799 			(void)printf("%*s", v->width, "-");
    800 		return;
    801 	}
    802 
    803 	startt = k->p_ustart_sec;
    804 	tp = localtime(&startt);
    805 	if (now == 0)
    806 		(void)time(&now);
    807 	if (now - k->p_ustart_sec < SECSPERDAY)
    808 		/* I *hate* SCCS... */
    809 		(void)strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
    810 	else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY)
    811 		/* I *hate* SCCS... */
    812 		(void)strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
    813 	else
    814 		(void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
    815 	/* %e and %l can start with a space. */
    816 	cp = buf;
    817 	if (*cp == ' ')
    818 		cp++;
    819 	strprintorsetwidth(v, cp, mode);
    820 }
    821 
    822 void
    823 lstarted(void *arg, VARENT *ve, int mode)
    824 {
    825 	struct kinfo_proc2 *k;
    826 	VAR *v;
    827 	time_t startt;
    828 	char buf[100];
    829 
    830 	k = arg;
    831 	v = ve->var;
    832 	if (!k->p_uvalid) {
    833 		/*
    834 		 * Minimum width is less than header - we don't
    835 		 * need to check it every time.
    836 		 */
    837 		if (mode == PRINTMODE)
    838 			(void)printf("%*s", v->width, "-");
    839 		return;
    840 	}
    841 	startt = k->p_ustart_sec;
    842 
    843 	/* assume all times are the same length */
    844 	if (mode != WIDTHMODE || v->width == 0) {
    845 		(void)strftime(buf, sizeof(buf) -1, "%c",
    846 		    localtime(&startt));
    847 		strprintorsetwidth(v, buf, mode);
    848 	}
    849 }
    850 
    851 void
    852 elapsed(void *arg, VARENT *ve, int mode)
    853 {
    854 	struct kinfo_proc2 *k;
    855 	VAR *v;
    856 	int32_t origseconds, secs, mins, hours, days;
    857 	int fmtlen, printed_something;
    858 
    859 	k = arg;
    860 	v = ve->var;
    861 	if (k->p_uvalid == 0) {
    862 		origseconds = 0;
    863 	} else {
    864 		if (now == 0)
    865 			(void)time(&now);
    866 		origseconds = now - k->p_ustart_sec;
    867 		if (origseconds < 0) {
    868 			/*
    869 			 * Don't try to be fancy if the machine's
    870 			 * clock has been rewound to before the
    871 			 * process "started".
    872 			 */
    873 			origseconds = 0;
    874 		}
    875 	}
    876 
    877 	secs = origseconds;
    878 	mins = secs / SECSPERMIN;
    879 	secs %= SECSPERMIN;
    880 	hours = mins / MINSPERHOUR;
    881 	mins %= MINSPERHOUR;
    882 	days = hours / HOURSPERDAY;
    883 	hours %= HOURSPERDAY;
    884 
    885 	if (mode == WIDTHMODE) {
    886 		if (origseconds == 0)
    887 			/* non-zero so fmtlen is calculated at least once */
    888 			origseconds = 1;
    889 
    890 		if (origseconds > v->longestp) {
    891 			v->longestp = origseconds;
    892 
    893 			if (days > 0) {
    894 				/* +9 for "-hh:mm:ss" */
    895 				fmtlen = iwidth(days) + 9;
    896 			} else if (hours > 0) {
    897 				/* +6 for "mm:ss" */
    898 				fmtlen = iwidth(hours) + 6;
    899 			} else {
    900 				/* +3 for ":ss" */
    901 				fmtlen = iwidth(mins) + 3;
    902 			}
    903 
    904 			if (fmtlen > v->width)
    905 				v->width = fmtlen;
    906 		}
    907 	} else {
    908 		printed_something = 0;
    909 		fmtlen = v->width;
    910 
    911 		if (days > 0) {
    912 			(void)printf("%*d", fmtlen - 9, days);
    913 			printed_something = 1;
    914 		} else if (fmtlen > 9) {
    915 			(void)printf("%*s", fmtlen - 9, "");
    916 		}
    917 		if (fmtlen > 9)
    918 			fmtlen = 9;
    919 
    920 		if (printed_something) {
    921 			(void)printf("-%.*d", fmtlen - 7, hours);
    922 			printed_something = 1;
    923 		} else if (hours > 0) {
    924 			(void)printf("%*d", fmtlen - 6, hours);
    925 			printed_something = 1;
    926 		} else if (fmtlen > 6) {
    927 			(void)printf("%*s", fmtlen - 6, "");
    928 		}
    929 		if (fmtlen > 6)
    930 			fmtlen = 6;
    931 
    932 		/* Don't need to set fmtlen or printed_something any more... */
    933 		if (printed_something) {
    934 			(void)printf(":%.*d", fmtlen - 4, mins);
    935 		} else if (mins > 0) {
    936 			(void)printf("%*d", fmtlen - 3, mins);
    937 		} else if (fmtlen > 3) {
    938 			(void)printf("%*s", fmtlen - 3, "0");
    939 		}
    940 
    941 		(void)printf(":%.2d", secs);
    942 	}
    943 }
    944 
    945 void
    946 wchan(void *arg, VARENT *ve, int mode)
    947 {
    948 	struct kinfo_lwp *l;
    949 	VAR *v;
    950 	char *buf;
    951 
    952 	l = arg;
    953 	v = ve->var;
    954 	if (l->l_wchan) {
    955 		if (l->l_wmesg) {
    956 			strprintorsetwidth(v, l->l_wmesg, mode);
    957 			v->width = min(v->width, KI_WMESGLEN);
    958 		} else {
    959 			(void)asprintf(&buf, "%-*" PRIx64, v->width,
    960 			    l->l_wchan);
    961 			if (buf == NULL)
    962 				err(1, "%s", "");
    963 			strprintorsetwidth(v, buf, mode);
    964 			v->width = min(v->width, KI_WMESGLEN);
    965 			free(buf);
    966 		}
    967 	} else {
    968 		if (mode == PRINTMODE)
    969 			(void)printf("%-*s", v->width, "-");
    970 	}
    971 }
    972 
    973 #define	pgtok(a)        (((a)*getpagesize())/1024)
    974 
    975 void
    976 vsize(void *arg, VARENT *ve, int mode)
    977 {
    978 	struct kinfo_proc2 *k;
    979 	VAR *v;
    980 
    981 	k = arg;
    982 	v = ve->var;
    983 	intprintorsetwidth(v,
    984 	    pgtok(k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize), mode);
    985 }
    986 
    987 void
    988 rssize(void *arg, VARENT *ve, int mode)
    989 {
    990 	struct kinfo_proc2 *k;
    991 	VAR *v;
    992 
    993 	k = arg;
    994 	v = ve->var;
    995 	/* XXX don't have info about shared */
    996 	intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
    997 }
    998 
    999 void
   1000 p_rssize(void *arg, VARENT *ve, int mode)	/* doesn't account for text */
   1001 {
   1002 	struct kinfo_proc2 *k;
   1003 	VAR *v;
   1004 
   1005 	k = arg;
   1006 	v = ve->var;
   1007 	intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
   1008 }
   1009 
   1010 void
   1011 cputime(void *arg, VARENT *ve, int mode)
   1012 {
   1013 	struct kinfo_proc2 *k;
   1014 	VAR *v;
   1015 	int32_t secs;
   1016 	int32_t psecs;	/* "parts" of a second. first micro, then centi */
   1017 	int fmtlen;
   1018 
   1019 	k = arg;
   1020 	v = ve->var;
   1021 	if (P_ZOMBIE(k) || k->p_uvalid == 0) {
   1022 		secs = 0;
   1023 		psecs = 0;
   1024 	} else {
   1025 		/*
   1026 		 * This counts time spent handling interrupts.  We could
   1027 		 * fix this, but it is not 100% trivial (and interrupt
   1028 		 * time fractions only work on the sparc anyway).	XXX
   1029 		 */
   1030 		secs = k->p_rtime_sec;
   1031 		psecs = k->p_rtime_usec;
   1032 		if (sumrusage) {
   1033 			secs += k->p_uctime_sec;
   1034 			psecs += k->p_uctime_usec;
   1035 		}
   1036 		/*
   1037 		 * round and scale to 100's
   1038 		 */
   1039 		psecs = (psecs + 5000) / 10000;
   1040 		secs += psecs / 100;
   1041 		psecs = psecs % 100;
   1042 	}
   1043 	if (mode == WIDTHMODE) {
   1044 		/*
   1045 		 * Ugg, this is the only field where a value of 0 is longer
   1046 		 * than the column title.
   1047 		 * Use SECSPERMIN, because secs is divided by that when
   1048 		 * passed to iwidth().
   1049 		 */
   1050 		if (secs == 0)
   1051 			secs = SECSPERMIN;
   1052 
   1053 		if (secs > v->longestp) {
   1054 			v->longestp = secs;
   1055 			/* "+6" for the ":%02ld.%02ld" in the printf() below */
   1056 			fmtlen = iwidth(secs / SECSPERMIN) + 6;
   1057 			if (fmtlen > v->width)
   1058 				v->width = fmtlen;
   1059 		}
   1060 	} else {
   1061 		(void)printf("%*ld:%02ld.%02ld", v->width - 6,
   1062 		    (long)(secs / SECSPERMIN), (long)(secs % SECSPERMIN),
   1063 		    (long)psecs);
   1064 	}
   1065 }
   1066 
   1067 double
   1068 getpcpu(k)
   1069 	const struct kinfo_proc2 *k;
   1070 {
   1071 	static int failure;
   1072 
   1073 	if (!nlistread)
   1074 		failure = (kd) ? donlist() : 1;
   1075 	if (failure)
   1076 		return (0.0);
   1077 
   1078 #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
   1079 
   1080 	/* XXX - I don't like this */
   1081 	if (k->p_swtime == 0 || (k->p_flag & KP_INMEM) == 0 ||
   1082 	    k->p_stat == SZOMB)
   1083 		return (0.0);
   1084 	if (rawcpu)
   1085 		return (100.0 * fxtofl(k->p_pctcpu));
   1086 	return (100.0 * fxtofl(k->p_pctcpu) /
   1087 		(1.0 - exp(k->p_swtime * log(ccpu))));
   1088 }
   1089 
   1090 void
   1091 pcpu(void *arg, VARENT *ve, int mode)
   1092 {
   1093 	struct kinfo_proc2 *k;
   1094 	VAR *v;
   1095 
   1096 	k = arg;
   1097 	v = ve->var;
   1098 	doubleprintorsetwidth(v, getpcpu(k), 1, mode);
   1099 }
   1100 
   1101 double
   1102 getpmem(k)
   1103 	const struct kinfo_proc2 *k;
   1104 {
   1105 	static int failure;
   1106 	double fracmem;
   1107 	int szptudot;
   1108 
   1109 	if (!nlistread)
   1110 		failure = (kd) ? donlist() : 1;
   1111 	if (failure)
   1112 		return (0.0);
   1113 
   1114 	if ((k->p_flag & KP_INMEM) == 0)
   1115 		return (0.0);
   1116 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
   1117 	szptudot = uspace/getpagesize();
   1118 	/* XXX don't have info about shared */
   1119 	fracmem = ((float)k->p_vm_rssize + szptudot)/mempages;
   1120 	return (100.0 * fracmem);
   1121 }
   1122 
   1123 void
   1124 pmem(void *arg, VARENT *ve, int mode)
   1125 {
   1126 	struct kinfo_proc2 *k;
   1127 	VAR *v;
   1128 
   1129 	k = arg;
   1130 	v = ve->var;
   1131 	doubleprintorsetwidth(v, getpmem(k), 1, mode);
   1132 }
   1133 
   1134 void
   1135 pagein(void *arg, VARENT *ve, int mode)
   1136 {
   1137 	struct kinfo_proc2 *k;
   1138 	VAR *v;
   1139 
   1140 	k = arg;
   1141 	v = ve->var;
   1142 	intprintorsetwidth(v, k->p_uvalid ? k->p_uru_majflt : 0, mode);
   1143 }
   1144 
   1145 void
   1146 maxrss(void *arg, VARENT *ve, int mode)
   1147 {
   1148 	VAR *v;
   1149 
   1150 	v = ve->var;
   1151 	/* No need to check width! */
   1152 	if (mode == PRINTMODE)
   1153 		(void)printf("%*s", v->width, "-");
   1154 }
   1155 
   1156 void
   1157 tsize(void *arg, VARENT *ve, int mode)
   1158 {
   1159 	struct kinfo_proc2 *k;
   1160 	VAR *v;
   1161 
   1162 	k = arg;
   1163 	v = ve->var;
   1164 	intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode);
   1165 }
   1166 
   1167 /*
   1168  * Generic output routines.  Print fields from various prototype
   1169  * structures.
   1170  */
   1171 static void
   1172 printval(bp, v, mode)
   1173 	void *bp;
   1174 	VAR *v;
   1175 	int mode;
   1176 {
   1177 	static char ofmt[32] = "%";
   1178 	int width, vok, fmtlen;
   1179 	const char *fcp;
   1180 	char *cp;
   1181 	int64_t val;
   1182 	u_int64_t uval;
   1183 
   1184 	val = 0;	/* XXXGCC -Wuninitialized [hpcarm] */
   1185 	uval = 0;	/* XXXGCC -Wuninitialized [hpcarm] */
   1186 
   1187 	/*
   1188 	 * Note that the "INF127" check is nonsensical for types
   1189 	 * that are or can be signed.
   1190 	 */
   1191 #define	GET(type)		(*(type *)bp)
   1192 #define	CHK_INF127(n)		(((n) > 127) && (v->flag & INF127) ? 127 : (n))
   1193 
   1194 #define	VSIGN	1
   1195 #define	VUNSIGN	2
   1196 #define	VPTR	3
   1197 
   1198 	if (mode == WIDTHMODE) {
   1199 		vok = 0;
   1200 		switch (v->type) {
   1201 		case CHAR:
   1202 			val = GET(char);
   1203 			vok = VSIGN;
   1204 			break;
   1205 		case UCHAR:
   1206 			uval = CHK_INF127(GET(u_char));
   1207 			vok = VUNSIGN;
   1208 			break;
   1209 		case SHORT:
   1210 			val = GET(short);
   1211 			vok = VSIGN;
   1212 			break;
   1213 		case USHORT:
   1214 			uval = CHK_INF127(GET(u_short));
   1215 			vok = VUNSIGN;
   1216 			break;
   1217 		case INT32:
   1218 			val = GET(int32_t);
   1219 			vok = VSIGN;
   1220 			break;
   1221 		case INT:
   1222 			val = GET(int);
   1223 			vok = VSIGN;
   1224 			break;
   1225 		case UINT:
   1226 		case UINT32:
   1227 			uval = CHK_INF127(GET(u_int));
   1228 			vok = VUNSIGN;
   1229 			break;
   1230 		case LONG:
   1231 			val = GET(long);
   1232 			vok = VSIGN;
   1233 			break;
   1234 		case ULONG:
   1235 			uval = CHK_INF127(GET(u_long));
   1236 			vok = VUNSIGN;
   1237 			break;
   1238 		case KPTR:
   1239 			uval = GET(u_int64_t);
   1240 			vok = VPTR;
   1241 			break;
   1242 		case KPTR24:
   1243 			uval = GET(u_int64_t);
   1244 			uval &= 0xffffff;
   1245 			vok = VPTR;
   1246 			break;
   1247 		case INT64:
   1248 			val = GET(int64_t);
   1249 			vok = VSIGN;
   1250 			break;
   1251 		case UINT64:
   1252 			uval = CHK_INF127(GET(u_int64_t));
   1253 			vok = VUNSIGN;
   1254 			break;
   1255 
   1256 		case SIGLIST:
   1257 		default:
   1258 			/* nothing... */;
   1259 		}
   1260 		switch (vok) {
   1261 		case VSIGN:
   1262 			if (val < 0 && val < v->longestn) {
   1263 				v->longestn = val;
   1264 				fmtlen = iwidth(-val) + 1;
   1265 				if (fmtlen > v->width)
   1266 					v->width = fmtlen;
   1267 			} else if (val > 0 && val > v->longestp) {
   1268 				v->longestp = val;
   1269 				fmtlen = iwidth(val);
   1270 				if (fmtlen > v->width)
   1271 					v->width = fmtlen;
   1272 			}
   1273 			return;
   1274 		case VUNSIGN:
   1275 			if (uval > v->longestu) {
   1276 				v->longestu = uval;
   1277 				v->width = iwidth(uval);
   1278 			}
   1279 			return;
   1280 		case VPTR:
   1281 			fmtlen = 0;
   1282 			while (uval > 0) {
   1283 				uval >>= 4;
   1284 				fmtlen++;
   1285 			}
   1286 			if (fmtlen > v->width)
   1287 				v->width = fmtlen;
   1288 			return;
   1289 		}
   1290 	}
   1291 
   1292 	width = v->width;
   1293 	cp = ofmt + 1;
   1294 	fcp = v->fmt;
   1295 	if (v->flag & LJUST)
   1296 		*cp++ = '-';
   1297 	*cp++ = '*';
   1298 	while ((*cp++ = *fcp++) != '\0')
   1299 		continue;
   1300 
   1301 	switch (v->type) {
   1302 	case CHAR:
   1303 		(void)printf(ofmt, width, GET(char));
   1304 		return;
   1305 	case UCHAR:
   1306 		(void)printf(ofmt, width, CHK_INF127(GET(u_char)));
   1307 		return;
   1308 	case SHORT:
   1309 		(void)printf(ofmt, width, GET(short));
   1310 		return;
   1311 	case USHORT:
   1312 		(void)printf(ofmt, width, CHK_INF127(GET(u_short)));
   1313 		return;
   1314 	case INT:
   1315 		(void)printf(ofmt, width, GET(int));
   1316 		return;
   1317 	case UINT:
   1318 		(void)printf(ofmt, width, CHK_INF127(GET(u_int)));
   1319 		return;
   1320 	case LONG:
   1321 		(void)printf(ofmt, width, GET(long));
   1322 		return;
   1323 	case ULONG:
   1324 		(void)printf(ofmt, width, CHK_INF127(GET(u_long)));
   1325 		return;
   1326 	case KPTR:
   1327 		(void)printf(ofmt, width, GET(u_int64_t));
   1328 		return;
   1329 	case KPTR24:
   1330 		(void)printf(ofmt, width, GET(u_int64_t) & 0xffffff);
   1331 		return;
   1332 	case INT32:
   1333 		(void)printf(ofmt, width, GET(int32_t));
   1334 		return;
   1335 	case UINT32:
   1336 		(void)printf(ofmt, width, CHK_INF127(GET(u_int32_t)));
   1337 		return;
   1338 	case SIGLIST:
   1339 		{
   1340 			sigset_t *s = (sigset_t *)(void *)bp;
   1341 			size_t i;
   1342 #define	SIGSETSIZE	(sizeof(s->__bits) / sizeof(s->__bits[0]))
   1343 			char buf[SIGSETSIZE * 8 + 1];
   1344 
   1345 			for (i = 0; i < SIGSETSIZE; i++)
   1346 				(void)snprintf(&buf[i * 8], 9, "%.8x",
   1347 				    s->__bits[(SIGSETSIZE - 1) - i]);
   1348 
   1349 			/* Skip leading zeroes */
   1350 			for (i = 0; buf[i] == '0'; i++)
   1351 				continue;
   1352 
   1353 			if (buf[i] == '\0')
   1354 				i--;
   1355 			strprintorsetwidth(v, buf + i, mode);
   1356 #undef SIGSETSIZE
   1357 		}
   1358 		return;
   1359 	case INT64:
   1360 		(void)printf(ofmt, width, GET(int64_t));
   1361 		return;
   1362 	case UINT64:
   1363 		(void)printf(ofmt, width, CHK_INF127(GET(u_int64_t)));
   1364 		return;
   1365 	default:
   1366 		errx(1, "unknown type %d", v->type);
   1367 	}
   1368 #undef GET
   1369 #undef CHK_INF127
   1370 }
   1371 
   1372 void
   1373 pvar(void *arg, VARENT *ve, int mode)
   1374 {
   1375 	VAR *v;
   1376 
   1377 	v = ve->var;
   1378 	if (v->flag & UAREA && !((struct kinfo_proc2 *)arg)->p_uvalid) {
   1379 		if (mode == PRINTMODE)
   1380 			(void)printf("%*s", v->width, "-");
   1381 		return;
   1382 	}
   1383 
   1384 	(void)printval((char *)arg + v->off, v, mode);
   1385 }
   1386 
   1387 void
   1388 putimeval(void *arg, VARENT *ve, int mode)
   1389 {
   1390 	VAR *v = ve->var;
   1391 	struct kinfo_proc2 *k = arg;
   1392 	ulong secs = *(uint32_t *)((char *)arg + v->off);
   1393 	ulong usec = *(uint32_t *)((char *)arg + v->off + sizeof (uint32_t));
   1394 	int fmtlen;
   1395 
   1396 	if (!k->p_uvalid) {
   1397 		if (mode == PRINTMODE)
   1398 			(void)printf("%*s", v->width, "-");
   1399 		return;
   1400 	}
   1401 
   1402 	if (mode == WIDTHMODE) {
   1403 		if (secs == 0)
   1404 			/* non-zero so fmtlen is calculated at least once */
   1405 			secs = 1;
   1406 		if (secs > v->longestu) {
   1407 			v->longestu = secs;
   1408 			if (secs <= 999)
   1409 				/* sss.ssssss */
   1410 				fmtlen = iwidth(secs) + 6 + 1;
   1411 			else
   1412 				/* hh:mm:ss.ss */
   1413 				fmtlen = iwidth((secs + 1) / SECSPERHOUR)
   1414 					+ 2 + 1 + 2 + 1 + 2 + 1;
   1415 			if (fmtlen > v->width)
   1416 				v->width = fmtlen;
   1417 		}
   1418 		return;
   1419 	}
   1420 
   1421 	if (secs < 999)
   1422 		(void)printf( "%*lu.%.6lu", v->width - 6 - 1, secs, usec);
   1423 	else {
   1424 		uint h, m;
   1425 		usec += 5000;
   1426 		if (usec >= 1000000) {
   1427 			usec -= 1000000;
   1428 			secs++;
   1429 		}
   1430 		m = secs / SECSPERMIN;
   1431 		secs -= m * SECSPERMIN;
   1432 		h = m / MINSPERHOUR;
   1433 		m -= h * MINSPERHOUR;
   1434 		(void)printf( "%*u:%.2u:%.2lu.%.2lu", v->width - 9, h, m, secs,
   1435 		    usec / 10000u );
   1436 	}
   1437 }
   1438