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