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