xref: /freebsd/bin/ps/print.c (revision 576541a9e697991807c130dfea6f94a788a1e6fe)
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>
54576541a9SWarner Losh #include <grp.h>
55f59105eeSAndrey A. Chernov #include <langinfo.h>
568073a93cSAndrey A. Chernov #include <locale.h>
574b88c807SRodney W. Grimes #include <math.h>
584b88c807SRodney W. Grimes #include <nlist.h>
59576541a9SWarner Losh #include <pwd.h>
604b88c807SRodney W. Grimes #include <stddef.h>
614b88c807SRodney W. Grimes #include <stdio.h>
624b88c807SRodney W. Grimes #include <stdlib.h>
634b88c807SRodney W. Grimes #include <string.h>
64871e8d8cSMark Murray #include <unistd.h>
654b88c807SRodney W. Grimes #include <vis.h>
664b88c807SRodney W. Grimes 
67f1ff35aeSMaxim Sobolev #include "lomac.h"
684b88c807SRodney W. Grimes #include "ps.h"
694b88c807SRodney W. Grimes 
70871e8d8cSMark Murray static void printval __P((char *, VAR *));
71871e8d8cSMark Murray 
724b88c807SRodney W. Grimes void
7346251ddeSWarner Losh printheader(void)
744b88c807SRodney W. Grimes {
754b88c807SRodney W. Grimes 	VAR *v;
764b88c807SRodney W. Grimes 	struct varent *vent;
774b88c807SRodney W. Grimes 
784b88c807SRodney W. Grimes 	for (vent = vhead; vent; vent = vent->next) {
794b88c807SRodney W. Grimes 		v = vent->var;
804b88c807SRodney W. Grimes 		if (v->flag & LJUST) {
814b88c807SRodney W. Grimes 			if (vent->next == NULL)	/* last one */
824b88c807SRodney W. Grimes 				(void)printf("%s", v->header);
834b88c807SRodney W. Grimes 			else
844b88c807SRodney W. Grimes 				(void)printf("%-*s", v->width, v->header);
854b88c807SRodney W. Grimes 		} else
864b88c807SRodney W. Grimes 			(void)printf("%*s", v->width, v->header);
874b88c807SRodney W. Grimes 		if (vent->next != NULL)
884b88c807SRodney W. Grimes 			(void)putchar(' ');
894b88c807SRodney W. Grimes 	}
904b88c807SRodney W. Grimes 	(void)putchar('\n');
914b88c807SRodney W. Grimes }
924b88c807SRodney W. Grimes 
934b88c807SRodney W. Grimes void
9446251ddeSWarner Losh command(KINFO *k, VARENT *ve)
954b88c807SRodney W. Grimes {
964b88c807SRodney W. Grimes 	VAR *v;
974b88c807SRodney W. Grimes 	int left;
984b88c807SRodney W. Grimes 	char *cp, *vis_env, *vis_args;
994b88c807SRodney W. Grimes 
100db91faacSPeter Wemm 	v = ve->var;
101db91faacSPeter Wemm 
102db91faacSPeter Wemm 	if (cflag) {
1033cc273e0SJohn Polstra 		if (ve->next == NULL)	/* last field, don't pad */
1041f7d2501SKirk McKusick 			(void)printf("%s", k->ki_p->ki_comm);
1053cc273e0SJohn Polstra 		else
1061f7d2501SKirk McKusick 			(void)printf("%-*s", v->width, k->ki_p->ki_comm);
107db91faacSPeter Wemm 		return;
108db91faacSPeter Wemm 	}
109db91faacSPeter Wemm 
1104b88c807SRodney W. Grimes 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
1114b88c807SRodney W. Grimes 		err(1, NULL);
1124b88c807SRodney W. Grimes 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
1134b88c807SRodney W. Grimes 	if (k->ki_env) {
1144b88c807SRodney W. Grimes 		if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
1154b88c807SRodney W. Grimes 			err(1, NULL);
1164b88c807SRodney W. Grimes 		strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
1174b88c807SRodney W. Grimes 	} else
1184b88c807SRodney W. Grimes 		vis_env = NULL;
1194b88c807SRodney W. Grimes 
1204b88c807SRodney W. Grimes 	if (ve->next == NULL) {
1214b88c807SRodney W. Grimes 		/* last field */
1224b88c807SRodney W. Grimes 		if (termwidth == UNLIMITED) {
1234b88c807SRodney W. Grimes 			if (vis_env)
1244b88c807SRodney W. Grimes 				(void)printf("%s ", vis_env);
1254b88c807SRodney W. Grimes 			(void)printf("%s", vis_args);
1264b88c807SRodney W. Grimes 		} else {
1274b88c807SRodney W. Grimes 			left = termwidth - (totwidth - v->width);
1284b88c807SRodney W. Grimes 			if (left < 1) /* already wrapped, just use std width */
1294b88c807SRodney W. Grimes 				left = v->width;
1304b88c807SRodney W. Grimes 			if ((cp = vis_env) != NULL) {
1314b88c807SRodney W. Grimes 				while (--left >= 0 && *cp)
1324b88c807SRodney W. Grimes 					(void)putchar(*cp++);
1334b88c807SRodney W. Grimes 				if (--left >= 0)
1344b88c807SRodney W. Grimes 					putchar(' ');
1354b88c807SRodney W. Grimes 			}
1364b88c807SRodney W. Grimes 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
1374b88c807SRodney W. Grimes 				(void)putchar(*cp++);
1384b88c807SRodney W. Grimes 		}
1394b88c807SRodney W. Grimes 	} else
1404b88c807SRodney W. Grimes 		/* XXX env? */
1414b88c807SRodney W. Grimes 		(void)printf("%-*.*s", v->width, v->width, vis_args);
1424b88c807SRodney W. Grimes 	free(vis_args);
1434b88c807SRodney W. Grimes 	if (vis_env != NULL)
1444b88c807SRodney W. Grimes 		free(vis_env);
1454b88c807SRodney W. Grimes }
1464b88c807SRodney W. Grimes 
1474b88c807SRodney W. Grimes void
14846251ddeSWarner Losh ucomm(KINFO *k, VARENT *ve)
1494b88c807SRodney W. Grimes {
1504b88c807SRodney W. Grimes 	VAR *v;
1514b88c807SRodney W. Grimes 
1524b88c807SRodney W. Grimes 	v = ve->var;
1531f7d2501SKirk McKusick 	(void)printf("%-*s", v->width, k->ki_p->ki_comm);
1544b88c807SRodney W. Grimes }
1554b88c807SRodney W. Grimes 
1564b88c807SRodney W. Grimes void
15746251ddeSWarner Losh logname(KINFO *k, VARENT *ve)
1584b88c807SRodney W. Grimes {
1594b88c807SRodney W. Grimes 	VAR *v;
160ad863cacSSteve Price 	char *s;
1614b88c807SRodney W. Grimes 
1624b88c807SRodney W. Grimes 	v = ve->var;
1631f7d2501SKirk McKusick 	(void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
1644b88c807SRodney W. Grimes }
1654b88c807SRodney W. Grimes 
1664b88c807SRodney W. Grimes void
16746251ddeSWarner Losh state(KINFO *k, VARENT *ve)
1684b88c807SRodney W. Grimes {
169b40ce416SJulian Elischer 	int flag, sflag, tdflags;
1704b88c807SRodney W. Grimes 	char *cp;
1714b88c807SRodney W. Grimes 	VAR *v;
1724b88c807SRodney W. Grimes 	char buf[16];
1734b88c807SRodney W. Grimes 
1744b88c807SRodney W. Grimes 	v = ve->var;
1751f7d2501SKirk McKusick 	flag = k->ki_p->ki_flag;
176564efb8fSTor Egge 	sflag = k->ki_p->ki_sflag;
177b40ce416SJulian Elischer 	tdflags = k->ki_p->ki_tdflags;	/* XXXKSE */
1784b88c807SRodney W. Grimes 	cp = buf;
1794b88c807SRodney W. Grimes 
1801f7d2501SKirk McKusick 	switch (k->ki_p->ki_stat) {
1814b88c807SRodney W. Grimes 
1824b88c807SRodney W. Grimes 	case SSTOP:
1834b88c807SRodney W. Grimes 		*cp = 'T';
1844b88c807SRodney W. Grimes 		break;
1854b88c807SRodney W. Grimes 
1864b88c807SRodney W. Grimes 	case SSLEEP:
187b40ce416SJulian Elischer 		if (tdflags & TDF_SINTR)	/* interruptable (long) */
1881f7d2501SKirk McKusick 			*cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
1894b88c807SRodney W. Grimes 		else
1904b88c807SRodney W. Grimes 			*cp = 'D';
1914b88c807SRodney W. Grimes 		break;
1924b88c807SRodney W. Grimes 
1934b88c807SRodney W. Grimes 	case SRUN:
1944b88c807SRodney W. Grimes 	case SIDL:
1954b88c807SRodney W. Grimes 		*cp = 'R';
1964b88c807SRodney W. Grimes 		break;
1974b88c807SRodney W. Grimes 
1980384fff8SJason Evans 	case SWAIT:
1990384fff8SJason Evans 		*cp = 'W';
2000384fff8SJason Evans 		break;
2010384fff8SJason Evans 
2020384fff8SJason Evans 	case SMTX:
2030384fff8SJason Evans 		*cp = 'M';
2040384fff8SJason Evans 		break;
2050384fff8SJason Evans 
2064b88c807SRodney W. Grimes 	case SZOMB:
2074b88c807SRodney W. Grimes 		*cp = 'Z';
2084b88c807SRodney W. Grimes 		break;
2094b88c807SRodney W. Grimes 
2104b88c807SRodney W. Grimes 	default:
2114b88c807SRodney W. Grimes 		*cp = '?';
2124b88c807SRodney W. Grimes 	}
2134b88c807SRodney W. Grimes 	cp++;
214e0aa5ab7SJohn Baldwin 	if (!(sflag & PS_INMEM))
2154b88c807SRodney W. Grimes 		*cp++ = 'W';
2161f7d2501SKirk McKusick 	if (k->ki_p->ki_nice < NZERO)
2174b88c807SRodney W. Grimes 		*cp++ = '<';
2181f7d2501SKirk McKusick 	else if (k->ki_p->ki_nice > NZERO)
2194b88c807SRodney W. Grimes 		*cp++ = 'N';
2204b88c807SRodney W. Grimes 	if (flag & P_TRACED)
2214b88c807SRodney W. Grimes 		*cp++ = 'X';
2221f7d2501SKirk McKusick 	if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
2234b88c807SRodney W. Grimes 		*cp++ = 'E';
2244b88c807SRodney W. Grimes 	if (flag & P_PPWAIT)
2254b88c807SRodney W. Grimes 		*cp++ = 'V';
2261f7d2501SKirk McKusick 	if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
2274b88c807SRodney W. Grimes 		*cp++ = 'L';
2281f7d2501SKirk McKusick 	if (k->ki_p->ki_kiflag & KI_SLEADER)
2294b88c807SRodney W. Grimes 		*cp++ = 's';
2301f7d2501SKirk McKusick 	if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
2314b88c807SRodney W. Grimes 		*cp++ = '+';
23275c13541SPoul-Henning Kamp 	if (flag & P_JAILED)
23375c13541SPoul-Henning Kamp 		*cp++ = 'J';
2344b88c807SRodney W. Grimes 	*cp = '\0';
2354b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
2364b88c807SRodney W. Grimes }
2374b88c807SRodney W. Grimes 
2384b88c807SRodney W. Grimes void
23946251ddeSWarner Losh pri(KINFO *k, VARENT *ve)
2404b88c807SRodney W. Grimes {
2414b88c807SRodney W. Grimes 	VAR *v;
2424b88c807SRodney W. Grimes 
2434b88c807SRodney W. Grimes 	v = ve->var;
2444c85452bSJake Burkholder 	(void)printf("%*d", v->width, k->ki_p->ki_pri.pri_level - PZERO);
2454b88c807SRodney W. Grimes }
2464b88c807SRodney W. Grimes 
2474b88c807SRodney W. Grimes void
24846251ddeSWarner Losh uname(KINFO *k, VARENT *ve)
2494b88c807SRodney W. Grimes {
2504b88c807SRodney W. Grimes 	VAR *v;
2514b88c807SRodney W. Grimes 
2524b88c807SRodney W. Grimes 	v = ve->var;
2534b88c807SRodney W. Grimes 	(void)printf("%-*s",
2541f7d2501SKirk McKusick 	    (int)v->width, user_from_uid(k->ki_p->ki_uid, 0));
2554b88c807SRodney W. Grimes }
2564b88c807SRodney W. Grimes 
2576a2d726bSJordan K. Hubbard int
25846251ddeSWarner Losh s_uname(KINFO *k)
2596a2d726bSJordan K. Hubbard {
2601f7d2501SKirk McKusick 	    return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
2616a2d726bSJordan K. Hubbard }
2626a2d726bSJordan K. Hubbard 
2634b88c807SRodney W. Grimes void
26446251ddeSWarner Losh runame(KINFO *k, VARENT *ve)
2654b88c807SRodney W. Grimes {
2664b88c807SRodney W. Grimes 	VAR *v;
2674b88c807SRodney W. Grimes 
2684b88c807SRodney W. Grimes 	v = ve->var;
2694b88c807SRodney W. Grimes 	(void)printf("%-*s",
2701f7d2501SKirk McKusick 	    (int)v->width, user_from_uid(k->ki_p->ki_ruid, 0));
2714b88c807SRodney W. Grimes }
2724b88c807SRodney W. Grimes 
2736a2d726bSJordan K. Hubbard int
27446251ddeSWarner Losh s_runame(KINFO *k)
2756a2d726bSJordan K. Hubbard {
2761f7d2501SKirk McKusick 	    return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
2776a2d726bSJordan K. Hubbard }
2786a2d726bSJordan K. Hubbard 
2794b88c807SRodney W. Grimes void
28046251ddeSWarner Losh tdev(KINFO *k, VARENT *ve)
2814b88c807SRodney W. Grimes {
2824b88c807SRodney W. Grimes 	VAR *v;
2834b88c807SRodney W. Grimes 	dev_t dev;
2844b88c807SRodney W. Grimes 	char buff[16];
2854b88c807SRodney W. Grimes 
2864b88c807SRodney W. Grimes 	v = ve->var;
2871f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
2884b88c807SRodney W. Grimes 	if (dev == NODEV)
2894b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "??");
2904b88c807SRodney W. Grimes 	else {
2914b88c807SRodney W. Grimes 		(void)snprintf(buff, sizeof(buff),
2924b88c807SRodney W. Grimes 		    "%d/%d", major(dev), minor(dev));
2934b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, buff);
2944b88c807SRodney W. Grimes 	}
2954b88c807SRodney W. Grimes }
2964b88c807SRodney W. Grimes 
2974b88c807SRodney W. Grimes void
29846251ddeSWarner Losh tname(KINFO *k, VARENT *ve)
2994b88c807SRodney W. Grimes {
3004b88c807SRodney W. Grimes 	VAR *v;
3014b88c807SRodney W. Grimes 	dev_t dev;
3024b88c807SRodney W. Grimes 	char *ttname;
3034b88c807SRodney W. Grimes 
3044b88c807SRodney W. Grimes 	v = ve->var;
3051f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
3064b88c807SRodney W. Grimes 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
307c55931c7SPeter Wemm 		(void)printf("%*s ", v->width-1, "??");
3084b88c807SRodney W. Grimes 	else {
309efc18e2cSAndrey A. Chernov 		if (strncmp(ttname, "tty", 3) == 0 ||
310efc18e2cSAndrey A. Chernov 		    strncmp(ttname, "cua", 3) == 0)
3114b88c807SRodney W. Grimes 			ttname += 3;
312c55931c7SPeter Wemm 		(void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
3131f7d2501SKirk McKusick 			k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
3144b88c807SRodney W. Grimes 	}
3154b88c807SRodney W. Grimes }
3164b88c807SRodney W. Grimes 
3174b88c807SRodney W. Grimes void
31846251ddeSWarner Losh longtname(KINFO *k, VARENT *ve)
3194b88c807SRodney W. Grimes {
3204b88c807SRodney W. Grimes 	VAR *v;
3214b88c807SRodney W. Grimes 	dev_t dev;
3224b88c807SRodney W. Grimes 	char *ttname;
3234b88c807SRodney W. Grimes 
3244b88c807SRodney W. Grimes 	v = ve->var;
3251f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
3264b88c807SRodney W. Grimes 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
3274b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "??");
3284b88c807SRodney W. Grimes 	else
3294b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, ttname);
3304b88c807SRodney W. Grimes }
3314b88c807SRodney W. Grimes 
3324b88c807SRodney W. Grimes void
33346251ddeSWarner Losh started(KINFO *k, VARENT *ve)
3344b88c807SRodney W. Grimes {
3354b88c807SRodney W. Grimes 	VAR *v;
3364b88c807SRodney W. Grimes 	static time_t now;
337f8ec5fe2SBruce Evans 	time_t then;
3384b88c807SRodney W. Grimes 	struct tm *tp;
3394b88c807SRodney W. Grimes 	char buf[100];
340f59105eeSAndrey A. Chernov 	static int  use_ampm = -1;
3414b88c807SRodney W. Grimes 
3424b88c807SRodney W. Grimes 	v = ve->var;
3431f7d2501SKirk McKusick 	if (!k->ki_valid) {
3444b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
3454b88c807SRodney W. Grimes 		return;
3464b88c807SRodney W. Grimes 	}
3474b88c807SRodney W. Grimes 
348f59105eeSAndrey A. Chernov 	if (use_ampm < 0)
349f59105eeSAndrey A. Chernov 		use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
350f59105eeSAndrey A. Chernov 
3511f7d2501SKirk McKusick 	then = k->ki_p->ki_start.tv_sec;
352f8ec5fe2SBruce Evans 	tp = localtime(&then);
3534b88c807SRodney W. Grimes 	if (!now)
3544b88c807SRodney W. Grimes 		(void)time(&now);
3551f7d2501SKirk McKusick 	if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
35608017519SAndrey A. Chernov 		(void)strftime(buf, sizeof(buf) - 1,
35708017519SAndrey A. Chernov 		use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
3581f7d2501SKirk McKusick 	} else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
35908017519SAndrey A. Chernov 		(void)strftime(buf, sizeof(buf) - 1,
36008017519SAndrey A. Chernov 		use_ampm ? "%a%I%p" : "%a%H  ", tp);
3614b88c807SRodney W. Grimes 	} else
3624b88c807SRodney W. Grimes 		(void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
3634b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
3644b88c807SRodney W. Grimes }
3654b88c807SRodney W. Grimes 
3664b88c807SRodney W. Grimes void
36746251ddeSWarner Losh lstarted(KINFO *k, VARENT *ve)
3684b88c807SRodney W. Grimes {
3694b88c807SRodney W. Grimes 	VAR *v;
370f8ec5fe2SBruce Evans 	time_t then;
3714b88c807SRodney W. Grimes 	char buf[100];
3724b88c807SRodney W. Grimes 
3734b88c807SRodney W. Grimes 	v = ve->var;
3741f7d2501SKirk McKusick 	if (!k->ki_valid) {
3754b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
3764b88c807SRodney W. Grimes 		return;
3774b88c807SRodney W. Grimes 	}
3781f7d2501SKirk McKusick 	then = k->ki_p->ki_start.tv_sec;
379f8ec5fe2SBruce Evans 	(void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
3804b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
3814b88c807SRodney W. Grimes }
3824b88c807SRodney W. Grimes 
3834b88c807SRodney W. Grimes void
38446251ddeSWarner Losh mtxname(KINFO *k, VARENT *ve)
385fd5f30bfSJohn Baldwin {
386fd5f30bfSJohn Baldwin 	VAR *v;
387fd5f30bfSJohn Baldwin 
388fd5f30bfSJohn Baldwin 	v = ve->var;
3891f7d2501SKirk McKusick 	if (k->ki_p->ki_kiflag & KI_MTXBLOCK) {
3901f7d2501SKirk McKusick 		if (k->ki_p->ki_mtxname[0] != 0)
391fd5f30bfSJohn Baldwin 			(void)printf("%-*.*s", v->width, v->width,
3921f7d2501SKirk McKusick 				      k->ki_p->ki_mtxname);
393fd5f30bfSJohn Baldwin 		else
394fd5f30bfSJohn Baldwin 			(void)printf("%-*s", v->width, "???");
395fd5f30bfSJohn Baldwin 	} else
396fd5f30bfSJohn Baldwin 		(void)printf("%-*s", v->width, "-");
397fd5f30bfSJohn Baldwin }
398fd5f30bfSJohn Baldwin 
399fd5f30bfSJohn Baldwin void
40046251ddeSWarner Losh wchan(KINFO *k, VARENT *ve)
4014b88c807SRodney W. Grimes {
4024b88c807SRodney W. Grimes 	VAR *v;
4034b88c807SRodney W. Grimes 
4044b88c807SRodney W. Grimes 	v = ve->var;
4051f7d2501SKirk McKusick 	if (k->ki_p->ki_wchan) {
4061f7d2501SKirk McKusick 		if (k->ki_p->ki_wmesg[0] != 0)
4074b88c807SRodney W. Grimes 			(void)printf("%-*.*s", v->width, v->width,
4081f7d2501SKirk McKusick 				      k->ki_p->ki_wmesg);
4094b88c807SRodney W. Grimes 		else
4103929d518SDoug Rabson 			(void)printf("%-*lx", v->width,
4117d1192a7SPeter Wemm 			    (long)k->ki_p->ki_wchan);
412ebc1fff9SMatthew Dillon 	} else if (k->ki_p->ki_kiflag & KI_MTXBLOCK) {
413ebc1fff9SMatthew Dillon 		(void)printf("%-*.*s", v->width, v->width, k->ki_p->ki_mtxname);
414ebc1fff9SMatthew Dillon 	} else {
4154b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
4164b88c807SRodney W. Grimes 	}
417ebc1fff9SMatthew Dillon }
4184b88c807SRodney W. Grimes 
4194a40c5e7SPeter Wemm #ifndef pgtok
4203a91de06SPoul-Henning Kamp #define pgtok(a)        (((a)*getpagesize())/1024)
4214a40c5e7SPeter Wemm #endif
4224b88c807SRodney W. Grimes 
4234b88c807SRodney W. Grimes void
42446251ddeSWarner Losh vsize(KINFO *k, VARENT *ve)
4254b88c807SRodney W. Grimes {
4264b88c807SRodney W. Grimes 	VAR *v;
4274b88c807SRodney W. Grimes 
4284b88c807SRodney W. Grimes 	v = ve->var;
4294b88c807SRodney W. Grimes 	(void)printf("%*d", v->width,
4301f7d2501SKirk McKusick 	    (k->ki_p->ki_size/1024));
4314b88c807SRodney W. Grimes }
4324b88c807SRodney W. Grimes 
4334b88c807SRodney W. Grimes void
43446251ddeSWarner Losh cputime(KINFO *k, VARENT *ve)
4354b88c807SRodney W. Grimes {
4364b88c807SRodney W. Grimes 	VAR *v;
4374b88c807SRodney W. Grimes 	long secs;
4384b88c807SRodney W. Grimes 	long psecs;	/* "parts" of a second. first micro, then centi */
4394b88c807SRodney W. Grimes 	char obuff[128];
4408073a93cSAndrey A. Chernov 	static char decimal_point = 0;
4414b88c807SRodney W. Grimes 
4428073a93cSAndrey A. Chernov 	if (!decimal_point)
4438073a93cSAndrey A. Chernov 		decimal_point = localeconv()->decimal_point[0];
4444b88c807SRodney W. Grimes 	v = ve->var;
4451f7d2501SKirk McKusick 	if (k->ki_p->ki_stat == SZOMB || !k->ki_valid) {
4465832a752SBruce Evans 		secs = 0;
4475832a752SBruce Evans 		psecs = 0;
4484b88c807SRodney W. Grimes 	} else {
4494b88c807SRodney W. Grimes 		/*
4504b88c807SRodney W. Grimes 		 * This counts time spent handling interrupts.  We could
4514b88c807SRodney W. Grimes 		 * fix this, but it is not 100% trivial (and interrupt
4524b88c807SRodney W. Grimes 		 * time fractions only work on the sparc anyway).	XXX
4534b88c807SRodney W. Grimes 		 */
4541f7d2501SKirk McKusick 		secs = k->ki_p->ki_runtime / 1000000;
4551f7d2501SKirk McKusick 		psecs = k->ki_p->ki_runtime % 1000000;
4564b88c807SRodney W. Grimes 		if (sumrusage) {
4571f7d2501SKirk McKusick 			secs += k->ki_p->ki_childtime.tv_sec;
4581f7d2501SKirk McKusick 			psecs += k->ki_p->ki_childtime.tv_usec;
4594b88c807SRodney W. Grimes 		}
4604b88c807SRodney W. Grimes 		/*
4614b88c807SRodney W. Grimes 		 * round and scale to 100's
4624b88c807SRodney W. Grimes 		 */
4635832a752SBruce Evans 		psecs = (psecs + 5000) / 10000;
4645832a752SBruce Evans 		secs += psecs / 100;
4655832a752SBruce Evans 		psecs = psecs % 100;
4665832a752SBruce Evans 	}
4674b88c807SRodney W. Grimes 	(void)snprintf(obuff, sizeof(obuff),
4688073a93cSAndrey A. Chernov 	    "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
4694b88c807SRodney W. Grimes 	(void)printf("%*s", v->width, obuff);
4704b88c807SRodney W. Grimes }
4714b88c807SRodney W. Grimes 
4724b88c807SRodney W. Grimes double
473871e8d8cSMark Murray getpcpu(const KINFO *k)
4744b88c807SRodney W. Grimes {
4754b88c807SRodney W. Grimes 	static int failure;
4764b88c807SRodney W. Grimes 
4774b88c807SRodney W. Grimes 	if (!nlistread)
4784b88c807SRodney W. Grimes 		failure = donlist();
4794b88c807SRodney W. Grimes 	if (failure)
4804b88c807SRodney W. Grimes 		return (0.0);
4814b88c807SRodney W. Grimes 
4824b88c807SRodney W. Grimes #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
4834b88c807SRodney W. Grimes 
4844b88c807SRodney W. Grimes 	/* XXX - I don't like this */
485e0aa5ab7SJohn Baldwin 	if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0)
4864b88c807SRodney W. Grimes 		return (0.0);
4874b88c807SRodney W. Grimes 	if (rawcpu)
4881f7d2501SKirk McKusick 		return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
4891f7d2501SKirk McKusick 	return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
4901f7d2501SKirk McKusick 		(1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
4914b88c807SRodney W. Grimes }
4924b88c807SRodney W. Grimes 
4934b88c807SRodney W. Grimes void
49446251ddeSWarner Losh pcpu(KINFO *k, VARENT *ve)
4954b88c807SRodney W. Grimes {
4964b88c807SRodney W. Grimes 	VAR *v;
4974b88c807SRodney W. Grimes 
4984b88c807SRodney W. Grimes 	v = ve->var;
4994b88c807SRodney W. Grimes 	(void)printf("%*.1f", v->width, getpcpu(k));
5004b88c807SRodney W. Grimes }
5014b88c807SRodney W. Grimes 
502871e8d8cSMark Murray static double
50346251ddeSWarner Losh getpmem(KINFO *k)
5044b88c807SRodney W. Grimes {
5054b88c807SRodney W. Grimes 	static int failure;
5064b88c807SRodney W. Grimes 	double fracmem;
5074b88c807SRodney W. Grimes 
5084b88c807SRodney W. Grimes 	if (!nlistread)
5094b88c807SRodney W. Grimes 		failure = donlist();
5104b88c807SRodney W. Grimes 	if (failure)
5114b88c807SRodney W. Grimes 		return (0.0);
5124b88c807SRodney W. Grimes 
513e0aa5ab7SJohn Baldwin 	if ((k->ki_p->ki_sflag & PS_INMEM) == 0)
5144b88c807SRodney W. Grimes 		return (0.0);
5154b88c807SRodney W. Grimes 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
5164b88c807SRodney W. Grimes 	/* XXX don't have info about shared */
51785012e7cSPeter Wemm 	fracmem = ((float)k->ki_p->ki_rssize)/mempages;
5184b88c807SRodney W. Grimes 	return (100.0 * fracmem);
5194b88c807SRodney W. Grimes }
5204b88c807SRodney W. Grimes 
5214b88c807SRodney W. Grimes void
52246251ddeSWarner Losh pmem(KINFO *k, VARENT *ve)
5234b88c807SRodney W. Grimes {
5244b88c807SRodney W. Grimes 	VAR *v;
5254b88c807SRodney W. Grimes 
5264b88c807SRodney W. Grimes 	v = ve->var;
5274b88c807SRodney W. Grimes 	(void)printf("%*.1f", v->width, getpmem(k));
5284b88c807SRodney W. Grimes }
5294b88c807SRodney W. Grimes 
5304b88c807SRodney W. Grimes void
53146251ddeSWarner Losh pagein(KINFO *k, VARENT *ve)
5324b88c807SRodney W. Grimes {
5334b88c807SRodney W. Grimes 	VAR *v;
5344b88c807SRodney W. Grimes 
5354b88c807SRodney W. Grimes 	v = ve->var;
5360fd510b7SJoerg Wunsch 	(void)printf("%*ld", v->width,
5371f7d2501SKirk McKusick 	    k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
5384b88c807SRodney W. Grimes }
5394b88c807SRodney W. Grimes 
540871e8d8cSMark Murray /* ARGSUSED */
5414b88c807SRodney W. Grimes void
542871e8d8cSMark Murray maxrss(KINFO *k __unused, VARENT *ve)
5434b88c807SRodney W. Grimes {
5444b88c807SRodney W. Grimes 	VAR *v;
5454b88c807SRodney W. Grimes 
5464b88c807SRodney W. Grimes 	v = ve->var;
547940cca66SPeter Wemm 	/* XXX not yet */
5484b88c807SRodney W. Grimes 	(void)printf("%*s", v->width, "-");
5494b88c807SRodney W. Grimes }
5504b88c807SRodney W. Grimes 
5514b88c807SRodney W. Grimes void
55246251ddeSWarner Losh tsize(KINFO *k, VARENT *ve)
5534b88c807SRodney W. Grimes {
5544b88c807SRodney W. Grimes 	VAR *v;
5554b88c807SRodney W. Grimes 
5564b88c807SRodney W. Grimes 	v = ve->var;
5571f7d2501SKirk McKusick 	(void)printf("%*ld", v->width, (long)pgtok(k->ki_p->ki_tsize));
5584b88c807SRodney W. Grimes }
5594b88c807SRodney W. Grimes 
560ad863cacSSteve Price void
56146251ddeSWarner Losh priorityr(KINFO *k, VARENT *ve)
562ad863cacSSteve Price {
563ad863cacSSteve Price 	VAR *v;
564871e8d8cSMark Murray 	struct priority *lpri;
565ad863cacSSteve Price 	char str[8];
5664c85452bSJake Burkholder 	unsigned class, level;
567ad863cacSSteve Price 
568ad863cacSSteve Price 	v = ve->var;
569871e8d8cSMark Murray 	lpri = (struct priority *) ((char *)k + v->off);
570871e8d8cSMark Murray 	class = lpri->pri_class;
571871e8d8cSMark Murray 	level = lpri->pri_level;
5724c85452bSJake Burkholder 	switch (class) {
5734c85452bSJake Burkholder 	case PRI_REALTIME:
5744c85452bSJake Burkholder 		snprintf(str, sizeof(str), "real:%u", level);
575ad863cacSSteve Price 		break;
5764c85452bSJake Burkholder 	case PRI_TIMESHARE:
577ad863cacSSteve Price 		strncpy(str, "normal", sizeof(str));
578ad863cacSSteve Price 		break;
5794c85452bSJake Burkholder 	case PRI_IDLE:
5804c85452bSJake Burkholder 		snprintf(str, sizeof(str), "idle:%u", level);
581ad863cacSSteve Price 		break;
582ad863cacSSteve Price 	default:
5834c85452bSJake Burkholder 		snprintf(str, sizeof(str), "%u:%u", class, level);
584ad863cacSSteve Price 		break;
585ad863cacSSteve Price 	}
586ad863cacSSteve Price 	str[sizeof(str) - 1] = '\0';
587ad863cacSSteve Price 	(void)printf("%*s", v->width, str);
588ad863cacSSteve Price }
589ad863cacSSteve Price 
5904b88c807SRodney W. Grimes /*
5914b88c807SRodney W. Grimes  * Generic output routines.  Print fields from various prototype
5924b88c807SRodney W. Grimes  * structures.
5934b88c807SRodney W. Grimes  */
5944b88c807SRodney W. Grimes static void
59546251ddeSWarner Losh printval(char *bp, VAR *v)
5964b88c807SRodney W. Grimes {
5974b88c807SRodney W. Grimes 	static char ofmt[32] = "%";
5984b88c807SRodney W. Grimes 	char *fcp, *cp;
5994b88c807SRodney W. Grimes 
6004b88c807SRodney W. Grimes 	cp = ofmt + 1;
6014b88c807SRodney W. Grimes 	fcp = v->fmt;
6024b88c807SRodney W. Grimes 	if (v->flag & LJUST)
6034b88c807SRodney W. Grimes 		*cp++ = '-';
6044b88c807SRodney W. Grimes 	*cp++ = '*';
6050fd510b7SJoerg Wunsch 	while ((*cp++ = *fcp++));
6064b88c807SRodney W. Grimes 
6074b88c807SRodney W. Grimes 	switch (v->type) {
6084b88c807SRodney W. Grimes 	case CHAR:
6094b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(char *)bp);
6104b88c807SRodney W. Grimes 		break;
6114b88c807SRodney W. Grimes 	case UCHAR:
6124b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_char *)bp);
6134b88c807SRodney W. Grimes 		break;
6144b88c807SRodney W. Grimes 	case SHORT:
6154b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(short *)bp);
6164b88c807SRodney W. Grimes 		break;
6174b88c807SRodney W. Grimes 	case USHORT:
6184b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_short *)bp);
6194b88c807SRodney W. Grimes 		break;
6203929d518SDoug Rabson 	case INT:
6213929d518SDoug Rabson 		(void)printf(ofmt, v->width, *(int *)bp);
6223929d518SDoug Rabson 		break;
6233929d518SDoug Rabson 	case UINT:
6243929d518SDoug Rabson 		(void)printf(ofmt, v->width, *(u_int *)bp);
6253929d518SDoug Rabson 		break;
6264b88c807SRodney W. Grimes 	case LONG:
6274b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(long *)bp);
6284b88c807SRodney W. Grimes 		break;
6294b88c807SRodney W. Grimes 	case ULONG:
6304b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_long *)bp);
6314b88c807SRodney W. Grimes 		break;
6324b88c807SRodney W. Grimes 	case KPTR:
6337d1192a7SPeter Wemm 		(void)printf(ofmt, v->width, *(u_long *)bp);
6344b88c807SRodney W. Grimes 		break;
6354b88c807SRodney W. Grimes 	default:
6364b88c807SRodney W. Grimes 		errx(1, "unknown type %d", v->type);
6374b88c807SRodney W. Grimes 	}
6384b88c807SRodney W. Grimes }
6394b88c807SRodney W. Grimes 
6404b88c807SRodney W. Grimes void
64146251ddeSWarner Losh kvar(KINFO *k, VARENT *ve)
6424b88c807SRodney W. Grimes {
6434b88c807SRodney W. Grimes 	VAR *v;
6444b88c807SRodney W. Grimes 
6454b88c807SRodney W. Grimes 	v = ve->var;
6461f7d2501SKirk McKusick 	printval((char *)((char *)k->ki_p + v->off), v);
6474b88c807SRodney W. Grimes }
6484b88c807SRodney W. Grimes 
6494b88c807SRodney W. Grimes void
65046251ddeSWarner Losh rvar(KINFO *k, VARENT *ve)
6514b88c807SRodney W. Grimes {
6524b88c807SRodney W. Grimes 	VAR *v;
6534b88c807SRodney W. Grimes 
6544b88c807SRodney W. Grimes 	v = ve->var;
6551f7d2501SKirk McKusick 	if (k->ki_valid)
6561f7d2501SKirk McKusick 		printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
6574b88c807SRodney W. Grimes 	else
6584b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "-");
6594b88c807SRodney W. Grimes }
6607304f61fSBrian Feldman 
6617304f61fSBrian Feldman void
66246251ddeSWarner Losh lattr(KINFO *k, VARENT *ve)
6637304f61fSBrian Feldman {
6647304f61fSBrian Feldman 	VAR *v;
6657304f61fSBrian Feldman 
6667304f61fSBrian Feldman 	v = ve->var;
6677304f61fSBrian Feldman 	(void)printf("%-*d", (int)v->width, get_lattr(k->ki_p->ki_pid));
6687304f61fSBrian Feldman }
669