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