xref: /freebsd/bin/ps/print.c (revision a870bf2c31b5cb69fb451666a3e7bb94e8964777)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
64b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
74b88c807SRodney W. Grimes  * are met:
84b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
94b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
104b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
124b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
134b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
144b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
154b88c807SRodney W. Grimes  *    without specific prior written permission.
164b88c807SRodney W. Grimes  *
174b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
184b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
194b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
214b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
224b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
254b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274b88c807SRodney W. Grimes  * SUCH DAMAGE.
284b88c807SRodney W. Grimes  */
294b88c807SRodney W. Grimes 
30c9a8d1f4SPhilippe Charnier #if 0
31871e8d8cSMark Murray #ifndef lint
32c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
334b88c807SRodney W. Grimes #endif /* not lint */
34871e8d8cSMark Murray #endif
35eaed5652SPhilippe Charnier 
362749b141SDavid E. O'Brien #include <sys/cdefs.h>
372749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
384b88c807SRodney W. Grimes 
394b88c807SRodney W. Grimes #include <sys/param.h>
404b88c807SRodney W. Grimes #include <sys/time.h>
414b88c807SRodney W. Grimes #include <sys/resource.h>
424b88c807SRodney W. Grimes #include <sys/proc.h>
434b88c807SRodney W. Grimes #include <sys/stat.h>
444b88c807SRodney W. Grimes 
452af538ebSRobert Watson #include <sys/mac.h>
468b9b0e39SPoul-Henning Kamp #include <sys/user.h>
474b88c807SRodney W. Grimes #include <sys/sysctl.h>
4850a57dfbSKonstantin Belousov #include <sys/vmmeter.h>
494b88c807SRodney W. Grimes 
504b88c807SRodney W. Grimes #include <err.h>
51576541a9SWarner Losh #include <grp.h>
52f59105eeSAndrey A. Chernov #include <langinfo.h>
538073a93cSAndrey A. Chernov #include <locale.h>
544b88c807SRodney W. Grimes #include <math.h>
554b88c807SRodney W. Grimes #include <nlist.h>
56576541a9SWarner Losh #include <pwd.h>
574b88c807SRodney W. Grimes #include <stddef.h>
5841ded75dSJuli Mallett #include <stdint.h>
594b88c807SRodney W. Grimes #include <stdio.h>
604b88c807SRodney W. Grimes #include <stdlib.h>
614b88c807SRodney W. Grimes #include <string.h>
62871e8d8cSMark Murray #include <unistd.h>
634b88c807SRodney W. Grimes #include <vis.h>
644b88c807SRodney W. Grimes 
654b88c807SRodney W. Grimes #include "ps.h"
664b88c807SRodney W. Grimes 
676327ab9cSPeter Wemm #define	ps_pgtok(a)	(((a) * getpagesize()) / 1024)
68871e8d8cSMark Murray 
694b88c807SRodney W. Grimes void
7046251ddeSWarner Losh printheader(void)
714b88c807SRodney W. Grimes {
724b88c807SRodney W. Grimes 	VAR *v;
734b88c807SRodney W. Grimes 	struct varent *vent;
744b88c807SRodney W. Grimes 
75bdf8ab46SGarance A Drosehn 	STAILQ_FOREACH(vent, &varlist, next_ve)
76bdf8ab46SGarance A Drosehn 		if (*vent->header != '\0')
7701e5f166STim J. Robbins 			break;
78bdf8ab46SGarance A Drosehn 	if (!vent)
7901e5f166STim J. Robbins 		return;
80bdf8ab46SGarance A Drosehn 
81bdf8ab46SGarance A Drosehn 	STAILQ_FOREACH(vent, &varlist, next_ve) {
824b88c807SRodney W. Grimes 		v = vent->var;
834b88c807SRodney W. Grimes 		if (v->flag & LJUST) {
84bdf8ab46SGarance A Drosehn 			if (STAILQ_NEXT(vent, next_ve) == NULL)	/* last one */
8578b1878aSJuli Mallett 				(void)printf("%s", vent->header);
864b88c807SRodney W. Grimes 			else
8778b1878aSJuli Mallett 				(void)printf("%-*s", v->width, vent->header);
884b88c807SRodney W. Grimes 		} else
8978b1878aSJuli Mallett 			(void)printf("%*s", v->width, vent->header);
90bdf8ab46SGarance A Drosehn 		if (STAILQ_NEXT(vent, next_ve) != NULL)
914b88c807SRodney W. Grimes 			(void)putchar(' ');
924b88c807SRodney W. Grimes 	}
934b88c807SRodney W. Grimes 	(void)putchar('\n');
944b88c807SRodney W. Grimes }
954b88c807SRodney W. Grimes 
964b88c807SRodney W. Grimes void
9703334017SJuli Mallett arguments(KINFO *k, VARENT *ve)
9803334017SJuli Mallett {
9903334017SJuli Mallett 	VAR *v;
10003334017SJuli Mallett 	int left;
10103334017SJuli Mallett 	char *cp, *vis_args;
10203334017SJuli Mallett 
10303334017SJuli Mallett 	v = ve->var;
10403334017SJuli Mallett 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
10503334017SJuli Mallett 		errx(1, "malloc failed");
10603334017SJuli Mallett 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
107bdf8ab46SGarance A Drosehn 	if (STAILQ_NEXT(ve, next_ve) == NULL) {
10803334017SJuli Mallett 		/* last field */
10903334017SJuli Mallett 		if (termwidth == UNLIMITED) {
11003334017SJuli Mallett 			(void)printf("%s", vis_args);
11103334017SJuli Mallett 		} else {
11203334017SJuli Mallett 			left = termwidth - (totwidth - v->width);
11303334017SJuli Mallett 			if (left < 1) /* already wrapped, just use std width */
11403334017SJuli Mallett 				left = v->width;
11503334017SJuli Mallett 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
11603334017SJuli Mallett 				(void)putchar(*cp++);
11703334017SJuli Mallett 		}
11803334017SJuli Mallett 	} else {
11903334017SJuli Mallett 		(void)printf("%-*.*s", v->width, v->width, vis_args);
12003334017SJuli Mallett 	}
12103334017SJuli Mallett 	free(vis_args);
12203334017SJuli Mallett }
12303334017SJuli Mallett 
12403334017SJuli Mallett void
12546251ddeSWarner Losh command(KINFO *k, VARENT *ve)
1264b88c807SRodney W. Grimes {
1274b88c807SRodney W. Grimes 	VAR *v;
1284b88c807SRodney W. Grimes 	int left;
1294b88c807SRodney W. Grimes 	char *cp, *vis_env, *vis_args;
1304b88c807SRodney W. Grimes 
131db91faacSPeter Wemm 	v = ve->var;
132db91faacSPeter Wemm 	if (cflag) {
133bdf8ab46SGarance A Drosehn 		/* If it is the last field, then don't pad */
1347ab24ea3SJulian Elischer 		if (STAILQ_NEXT(ve, next_ve) == NULL) {
135044fce53SBrian Somers 			if (k->ki_d.prefix)
136044fce53SBrian Somers 				(void)printf("%s", k->ki_d.prefix);
1371f7d2501SKirk McKusick 			(void)printf("%s", k->ki_p->ki_comm);
1387ab24ea3SJulian Elischer 			if (showthreads && k->ki_p->ki_numthreads > 1)
139044fce53SBrian Somers 				(void)printf("/%s", k->ki_p->ki_ocomm);
1407ab24ea3SJulian Elischer 		} else
1411f7d2501SKirk McKusick 			(void)printf("%-*s", v->width, k->ki_p->ki_comm);
142db91faacSPeter Wemm 		return;
143db91faacSPeter Wemm 	}
1444b88c807SRodney W. Grimes 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
1454fa7d788SJuli Mallett 		errx(1, "malloc failed");
1464b88c807SRodney W. Grimes 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
1474b88c807SRodney W. Grimes 
148bdf8ab46SGarance A Drosehn 	if (STAILQ_NEXT(ve, next_ve) == NULL) {
1494b88c807SRodney W. Grimes 		/* last field */
150044fce53SBrian Somers 
151044fce53SBrian Somers 		if (k->ki_env) {
152044fce53SBrian Somers 			if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1))
153044fce53SBrian Somers 			    == NULL)
154044fce53SBrian Somers 				errx(1, "malloc failed");
155044fce53SBrian Somers 			strvis(vis_env, k->ki_env,
156044fce53SBrian Somers 			    VIS_TAB | VIS_NL | VIS_NOSLASH);
157044fce53SBrian Somers 		} else
158044fce53SBrian Somers 			vis_env = NULL;
159044fce53SBrian Somers 
1604b88c807SRodney W. Grimes 		if (termwidth == UNLIMITED) {
161044fce53SBrian Somers 			if (k->ki_d.prefix)
162044fce53SBrian Somers 				(void)printf("%s", k->ki_d.prefix);
1634b88c807SRodney W. Grimes 			if (vis_env)
1644b88c807SRodney W. Grimes 				(void)printf("%s ", vis_env);
1654b88c807SRodney W. Grimes 			(void)printf("%s", vis_args);
1664b88c807SRodney W. Grimes 		} else {
1674b88c807SRodney W. Grimes 			left = termwidth - (totwidth - v->width);
1684b88c807SRodney W. Grimes 			if (left < 1) /* already wrapped, just use std width */
1694b88c807SRodney W. Grimes 				left = v->width;
170044fce53SBrian Somers 			if ((cp = k->ki_d.prefix) != NULL)
171044fce53SBrian Somers 				while (--left >= 0 && *cp)
172044fce53SBrian Somers 					(void)putchar(*cp++);
1734b88c807SRodney W. Grimes 			if ((cp = vis_env) != NULL) {
1744b88c807SRodney W. Grimes 				while (--left >= 0 && *cp)
1754b88c807SRodney W. Grimes 					(void)putchar(*cp++);
1764b88c807SRodney W. Grimes 				if (--left >= 0)
1774b88c807SRodney W. Grimes 					putchar(' ');
1784b88c807SRodney W. Grimes 			}
1794b88c807SRodney W. Grimes 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
1804b88c807SRodney W. Grimes 				(void)putchar(*cp++);
1814b88c807SRodney W. Grimes 		}
1824b88c807SRodney W. Grimes 		if (vis_env != NULL)
1834b88c807SRodney W. Grimes 			free(vis_env);
184044fce53SBrian Somers 	} else
185044fce53SBrian Somers 		/* ki_d.prefix & ki_env aren't shown for interim fields */
186044fce53SBrian Somers 		(void)printf("%-*.*s", v->width, v->width, vis_args);
187044fce53SBrian Somers 	free(vis_args);
1884b88c807SRodney W. Grimes }
1894b88c807SRodney W. Grimes 
1904b88c807SRodney W. Grimes void
19146251ddeSWarner Losh ucomm(KINFO *k, VARENT *ve)
1924b88c807SRodney W. Grimes {
1934610a811SAttilio Rao 	char tmpbuff[COMMLEN + OCOMMLEN + 2];
1944b88c807SRodney W. Grimes 	VAR *v;
1954b88c807SRodney W. Grimes 
1964b88c807SRodney W. Grimes 	v = ve->var;
1977ab24ea3SJulian Elischer 	if (STAILQ_NEXT(ve, next_ve) == NULL) {	/* last field, don't pad */
198044fce53SBrian Somers 		if (k->ki_d.prefix)
199044fce53SBrian Somers 			(void)printf("%s", k->ki_d.prefix);
2004e8a8d9fSGarance A Drosehn 		(void)printf("%s", k->ki_p->ki_comm);
2017ab24ea3SJulian Elischer 		if (showthreads && k->ki_p->ki_numthreads > 1)
2027ab24ea3SJulian Elischer 			printf("/%s", k->ki_p->ki_ocomm);
2034610a811SAttilio Rao 	} else {
2044610a811SAttilio Rao 		bzero(tmpbuff, sizeof(tmpbuff));
2054610a811SAttilio Rao 		if (showthreads && k->ki_p->ki_numthreads > 1)
2064610a811SAttilio Rao 			sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
2074610a811SAttilio Rao 			    k->ki_p->ki_ocomm);
2084610a811SAttilio Rao 		else
2094610a811SAttilio Rao 			sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
2104610a811SAttilio Rao 		(void)printf("%-*s", v->width, tmpbuff);
2114610a811SAttilio Rao 	}
2124b88c807SRodney W. Grimes }
2134b88c807SRodney W. Grimes 
2144b88c807SRodney W. Grimes void
2157ab24ea3SJulian Elischer tdnam(KINFO *k, VARENT *ve)
2167ab24ea3SJulian Elischer {
2177ab24ea3SJulian Elischer 	VAR *v;
2187ab24ea3SJulian Elischer 
2197ab24ea3SJulian Elischer 	v = ve->var;
2207ab24ea3SJulian Elischer 	if (showthreads && k->ki_p->ki_numthreads > 1)
2217ab24ea3SJulian Elischer 		(void)printf("%-*s", v->width, k->ki_p->ki_ocomm);
2227ab24ea3SJulian Elischer 	else
2237ab24ea3SJulian Elischer 		(void)printf("%-*s", v->width, "      ");
2247ab24ea3SJulian Elischer }
2257ab24ea3SJulian Elischer 
2267ab24ea3SJulian Elischer void
22746251ddeSWarner Losh logname(KINFO *k, VARENT *ve)
2284b88c807SRodney W. Grimes {
2294b88c807SRodney W. Grimes 	VAR *v;
230ad863cacSSteve Price 	char *s;
2314b88c807SRodney W. Grimes 
2324b88c807SRodney W. Grimes 	v = ve->var;
2331f7d2501SKirk McKusick 	(void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
2344b88c807SRodney W. Grimes }
2354b88c807SRodney W. Grimes 
2364b88c807SRodney W. Grimes void
23746251ddeSWarner Losh state(KINFO *k, VARENT *ve)
2384b88c807SRodney W. Grimes {
239b61ce5b0SJeff Roberson 	int flag, tdflags;
2404b88c807SRodney W. Grimes 	char *cp;
2414b88c807SRodney W. Grimes 	VAR *v;
2424b88c807SRodney W. Grimes 	char buf[16];
2434b88c807SRodney W. Grimes 
2444b88c807SRodney W. Grimes 	v = ve->var;
2451f7d2501SKirk McKusick 	flag = k->ki_p->ki_flag;
246b40ce416SJulian Elischer 	tdflags = k->ki_p->ki_tdflags;	/* XXXKSE */
2474b88c807SRodney W. Grimes 	cp = buf;
2484b88c807SRodney W. Grimes 
2491f7d2501SKirk McKusick 	switch (k->ki_p->ki_stat) {
2504b88c807SRodney W. Grimes 
2514b88c807SRodney W. Grimes 	case SSTOP:
2524b88c807SRodney W. Grimes 		*cp = 'T';
2534b88c807SRodney W. Grimes 		break;
2544b88c807SRodney W. Grimes 
2554b88c807SRodney W. Grimes 	case SSLEEP:
256b40ce416SJulian Elischer 		if (tdflags & TDF_SINTR)	/* interruptable (long) */
2571f7d2501SKirk McKusick 			*cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
2584b88c807SRodney W. Grimes 		else
2594b88c807SRodney W. Grimes 			*cp = 'D';
2604b88c807SRodney W. Grimes 		break;
2614b88c807SRodney W. Grimes 
2624b88c807SRodney W. Grimes 	case SRUN:
2634b88c807SRodney W. Grimes 	case SIDL:
2644b88c807SRodney W. Grimes 		*cp = 'R';
2654b88c807SRodney W. Grimes 		break;
2664b88c807SRodney W. Grimes 
2670384fff8SJason Evans 	case SWAIT:
2680384fff8SJason Evans 		*cp = 'W';
2690384fff8SJason Evans 		break;
2700384fff8SJason Evans 
2710d632649SJohn Baldwin 	case SLOCK:
2720d632649SJohn Baldwin 		*cp = 'L';
2730384fff8SJason Evans 		break;
2740384fff8SJason Evans 
2754b88c807SRodney W. Grimes 	case SZOMB:
2764b88c807SRodney W. Grimes 		*cp = 'Z';
2774b88c807SRodney W. Grimes 		break;
2784b88c807SRodney W. Grimes 
2794b88c807SRodney W. Grimes 	default:
2804b88c807SRodney W. Grimes 		*cp = '?';
2814b88c807SRodney W. Grimes 	}
2824b88c807SRodney W. Grimes 	cp++;
283b61ce5b0SJeff Roberson 	if (!(flag & P_INMEM))
2844b88c807SRodney W. Grimes 		*cp++ = 'W';
2851f7d2501SKirk McKusick 	if (k->ki_p->ki_nice < NZERO)
2864b88c807SRodney W. Grimes 		*cp++ = '<';
2871f7d2501SKirk McKusick 	else if (k->ki_p->ki_nice > NZERO)
2884b88c807SRodney W. Grimes 		*cp++ = 'N';
2894b88c807SRodney W. Grimes 	if (flag & P_TRACED)
2904b88c807SRodney W. Grimes 		*cp++ = 'X';
2911f7d2501SKirk McKusick 	if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
2924b88c807SRodney W. Grimes 		*cp++ = 'E';
2934b88c807SRodney W. Grimes 	if (flag & P_PPWAIT)
2944b88c807SRodney W. Grimes 		*cp++ = 'V';
2951f7d2501SKirk McKusick 	if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
2964b88c807SRodney W. Grimes 		*cp++ = 'L';
2971f7d2501SKirk McKusick 	if (k->ki_p->ki_kiflag & KI_SLEADER)
2984b88c807SRodney W. Grimes 		*cp++ = 's';
2991f7d2501SKirk McKusick 	if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
3004b88c807SRodney W. Grimes 		*cp++ = '+';
30175c13541SPoul-Henning Kamp 	if (flag & P_JAILED)
30275c13541SPoul-Henning Kamp 		*cp++ = 'J';
3034b88c807SRodney W. Grimes 	*cp = '\0';
3044b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
3054b88c807SRodney W. Grimes }
3064b88c807SRodney W. Grimes 
307820434b2SGarance A Drosehn #define	scalepri(x)	((x) - PZERO)
308820434b2SGarance A Drosehn 
3094b88c807SRodney W. Grimes void
31046251ddeSWarner Losh pri(KINFO *k, VARENT *ve)
3114b88c807SRodney W. Grimes {
3124b88c807SRodney W. Grimes 	VAR *v;
3134b88c807SRodney W. Grimes 
3144b88c807SRodney W. Grimes 	v = ve->var;
315820434b2SGarance A Drosehn 	(void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level));
3164b88c807SRodney W. Grimes }
3174b88c807SRodney W. Grimes 
3184b88c807SRodney W. Grimes void
319820434b2SGarance A Drosehn upr(KINFO *k, VARENT *ve)
320820434b2SGarance A Drosehn {
321820434b2SGarance A Drosehn 	VAR *v;
322820434b2SGarance A Drosehn 
323820434b2SGarance A Drosehn 	v = ve->var;
324820434b2SGarance A Drosehn 	(void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user));
325820434b2SGarance A Drosehn }
326820434b2SGarance A Drosehn #undef scalepri
327820434b2SGarance A Drosehn 
328820434b2SGarance A Drosehn void
32946251ddeSWarner Losh uname(KINFO *k, VARENT *ve)
3304b88c807SRodney W. Grimes {
3314b88c807SRodney W. Grimes 	VAR *v;
3324b88c807SRodney W. Grimes 
3334b88c807SRodney W. Grimes 	v = ve->var;
334e8eef4bbSJuli Mallett 	(void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
3354b88c807SRodney W. Grimes }
3364b88c807SRodney W. Grimes 
3376a2d726bSJordan K. Hubbard int
33846251ddeSWarner Losh s_uname(KINFO *k)
3396a2d726bSJordan K. Hubbard {
3401f7d2501SKirk McKusick 	return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
3416a2d726bSJordan K. Hubbard }
3426a2d726bSJordan K. Hubbard 
3434b88c807SRodney W. Grimes void
344e8eef4bbSJuli Mallett rgroupname(KINFO *k, VARENT *ve)
345e8eef4bbSJuli Mallett {
346e8eef4bbSJuli Mallett 	VAR *v;
347e8eef4bbSJuli Mallett 
348e8eef4bbSJuli Mallett 	v = ve->var;
349e8eef4bbSJuli Mallett 	(void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
350e8eef4bbSJuli Mallett }
351e8eef4bbSJuli Mallett 
352e8eef4bbSJuli Mallett int
353e8eef4bbSJuli Mallett s_rgroupname(KINFO *k)
354e8eef4bbSJuli Mallett {
355e8eef4bbSJuli Mallett 	return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
356e8eef4bbSJuli Mallett }
357e8eef4bbSJuli Mallett 
358e8eef4bbSJuli Mallett void
35946251ddeSWarner Losh runame(KINFO *k, VARENT *ve)
3604b88c807SRodney W. Grimes {
3614b88c807SRodney W. Grimes 	VAR *v;
3624b88c807SRodney W. Grimes 
3634b88c807SRodney W. Grimes 	v = ve->var;
364e8eef4bbSJuli Mallett 	(void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
3654b88c807SRodney W. Grimes }
3664b88c807SRodney W. Grimes 
3676a2d726bSJordan K. Hubbard int
36846251ddeSWarner Losh s_runame(KINFO *k)
3696a2d726bSJordan K. Hubbard {
3701f7d2501SKirk McKusick 	return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
3716a2d726bSJordan K. Hubbard }
3726a2d726bSJordan K. Hubbard 
373f3073b05SJuli Mallett 
374f3073b05SJuli Mallett void
37546251ddeSWarner Losh tdev(KINFO *k, VARENT *ve)
3764b88c807SRodney W. Grimes {
3774b88c807SRodney W. Grimes 	VAR *v;
3784b88c807SRodney W. Grimes 	dev_t dev;
3794b88c807SRodney W. Grimes 	char buff[16];
3804b88c807SRodney W. Grimes 
3814b88c807SRodney W. Grimes 	v = ve->var;
3821f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
3834b88c807SRodney W. Grimes 	if (dev == NODEV)
3844b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "??");
3854b88c807SRodney W. Grimes 	else {
3864b88c807SRodney W. Grimes 		(void)snprintf(buff, sizeof(buff),
3874b88c807SRodney W. Grimes 		    "%d/%d", major(dev), minor(dev));
3884b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, buff);
3894b88c807SRodney W. Grimes 	}
3904b88c807SRodney W. Grimes }
3914b88c807SRodney W. Grimes 
3924b88c807SRodney W. Grimes void
39346251ddeSWarner Losh tname(KINFO *k, VARENT *ve)
3944b88c807SRodney W. Grimes {
3954b88c807SRodney W. Grimes 	VAR *v;
3964b88c807SRodney W. Grimes 	dev_t dev;
3974b88c807SRodney W. Grimes 	char *ttname;
3984b88c807SRodney W. Grimes 
3994b88c807SRodney W. Grimes 	v = ve->var;
4001f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
4014b88c807SRodney W. Grimes 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
402c55931c7SPeter Wemm 		(void)printf("%*s ", v->width - 1, "??");
4034b88c807SRodney W. Grimes 	else {
404efc18e2cSAndrey A. Chernov 		if (strncmp(ttname, "tty", 3) == 0 ||
405efc18e2cSAndrey A. Chernov 		    strncmp(ttname, "cua", 3) == 0)
4064b88c807SRodney W. Grimes 			ttname += 3;
4077bd5296dSOlivier Houchard 		if (strncmp(ttname, "pts/", 4) == 0)
4087bd5296dSOlivier Houchard 			ttname += 4;
409c55931c7SPeter Wemm 		(void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname,
4101f7d2501SKirk McKusick 		    k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
4114b88c807SRodney W. Grimes 	}
4124b88c807SRodney W. Grimes }
4134b88c807SRodney W. Grimes 
4144b88c807SRodney W. Grimes void
41546251ddeSWarner Losh longtname(KINFO *k, VARENT *ve)
4164b88c807SRodney W. Grimes {
4174b88c807SRodney W. Grimes 	VAR *v;
4184b88c807SRodney W. Grimes 	dev_t dev;
4194b88c807SRodney W. Grimes 	char *ttname;
4204b88c807SRodney W. Grimes 
4214b88c807SRodney W. Grimes 	v = ve->var;
4221f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
4234b88c807SRodney W. Grimes 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
4244b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "??");
4254b88c807SRodney W. Grimes 	else
4264b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, ttname);
4274b88c807SRodney W. Grimes }
4284b88c807SRodney W. Grimes 
4294b88c807SRodney W. Grimes void
43046251ddeSWarner Losh started(KINFO *k, VARENT *ve)
4314b88c807SRodney W. Grimes {
4324b88c807SRodney W. Grimes 	VAR *v;
433f8ec5fe2SBruce Evans 	time_t then;
4344b88c807SRodney W. Grimes 	struct tm *tp;
435f59105eeSAndrey A. Chernov 	static int use_ampm = -1;
436b85add5fSPhilippe Charnier 	char buf[100];
4374b88c807SRodney W. Grimes 
4384b88c807SRodney W. Grimes 	v = ve->var;
4391f7d2501SKirk McKusick 	if (!k->ki_valid) {
4404b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
4414b88c807SRodney W. Grimes 		return;
4424b88c807SRodney W. Grimes 	}
443f59105eeSAndrey A. Chernov 	if (use_ampm < 0)
444f59105eeSAndrey A. Chernov 		use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
4451f7d2501SKirk McKusick 	then = k->ki_p->ki_start.tv_sec;
446f8ec5fe2SBruce Evans 	tp = localtime(&then);
4471f7d2501SKirk McKusick 	if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
448b85add5fSPhilippe Charnier 		(void)strftime(buf, sizeof(buf),
44908017519SAndrey A. Chernov 		    use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
4501f7d2501SKirk McKusick 	} else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
451b85add5fSPhilippe Charnier 		(void)strftime(buf, sizeof(buf),
45208017519SAndrey A. Chernov 		    use_ampm ? "%a%I%p" : "%a%H  ", tp);
4534b88c807SRodney W. Grimes 	} else
454b85add5fSPhilippe Charnier 		(void)strftime(buf, sizeof(buf), "%e%b%y", tp);
4554b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
4564b88c807SRodney W. Grimes }
4574b88c807SRodney W. Grimes 
4584b88c807SRodney W. Grimes void
45946251ddeSWarner Losh lstarted(KINFO *k, VARENT *ve)
4604b88c807SRodney W. Grimes {
4614b88c807SRodney W. Grimes 	VAR *v;
462f8ec5fe2SBruce Evans 	time_t then;
4634b88c807SRodney W. Grimes 	char buf[100];
4644b88c807SRodney W. Grimes 
4654b88c807SRodney W. Grimes 	v = ve->var;
4661f7d2501SKirk McKusick 	if (!k->ki_valid) {
4674b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
4684b88c807SRodney W. Grimes 		return;
4694b88c807SRodney W. Grimes 	}
4701f7d2501SKirk McKusick 	then = k->ki_p->ki_start.tv_sec;
471b85add5fSPhilippe Charnier 	(void)strftime(buf, sizeof(buf), "%c", localtime(&then));
4724b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
4734b88c807SRodney W. Grimes }
4744b88c807SRodney W. Grimes 
4754b88c807SRodney W. Grimes void
4760d632649SJohn Baldwin lockname(KINFO *k, VARENT *ve)
477fd5f30bfSJohn Baldwin {
478fd5f30bfSJohn Baldwin 	VAR *v;
479fd5f30bfSJohn Baldwin 
480fd5f30bfSJohn Baldwin 	v = ve->var;
4810d632649SJohn Baldwin 	if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
4820d632649SJohn Baldwin 		if (k->ki_p->ki_lockname[0] != 0)
483fd5f30bfSJohn Baldwin 			(void)printf("%-*.*s", v->width, v->width,
4840d632649SJohn Baldwin 			    k->ki_p->ki_lockname);
485fd5f30bfSJohn Baldwin 		else
486fd5f30bfSJohn Baldwin 			(void)printf("%-*s", v->width, "???");
487fd5f30bfSJohn Baldwin 	} else
488fd5f30bfSJohn Baldwin 		(void)printf("%-*s", v->width, "-");
489fd5f30bfSJohn Baldwin }
490fd5f30bfSJohn Baldwin 
491fd5f30bfSJohn Baldwin void
49246251ddeSWarner Losh wchan(KINFO *k, VARENT *ve)
4934b88c807SRodney W. Grimes {
4944b88c807SRodney W. Grimes 	VAR *v;
4954b88c807SRodney W. Grimes 
4964b88c807SRodney W. Grimes 	v = ve->var;
4971f7d2501SKirk McKusick 	if (k->ki_p->ki_wchan) {
4981f7d2501SKirk McKusick 		if (k->ki_p->ki_wmesg[0] != 0)
4994b88c807SRodney W. Grimes 			(void)printf("%-*.*s", v->width, v->width,
5001f7d2501SKirk McKusick 			    k->ki_p->ki_wmesg);
5014b88c807SRodney W. Grimes 		else
5023929d518SDoug Rabson 			(void)printf("%-*lx", v->width,
5037d1192a7SPeter Wemm 			    (long)k->ki_p->ki_wchan);
504b85add5fSPhilippe Charnier 	} else
505d9a5f890SMatthew Dillon 		(void)printf("%-*s", v->width, "-");
506d9a5f890SMatthew Dillon }
507d9a5f890SMatthew Dillon 
508d9a5f890SMatthew Dillon void
509de244df7SHartmut Brandt nwchan(KINFO *k, VARENT *ve)
510de244df7SHartmut Brandt {
511de244df7SHartmut Brandt 	VAR *v;
512de244df7SHartmut Brandt 
513de244df7SHartmut Brandt 	v = ve->var;
514de244df7SHartmut Brandt 	if (k->ki_p->ki_wchan) {
515de244df7SHartmut Brandt 		(void)printf("%0*lx", v->width,
516de244df7SHartmut Brandt 		    (long)k->ki_p->ki_wchan);
517de244df7SHartmut Brandt 	} else
518de244df7SHartmut Brandt 		(void)printf("%-*s", v->width, "-");
519de244df7SHartmut Brandt }
520de244df7SHartmut Brandt 
521de244df7SHartmut Brandt void
522d9a5f890SMatthew Dillon mwchan(KINFO *k, VARENT *ve)
523d9a5f890SMatthew Dillon {
524d9a5f890SMatthew Dillon 	VAR *v;
525d9a5f890SMatthew Dillon 
526d9a5f890SMatthew Dillon 	v = ve->var;
527d9a5f890SMatthew Dillon 	if (k->ki_p->ki_wchan) {
528d9a5f890SMatthew Dillon 		if (k->ki_p->ki_wmesg[0] != 0)
529d9a5f890SMatthew Dillon 			(void)printf("%-*.*s", v->width, v->width,
530d9a5f890SMatthew Dillon 			    k->ki_p->ki_wmesg);
531d9a5f890SMatthew Dillon 		else
532d9a5f890SMatthew Dillon 			(void)printf("%-*lx", v->width,
533d9a5f890SMatthew Dillon 			    (long)k->ki_p->ki_wchan);
5340d632649SJohn Baldwin 	} else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
5350d632649SJohn Baldwin 		if (k->ki_p->ki_lockname[0]) {
536d9a5f890SMatthew Dillon 			(void)printf("%-*.*s", v->width, v->width,
5370d632649SJohn Baldwin 			    k->ki_p->ki_lockname);
538b85add5fSPhilippe Charnier 		} else
539d9a5f890SMatthew Dillon 			(void)printf("%-*s", v->width, "???");
540b85add5fSPhilippe Charnier 	} else
5414b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
5424b88c807SRodney W. Grimes }
5434b88c807SRodney W. Grimes 
5444b88c807SRodney W. Grimes void
54546251ddeSWarner Losh vsize(KINFO *k, VARENT *ve)
5464b88c807SRodney W. Grimes {
5474b88c807SRodney W. Grimes 	VAR *v;
5484b88c807SRodney W. Grimes 
5494b88c807SRodney W. Grimes 	v = ve->var;
550c1f903a0SBruce Evans 	(void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
5514b88c807SRodney W. Grimes }
5524b88c807SRodney W. Grimes 
553*a870bf2cSKonstantin Belousov static void
554*a870bf2cSKonstantin Belousov printtime(KINFO *k, VARENT *ve, long secs, long psecs)
555*a870bf2cSKonstantin Belousov /* psecs is "parts" of a second. first micro, then centi */
5564b88c807SRodney W. Grimes {
5574b88c807SRodney W. Grimes 	VAR *v;
5584b88c807SRodney W. Grimes 	char obuff[128];
559b85add5fSPhilippe Charnier 	static char decimal_point;
5604b88c807SRodney W. Grimes 
561b85add5fSPhilippe Charnier 	if (decimal_point == '\0')
5628073a93cSAndrey A. Chernov 		decimal_point = localeconv()->decimal_point[0];
5634b88c807SRodney W. Grimes 	v = ve->var;
56416ac318dSGarance A Drosehn 	if (!k->ki_valid) {
5655832a752SBruce Evans 		secs = 0;
5665832a752SBruce Evans 		psecs = 0;
5674b88c807SRodney W. Grimes 	} else {
568*a870bf2cSKonstantin Belousov 		/* round and scale to 100's */
569*a870bf2cSKonstantin Belousov 		psecs = (psecs + 5000) / 10000;
570*a870bf2cSKonstantin Belousov 		secs += psecs / 100;
571*a870bf2cSKonstantin Belousov 		psecs = psecs % 100;
572*a870bf2cSKonstantin Belousov 	}
573*a870bf2cSKonstantin Belousov 	(void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
574*a870bf2cSKonstantin Belousov 	    secs / 60, secs % 60, decimal_point, psecs);
575*a870bf2cSKonstantin Belousov 	(void)printf("%*s", v->width, obuff);
576*a870bf2cSKonstantin Belousov }
577*a870bf2cSKonstantin Belousov 
578*a870bf2cSKonstantin Belousov void
579*a870bf2cSKonstantin Belousov cputime(KINFO *k, VARENT *ve)
580*a870bf2cSKonstantin Belousov {
581*a870bf2cSKonstantin Belousov 	long secs, psecs;
582*a870bf2cSKonstantin Belousov 
5834b88c807SRodney W. Grimes 	/*
5844b88c807SRodney W. Grimes 	 * This counts time spent handling interrupts.  We could
5854b88c807SRodney W. Grimes 	 * fix this, but it is not 100% trivial (and interrupt
5864b88c807SRodney W. Grimes 	 * time fractions only work on the sparc anyway).	XXX
5874b88c807SRodney W. Grimes 	 */
5881f7d2501SKirk McKusick 	secs = k->ki_p->ki_runtime / 1000000;
5891f7d2501SKirk McKusick 	psecs = k->ki_p->ki_runtime % 1000000;
5904b88c807SRodney W. Grimes 	if (sumrusage) {
5911f7d2501SKirk McKusick 		secs += k->ki_p->ki_childtime.tv_sec;
5921f7d2501SKirk McKusick 		psecs += k->ki_p->ki_childtime.tv_usec;
5934b88c807SRodney W. Grimes 	}
594*a870bf2cSKonstantin Belousov 	printtime(k, ve, secs, psecs);
5955832a752SBruce Evans }
596*a870bf2cSKonstantin Belousov 
597*a870bf2cSKonstantin Belousov void
598*a870bf2cSKonstantin Belousov systime(KINFO *k, VARENT *ve)
599*a870bf2cSKonstantin Belousov {
600*a870bf2cSKonstantin Belousov 	long secs, psecs;
601*a870bf2cSKonstantin Belousov 
602*a870bf2cSKonstantin Belousov 	secs = k->ki_p->ki_rusage.ru_stime.tv_sec;
603*a870bf2cSKonstantin Belousov 	psecs = k->ki_p->ki_rusage.ru_stime.tv_usec;
604*a870bf2cSKonstantin Belousov 	if (sumrusage) {
605*a870bf2cSKonstantin Belousov 		secs += k->ki_p->ki_childstime.tv_sec;
606*a870bf2cSKonstantin Belousov 		psecs += k->ki_p->ki_childstime.tv_usec;
607*a870bf2cSKonstantin Belousov 	}
608*a870bf2cSKonstantin Belousov 	printtime(k, ve, secs, psecs);
609*a870bf2cSKonstantin Belousov }
610*a870bf2cSKonstantin Belousov 
611*a870bf2cSKonstantin Belousov void
612*a870bf2cSKonstantin Belousov usertime(KINFO *k, VARENT *ve)
613*a870bf2cSKonstantin Belousov {
614*a870bf2cSKonstantin Belousov 	long secs, psecs;
615*a870bf2cSKonstantin Belousov 
616*a870bf2cSKonstantin Belousov 	secs = k->ki_p->ki_rusage.ru_utime.tv_sec;
617*a870bf2cSKonstantin Belousov 	psecs = k->ki_p->ki_rusage.ru_utime.tv_usec;
618*a870bf2cSKonstantin Belousov 	if (sumrusage) {
619*a870bf2cSKonstantin Belousov 		secs += k->ki_p->ki_childutime.tv_sec;
620*a870bf2cSKonstantin Belousov 		psecs += k->ki_p->ki_childutime.tv_usec;
621*a870bf2cSKonstantin Belousov 	}
622*a870bf2cSKonstantin Belousov 	printtime(k, ve, secs, psecs);
6234b88c807SRodney W. Grimes }
6244b88c807SRodney W. Grimes 
62576e1a9feSJuli Mallett void
62676e1a9feSJuli Mallett elapsed(KINFO *k, VARENT *ve)
62776e1a9feSJuli Mallett {
62876e1a9feSJuli Mallett 	VAR *v;
629b85add5fSPhilippe Charnier 	time_t val;
630b85add5fSPhilippe Charnier 	int days, hours, mins, secs;
63176e1a9feSJuli Mallett 	char obuff[128];
63276e1a9feSJuli Mallett 
63376e1a9feSJuli Mallett 	v = ve->var;
6346dcfa92eSJilles Tjoelker 	if (!k->ki_valid) {
6356dcfa92eSJilles Tjoelker 		(void)printf("%-*s", v->width, "-");
6366dcfa92eSJilles Tjoelker 		return;
6376dcfa92eSJilles Tjoelker 	}
638b85add5fSPhilippe Charnier 	val = now - k->ki_p->ki_start.tv_sec;
639b85add5fSPhilippe Charnier 	days = val / (24 * 60 * 60);
640b85add5fSPhilippe Charnier 	val %= 24 * 60 * 60;
641b85add5fSPhilippe Charnier 	hours = val / (60 * 60);
642b85add5fSPhilippe Charnier 	val %= 60 * 60;
643b85add5fSPhilippe Charnier 	mins = val / 60;
644b85add5fSPhilippe Charnier 	secs = val % 60;
645b85add5fSPhilippe Charnier 	if (days != 0)
646b85add5fSPhilippe Charnier 		(void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
647b85add5fSPhilippe Charnier 		    days, hours, mins, secs);
648b85add5fSPhilippe Charnier 	else if (hours != 0)
649b85add5fSPhilippe Charnier 		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
650b85add5fSPhilippe Charnier 		    hours, mins, secs);
651b85add5fSPhilippe Charnier 	else
652b85add5fSPhilippe Charnier 		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
65376e1a9feSJuli Mallett 	(void)printf("%*s", v->width, obuff);
65476e1a9feSJuli Mallett }
65576e1a9feSJuli Mallett 
65641ded75dSJuli Mallett void
65741ded75dSJuli Mallett elapseds(KINFO *k, VARENT *ve)
65841ded75dSJuli Mallett {
65941ded75dSJuli Mallett 	VAR *v;
66041ded75dSJuli Mallett 	time_t val;
66141ded75dSJuli Mallett 
66241ded75dSJuli Mallett 	v = ve->var;
66341ded75dSJuli Mallett 	if (!k->ki_valid) {
66441ded75dSJuli Mallett 		(void)printf("%-*s", v->width, "-");
66541ded75dSJuli Mallett 		return;
66641ded75dSJuli Mallett 	}
66741ded75dSJuli Mallett 	val = now - k->ki_p->ki_start.tv_sec;
66841ded75dSJuli Mallett 	(void)printf("%*jd", v->width, (intmax_t)val);
66941ded75dSJuli Mallett }
67041ded75dSJuli Mallett 
6714b88c807SRodney W. Grimes double
672871e8d8cSMark Murray getpcpu(const KINFO *k)
6734b88c807SRodney W. Grimes {
6744b88c807SRodney W. Grimes 	static int failure;
6754b88c807SRodney W. Grimes 
6764b88c807SRodney W. Grimes 	if (!nlistread)
6774b88c807SRodney W. Grimes 		failure = donlist();
6784b88c807SRodney W. Grimes 	if (failure)
6794b88c807SRodney W. Grimes 		return (0.0);
6804b88c807SRodney W. Grimes 
6814b88c807SRodney W. Grimes #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
6824b88c807SRodney W. Grimes 
6834b88c807SRodney W. Grimes 	/* XXX - I don't like this */
684b61ce5b0SJeff Roberson 	if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0)
6854b88c807SRodney W. Grimes 		return (0.0);
6864b88c807SRodney W. Grimes 	if (rawcpu)
6871f7d2501SKirk McKusick 		return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
6881f7d2501SKirk McKusick 	return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
6891f7d2501SKirk McKusick 		(1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
6904b88c807SRodney W. Grimes }
6914b88c807SRodney W. Grimes 
6924b88c807SRodney W. Grimes void
69346251ddeSWarner Losh pcpu(KINFO *k, VARENT *ve)
6944b88c807SRodney W. Grimes {
6954b88c807SRodney W. Grimes 	VAR *v;
6964b88c807SRodney W. Grimes 
6974b88c807SRodney W. Grimes 	v = ve->var;
6984b88c807SRodney W. Grimes 	(void)printf("%*.1f", v->width, getpcpu(k));
6994b88c807SRodney W. Grimes }
7004b88c807SRodney W. Grimes 
701871e8d8cSMark Murray static double
70246251ddeSWarner Losh getpmem(KINFO *k)
7034b88c807SRodney W. Grimes {
7044b88c807SRodney W. Grimes 	static int failure;
7054b88c807SRodney W. Grimes 	double fracmem;
7064b88c807SRodney W. Grimes 
7074b88c807SRodney W. Grimes 	if (!nlistread)
7084b88c807SRodney W. Grimes 		failure = donlist();
7094b88c807SRodney W. Grimes 	if (failure)
7104b88c807SRodney W. Grimes 		return (0.0);
7114b88c807SRodney W. Grimes 
712b61ce5b0SJeff Roberson 	if ((k->ki_p->ki_flag & P_INMEM) == 0)
7134b88c807SRodney W. Grimes 		return (0.0);
7144b88c807SRodney W. Grimes 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
7154b88c807SRodney W. Grimes 	/* XXX don't have info about shared */
71685012e7cSPeter Wemm 	fracmem = ((float)k->ki_p->ki_rssize) / mempages;
7174b88c807SRodney W. Grimes 	return (100.0 * fracmem);
7184b88c807SRodney W. Grimes }
7194b88c807SRodney W. Grimes 
7204b88c807SRodney W. Grimes void
72146251ddeSWarner Losh pmem(KINFO *k, VARENT *ve)
7224b88c807SRodney W. Grimes {
7234b88c807SRodney W. Grimes 	VAR *v;
7244b88c807SRodney W. Grimes 
7254b88c807SRodney W. Grimes 	v = ve->var;
7264b88c807SRodney W. Grimes 	(void)printf("%*.1f", v->width, getpmem(k));
7274b88c807SRodney W. Grimes }
7284b88c807SRodney W. Grimes 
7294b88c807SRodney W. Grimes void
73046251ddeSWarner Losh pagein(KINFO *k, VARENT *ve)
7314b88c807SRodney W. Grimes {
7324b88c807SRodney W. Grimes 	VAR *v;
7334b88c807SRodney W. Grimes 
7344b88c807SRodney W. Grimes 	v = ve->var;
7350fd510b7SJoerg Wunsch 	(void)printf("%*ld", v->width,
7361f7d2501SKirk McKusick 	    k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
7374b88c807SRodney W. Grimes }
7384b88c807SRodney W. Grimes 
739871e8d8cSMark Murray /* ARGSUSED */
7404b88c807SRodney W. Grimes void
741871e8d8cSMark Murray maxrss(KINFO *k __unused, VARENT *ve)
7424b88c807SRodney W. Grimes {
7434b88c807SRodney W. Grimes 	VAR *v;
7444b88c807SRodney W. Grimes 
7454b88c807SRodney W. Grimes 	v = ve->var;
746940cca66SPeter Wemm 	/* XXX not yet */
7474b88c807SRodney W. Grimes 	(void)printf("%*s", v->width, "-");
7484b88c807SRodney W. Grimes }
7494b88c807SRodney W. Grimes 
7504b88c807SRodney W. Grimes void
75146251ddeSWarner Losh priorityr(KINFO *k, VARENT *ve)
752ad863cacSSteve Price {
753ad863cacSSteve Price 	VAR *v;
754871e8d8cSMark Murray 	struct priority *lpri;
755ad863cacSSteve Price 	char str[8];
7564c85452bSJake Burkholder 	unsigned class, level;
757ad863cacSSteve Price 
758ad863cacSSteve Price 	v = ve->var;
7593998d222SGarance A Drosehn 	lpri = &k->ki_p->ki_pri;
760871e8d8cSMark Murray 	class = lpri->pri_class;
761871e8d8cSMark Murray 	level = lpri->pri_level;
7624c85452bSJake Burkholder 	switch (class) {
76370548f64SGarance A Drosehn 	case PRI_ITHD:
76470548f64SGarance A Drosehn 		snprintf(str, sizeof(str), "intr:%u", level);
76570548f64SGarance A Drosehn 		break;
7664c85452bSJake Burkholder 	case PRI_REALTIME:
7674c85452bSJake Burkholder 		snprintf(str, sizeof(str), "real:%u", level);
768ad863cacSSteve Price 		break;
7694c85452bSJake Burkholder 	case PRI_TIMESHARE:
770ad863cacSSteve Price 		strncpy(str, "normal", sizeof(str));
771ad863cacSSteve Price 		break;
7724c85452bSJake Burkholder 	case PRI_IDLE:
7734c85452bSJake Burkholder 		snprintf(str, sizeof(str), "idle:%u", level);
774ad863cacSSteve Price 		break;
775ad863cacSSteve Price 	default:
7764c85452bSJake Burkholder 		snprintf(str, sizeof(str), "%u:%u", class, level);
777ad863cacSSteve Price 		break;
778ad863cacSSteve Price 	}
779ad863cacSSteve Price 	str[sizeof(str) - 1] = '\0';
780ad863cacSSteve Price 	(void)printf("%*s", v->width, str);
781ad863cacSSteve Price }
782ad863cacSSteve Price 
7834b88c807SRodney W. Grimes /*
7844b88c807SRodney W. Grimes  * Generic output routines.  Print fields from various prototype
7854b88c807SRodney W. Grimes  * structures.
7864b88c807SRodney W. Grimes  */
7874b88c807SRodney W. Grimes static void
788ffe25988SJuli Mallett printval(void *bp, VAR *v)
7894b88c807SRodney W. Grimes {
7904b88c807SRodney W. Grimes 	static char ofmt[32] = "%";
791fdbec398SJuli Mallett 	const char *fcp;
792fdbec398SJuli Mallett 	char *cp;
7934b88c807SRodney W. Grimes 
7944b88c807SRodney W. Grimes 	cp = ofmt + 1;
7954b88c807SRodney W. Grimes 	fcp = v->fmt;
7964b88c807SRodney W. Grimes 	if (v->flag & LJUST)
7974b88c807SRodney W. Grimes 		*cp++ = '-';
7984b88c807SRodney W. Grimes 	*cp++ = '*';
7990fd510b7SJoerg Wunsch 	while ((*cp++ = *fcp++));
8004b88c807SRodney W. Grimes 
801e2c9ac69STim J. Robbins #define	CHKINF127(n)	(((n) > 127) && (v->flag & INF127) ? 127 : (n))
802e2c9ac69STim J. Robbins 
8034b88c807SRodney W. Grimes 	switch (v->type) {
8044b88c807SRodney W. Grimes 	case CHAR:
8054b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(char *)bp);
8064b88c807SRodney W. Grimes 		break;
8074b88c807SRodney W. Grimes 	case UCHAR:
8084b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_char *)bp);
8094b88c807SRodney W. Grimes 		break;
8104b88c807SRodney W. Grimes 	case SHORT:
8114b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(short *)bp);
8124b88c807SRodney W. Grimes 		break;
8134b88c807SRodney W. Grimes 	case USHORT:
8144b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_short *)bp);
8154b88c807SRodney W. Grimes 		break;
8163929d518SDoug Rabson 	case INT:
8173929d518SDoug Rabson 		(void)printf(ofmt, v->width, *(int *)bp);
8183929d518SDoug Rabson 		break;
8193929d518SDoug Rabson 	case UINT:
820e2c9ac69STim J. Robbins 		(void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
8213929d518SDoug Rabson 		break;
8224b88c807SRodney W. Grimes 	case LONG:
8234b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(long *)bp);
8244b88c807SRodney W. Grimes 		break;
8254b88c807SRodney W. Grimes 	case ULONG:
8264b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_long *)bp);
8274b88c807SRodney W. Grimes 		break;
8284b88c807SRodney W. Grimes 	case KPTR:
8297d1192a7SPeter Wemm 		(void)printf(ofmt, v->width, *(u_long *)bp);
8304b88c807SRodney W. Grimes 		break;
831362d62baSJuli Mallett 	case PGTOK:
832760bbf7dSJuli Mallett 		(void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
833760bbf7dSJuli Mallett 		break;
8344b88c807SRodney W. Grimes 	default:
8354b88c807SRodney W. Grimes 		errx(1, "unknown type %d", v->type);
8364b88c807SRodney W. Grimes 	}
8374b88c807SRodney W. Grimes }
8384b88c807SRodney W. Grimes 
8394b88c807SRodney W. Grimes void
84046251ddeSWarner Losh kvar(KINFO *k, VARENT *ve)
8414b88c807SRodney W. Grimes {
8424b88c807SRodney W. Grimes 	VAR *v;
8434b88c807SRodney W. Grimes 
8444b88c807SRodney W. Grimes 	v = ve->var;
8451f7d2501SKirk McKusick 	printval((char *)((char *)k->ki_p + v->off), v);
8464b88c807SRodney W. Grimes }
8474b88c807SRodney W. Grimes 
8484b88c807SRodney W. Grimes void
84946251ddeSWarner Losh rvar(KINFO *k, VARENT *ve)
8504b88c807SRodney W. Grimes {
8514b88c807SRodney W. Grimes 	VAR *v;
8524b88c807SRodney W. Grimes 
8534b88c807SRodney W. Grimes 	v = ve->var;
8541f7d2501SKirk McKusick 	if (k->ki_valid)
8551f7d2501SKirk McKusick 		printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
8564b88c807SRodney W. Grimes 	else
8574b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "-");
8584b88c807SRodney W. Grimes }
8597304f61fSBrian Feldman 
8607304f61fSBrian Feldman void
86115b87b53SGarance A Drosehn emulname(KINFO *k, VARENT *ve)
86215b87b53SGarance A Drosehn {
86315b87b53SGarance A Drosehn 	VAR *v;
86415b87b53SGarance A Drosehn 
86515b87b53SGarance A Drosehn 	v = ve->var;
86615b87b53SGarance A Drosehn 	printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
86715b87b53SGarance A Drosehn }
86815b87b53SGarance A Drosehn 
86915b87b53SGarance A Drosehn void
8702af538ebSRobert Watson label(KINFO *k, VARENT *ve)
8717304f61fSBrian Feldman {
8722af538ebSRobert Watson 	char *string;
873b85add5fSPhilippe Charnier 	VAR *v;
874775bba9fSJuli Mallett 	mac_t proclabel;
8752af538ebSRobert Watson 	int error;
8767304f61fSBrian Feldman 
8777304f61fSBrian Feldman 	v = ve->var;
8782af538ebSRobert Watson 	string = NULL;
879775bba9fSJuli Mallett 	if (mac_prepare_process_label(&proclabel) == -1) {
8802c61418dSTim J. Robbins 		warn("mac_prepare_process_label");
8812af538ebSRobert Watson 		goto out;
8822af538ebSRobert Watson 	}
883775bba9fSJuli Mallett 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
8842af538ebSRobert Watson 	if (error == 0) {
885775bba9fSJuli Mallett 		if (mac_to_text(proclabel, &string) == -1)
8862af538ebSRobert Watson 			string = NULL;
8872af538ebSRobert Watson 	}
888775bba9fSJuli Mallett 	mac_free(proclabel);
8892af538ebSRobert Watson out:
8902af538ebSRobert Watson 	if (string != NULL) {
8912af538ebSRobert Watson 		(void)printf("%-*s", v->width, string);
8922af538ebSRobert Watson 		free(string);
8932af538ebSRobert Watson 	} else
894ce1affa2SGarance A Drosehn 		(void)printf("%-*s", v->width, "  -");
8952af538ebSRobert Watson 	return;
8962af538ebSRobert Watson }
8972af538ebSRobert Watson 
8987123f4cdSEdward Tomasz Napierala void
8997123f4cdSEdward Tomasz Napierala loginclass(KINFO *k, VARENT *ve)
9007123f4cdSEdward Tomasz Napierala {
9017123f4cdSEdward Tomasz Napierala 	VAR *v;
9027123f4cdSEdward Tomasz Napierala 	char *s;
9037123f4cdSEdward Tomasz Napierala 
9047123f4cdSEdward Tomasz Napierala 	v = ve->var;
9057123f4cdSEdward Tomasz Napierala 	/*
9067123f4cdSEdward Tomasz Napierala 	 * Don't display login class for system processes;
9077123f4cdSEdward Tomasz Napierala 	 * login classes are used for resource limits,
9087123f4cdSEdward Tomasz Napierala 	 * and limits don't apply to system processes.
9097123f4cdSEdward Tomasz Napierala 	 */
9107123f4cdSEdward Tomasz Napierala 	if (k->ki_p->ki_flag & P_SYSTEM) {
9117123f4cdSEdward Tomasz Napierala 		(void)printf("%-*s", v->width, "  -");
9127123f4cdSEdward Tomasz Napierala 		return;
9137123f4cdSEdward Tomasz Napierala 	}
9147123f4cdSEdward Tomasz Napierala 	s = k->ki_p->ki_loginclass;
9157123f4cdSEdward Tomasz Napierala 	(void)printf("%-*s", v->width, *s ? s : "-");
9167123f4cdSEdward Tomasz Napierala }
9177123f4cdSEdward Tomasz Napierala 
9182af538ebSRobert Watson int
9194610a811SAttilio Rao s_comm(KINFO *k)
9204610a811SAttilio Rao {
9214610a811SAttilio Rao 	char tmpbuff[COMMLEN + OCOMMLEN + 2];
9224610a811SAttilio Rao 
9234610a811SAttilio Rao 	bzero(tmpbuff, sizeof(tmpbuff));
9244610a811SAttilio Rao 	if (showthreads && k->ki_p->ki_numthreads > 1)
9254610a811SAttilio Rao 		sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
9264610a811SAttilio Rao 		    k->ki_p->ki_ocomm);
9274610a811SAttilio Rao 	else
9284610a811SAttilio Rao 		sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
9294610a811SAttilio Rao 	return (strlen(tmpbuff));
9304610a811SAttilio Rao }
9314610a811SAttilio Rao 
9324610a811SAttilio Rao int
9332af538ebSRobert Watson s_label(KINFO *k)
9342af538ebSRobert Watson {
9352af538ebSRobert Watson 	char *string = NULL;
936775bba9fSJuli Mallett 	mac_t proclabel;
9372af538ebSRobert Watson 	int error, size = 0;
9382af538ebSRobert Watson 
939775bba9fSJuli Mallett 	if (mac_prepare_process_label(&proclabel) == -1) {
9402c61418dSTim J. Robbins 		warn("mac_prepare_process_label");
9412af538ebSRobert Watson 		return (0);
9422af538ebSRobert Watson 	}
943775bba9fSJuli Mallett 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
944775bba9fSJuli Mallett 	if (error == 0 && mac_to_text(proclabel, &string) == 0) {
9452af538ebSRobert Watson 		size = strlen(string);
9462af538ebSRobert Watson 		free(string);
9472af538ebSRobert Watson 	}
948775bba9fSJuli Mallett 	mac_free(proclabel);
9492af538ebSRobert Watson 	return (size);
9507304f61fSBrian Feldman }
951