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