xref: /freebsd/bin/ps/print.c (revision f9db2550b28ff7b5015f2628f172c4a123b5663d)
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 
553a870bf2cSKonstantin Belousov static void
554a870bf2cSKonstantin Belousov printtime(KINFO *k, VARENT *ve, long secs, long psecs)
555a870bf2cSKonstantin 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 {
568a870bf2cSKonstantin Belousov 		/* round and scale to 100's */
569a870bf2cSKonstantin Belousov 		psecs = (psecs + 5000) / 10000;
570a870bf2cSKonstantin Belousov 		secs += psecs / 100;
571a870bf2cSKonstantin Belousov 		psecs = psecs % 100;
572a870bf2cSKonstantin Belousov 	}
573*f9db2550SEdward Tomasz Napierala 	(void)snprintf(obuff, sizeof(obuff), "%ld:%02ld%c%02ld",
574a870bf2cSKonstantin Belousov 	    secs / 60, secs % 60, decimal_point, psecs);
575a870bf2cSKonstantin Belousov 	(void)printf("%*s", v->width, obuff);
576a870bf2cSKonstantin Belousov }
577a870bf2cSKonstantin Belousov 
578*f9db2550SEdward Tomasz Napierala static int
579*f9db2550SEdward Tomasz Napierala sizetime(long secs)
580*f9db2550SEdward Tomasz Napierala {
581*f9db2550SEdward Tomasz Napierala 
582*f9db2550SEdward Tomasz Napierala 	if (secs < 60)
583*f9db2550SEdward Tomasz Napierala 		return (7);
584*f9db2550SEdward Tomasz Napierala 	return (log10(secs / 60) + 7);
585*f9db2550SEdward Tomasz Napierala }
586*f9db2550SEdward Tomasz Napierala 
587a870bf2cSKonstantin Belousov void
588a870bf2cSKonstantin Belousov cputime(KINFO *k, VARENT *ve)
589a870bf2cSKonstantin Belousov {
590a870bf2cSKonstantin Belousov 	long secs, psecs;
591a870bf2cSKonstantin Belousov 
5924b88c807SRodney W. Grimes 	/*
5934b88c807SRodney W. Grimes 	 * This counts time spent handling interrupts.  We could
5944b88c807SRodney W. Grimes 	 * fix this, but it is not 100% trivial (and interrupt
5954b88c807SRodney W. Grimes 	 * time fractions only work on the sparc anyway).	XXX
5964b88c807SRodney W. Grimes 	 */
5971f7d2501SKirk McKusick 	secs = k->ki_p->ki_runtime / 1000000;
5981f7d2501SKirk McKusick 	psecs = k->ki_p->ki_runtime % 1000000;
5994b88c807SRodney W. Grimes 	if (sumrusage) {
6001f7d2501SKirk McKusick 		secs += k->ki_p->ki_childtime.tv_sec;
6011f7d2501SKirk McKusick 		psecs += k->ki_p->ki_childtime.tv_usec;
6024b88c807SRodney W. Grimes 	}
603a870bf2cSKonstantin Belousov 	printtime(k, ve, secs, psecs);
6045832a752SBruce Evans }
605a870bf2cSKonstantin Belousov 
606a870bf2cSKonstantin Belousov void
607a870bf2cSKonstantin Belousov systime(KINFO *k, VARENT *ve)
608a870bf2cSKonstantin Belousov {
609a870bf2cSKonstantin Belousov 	long secs, psecs;
610a870bf2cSKonstantin Belousov 
611a870bf2cSKonstantin Belousov 	secs = k->ki_p->ki_rusage.ru_stime.tv_sec;
612a870bf2cSKonstantin Belousov 	psecs = k->ki_p->ki_rusage.ru_stime.tv_usec;
613a870bf2cSKonstantin Belousov 	if (sumrusage) {
614a870bf2cSKonstantin Belousov 		secs += k->ki_p->ki_childstime.tv_sec;
615a870bf2cSKonstantin Belousov 		psecs += k->ki_p->ki_childstime.tv_usec;
616a870bf2cSKonstantin Belousov 	}
617a870bf2cSKonstantin Belousov 	printtime(k, ve, secs, psecs);
618a870bf2cSKonstantin Belousov }
619a870bf2cSKonstantin Belousov 
620a870bf2cSKonstantin Belousov void
621a870bf2cSKonstantin Belousov usertime(KINFO *k, VARENT *ve)
622a870bf2cSKonstantin Belousov {
623a870bf2cSKonstantin Belousov 	long secs, psecs;
624a870bf2cSKonstantin Belousov 
625a870bf2cSKonstantin Belousov 	secs = k->ki_p->ki_rusage.ru_utime.tv_sec;
626a870bf2cSKonstantin Belousov 	psecs = k->ki_p->ki_rusage.ru_utime.tv_usec;
627a870bf2cSKonstantin Belousov 	if (sumrusage) {
628a870bf2cSKonstantin Belousov 		secs += k->ki_p->ki_childutime.tv_sec;
629a870bf2cSKonstantin Belousov 		psecs += k->ki_p->ki_childutime.tv_usec;
630a870bf2cSKonstantin Belousov 	}
631a870bf2cSKonstantin Belousov 	printtime(k, ve, secs, psecs);
6324b88c807SRodney W. Grimes }
6334b88c807SRodney W. Grimes 
63476e1a9feSJuli Mallett void
63576e1a9feSJuli Mallett elapsed(KINFO *k, VARENT *ve)
63676e1a9feSJuli Mallett {
63776e1a9feSJuli Mallett 	VAR *v;
638b85add5fSPhilippe Charnier 	time_t val;
639b85add5fSPhilippe Charnier 	int days, hours, mins, secs;
64076e1a9feSJuli Mallett 	char obuff[128];
64176e1a9feSJuli Mallett 
64276e1a9feSJuli Mallett 	v = ve->var;
6436dcfa92eSJilles Tjoelker 	if (!k->ki_valid) {
6446dcfa92eSJilles Tjoelker 		(void)printf("%-*s", v->width, "-");
6456dcfa92eSJilles Tjoelker 		return;
6466dcfa92eSJilles Tjoelker 	}
647b85add5fSPhilippe Charnier 	val = now - k->ki_p->ki_start.tv_sec;
648b85add5fSPhilippe Charnier 	days = val / (24 * 60 * 60);
649b85add5fSPhilippe Charnier 	val %= 24 * 60 * 60;
650b85add5fSPhilippe Charnier 	hours = val / (60 * 60);
651b85add5fSPhilippe Charnier 	val %= 60 * 60;
652b85add5fSPhilippe Charnier 	mins = val / 60;
653b85add5fSPhilippe Charnier 	secs = val % 60;
654b85add5fSPhilippe Charnier 	if (days != 0)
655b85add5fSPhilippe Charnier 		(void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
656b85add5fSPhilippe Charnier 		    days, hours, mins, secs);
657b85add5fSPhilippe Charnier 	else if (hours != 0)
658b85add5fSPhilippe Charnier 		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
659b85add5fSPhilippe Charnier 		    hours, mins, secs);
660b85add5fSPhilippe Charnier 	else
661b85add5fSPhilippe Charnier 		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
66276e1a9feSJuli Mallett 	(void)printf("%*s", v->width, obuff);
66376e1a9feSJuli Mallett }
66476e1a9feSJuli Mallett 
66541ded75dSJuli Mallett void
66641ded75dSJuli Mallett elapseds(KINFO *k, VARENT *ve)
66741ded75dSJuli Mallett {
66841ded75dSJuli Mallett 	VAR *v;
66941ded75dSJuli Mallett 	time_t val;
67041ded75dSJuli Mallett 
67141ded75dSJuli Mallett 	v = ve->var;
67241ded75dSJuli Mallett 	if (!k->ki_valid) {
67341ded75dSJuli Mallett 		(void)printf("%-*s", v->width, "-");
67441ded75dSJuli Mallett 		return;
67541ded75dSJuli Mallett 	}
67641ded75dSJuli Mallett 	val = now - k->ki_p->ki_start.tv_sec;
67741ded75dSJuli Mallett 	(void)printf("%*jd", v->width, (intmax_t)val);
67841ded75dSJuli Mallett }
67941ded75dSJuli Mallett 
6804b88c807SRodney W. Grimes double
681871e8d8cSMark Murray getpcpu(const KINFO *k)
6824b88c807SRodney W. Grimes {
6834b88c807SRodney W. Grimes 	static int failure;
6844b88c807SRodney W. Grimes 
6854b88c807SRodney W. Grimes 	if (!nlistread)
6864b88c807SRodney W. Grimes 		failure = donlist();
6874b88c807SRodney W. Grimes 	if (failure)
6884b88c807SRodney W. Grimes 		return (0.0);
6894b88c807SRodney W. Grimes 
6904b88c807SRodney W. Grimes #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
6914b88c807SRodney W. Grimes 
6924b88c807SRodney W. Grimes 	/* XXX - I don't like this */
693b61ce5b0SJeff Roberson 	if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0)
6944b88c807SRodney W. Grimes 		return (0.0);
6954b88c807SRodney W. Grimes 	if (rawcpu)
6961f7d2501SKirk McKusick 		return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
6971f7d2501SKirk McKusick 	return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
6981f7d2501SKirk McKusick 		(1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
6994b88c807SRodney W. Grimes }
7004b88c807SRodney W. Grimes 
7014b88c807SRodney W. Grimes void
70246251ddeSWarner Losh pcpu(KINFO *k, VARENT *ve)
7034b88c807SRodney W. Grimes {
7044b88c807SRodney W. Grimes 	VAR *v;
7054b88c807SRodney W. Grimes 
7064b88c807SRodney W. Grimes 	v = ve->var;
7074b88c807SRodney W. Grimes 	(void)printf("%*.1f", v->width, getpcpu(k));
7084b88c807SRodney W. Grimes }
7094b88c807SRodney W. Grimes 
710871e8d8cSMark Murray static double
71146251ddeSWarner Losh getpmem(KINFO *k)
7124b88c807SRodney W. Grimes {
7134b88c807SRodney W. Grimes 	static int failure;
7144b88c807SRodney W. Grimes 	double fracmem;
7154b88c807SRodney W. Grimes 
7164b88c807SRodney W. Grimes 	if (!nlistread)
7174b88c807SRodney W. Grimes 		failure = donlist();
7184b88c807SRodney W. Grimes 	if (failure)
7194b88c807SRodney W. Grimes 		return (0.0);
7204b88c807SRodney W. Grimes 
721b61ce5b0SJeff Roberson 	if ((k->ki_p->ki_flag & P_INMEM) == 0)
7224b88c807SRodney W. Grimes 		return (0.0);
7234b88c807SRodney W. Grimes 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
7244b88c807SRodney W. Grimes 	/* XXX don't have info about shared */
72585012e7cSPeter Wemm 	fracmem = ((float)k->ki_p->ki_rssize) / mempages;
7264b88c807SRodney W. Grimes 	return (100.0 * fracmem);
7274b88c807SRodney W. Grimes }
7284b88c807SRodney W. Grimes 
7294b88c807SRodney W. Grimes void
73046251ddeSWarner Losh pmem(KINFO *k, VARENT *ve)
7314b88c807SRodney W. Grimes {
7324b88c807SRodney W. Grimes 	VAR *v;
7334b88c807SRodney W. Grimes 
7344b88c807SRodney W. Grimes 	v = ve->var;
7354b88c807SRodney W. Grimes 	(void)printf("%*.1f", v->width, getpmem(k));
7364b88c807SRodney W. Grimes }
7374b88c807SRodney W. Grimes 
7384b88c807SRodney W. Grimes void
73946251ddeSWarner Losh pagein(KINFO *k, VARENT *ve)
7404b88c807SRodney W. Grimes {
7414b88c807SRodney W. Grimes 	VAR *v;
7424b88c807SRodney W. Grimes 
7434b88c807SRodney W. Grimes 	v = ve->var;
7440fd510b7SJoerg Wunsch 	(void)printf("%*ld", v->width,
7451f7d2501SKirk McKusick 	    k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
7464b88c807SRodney W. Grimes }
7474b88c807SRodney W. Grimes 
748871e8d8cSMark Murray /* ARGSUSED */
7494b88c807SRodney W. Grimes void
750871e8d8cSMark Murray maxrss(KINFO *k __unused, VARENT *ve)
7514b88c807SRodney W. Grimes {
7524b88c807SRodney W. Grimes 	VAR *v;
7534b88c807SRodney W. Grimes 
7544b88c807SRodney W. Grimes 	v = ve->var;
755940cca66SPeter Wemm 	/* XXX not yet */
7564b88c807SRodney W. Grimes 	(void)printf("%*s", v->width, "-");
7574b88c807SRodney W. Grimes }
7584b88c807SRodney W. Grimes 
7594b88c807SRodney W. Grimes void
76046251ddeSWarner Losh priorityr(KINFO *k, VARENT *ve)
761ad863cacSSteve Price {
762ad863cacSSteve Price 	VAR *v;
763871e8d8cSMark Murray 	struct priority *lpri;
764ad863cacSSteve Price 	char str[8];
7654c85452bSJake Burkholder 	unsigned class, level;
766ad863cacSSteve Price 
767ad863cacSSteve Price 	v = ve->var;
7683998d222SGarance A Drosehn 	lpri = &k->ki_p->ki_pri;
769871e8d8cSMark Murray 	class = lpri->pri_class;
770871e8d8cSMark Murray 	level = lpri->pri_level;
7714c85452bSJake Burkholder 	switch (class) {
77270548f64SGarance A Drosehn 	case PRI_ITHD:
77370548f64SGarance A Drosehn 		snprintf(str, sizeof(str), "intr:%u", level);
77470548f64SGarance A Drosehn 		break;
7754c85452bSJake Burkholder 	case PRI_REALTIME:
7764c85452bSJake Burkholder 		snprintf(str, sizeof(str), "real:%u", level);
777ad863cacSSteve Price 		break;
7784c85452bSJake Burkholder 	case PRI_TIMESHARE:
779ad863cacSSteve Price 		strncpy(str, "normal", sizeof(str));
780ad863cacSSteve Price 		break;
7814c85452bSJake Burkholder 	case PRI_IDLE:
7824c85452bSJake Burkholder 		snprintf(str, sizeof(str), "idle:%u", level);
783ad863cacSSteve Price 		break;
784ad863cacSSteve Price 	default:
7854c85452bSJake Burkholder 		snprintf(str, sizeof(str), "%u:%u", class, level);
786ad863cacSSteve Price 		break;
787ad863cacSSteve Price 	}
788ad863cacSSteve Price 	str[sizeof(str) - 1] = '\0';
789ad863cacSSteve Price 	(void)printf("%*s", v->width, str);
790ad863cacSSteve Price }
791ad863cacSSteve Price 
7924b88c807SRodney W. Grimes /*
7934b88c807SRodney W. Grimes  * Generic output routines.  Print fields from various prototype
7944b88c807SRodney W. Grimes  * structures.
7954b88c807SRodney W. Grimes  */
7964b88c807SRodney W. Grimes static void
797ffe25988SJuli Mallett printval(void *bp, VAR *v)
7984b88c807SRodney W. Grimes {
7994b88c807SRodney W. Grimes 	static char ofmt[32] = "%";
800fdbec398SJuli Mallett 	const char *fcp;
801fdbec398SJuli Mallett 	char *cp;
8024b88c807SRodney W. Grimes 
8034b88c807SRodney W. Grimes 	cp = ofmt + 1;
8044b88c807SRodney W. Grimes 	fcp = v->fmt;
8054b88c807SRodney W. Grimes 	if (v->flag & LJUST)
8064b88c807SRodney W. Grimes 		*cp++ = '-';
8074b88c807SRodney W. Grimes 	*cp++ = '*';
8080fd510b7SJoerg Wunsch 	while ((*cp++ = *fcp++));
8094b88c807SRodney W. Grimes 
810e2c9ac69STim J. Robbins #define	CHKINF127(n)	(((n) > 127) && (v->flag & INF127) ? 127 : (n))
811e2c9ac69STim J. Robbins 
8124b88c807SRodney W. Grimes 	switch (v->type) {
8134b88c807SRodney W. Grimes 	case CHAR:
8144b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(char *)bp);
8154b88c807SRodney W. Grimes 		break;
8164b88c807SRodney W. Grimes 	case UCHAR:
8174b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_char *)bp);
8184b88c807SRodney W. Grimes 		break;
8194b88c807SRodney W. Grimes 	case SHORT:
8204b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(short *)bp);
8214b88c807SRodney W. Grimes 		break;
8224b88c807SRodney W. Grimes 	case USHORT:
8234b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_short *)bp);
8244b88c807SRodney W. Grimes 		break;
8253929d518SDoug Rabson 	case INT:
8263929d518SDoug Rabson 		(void)printf(ofmt, v->width, *(int *)bp);
8273929d518SDoug Rabson 		break;
8283929d518SDoug Rabson 	case UINT:
829e2c9ac69STim J. Robbins 		(void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
8303929d518SDoug Rabson 		break;
8314b88c807SRodney W. Grimes 	case LONG:
8324b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(long *)bp);
8334b88c807SRodney W. Grimes 		break;
8344b88c807SRodney W. Grimes 	case ULONG:
8354b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_long *)bp);
8364b88c807SRodney W. Grimes 		break;
8374b88c807SRodney W. Grimes 	case KPTR:
8387d1192a7SPeter Wemm 		(void)printf(ofmt, v->width, *(u_long *)bp);
8394b88c807SRodney W. Grimes 		break;
840362d62baSJuli Mallett 	case PGTOK:
841760bbf7dSJuli Mallett 		(void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
842760bbf7dSJuli Mallett 		break;
8434b88c807SRodney W. Grimes 	default:
8444b88c807SRodney W. Grimes 		errx(1, "unknown type %d", v->type);
8454b88c807SRodney W. Grimes 	}
8464b88c807SRodney W. Grimes }
8474b88c807SRodney W. Grimes 
8484b88c807SRodney W. Grimes void
84946251ddeSWarner Losh kvar(KINFO *k, VARENT *ve)
8504b88c807SRodney W. Grimes {
8514b88c807SRodney W. Grimes 	VAR *v;
8524b88c807SRodney W. Grimes 
8534b88c807SRodney W. Grimes 	v = ve->var;
8541f7d2501SKirk McKusick 	printval((char *)((char *)k->ki_p + v->off), v);
8554b88c807SRodney W. Grimes }
8564b88c807SRodney W. Grimes 
8574b88c807SRodney W. Grimes void
85846251ddeSWarner Losh rvar(KINFO *k, VARENT *ve)
8594b88c807SRodney W. Grimes {
8604b88c807SRodney W. Grimes 	VAR *v;
8614b88c807SRodney W. Grimes 
8624b88c807SRodney W. Grimes 	v = ve->var;
8631f7d2501SKirk McKusick 	if (k->ki_valid)
8641f7d2501SKirk McKusick 		printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
8654b88c807SRodney W. Grimes 	else
8664b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "-");
8674b88c807SRodney W. Grimes }
8687304f61fSBrian Feldman 
8697304f61fSBrian Feldman void
87015b87b53SGarance A Drosehn emulname(KINFO *k, VARENT *ve)
87115b87b53SGarance A Drosehn {
87215b87b53SGarance A Drosehn 	VAR *v;
87315b87b53SGarance A Drosehn 
87415b87b53SGarance A Drosehn 	v = ve->var;
87515b87b53SGarance A Drosehn 	printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
87615b87b53SGarance A Drosehn }
87715b87b53SGarance A Drosehn 
87815b87b53SGarance A Drosehn void
8792af538ebSRobert Watson label(KINFO *k, VARENT *ve)
8807304f61fSBrian Feldman {
8812af538ebSRobert Watson 	char *string;
882b85add5fSPhilippe Charnier 	VAR *v;
883775bba9fSJuli Mallett 	mac_t proclabel;
8842af538ebSRobert Watson 	int error;
8857304f61fSBrian Feldman 
8867304f61fSBrian Feldman 	v = ve->var;
8872af538ebSRobert Watson 	string = NULL;
888775bba9fSJuli Mallett 	if (mac_prepare_process_label(&proclabel) == -1) {
8892c61418dSTim J. Robbins 		warn("mac_prepare_process_label");
8902af538ebSRobert Watson 		goto out;
8912af538ebSRobert Watson 	}
892775bba9fSJuli Mallett 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
8932af538ebSRobert Watson 	if (error == 0) {
894775bba9fSJuli Mallett 		if (mac_to_text(proclabel, &string) == -1)
8952af538ebSRobert Watson 			string = NULL;
8962af538ebSRobert Watson 	}
897775bba9fSJuli Mallett 	mac_free(proclabel);
8982af538ebSRobert Watson out:
8992af538ebSRobert Watson 	if (string != NULL) {
9002af538ebSRobert Watson 		(void)printf("%-*s", v->width, string);
9012af538ebSRobert Watson 		free(string);
9022af538ebSRobert Watson 	} else
903ce1affa2SGarance A Drosehn 		(void)printf("%-*s", v->width, "  -");
9042af538ebSRobert Watson 	return;
9052af538ebSRobert Watson }
9062af538ebSRobert Watson 
9077123f4cdSEdward Tomasz Napierala void
9087123f4cdSEdward Tomasz Napierala loginclass(KINFO *k, VARENT *ve)
9097123f4cdSEdward Tomasz Napierala {
9107123f4cdSEdward Tomasz Napierala 	VAR *v;
9117123f4cdSEdward Tomasz Napierala 	char *s;
9127123f4cdSEdward Tomasz Napierala 
9137123f4cdSEdward Tomasz Napierala 	v = ve->var;
9147123f4cdSEdward Tomasz Napierala 	/*
9157123f4cdSEdward Tomasz Napierala 	 * Don't display login class for system processes;
9167123f4cdSEdward Tomasz Napierala 	 * login classes are used for resource limits,
9177123f4cdSEdward Tomasz Napierala 	 * and limits don't apply to system processes.
9187123f4cdSEdward Tomasz Napierala 	 */
9197123f4cdSEdward Tomasz Napierala 	if (k->ki_p->ki_flag & P_SYSTEM) {
9207123f4cdSEdward Tomasz Napierala 		(void)printf("%-*s", v->width, "-");
9217123f4cdSEdward Tomasz Napierala 		return;
9227123f4cdSEdward Tomasz Napierala 	}
9237123f4cdSEdward Tomasz Napierala 	s = k->ki_p->ki_loginclass;
9247123f4cdSEdward Tomasz Napierala 	(void)printf("%-*s", v->width, *s ? s : "-");
9257123f4cdSEdward Tomasz Napierala }
9267123f4cdSEdward Tomasz Napierala 
9272af538ebSRobert Watson int
9284610a811SAttilio Rao s_comm(KINFO *k)
9294610a811SAttilio Rao {
9304610a811SAttilio Rao 	char tmpbuff[COMMLEN + OCOMMLEN + 2];
9314610a811SAttilio Rao 
9324610a811SAttilio Rao 	bzero(tmpbuff, sizeof(tmpbuff));
9334610a811SAttilio Rao 	if (showthreads && k->ki_p->ki_numthreads > 1)
9344610a811SAttilio Rao 		sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
9354610a811SAttilio Rao 		    k->ki_p->ki_ocomm);
9364610a811SAttilio Rao 	else
9374610a811SAttilio Rao 		sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
9384610a811SAttilio Rao 	return (strlen(tmpbuff));
9394610a811SAttilio Rao }
9404610a811SAttilio Rao 
9414610a811SAttilio Rao int
942*f9db2550SEdward Tomasz Napierala s_cputime(KINFO *k)
943*f9db2550SEdward Tomasz Napierala {
944*f9db2550SEdward Tomasz Napierala 	long secs;
945*f9db2550SEdward Tomasz Napierala 
946*f9db2550SEdward Tomasz Napierala 	secs = k->ki_p->ki_runtime / 1000000;
947*f9db2550SEdward Tomasz Napierala 	if (sumrusage)
948*f9db2550SEdward Tomasz Napierala 		secs += k->ki_p->ki_childtime.tv_sec;
949*f9db2550SEdward Tomasz Napierala 	return (sizetime(secs));
950*f9db2550SEdward Tomasz Napierala }
951*f9db2550SEdward Tomasz Napierala 
952*f9db2550SEdward Tomasz Napierala int
9532af538ebSRobert Watson s_label(KINFO *k)
9542af538ebSRobert Watson {
9552af538ebSRobert Watson 	char *string = NULL;
956775bba9fSJuli Mallett 	mac_t proclabel;
9572af538ebSRobert Watson 	int error, size = 0;
9582af538ebSRobert Watson 
959775bba9fSJuli Mallett 	if (mac_prepare_process_label(&proclabel) == -1) {
9602c61418dSTim J. Robbins 		warn("mac_prepare_process_label");
9612af538ebSRobert Watson 		return (0);
9622af538ebSRobert Watson 	}
963775bba9fSJuli Mallett 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
964775bba9fSJuli Mallett 	if (error == 0 && mac_to_text(proclabel, &string) == 0) {
9652af538ebSRobert Watson 		size = strlen(string);
9662af538ebSRobert Watson 		free(string);
9672af538ebSRobert Watson 	}
968775bba9fSJuli Mallett 	mac_free(proclabel);
9692af538ebSRobert Watson 	return (size);
9703bf92decSEdward Tomasz Napierala }
9713bf92decSEdward Tomasz Napierala 
9723bf92decSEdward Tomasz Napierala int
9733bf92decSEdward Tomasz Napierala s_loginclass(KINFO *k)
9743bf92decSEdward Tomasz Napierala {
9753bf92decSEdward Tomasz Napierala 	char *s;
9763bf92decSEdward Tomasz Napierala 
9773bf92decSEdward Tomasz Napierala 	if (k->ki_p->ki_flag & P_SYSTEM)
9783bf92decSEdward Tomasz Napierala 		return (1);
9793bf92decSEdward Tomasz Napierala 
9803bf92decSEdward Tomasz Napierala 	s = k->ki_p->ki_loginclass;
9813bf92decSEdward Tomasz Napierala 	if (s == NULL)
9823bf92decSEdward Tomasz Napierala 		return (1);
9833bf92decSEdward Tomasz Napierala 
9843bf92decSEdward Tomasz Napierala 	return (strlen(s));
9853bf92decSEdward Tomasz Napierala }
9863bf92decSEdward Tomasz Napierala 
9873bf92decSEdward Tomasz Napierala int
9883bf92decSEdward Tomasz Napierala s_logname(KINFO *k)
9893bf92decSEdward Tomasz Napierala {
9903bf92decSEdward Tomasz Napierala 	char *s;
9913bf92decSEdward Tomasz Napierala 
9923bf92decSEdward Tomasz Napierala 	s = k->ki_p->ki_login;
9933bf92decSEdward Tomasz Napierala 	if (s == NULL)
9943bf92decSEdward Tomasz Napierala 		return (1);
9953bf92decSEdward Tomasz Napierala 
9963bf92decSEdward Tomasz Napierala 	return (strlen(s));
9977304f61fSBrian Feldman }
998*f9db2550SEdward Tomasz Napierala 
999*f9db2550SEdward Tomasz Napierala int
1000*f9db2550SEdward Tomasz Napierala s_systime(KINFO *k)
1001*f9db2550SEdward Tomasz Napierala {
1002*f9db2550SEdward Tomasz Napierala 	long secs;
1003*f9db2550SEdward Tomasz Napierala 
1004*f9db2550SEdward Tomasz Napierala 	secs = k->ki_p->ki_rusage.ru_stime.tv_sec;
1005*f9db2550SEdward Tomasz Napierala 	if (sumrusage)
1006*f9db2550SEdward Tomasz Napierala 		secs += k->ki_p->ki_childstime.tv_sec;
1007*f9db2550SEdward Tomasz Napierala 	return (sizetime(secs));
1008*f9db2550SEdward Tomasz Napierala }
1009*f9db2550SEdward Tomasz Napierala 
1010*f9db2550SEdward Tomasz Napierala int
1011*f9db2550SEdward Tomasz Napierala s_usertime(KINFO *k)
1012*f9db2550SEdward Tomasz Napierala {
1013*f9db2550SEdward Tomasz Napierala 	long secs;
1014*f9db2550SEdward Tomasz Napierala 
1015*f9db2550SEdward Tomasz Napierala 	secs = k->ki_p->ki_rusage.ru_utime.tv_sec;
1016*f9db2550SEdward Tomasz Napierala 	if (sumrusage)
1017*f9db2550SEdward Tomasz Napierala 		secs += k->ki_p->ki_childutime.tv_sec;
1018*f9db2550SEdward Tomasz Napierala 	return (sizetime(secs));
1019*f9db2550SEdward Tomasz Napierala }
1020