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