xref: /freebsd/bin/ps/print.c (revision 871e8d8c5950e15d0393b20df06b0798052d710c)
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  * 3. All advertising materials mentioning features or use of this software
144b88c807SRodney W. Grimes  *    must display the following acknowledgement:
154b88c807SRodney W. Grimes  *	This product includes software developed by the University of
164b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
174b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
184b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
194b88c807SRodney W. Grimes  *    without specific prior written permission.
204b88c807SRodney W. Grimes  *
214b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314b88c807SRodney W. Grimes  * SUCH DAMAGE.
324b88c807SRodney W. Grimes  */
334b88c807SRodney W. Grimes 
34871e8d8cSMark Murray #include <sys/cdefs.h>
35871e8d8cSMark Murray 
36871e8d8cSMark Murray __FBSDID("$FreeBSD$");
37871e8d8cSMark Murray 
38c9a8d1f4SPhilippe Charnier #if 0
39871e8d8cSMark Murray #ifndef lint
40c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
414b88c807SRodney W. Grimes #endif /* not lint */
42871e8d8cSMark Murray #endif
434b88c807SRodney W. Grimes 
444b88c807SRodney W. Grimes #include <sys/param.h>
454b88c807SRodney W. Grimes #include <sys/time.h>
464b88c807SRodney W. Grimes #include <sys/resource.h>
474b88c807SRodney W. Grimes #include <sys/proc.h>
484b88c807SRodney W. Grimes #include <sys/stat.h>
494b88c807SRodney W. Grimes 
508b9b0e39SPoul-Henning Kamp #include <sys/user.h>
514b88c807SRodney W. Grimes #include <sys/sysctl.h>
524b88c807SRodney W. Grimes 
534b88c807SRodney W. Grimes #include <err.h>
54f59105eeSAndrey A. Chernov #include <langinfo.h>
558073a93cSAndrey A. Chernov #include <locale.h>
564b88c807SRodney W. Grimes #include <math.h>
574b88c807SRodney W. Grimes #include <nlist.h>
584b88c807SRodney W. Grimes #include <stddef.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 
65f1ff35aeSMaxim Sobolev #include "lomac.h"
664b88c807SRodney W. Grimes #include "ps.h"
674b88c807SRodney W. Grimes 
68871e8d8cSMark Murray static void printval __P((char *, VAR *));
69871e8d8cSMark Murray 
704b88c807SRodney W. Grimes void
7146251ddeSWarner Losh printheader(void)
724b88c807SRodney W. Grimes {
734b88c807SRodney W. Grimes 	VAR *v;
744b88c807SRodney W. Grimes 	struct varent *vent;
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes 	for (vent = vhead; vent; vent = vent->next) {
774b88c807SRodney W. Grimes 		v = vent->var;
784b88c807SRodney W. Grimes 		if (v->flag & LJUST) {
794b88c807SRodney W. Grimes 			if (vent->next == NULL)	/* last one */
804b88c807SRodney W. Grimes 				(void)printf("%s", v->header);
814b88c807SRodney W. Grimes 			else
824b88c807SRodney W. Grimes 				(void)printf("%-*s", v->width, v->header);
834b88c807SRodney W. Grimes 		} else
844b88c807SRodney W. Grimes 			(void)printf("%*s", v->width, v->header);
854b88c807SRodney W. Grimes 		if (vent->next != NULL)
864b88c807SRodney W. Grimes 			(void)putchar(' ');
874b88c807SRodney W. Grimes 	}
884b88c807SRodney W. Grimes 	(void)putchar('\n');
894b88c807SRodney W. Grimes }
904b88c807SRodney W. Grimes 
914b88c807SRodney W. Grimes void
9246251ddeSWarner Losh command(KINFO *k, VARENT *ve)
934b88c807SRodney W. Grimes {
944b88c807SRodney W. Grimes 	VAR *v;
954b88c807SRodney W. Grimes 	int left;
964b88c807SRodney W. Grimes 	char *cp, *vis_env, *vis_args;
974b88c807SRodney W. Grimes 
98db91faacSPeter Wemm 	v = ve->var;
99db91faacSPeter Wemm 
100db91faacSPeter Wemm 	if (cflag) {
1013cc273e0SJohn Polstra 		if (ve->next == NULL)	/* last field, don't pad */
1021f7d2501SKirk McKusick 			(void)printf("%s", k->ki_p->ki_comm);
1033cc273e0SJohn Polstra 		else
1041f7d2501SKirk McKusick 			(void)printf("%-*s", v->width, k->ki_p->ki_comm);
105db91faacSPeter Wemm 		return;
106db91faacSPeter Wemm 	}
107db91faacSPeter Wemm 
1084b88c807SRodney W. Grimes 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
1094b88c807SRodney W. Grimes 		err(1, NULL);
1104b88c807SRodney W. Grimes 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
1114b88c807SRodney W. Grimes 	if (k->ki_env) {
1124b88c807SRodney W. Grimes 		if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
1134b88c807SRodney W. Grimes 			err(1, NULL);
1144b88c807SRodney W. Grimes 		strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
1154b88c807SRodney W. Grimes 	} else
1164b88c807SRodney W. Grimes 		vis_env = NULL;
1174b88c807SRodney W. Grimes 
1184b88c807SRodney W. Grimes 	if (ve->next == NULL) {
1194b88c807SRodney W. Grimes 		/* last field */
1204b88c807SRodney W. Grimes 		if (termwidth == UNLIMITED) {
1214b88c807SRodney W. Grimes 			if (vis_env)
1224b88c807SRodney W. Grimes 				(void)printf("%s ", vis_env);
1234b88c807SRodney W. Grimes 			(void)printf("%s", vis_args);
1244b88c807SRodney W. Grimes 		} else {
1254b88c807SRodney W. Grimes 			left = termwidth - (totwidth - v->width);
1264b88c807SRodney W. Grimes 			if (left < 1) /* already wrapped, just use std width */
1274b88c807SRodney W. Grimes 				left = v->width;
1284b88c807SRodney W. Grimes 			if ((cp = vis_env) != NULL) {
1294b88c807SRodney W. Grimes 				while (--left >= 0 && *cp)
1304b88c807SRodney W. Grimes 					(void)putchar(*cp++);
1314b88c807SRodney W. Grimes 				if (--left >= 0)
1324b88c807SRodney W. Grimes 					putchar(' ');
1334b88c807SRodney W. Grimes 			}
1344b88c807SRodney W. Grimes 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
1354b88c807SRodney W. Grimes 				(void)putchar(*cp++);
1364b88c807SRodney W. Grimes 		}
1374b88c807SRodney W. Grimes 	} else
1384b88c807SRodney W. Grimes 		/* XXX env? */
1394b88c807SRodney W. Grimes 		(void)printf("%-*.*s", v->width, v->width, vis_args);
1404b88c807SRodney W. Grimes 	free(vis_args);
1414b88c807SRodney W. Grimes 	if (vis_env != NULL)
1424b88c807SRodney W. Grimes 		free(vis_env);
1434b88c807SRodney W. Grimes }
1444b88c807SRodney W. Grimes 
1454b88c807SRodney W. Grimes void
14646251ddeSWarner Losh ucomm(KINFO *k, VARENT *ve)
1474b88c807SRodney W. Grimes {
1484b88c807SRodney W. Grimes 	VAR *v;
1494b88c807SRodney W. Grimes 
1504b88c807SRodney W. Grimes 	v = ve->var;
1511f7d2501SKirk McKusick 	(void)printf("%-*s", v->width, k->ki_p->ki_comm);
1524b88c807SRodney W. Grimes }
1534b88c807SRodney W. Grimes 
1544b88c807SRodney W. Grimes void
15546251ddeSWarner Losh logname(KINFO *k, VARENT *ve)
1564b88c807SRodney W. Grimes {
1574b88c807SRodney W. Grimes 	VAR *v;
158ad863cacSSteve Price 	char *s;
1594b88c807SRodney W. Grimes 
1604b88c807SRodney W. Grimes 	v = ve->var;
1611f7d2501SKirk McKusick 	(void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
1624b88c807SRodney W. Grimes }
1634b88c807SRodney W. Grimes 
1644b88c807SRodney W. Grimes void
16546251ddeSWarner Losh state(KINFO *k, VARENT *ve)
1664b88c807SRodney W. Grimes {
167b40ce416SJulian Elischer 	int flag, sflag, tdflags;
1684b88c807SRodney W. Grimes 	char *cp;
1694b88c807SRodney W. Grimes 	VAR *v;
1704b88c807SRodney W. Grimes 	char buf[16];
1714b88c807SRodney W. Grimes 
1724b88c807SRodney W. Grimes 	v = ve->var;
1731f7d2501SKirk McKusick 	flag = k->ki_p->ki_flag;
174564efb8fSTor Egge 	sflag = k->ki_p->ki_sflag;
175b40ce416SJulian Elischer 	tdflags = k->ki_p->ki_tdflags;	/* XXXKSE */
1764b88c807SRodney W. Grimes 	cp = buf;
1774b88c807SRodney W. Grimes 
1781f7d2501SKirk McKusick 	switch (k->ki_p->ki_stat) {
1794b88c807SRodney W. Grimes 
1804b88c807SRodney W. Grimes 	case SSTOP:
1814b88c807SRodney W. Grimes 		*cp = 'T';
1824b88c807SRodney W. Grimes 		break;
1834b88c807SRodney W. Grimes 
1844b88c807SRodney W. Grimes 	case SSLEEP:
185b40ce416SJulian Elischer 		if (tdflags & TDF_SINTR)	/* interruptable (long) */
1861f7d2501SKirk McKusick 			*cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
1874b88c807SRodney W. Grimes 		else
1884b88c807SRodney W. Grimes 			*cp = 'D';
1894b88c807SRodney W. Grimes 		break;
1904b88c807SRodney W. Grimes 
1914b88c807SRodney W. Grimes 	case SRUN:
1924b88c807SRodney W. Grimes 	case SIDL:
1934b88c807SRodney W. Grimes 		*cp = 'R';
1944b88c807SRodney W. Grimes 		break;
1954b88c807SRodney W. Grimes 
1960384fff8SJason Evans 	case SWAIT:
1970384fff8SJason Evans 		*cp = 'W';
1980384fff8SJason Evans 		break;
1990384fff8SJason Evans 
2000384fff8SJason Evans 	case SMTX:
2010384fff8SJason Evans 		*cp = 'M';
2020384fff8SJason Evans 		break;
2030384fff8SJason Evans 
2044b88c807SRodney W. Grimes 	case SZOMB:
2054b88c807SRodney W. Grimes 		*cp = 'Z';
2064b88c807SRodney W. Grimes 		break;
2074b88c807SRodney W. Grimes 
2084b88c807SRodney W. Grimes 	default:
2094b88c807SRodney W. Grimes 		*cp = '?';
2104b88c807SRodney W. Grimes 	}
2114b88c807SRodney W. Grimes 	cp++;
212e0aa5ab7SJohn Baldwin 	if (!(sflag & PS_INMEM))
2134b88c807SRodney W. Grimes 		*cp++ = 'W';
2141f7d2501SKirk McKusick 	if (k->ki_p->ki_nice < NZERO)
2154b88c807SRodney W. Grimes 		*cp++ = '<';
2161f7d2501SKirk McKusick 	else if (k->ki_p->ki_nice > NZERO)
2174b88c807SRodney W. Grimes 		*cp++ = 'N';
2184b88c807SRodney W. Grimes 	if (flag & P_TRACED)
2194b88c807SRodney W. Grimes 		*cp++ = 'X';
2201f7d2501SKirk McKusick 	if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
2214b88c807SRodney W. Grimes 		*cp++ = 'E';
2224b88c807SRodney W. Grimes 	if (flag & P_PPWAIT)
2234b88c807SRodney W. Grimes 		*cp++ = 'V';
2241f7d2501SKirk McKusick 	if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
2254b88c807SRodney W. Grimes 		*cp++ = 'L';
2261f7d2501SKirk McKusick 	if (k->ki_p->ki_kiflag & KI_SLEADER)
2274b88c807SRodney W. Grimes 		*cp++ = 's';
2281f7d2501SKirk McKusick 	if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
2294b88c807SRodney W. Grimes 		*cp++ = '+';
23075c13541SPoul-Henning Kamp 	if (flag & P_JAILED)
23175c13541SPoul-Henning Kamp 		*cp++ = 'J';
2324b88c807SRodney W. Grimes 	*cp = '\0';
2334b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
2344b88c807SRodney W. Grimes }
2354b88c807SRodney W. Grimes 
2364b88c807SRodney W. Grimes void
23746251ddeSWarner Losh pri(KINFO *k, VARENT *ve)
2384b88c807SRodney W. Grimes {
2394b88c807SRodney W. Grimes 	VAR *v;
2404b88c807SRodney W. Grimes 
2414b88c807SRodney W. Grimes 	v = ve->var;
2424c85452bSJake Burkholder 	(void)printf("%*d", v->width, k->ki_p->ki_pri.pri_level - PZERO);
2434b88c807SRodney W. Grimes }
2444b88c807SRodney W. Grimes 
2454b88c807SRodney W. Grimes void
24646251ddeSWarner Losh uname(KINFO *k, VARENT *ve)
2474b88c807SRodney W. Grimes {
2484b88c807SRodney W. Grimes 	VAR *v;
2494b88c807SRodney W. Grimes 
2504b88c807SRodney W. Grimes 	v = ve->var;
2514b88c807SRodney W. Grimes 	(void)printf("%-*s",
2521f7d2501SKirk McKusick 	    (int)v->width, user_from_uid(k->ki_p->ki_uid, 0));
2534b88c807SRodney W. Grimes }
2544b88c807SRodney W. Grimes 
2556a2d726bSJordan K. Hubbard int
25646251ddeSWarner Losh s_uname(KINFO *k)
2576a2d726bSJordan K. Hubbard {
2581f7d2501SKirk McKusick 	    return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
2596a2d726bSJordan K. Hubbard }
2606a2d726bSJordan K. Hubbard 
2614b88c807SRodney W. Grimes void
26246251ddeSWarner Losh runame(KINFO *k, VARENT *ve)
2634b88c807SRodney W. Grimes {
2644b88c807SRodney W. Grimes 	VAR *v;
2654b88c807SRodney W. Grimes 
2664b88c807SRodney W. Grimes 	v = ve->var;
2674b88c807SRodney W. Grimes 	(void)printf("%-*s",
2681f7d2501SKirk McKusick 	    (int)v->width, user_from_uid(k->ki_p->ki_ruid, 0));
2694b88c807SRodney W. Grimes }
2704b88c807SRodney W. Grimes 
2716a2d726bSJordan K. Hubbard int
27246251ddeSWarner Losh s_runame(KINFO *k)
2736a2d726bSJordan K. Hubbard {
2741f7d2501SKirk McKusick 	    return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
2756a2d726bSJordan K. Hubbard }
2766a2d726bSJordan K. Hubbard 
2774b88c807SRodney W. Grimes void
27846251ddeSWarner Losh tdev(KINFO *k, VARENT *ve)
2794b88c807SRodney W. Grimes {
2804b88c807SRodney W. Grimes 	VAR *v;
2814b88c807SRodney W. Grimes 	dev_t dev;
2824b88c807SRodney W. Grimes 	char buff[16];
2834b88c807SRodney W. Grimes 
2844b88c807SRodney W. Grimes 	v = ve->var;
2851f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
2864b88c807SRodney W. Grimes 	if (dev == NODEV)
2874b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "??");
2884b88c807SRodney W. Grimes 	else {
2894b88c807SRodney W. Grimes 		(void)snprintf(buff, sizeof(buff),
2904b88c807SRodney W. Grimes 		    "%d/%d", major(dev), minor(dev));
2914b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, buff);
2924b88c807SRodney W. Grimes 	}
2934b88c807SRodney W. Grimes }
2944b88c807SRodney W. Grimes 
2954b88c807SRodney W. Grimes void
29646251ddeSWarner Losh tname(KINFO *k, VARENT *ve)
2974b88c807SRodney W. Grimes {
2984b88c807SRodney W. Grimes 	VAR *v;
2994b88c807SRodney W. Grimes 	dev_t dev;
3004b88c807SRodney W. Grimes 	char *ttname;
3014b88c807SRodney W. Grimes 
3024b88c807SRodney W. Grimes 	v = ve->var;
3031f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
3044b88c807SRodney W. Grimes 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
305c55931c7SPeter Wemm 		(void)printf("%*s ", v->width-1, "??");
3064b88c807SRodney W. Grimes 	else {
307efc18e2cSAndrey A. Chernov 		if (strncmp(ttname, "tty", 3) == 0 ||
308efc18e2cSAndrey A. Chernov 		    strncmp(ttname, "cua", 3) == 0)
3094b88c807SRodney W. Grimes 			ttname += 3;
310c55931c7SPeter Wemm 		(void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
3111f7d2501SKirk McKusick 			k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
3124b88c807SRodney W. Grimes 	}
3134b88c807SRodney W. Grimes }
3144b88c807SRodney W. Grimes 
3154b88c807SRodney W. Grimes void
31646251ddeSWarner Losh longtname(KINFO *k, VARENT *ve)
3174b88c807SRodney W. Grimes {
3184b88c807SRodney W. Grimes 	VAR *v;
3194b88c807SRodney W. Grimes 	dev_t dev;
3204b88c807SRodney W. Grimes 	char *ttname;
3214b88c807SRodney W. Grimes 
3224b88c807SRodney W. Grimes 	v = ve->var;
3231f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
3244b88c807SRodney W. Grimes 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
3254b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "??");
3264b88c807SRodney W. Grimes 	else
3274b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, ttname);
3284b88c807SRodney W. Grimes }
3294b88c807SRodney W. Grimes 
3304b88c807SRodney W. Grimes void
33146251ddeSWarner Losh started(KINFO *k, VARENT *ve)
3324b88c807SRodney W. Grimes {
3334b88c807SRodney W. Grimes 	VAR *v;
3344b88c807SRodney W. Grimes 	static time_t now;
335f8ec5fe2SBruce Evans 	time_t then;
3364b88c807SRodney W. Grimes 	struct tm *tp;
3374b88c807SRodney W. Grimes 	char buf[100];
338f59105eeSAndrey A. Chernov 	static int  use_ampm = -1;
3394b88c807SRodney W. Grimes 
3404b88c807SRodney W. Grimes 	v = ve->var;
3411f7d2501SKirk McKusick 	if (!k->ki_valid) {
3424b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
3434b88c807SRodney W. Grimes 		return;
3444b88c807SRodney W. Grimes 	}
3454b88c807SRodney W. Grimes 
346f59105eeSAndrey A. Chernov 	if (use_ampm < 0)
347f59105eeSAndrey A. Chernov 		use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
348f59105eeSAndrey A. Chernov 
3491f7d2501SKirk McKusick 	then = k->ki_p->ki_start.tv_sec;
350f8ec5fe2SBruce Evans 	tp = localtime(&then);
3514b88c807SRodney W. Grimes 	if (!now)
3524b88c807SRodney W. Grimes 		(void)time(&now);
3531f7d2501SKirk McKusick 	if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
35408017519SAndrey A. Chernov 		(void)strftime(buf, sizeof(buf) - 1,
35508017519SAndrey A. Chernov 		use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
3561f7d2501SKirk McKusick 	} else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
35708017519SAndrey A. Chernov 		(void)strftime(buf, sizeof(buf) - 1,
35808017519SAndrey A. Chernov 		use_ampm ? "%a%I%p" : "%a%H  ", tp);
3594b88c807SRodney W. Grimes 	} else
3604b88c807SRodney W. Grimes 		(void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
3614b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
3624b88c807SRodney W. Grimes }
3634b88c807SRodney W. Grimes 
3644b88c807SRodney W. Grimes void
36546251ddeSWarner Losh lstarted(KINFO *k, VARENT *ve)
3664b88c807SRodney W. Grimes {
3674b88c807SRodney W. Grimes 	VAR *v;
368f8ec5fe2SBruce Evans 	time_t then;
3694b88c807SRodney W. Grimes 	char buf[100];
3704b88c807SRodney W. Grimes 
3714b88c807SRodney W. Grimes 	v = ve->var;
3721f7d2501SKirk McKusick 	if (!k->ki_valid) {
3734b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
3744b88c807SRodney W. Grimes 		return;
3754b88c807SRodney W. Grimes 	}
3761f7d2501SKirk McKusick 	then = k->ki_p->ki_start.tv_sec;
377f8ec5fe2SBruce Evans 	(void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
3784b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
3794b88c807SRodney W. Grimes }
3804b88c807SRodney W. Grimes 
3814b88c807SRodney W. Grimes void
38246251ddeSWarner Losh mtxname(KINFO *k, VARENT *ve)
383fd5f30bfSJohn Baldwin {
384fd5f30bfSJohn Baldwin 	VAR *v;
385fd5f30bfSJohn Baldwin 
386fd5f30bfSJohn Baldwin 	v = ve->var;
3871f7d2501SKirk McKusick 	if (k->ki_p->ki_kiflag & KI_MTXBLOCK) {
3881f7d2501SKirk McKusick 		if (k->ki_p->ki_mtxname[0] != 0)
389fd5f30bfSJohn Baldwin 			(void)printf("%-*.*s", v->width, v->width,
3901f7d2501SKirk McKusick 				      k->ki_p->ki_mtxname);
391fd5f30bfSJohn Baldwin 		else
392fd5f30bfSJohn Baldwin 			(void)printf("%-*s", v->width, "???");
393fd5f30bfSJohn Baldwin 	} else
394fd5f30bfSJohn Baldwin 		(void)printf("%-*s", v->width, "-");
395fd5f30bfSJohn Baldwin }
396fd5f30bfSJohn Baldwin 
397fd5f30bfSJohn Baldwin void
39846251ddeSWarner Losh wchan(KINFO *k, VARENT *ve)
3994b88c807SRodney W. Grimes {
4004b88c807SRodney W. Grimes 	VAR *v;
4014b88c807SRodney W. Grimes 
4024b88c807SRodney W. Grimes 	v = ve->var;
4031f7d2501SKirk McKusick 	if (k->ki_p->ki_wchan) {
4041f7d2501SKirk McKusick 		if (k->ki_p->ki_wmesg[0] != 0)
4054b88c807SRodney W. Grimes 			(void)printf("%-*.*s", v->width, v->width,
4061f7d2501SKirk McKusick 				      k->ki_p->ki_wmesg);
4074b88c807SRodney W. Grimes 		else
4083929d518SDoug Rabson 			(void)printf("%-*lx", v->width,
4097d1192a7SPeter Wemm 			    (long)k->ki_p->ki_wchan);
4104b88c807SRodney W. Grimes 	} else
4114b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
4124b88c807SRodney W. Grimes }
4134b88c807SRodney W. Grimes 
4144a40c5e7SPeter Wemm #ifndef pgtok
4153a91de06SPoul-Henning Kamp #define pgtok(a)        (((a)*getpagesize())/1024)
4164a40c5e7SPeter Wemm #endif
4174b88c807SRodney W. Grimes 
4184b88c807SRodney W. Grimes void
41946251ddeSWarner Losh vsize(KINFO *k, VARENT *ve)
4204b88c807SRodney W. Grimes {
4214b88c807SRodney W. Grimes 	VAR *v;
4224b88c807SRodney W. Grimes 
4234b88c807SRodney W. Grimes 	v = ve->var;
4244b88c807SRodney W. Grimes 	(void)printf("%*d", v->width,
4251f7d2501SKirk McKusick 	    (k->ki_p->ki_size/1024));
4264b88c807SRodney W. Grimes }
4274b88c807SRodney W. Grimes 
4284b88c807SRodney W. Grimes void
42946251ddeSWarner Losh cputime(KINFO *k, VARENT *ve)
4304b88c807SRodney W. Grimes {
4314b88c807SRodney W. Grimes 	VAR *v;
4324b88c807SRodney W. Grimes 	long secs;
4334b88c807SRodney W. Grimes 	long psecs;	/* "parts" of a second. first micro, then centi */
4344b88c807SRodney W. Grimes 	char obuff[128];
4358073a93cSAndrey A. Chernov 	static char decimal_point = 0;
4364b88c807SRodney W. Grimes 
4378073a93cSAndrey A. Chernov 	if (!decimal_point)
4388073a93cSAndrey A. Chernov 		decimal_point = localeconv()->decimal_point[0];
4394b88c807SRodney W. Grimes 	v = ve->var;
4401f7d2501SKirk McKusick 	if (k->ki_p->ki_stat == SZOMB || !k->ki_valid) {
4415832a752SBruce Evans 		secs = 0;
4425832a752SBruce Evans 		psecs = 0;
4434b88c807SRodney W. Grimes 	} else {
4444b88c807SRodney W. Grimes 		/*
4454b88c807SRodney W. Grimes 		 * This counts time spent handling interrupts.  We could
4464b88c807SRodney W. Grimes 		 * fix this, but it is not 100% trivial (and interrupt
4474b88c807SRodney W. Grimes 		 * time fractions only work on the sparc anyway).	XXX
4484b88c807SRodney W. Grimes 		 */
4491f7d2501SKirk McKusick 		secs = k->ki_p->ki_runtime / 1000000;
4501f7d2501SKirk McKusick 		psecs = k->ki_p->ki_runtime % 1000000;
4514b88c807SRodney W. Grimes 		if (sumrusage) {
4521f7d2501SKirk McKusick 			secs += k->ki_p->ki_childtime.tv_sec;
4531f7d2501SKirk McKusick 			psecs += k->ki_p->ki_childtime.tv_usec;
4544b88c807SRodney W. Grimes 		}
4554b88c807SRodney W. Grimes 		/*
4564b88c807SRodney W. Grimes 		 * round and scale to 100's
4574b88c807SRodney W. Grimes 		 */
4585832a752SBruce Evans 		psecs = (psecs + 5000) / 10000;
4595832a752SBruce Evans 		secs += psecs / 100;
4605832a752SBruce Evans 		psecs = psecs % 100;
4615832a752SBruce Evans 	}
4624b88c807SRodney W. Grimes 	(void)snprintf(obuff, sizeof(obuff),
4638073a93cSAndrey A. Chernov 	    "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
4644b88c807SRodney W. Grimes 	(void)printf("%*s", v->width, obuff);
4654b88c807SRodney W. Grimes }
4664b88c807SRodney W. Grimes 
4674b88c807SRodney W. Grimes double
468871e8d8cSMark Murray getpcpu(const KINFO *k)
4694b88c807SRodney W. Grimes {
4704b88c807SRodney W. Grimes 	static int failure;
4714b88c807SRodney W. Grimes 
4724b88c807SRodney W. Grimes 	if (!nlistread)
4734b88c807SRodney W. Grimes 		failure = donlist();
4744b88c807SRodney W. Grimes 	if (failure)
4754b88c807SRodney W. Grimes 		return (0.0);
4764b88c807SRodney W. Grimes 
4774b88c807SRodney W. Grimes #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
4784b88c807SRodney W. Grimes 
4794b88c807SRodney W. Grimes 	/* XXX - I don't like this */
480e0aa5ab7SJohn Baldwin 	if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0)
4814b88c807SRodney W. Grimes 		return (0.0);
4824b88c807SRodney W. Grimes 	if (rawcpu)
4831f7d2501SKirk McKusick 		return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
4841f7d2501SKirk McKusick 	return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
4851f7d2501SKirk McKusick 		(1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
4864b88c807SRodney W. Grimes }
4874b88c807SRodney W. Grimes 
4884b88c807SRodney W. Grimes void
48946251ddeSWarner Losh pcpu(KINFO *k, VARENT *ve)
4904b88c807SRodney W. Grimes {
4914b88c807SRodney W. Grimes 	VAR *v;
4924b88c807SRodney W. Grimes 
4934b88c807SRodney W. Grimes 	v = ve->var;
4944b88c807SRodney W. Grimes 	(void)printf("%*.1f", v->width, getpcpu(k));
4954b88c807SRodney W. Grimes }
4964b88c807SRodney W. Grimes 
497871e8d8cSMark Murray static double
49846251ddeSWarner Losh getpmem(KINFO *k)
4994b88c807SRodney W. Grimes {
5004b88c807SRodney W. Grimes 	static int failure;
5014b88c807SRodney W. Grimes 	double fracmem;
5024b88c807SRodney W. Grimes 
5034b88c807SRodney W. Grimes 	if (!nlistread)
5044b88c807SRodney W. Grimes 		failure = donlist();
5054b88c807SRodney W. Grimes 	if (failure)
5064b88c807SRodney W. Grimes 		return (0.0);
5074b88c807SRodney W. Grimes 
508e0aa5ab7SJohn Baldwin 	if ((k->ki_p->ki_sflag & PS_INMEM) == 0)
5094b88c807SRodney W. Grimes 		return (0.0);
5104b88c807SRodney W. Grimes 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
5114b88c807SRodney W. Grimes 	/* XXX don't have info about shared */
51285012e7cSPeter Wemm 	fracmem = ((float)k->ki_p->ki_rssize)/mempages;
5134b88c807SRodney W. Grimes 	return (100.0 * fracmem);
5144b88c807SRodney W. Grimes }
5154b88c807SRodney W. Grimes 
5164b88c807SRodney W. Grimes void
51746251ddeSWarner Losh pmem(KINFO *k, VARENT *ve)
5184b88c807SRodney W. Grimes {
5194b88c807SRodney W. Grimes 	VAR *v;
5204b88c807SRodney W. Grimes 
5214b88c807SRodney W. Grimes 	v = ve->var;
5224b88c807SRodney W. Grimes 	(void)printf("%*.1f", v->width, getpmem(k));
5234b88c807SRodney W. Grimes }
5244b88c807SRodney W. Grimes 
5254b88c807SRodney W. Grimes void
52646251ddeSWarner Losh pagein(KINFO *k, VARENT *ve)
5274b88c807SRodney W. Grimes {
5284b88c807SRodney W. Grimes 	VAR *v;
5294b88c807SRodney W. Grimes 
5304b88c807SRodney W. Grimes 	v = ve->var;
5310fd510b7SJoerg Wunsch 	(void)printf("%*ld", v->width,
5321f7d2501SKirk McKusick 	    k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
5334b88c807SRodney W. Grimes }
5344b88c807SRodney W. Grimes 
535871e8d8cSMark Murray /* ARGSUSED */
5364b88c807SRodney W. Grimes void
537871e8d8cSMark Murray maxrss(KINFO *k __unused, VARENT *ve)
5384b88c807SRodney W. Grimes {
5394b88c807SRodney W. Grimes 	VAR *v;
5404b88c807SRodney W. Grimes 
5414b88c807SRodney W. Grimes 	v = ve->var;
542940cca66SPeter Wemm 	/* XXX not yet */
5434b88c807SRodney W. Grimes 	(void)printf("%*s", v->width, "-");
5444b88c807SRodney W. Grimes }
5454b88c807SRodney W. Grimes 
5464b88c807SRodney W. Grimes void
54746251ddeSWarner Losh tsize(KINFO *k, VARENT *ve)
5484b88c807SRodney W. Grimes {
5494b88c807SRodney W. Grimes 	VAR *v;
5504b88c807SRodney W. Grimes 
5514b88c807SRodney W. Grimes 	v = ve->var;
5521f7d2501SKirk McKusick 	(void)printf("%*ld", v->width, (long)pgtok(k->ki_p->ki_tsize));
5534b88c807SRodney W. Grimes }
5544b88c807SRodney W. Grimes 
555ad863cacSSteve Price void
55646251ddeSWarner Losh priorityr(KINFO *k, VARENT *ve)
557ad863cacSSteve Price {
558ad863cacSSteve Price 	VAR *v;
559871e8d8cSMark Murray 	struct priority *lpri;
560ad863cacSSteve Price 	char str[8];
5614c85452bSJake Burkholder 	unsigned class, level;
562ad863cacSSteve Price 
563ad863cacSSteve Price 	v = ve->var;
564871e8d8cSMark Murray 	lpri = (struct priority *) ((char *)k + v->off);
565871e8d8cSMark Murray 	class = lpri->pri_class;
566871e8d8cSMark Murray 	level = lpri->pri_level;
5674c85452bSJake Burkholder 	switch (class) {
5684c85452bSJake Burkholder 	case PRI_REALTIME:
5694c85452bSJake Burkholder 		snprintf(str, sizeof(str), "real:%u", level);
570ad863cacSSteve Price 		break;
5714c85452bSJake Burkholder 	case PRI_TIMESHARE:
572ad863cacSSteve Price 		strncpy(str, "normal", sizeof(str));
573ad863cacSSteve Price 		break;
5744c85452bSJake Burkholder 	case PRI_IDLE:
5754c85452bSJake Burkholder 		snprintf(str, sizeof(str), "idle:%u", level);
576ad863cacSSteve Price 		break;
577ad863cacSSteve Price 	default:
5784c85452bSJake Burkholder 		snprintf(str, sizeof(str), "%u:%u", class, level);
579ad863cacSSteve Price 		break;
580ad863cacSSteve Price 	}
581ad863cacSSteve Price 	str[sizeof(str) - 1] = '\0';
582ad863cacSSteve Price 	(void)printf("%*s", v->width, str);
583ad863cacSSteve Price }
584ad863cacSSteve Price 
5854b88c807SRodney W. Grimes /*
5864b88c807SRodney W. Grimes  * Generic output routines.  Print fields from various prototype
5874b88c807SRodney W. Grimes  * structures.
5884b88c807SRodney W. Grimes  */
5894b88c807SRodney W. Grimes static void
59046251ddeSWarner Losh printval(char *bp, VAR *v)
5914b88c807SRodney W. Grimes {
5924b88c807SRodney W. Grimes 	static char ofmt[32] = "%";
5934b88c807SRodney W. Grimes 	char *fcp, *cp;
5944b88c807SRodney W. Grimes 
5954b88c807SRodney W. Grimes 	cp = ofmt + 1;
5964b88c807SRodney W. Grimes 	fcp = v->fmt;
5974b88c807SRodney W. Grimes 	if (v->flag & LJUST)
5984b88c807SRodney W. Grimes 		*cp++ = '-';
5994b88c807SRodney W. Grimes 	*cp++ = '*';
6000fd510b7SJoerg Wunsch 	while ((*cp++ = *fcp++));
6014b88c807SRodney W. Grimes 
6024b88c807SRodney W. Grimes 	switch (v->type) {
6034b88c807SRodney W. Grimes 	case CHAR:
6044b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(char *)bp);
6054b88c807SRodney W. Grimes 		break;
6064b88c807SRodney W. Grimes 	case UCHAR:
6074b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_char *)bp);
6084b88c807SRodney W. Grimes 		break;
6094b88c807SRodney W. Grimes 	case SHORT:
6104b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(short *)bp);
6114b88c807SRodney W. Grimes 		break;
6124b88c807SRodney W. Grimes 	case USHORT:
6134b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_short *)bp);
6144b88c807SRodney W. Grimes 		break;
6153929d518SDoug Rabson 	case INT:
6163929d518SDoug Rabson 		(void)printf(ofmt, v->width, *(int *)bp);
6173929d518SDoug Rabson 		break;
6183929d518SDoug Rabson 	case UINT:
6193929d518SDoug Rabson 		(void)printf(ofmt, v->width, *(u_int *)bp);
6203929d518SDoug Rabson 		break;
6214b88c807SRodney W. Grimes 	case LONG:
6224b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(long *)bp);
6234b88c807SRodney W. Grimes 		break;
6244b88c807SRodney W. Grimes 	case ULONG:
6254b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_long *)bp);
6264b88c807SRodney W. Grimes 		break;
6274b88c807SRodney W. Grimes 	case KPTR:
6287d1192a7SPeter Wemm 		(void)printf(ofmt, v->width, *(u_long *)bp);
6294b88c807SRodney W. Grimes 		break;
6304b88c807SRodney W. Grimes 	default:
6314b88c807SRodney W. Grimes 		errx(1, "unknown type %d", v->type);
6324b88c807SRodney W. Grimes 	}
6334b88c807SRodney W. Grimes }
6344b88c807SRodney W. Grimes 
6354b88c807SRodney W. Grimes void
63646251ddeSWarner Losh kvar(KINFO *k, VARENT *ve)
6374b88c807SRodney W. Grimes {
6384b88c807SRodney W. Grimes 	VAR *v;
6394b88c807SRodney W. Grimes 
6404b88c807SRodney W. Grimes 	v = ve->var;
6411f7d2501SKirk McKusick 	printval((char *)((char *)k->ki_p + v->off), v);
6424b88c807SRodney W. Grimes }
6434b88c807SRodney W. Grimes 
6444b88c807SRodney W. Grimes void
64546251ddeSWarner Losh rvar(KINFO *k, VARENT *ve)
6464b88c807SRodney W. Grimes {
6474b88c807SRodney W. Grimes 	VAR *v;
6484b88c807SRodney W. Grimes 
6494b88c807SRodney W. Grimes 	v = ve->var;
6501f7d2501SKirk McKusick 	if (k->ki_valid)
6511f7d2501SKirk McKusick 		printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
6524b88c807SRodney W. Grimes 	else
6534b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "-");
6544b88c807SRodney W. Grimes }
6557304f61fSBrian Feldman 
6567304f61fSBrian Feldman void
65746251ddeSWarner Losh lattr(KINFO *k, VARENT *ve)
6587304f61fSBrian Feldman {
6597304f61fSBrian Feldman 	VAR *v;
6607304f61fSBrian Feldman 
6617304f61fSBrian Feldman 	v = ve->var;
6627304f61fSBrian Feldman 	(void)printf("%-*d", (int)v->width, get_lattr(k->ki_p->ki_pid));
6637304f61fSBrian Feldman }
664