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