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