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