xref: /freebsd/bin/ps/ps.c (revision 8b6f5f5f9ff0be54b48eed8dc4dad8c3cc9110e4)
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.
3289730b29SDavid Greenman  *
338b6f5f5fSDavid Greenman  *	$Id: ps.c,v 1.3 1994/10/02 08:19:13 davidg Exp $
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #ifndef lint
374b88c807SRodney W. Grimes static char copyright[] =
384b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\
394b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
404b88c807SRodney W. Grimes #endif /* not lint */
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes #ifndef lint
434b88c807SRodney W. Grimes static char sccsid[] = "@(#)ps.c	8.4 (Berkeley) 4/2/94";
444b88c807SRodney W. Grimes #endif /* not lint */
454b88c807SRodney W. Grimes 
464b88c807SRodney W. Grimes #include <sys/param.h>
478b6f5f5fSDavid Greenman #include <sys/user.h>
484b88c807SRodney W. Grimes #include <sys/time.h>
494b88c807SRodney W. Grimes #include <sys/resource.h>
504b88c807SRodney W. Grimes #include <sys/proc.h>
514b88c807SRodney W. Grimes #include <sys/stat.h>
524b88c807SRodney W. Grimes #include <sys/ioctl.h>
534b88c807SRodney W. Grimes #include <sys/sysctl.h>
544b88c807SRodney W. Grimes 
554b88c807SRodney W. Grimes #include <ctype.h>
564b88c807SRodney W. Grimes #include <err.h>
574b88c807SRodney W. Grimes #include <errno.h>
584b88c807SRodney W. Grimes #include <fcntl.h>
594b88c807SRodney W. Grimes #include <kvm.h>
604b88c807SRodney W. Grimes #include <nlist.h>
614b88c807SRodney W. Grimes #include <paths.h>
624b88c807SRodney W. Grimes #include <stdio.h>
634b88c807SRodney W. Grimes #include <stdlib.h>
644b88c807SRodney W. Grimes #include <string.h>
654b88c807SRodney W. Grimes #include <unistd.h>
664b88c807SRodney W. Grimes 
674b88c807SRodney W. Grimes #include "ps.h"
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes #ifdef P_PPWAIT
704b88c807SRodney W. Grimes #define NEWVM
714b88c807SRodney W. Grimes #endif
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes KINFO *kinfo;
744b88c807SRodney W. Grimes struct varent *vhead, *vtail;
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes int	eval;			/* exit value */
774b88c807SRodney W. Grimes int	rawcpu;			/* -C */
784b88c807SRodney W. Grimes int	sumrusage;		/* -S */
794b88c807SRodney W. Grimes int	termwidth;		/* width of screen (0 == infinity) */
804b88c807SRodney W. Grimes int	totwidth;		/* calculated width of requested variables */
814b88c807SRodney W. Grimes 
824b88c807SRodney W. Grimes static int needuser, needcomm, needenv;
834b88c807SRodney W. Grimes 
844b88c807SRodney W. Grimes enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
854b88c807SRodney W. Grimes 
864b88c807SRodney W. Grimes static char	*fmt __P((char **(*)(kvm_t *, const struct kinfo_proc *, int),
874b88c807SRodney W. Grimes 		    KINFO *, char *, int));
884b88c807SRodney W. Grimes static char	*kludge_oldps_options __P((char *));
894b88c807SRodney W. Grimes static int	 pscomp __P((const void *, const void *));
904b88c807SRodney W. Grimes static void	 saveuser __P((KINFO *));
914b88c807SRodney W. Grimes static void	 scanvars __P((void));
924b88c807SRodney W. Grimes static void	 usage __P((void));
934b88c807SRodney W. Grimes 
944b88c807SRodney W. Grimes char dfmt[] = "pid tt state time command";
954b88c807SRodney W. Grimes char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
964b88c807SRodney W. Grimes char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
974b88c807SRodney W. Grimes char   o1[] = "pid";
984b88c807SRodney W. Grimes char   o2[] = "tt state time command";
994b88c807SRodney W. Grimes char ufmt[] = "user pid %cpu %mem vsz rss tt state start time command";
1004b88c807SRodney W. Grimes char vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command";
1014b88c807SRodney W. Grimes 
1024b88c807SRodney W. Grimes kvm_t *kd;
1034b88c807SRodney W. Grimes 
1044b88c807SRodney W. Grimes int
1054b88c807SRodney W. Grimes main(argc, argv)
1064b88c807SRodney W. Grimes 	int argc;
1074b88c807SRodney W. Grimes 	char *argv[];
1084b88c807SRodney W. Grimes {
1094b88c807SRodney W. Grimes 	struct kinfo_proc *kp;
1104b88c807SRodney W. Grimes 	struct varent *vent;
1114b88c807SRodney W. Grimes 	struct winsize ws;
1124b88c807SRodney W. Grimes 	dev_t ttydev;
1134b88c807SRodney W. Grimes 	pid_t pid;
1144b88c807SRodney W. Grimes 	uid_t uid;
1154b88c807SRodney W. Grimes 	int all, ch, flag, i, fmt, lineno, nentries;
1164b88c807SRodney W. Grimes 	int prtheader, wflag, what, xflg;
1174b88c807SRodney W. Grimes 	char *nlistf, *memf, *swapf, errbuf[256];
1184b88c807SRodney W. Grimes 
1194b88c807SRodney W. Grimes 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1204b88c807SRodney W. Grimes 	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1214b88c807SRodney W. Grimes 	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
1224b88c807SRodney W. Grimes 	     ws.ws_col == 0)
1234b88c807SRodney W. Grimes 		termwidth = 79;
1244b88c807SRodney W. Grimes 	else
1254b88c807SRodney W. Grimes 		termwidth = ws.ws_col - 1;
1264b88c807SRodney W. Grimes 
1274b88c807SRodney W. Grimes 	if (argc > 1)
1284b88c807SRodney W. Grimes 		argv[1] = kludge_oldps_options(argv[1]);
1294b88c807SRodney W. Grimes 
1304b88c807SRodney W. Grimes 	all = fmt = prtheader = wflag = xflg = 0;
1314b88c807SRodney W. Grimes 	pid = -1;
1324b88c807SRodney W. Grimes 	uid = (uid_t) -1;
1334b88c807SRodney W. Grimes 	ttydev = NODEV;
1344b88c807SRodney W. Grimes 	memf = nlistf = swapf = NULL;
1354b88c807SRodney W. Grimes 	while ((ch = getopt(argc, argv,
1364b88c807SRodney W. Grimes 	    "aCeghjLlM:mN:O:o:p:rSTt:uvW:wx")) != EOF)
1374b88c807SRodney W. Grimes 		switch((char)ch) {
1384b88c807SRodney W. Grimes 		case 'a':
1394b88c807SRodney W. Grimes 			all = 1;
1404b88c807SRodney W. Grimes 			break;
1414b88c807SRodney W. Grimes 		case 'e':			/* XXX set ufmt */
1424b88c807SRodney W. Grimes 			needenv = 1;
1434b88c807SRodney W. Grimes 			break;
1444b88c807SRodney W. Grimes 		case 'C':
1454b88c807SRodney W. Grimes 			rawcpu = 1;
1464b88c807SRodney W. Grimes 			break;
1474b88c807SRodney W. Grimes 		case 'g':
1484b88c807SRodney W. Grimes 			break;			/* no-op */
1494b88c807SRodney W. Grimes 		case 'h':
1504b88c807SRodney W. Grimes 			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
1514b88c807SRodney W. Grimes 			break;
1524b88c807SRodney W. Grimes 		case 'j':
1534b88c807SRodney W. Grimes 			parsefmt(jfmt);
1544b88c807SRodney W. Grimes 			fmt = 1;
1554b88c807SRodney W. Grimes 			jfmt[0] = '\0';
1564b88c807SRodney W. Grimes 			break;
1574b88c807SRodney W. Grimes 		case 'L':
1584b88c807SRodney W. Grimes 			showkey();
1594b88c807SRodney W. Grimes 			exit(0);
1604b88c807SRodney W. Grimes 		case 'l':
1614b88c807SRodney W. Grimes 			parsefmt(lfmt);
1624b88c807SRodney W. Grimes 			fmt = 1;
1634b88c807SRodney W. Grimes 			lfmt[0] = '\0';
1644b88c807SRodney W. Grimes 			break;
1654b88c807SRodney W. Grimes 		case 'M':
1664b88c807SRodney W. Grimes 			memf = optarg;
1674b88c807SRodney W. Grimes 			break;
1684b88c807SRodney W. Grimes 		case 'm':
1694b88c807SRodney W. Grimes 			sortby = SORTMEM;
1704b88c807SRodney W. Grimes 			break;
1714b88c807SRodney W. Grimes 		case 'N':
1724b88c807SRodney W. Grimes 			nlistf = optarg;
1734b88c807SRodney W. Grimes 			break;
1744b88c807SRodney W. Grimes 		case 'O':
1754b88c807SRodney W. Grimes 			parsefmt(o1);
1764b88c807SRodney W. Grimes 			parsefmt(optarg);
1774b88c807SRodney W. Grimes 			parsefmt(o2);
1784b88c807SRodney W. Grimes 			o1[0] = o2[0] = '\0';
1794b88c807SRodney W. Grimes 			fmt = 1;
1804b88c807SRodney W. Grimes 			break;
1814b88c807SRodney W. Grimes 		case 'o':
1824b88c807SRodney W. Grimes 			parsefmt(optarg);
1834b88c807SRodney W. Grimes 			fmt = 1;
1844b88c807SRodney W. Grimes 			break;
1854b88c807SRodney W. Grimes 		case 'p':
1864b88c807SRodney W. Grimes 			pid = atol(optarg);
1874b88c807SRodney W. Grimes 			xflg = 1;
1884b88c807SRodney W. Grimes 			break;
1894b88c807SRodney W. Grimes 		case 'r':
1904b88c807SRodney W. Grimes 			sortby = SORTCPU;
1914b88c807SRodney W. Grimes 			break;
1924b88c807SRodney W. Grimes 		case 'S':
1934b88c807SRodney W. Grimes 			sumrusage = 1;
1944b88c807SRodney W. Grimes 			break;
1954b88c807SRodney W. Grimes 		case 'T':
1964b88c807SRodney W. Grimes 			if ((optarg = ttyname(STDIN_FILENO)) == NULL)
1974b88c807SRodney W. Grimes 				errx(1, "stdin: not a terminal");
1984b88c807SRodney W. Grimes 			/* FALLTHROUGH */
1994b88c807SRodney W. Grimes 		case 't': {
2004b88c807SRodney W. Grimes 			struct stat sb;
2014b88c807SRodney W. Grimes 			char *ttypath, pathbuf[MAXPATHLEN];
2024b88c807SRodney W. Grimes 
2034b88c807SRodney W. Grimes 			if (strcmp(optarg, "co") == 0)
2044b88c807SRodney W. Grimes 				ttypath = _PATH_CONSOLE;
2054b88c807SRodney W. Grimes 			else if (*optarg != '/')
2064b88c807SRodney W. Grimes 				(void)snprintf(ttypath = pathbuf,
2074b88c807SRodney W. Grimes 				    sizeof(pathbuf), "%s%s", _PATH_TTY, optarg);
2084b88c807SRodney W. Grimes 			else
2094b88c807SRodney W. Grimes 				ttypath = optarg;
2104b88c807SRodney W. Grimes 			if (stat(ttypath, &sb) == -1)
2114b88c807SRodney W. Grimes 				err(1, "%s", ttypath);
2124b88c807SRodney W. Grimes 			if (!S_ISCHR(sb.st_mode))
2134b88c807SRodney W. Grimes 				errx(1, "%s: not a terminal", ttypath);
2144b88c807SRodney W. Grimes 			ttydev = sb.st_rdev;
2154b88c807SRodney W. Grimes 			break;
2164b88c807SRodney W. Grimes 		}
2174b88c807SRodney W. Grimes 		case 'u':
2184b88c807SRodney W. Grimes 			parsefmt(ufmt);
2194b88c807SRodney W. Grimes 			sortby = SORTCPU;
2204b88c807SRodney W. Grimes 			fmt = 1;
2214b88c807SRodney W. Grimes 			ufmt[0] = '\0';
2224b88c807SRodney W. Grimes 			break;
2234b88c807SRodney W. Grimes 		case 'v':
2244b88c807SRodney W. Grimes 			parsefmt(vfmt);
2254b88c807SRodney W. Grimes 			sortby = SORTMEM;
2264b88c807SRodney W. Grimes 			fmt = 1;
2274b88c807SRodney W. Grimes 			vfmt[0] = '\0';
2284b88c807SRodney W. Grimes 			break;
2294b88c807SRodney W. Grimes 		case 'W':
2304b88c807SRodney W. Grimes 			swapf = optarg;
2314b88c807SRodney W. Grimes 			break;
2324b88c807SRodney W. Grimes 		case 'w':
2334b88c807SRodney W. Grimes 			if (wflag)
2344b88c807SRodney W. Grimes 				termwidth = UNLIMITED;
2354b88c807SRodney W. Grimes 			else if (termwidth < 131)
2364b88c807SRodney W. Grimes 				termwidth = 131;
2374b88c807SRodney W. Grimes 			wflag++;
2384b88c807SRodney W. Grimes 			break;
2394b88c807SRodney W. Grimes 		case 'x':
2404b88c807SRodney W. Grimes 			xflg = 1;
2414b88c807SRodney W. Grimes 			break;
2424b88c807SRodney W. Grimes 		case '?':
2434b88c807SRodney W. Grimes 		default:
2444b88c807SRodney W. Grimes 			usage();
2454b88c807SRodney W. Grimes 		}
2464b88c807SRodney W. Grimes 	argc -= optind;
2474b88c807SRodney W. Grimes 	argv += optind;
2484b88c807SRodney W. Grimes 
2494b88c807SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
2504b88c807SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
2514b88c807SRodney W. Grimes 	if (*argv) {
2524b88c807SRodney W. Grimes 		nlistf = *argv;
2534b88c807SRodney W. Grimes 		if (*++argv) {
2544b88c807SRodney W. Grimes 			memf = *argv;
2554b88c807SRodney W. Grimes 			if (*++argv)
2564b88c807SRodney W. Grimes 				swapf = *argv;
2574b88c807SRodney W. Grimes 		}
2584b88c807SRodney W. Grimes 	}
2594b88c807SRodney W. Grimes #endif
2604b88c807SRodney W. Grimes 	/*
2614b88c807SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
2624b88c807SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
2634b88c807SRodney W. Grimes 	 */
2644b88c807SRodney W. Grimes 	if (nlistf != NULL || memf != NULL || swapf != NULL)
2654b88c807SRodney W. Grimes 		setgid(getgid());
2664b88c807SRodney W. Grimes 
2674b88c807SRodney W. Grimes 	kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf);
2684b88c807SRodney W. Grimes 	if (kd == 0)
2694b88c807SRodney W. Grimes 		errx(1, "%s", errbuf);
2704b88c807SRodney W. Grimes 
2714b88c807SRodney W. Grimes 	if (!fmt)
2724b88c807SRodney W. Grimes 		parsefmt(dfmt);
2734b88c807SRodney W. Grimes 
2744b88c807SRodney W. Grimes 	if (!all && ttydev == NODEV && pid == -1)  /* XXX - should be cleaner */
2754b88c807SRodney W. Grimes 		uid = getuid();
2764b88c807SRodney W. Grimes 
2774b88c807SRodney W. Grimes 	/*
2784b88c807SRodney W. Grimes 	 * scan requested variables, noting what structures are needed,
2794b88c807SRodney W. Grimes 	 * and adjusting header widths as appropiate.
2804b88c807SRodney W. Grimes 	 */
2814b88c807SRodney W. Grimes 	scanvars();
2824b88c807SRodney W. Grimes 	/*
2834b88c807SRodney W. Grimes 	 * get proc list
2844b88c807SRodney W. Grimes 	 */
2854b88c807SRodney W. Grimes 	if (uid != (uid_t) -1) {
2864b88c807SRodney W. Grimes 		what = KERN_PROC_UID;
2874b88c807SRodney W. Grimes 		flag = uid;
2884b88c807SRodney W. Grimes 	} else if (ttydev != NODEV) {
2894b88c807SRodney W. Grimes 		what = KERN_PROC_TTY;
2904b88c807SRodney W. Grimes 		flag = ttydev;
2914b88c807SRodney W. Grimes 	} else if (pid != -1) {
2924b88c807SRodney W. Grimes 		what = KERN_PROC_PID;
2934b88c807SRodney W. Grimes 		flag = pid;
2944b88c807SRodney W. Grimes 	} else {
2954b88c807SRodney W. Grimes 		what = KERN_PROC_ALL;
2964b88c807SRodney W. Grimes 		flag = 0;
2974b88c807SRodney W. Grimes 	}
2984b88c807SRodney W. Grimes 	/*
2994b88c807SRodney W. Grimes 	 * select procs
3004b88c807SRodney W. Grimes 	 */
3014b88c807SRodney W. Grimes 	if ((kp = kvm_getprocs(kd, what, flag, &nentries)) == 0)
3024b88c807SRodney W. Grimes 		errx(1, "%s", kvm_geterr(kd));
3034b88c807SRodney W. Grimes 	if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL)
3044b88c807SRodney W. Grimes 		err(1, NULL);
3054b88c807SRodney W. Grimes 	for (i = nentries; --i >= 0; ++kp) {
3064b88c807SRodney W. Grimes 		kinfo[i].ki_p = kp;
3074b88c807SRodney W. Grimes 		if (needuser)
3084b88c807SRodney W. Grimes 			saveuser(&kinfo[i]);
3094b88c807SRodney W. Grimes 	}
3104b88c807SRodney W. Grimes 	/*
3114b88c807SRodney W. Grimes 	 * print header
3124b88c807SRodney W. Grimes 	 */
3134b88c807SRodney W. Grimes 	printheader();
3144b88c807SRodney W. Grimes 	if (nentries == 0)
3154b88c807SRodney W. Grimes 		exit(0);
3164b88c807SRodney W. Grimes 	/*
3174b88c807SRodney W. Grimes 	 * sort proc list
3184b88c807SRodney W. Grimes 	 */
3194b88c807SRodney W. Grimes 	qsort(kinfo, nentries, sizeof(KINFO), pscomp);
3204b88c807SRodney W. Grimes 	/*
3214b88c807SRodney W. Grimes 	 * for each proc, call each variable output function.
3224b88c807SRodney W. Grimes 	 */
3234b88c807SRodney W. Grimes 	for (i = lineno = 0; i < nentries; i++) {
3244b88c807SRodney W. Grimes 		if (xflg == 0 && (KI_EPROC(&kinfo[i])->e_tdev == NODEV ||
3254b88c807SRodney W. Grimes 		    (KI_PROC(&kinfo[i])->p_flag & P_CONTROLT ) == 0))
3264b88c807SRodney W. Grimes 			continue;
3274b88c807SRodney W. Grimes 		for (vent = vhead; vent; vent = vent->next) {
3284b88c807SRodney W. Grimes 			(vent->var->oproc)(&kinfo[i], vent);
3294b88c807SRodney W. Grimes 			if (vent->next != NULL)
3304b88c807SRodney W. Grimes 				(void)putchar(' ');
3314b88c807SRodney W. Grimes 		}
3324b88c807SRodney W. Grimes 		(void)putchar('\n');
3334b88c807SRodney W. Grimes 		if (prtheader && lineno++ == prtheader - 4) {
3344b88c807SRodney W. Grimes 			(void)putchar('\n');
3354b88c807SRodney W. Grimes 			printheader();
3364b88c807SRodney W. Grimes 			lineno = 0;
3374b88c807SRodney W. Grimes 		}
3384b88c807SRodney W. Grimes 	}
3394b88c807SRodney W. Grimes 	exit(eval);
3404b88c807SRodney W. Grimes }
3414b88c807SRodney W. Grimes 
3424b88c807SRodney W. Grimes static void
3434b88c807SRodney W. Grimes scanvars()
3444b88c807SRodney W. Grimes {
3454b88c807SRodney W. Grimes 	struct varent *vent;
3464b88c807SRodney W. Grimes 	VAR *v;
3474b88c807SRodney W. Grimes 	int i;
3484b88c807SRodney W. Grimes 
3494b88c807SRodney W. Grimes 	for (vent = vhead; vent; vent = vent->next) {
3504b88c807SRodney W. Grimes 		v = vent->var;
3514b88c807SRodney W. Grimes 		i = strlen(v->header);
3524b88c807SRodney W. Grimes 		if (v->width < i)
3534b88c807SRodney W. Grimes 			v->width = i;
3544b88c807SRodney W. Grimes 		totwidth += v->width + 1;	/* +1 for space */
3554b88c807SRodney W. Grimes 		if (v->flag & USER)
3564b88c807SRodney W. Grimes 			needuser = 1;
3574b88c807SRodney W. Grimes 		if (v->flag & COMM)
3584b88c807SRodney W. Grimes 			needcomm = 1;
3594b88c807SRodney W. Grimes 	}
3604b88c807SRodney W. Grimes 	totwidth--;
3614b88c807SRodney W. Grimes }
3624b88c807SRodney W. Grimes 
3634b88c807SRodney W. Grimes static char *
3644b88c807SRodney W. Grimes fmt(fn, ki, comm, maxlen)
3654b88c807SRodney W. Grimes 	char **(*fn) __P((kvm_t *, const struct kinfo_proc *, int));
3664b88c807SRodney W. Grimes 	KINFO *ki;
3674b88c807SRodney W. Grimes 	char *comm;
3684b88c807SRodney W. Grimes 	int maxlen;
3694b88c807SRodney W. Grimes {
3704b88c807SRodney W. Grimes 	char *s;
3714b88c807SRodney W. Grimes 
3724b88c807SRodney W. Grimes 	if ((s =
3734b88c807SRodney W. Grimes 	    fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen)) == NULL)
3744b88c807SRodney W. Grimes 		err(1, NULL);
3754b88c807SRodney W. Grimes 	return (s);
3764b88c807SRodney W. Grimes }
3774b88c807SRodney W. Grimes 
3784b88c807SRodney W. Grimes static void
3794b88c807SRodney W. Grimes saveuser(ki)
3804b88c807SRodney W. Grimes 	KINFO *ki;
3814b88c807SRodney W. Grimes {
3824b88c807SRodney W. Grimes 	struct pstats pstats;
3834b88c807SRodney W. Grimes 	struct usave *usp;
3844b88c807SRodney W. Grimes 
3854b88c807SRodney W. Grimes 	usp = &ki->ki_u;
3864b88c807SRodney W. Grimes 	if (kvm_read(kd, (u_long)&KI_PROC(ki)->p_addr->u_stats,
3874b88c807SRodney W. Grimes 	    (char *)&pstats, sizeof(pstats)) == sizeof(pstats)) {
3884b88c807SRodney W. Grimes 		/*
3894b88c807SRodney W. Grimes 		 * The u-area might be swapped out, and we can't get
3904b88c807SRodney W. Grimes 		 * at it because we have a crashdump and no swap.
3914b88c807SRodney W. Grimes 		 * If it's here fill in these fields, otherwise, just
3924b88c807SRodney W. Grimes 		 * leave them 0.
3934b88c807SRodney W. Grimes 		 */
3944b88c807SRodney W. Grimes 		usp->u_start = pstats.p_start;
3954b88c807SRodney W. Grimes 		usp->u_ru = pstats.p_ru;
3964b88c807SRodney W. Grimes 		usp->u_cru = pstats.p_cru;
3974b88c807SRodney W. Grimes 		usp->u_valid = 1;
3984b88c807SRodney W. Grimes 	} else
3994b88c807SRodney W. Grimes 		usp->u_valid = 0;
4004b88c807SRodney W. Grimes 	/*
4014b88c807SRodney W. Grimes 	 * save arguments if needed
4024b88c807SRodney W. Grimes 	 */
4034b88c807SRodney W. Grimes 	if (needcomm)
4044b88c807SRodney W. Grimes 		ki->ki_args = fmt(kvm_getargv, ki, KI_PROC(ki)->p_comm,
4054b88c807SRodney W. Grimes 		    MAXCOMLEN);
4064b88c807SRodney W. Grimes 	else
4074b88c807SRodney W. Grimes 		ki->ki_args = NULL;
4084b88c807SRodney W. Grimes 	if (needenv)
4094b88c807SRodney W. Grimes 		ki->ki_env = fmt(kvm_getenvv, ki, (char *)NULL, 0);
4104b88c807SRodney W. Grimes 	else
4114b88c807SRodney W. Grimes 		ki->ki_env = NULL;
4124b88c807SRodney W. Grimes }
4134b88c807SRodney W. Grimes 
4144b88c807SRodney W. Grimes static int
4154b88c807SRodney W. Grimes pscomp(a, b)
4164b88c807SRodney W. Grimes 	const void *a, *b;
4174b88c807SRodney W. Grimes {
4184b88c807SRodney W. Grimes 	int i;
4194b88c807SRodney W. Grimes #ifdef NEWVM
4204b88c807SRodney W. Grimes #define VSIZE(k) (KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize + \
4214b88c807SRodney W. Grimes 		  KI_EPROC(k)->e_vm.vm_tsize)
4224b88c807SRodney W. Grimes #else
4234b88c807SRodney W. Grimes #define VSIZE(k) ((k)->ki_p->p_dsize + (k)->ki_p->p_ssize + (k)->ki_e->e_xsize)
4244b88c807SRodney W. Grimes #endif
4254b88c807SRodney W. Grimes 
4264b88c807SRodney W. Grimes 	if (sortby == SORTCPU)
4274b88c807SRodney W. Grimes 		return (getpcpu((KINFO *)b) - getpcpu((KINFO *)a));
4284b88c807SRodney W. Grimes 	if (sortby == SORTMEM)
4294b88c807SRodney W. Grimes 		return (VSIZE((KINFO *)b) - VSIZE((KINFO *)a));
4304b88c807SRodney W. Grimes 	i =  KI_EPROC((KINFO *)a)->e_tdev - KI_EPROC((KINFO *)b)->e_tdev;
4314b88c807SRodney W. Grimes 	if (i == 0)
4324b88c807SRodney W. Grimes 		i = KI_PROC((KINFO *)a)->p_pid - KI_PROC((KINFO *)b)->p_pid;
4334b88c807SRodney W. Grimes 	return (i);
4344b88c807SRodney W. Grimes }
4354b88c807SRodney W. Grimes 
4364b88c807SRodney W. Grimes /*
4374b88c807SRodney W. Grimes  * ICK (all for getopt), would rather hide the ugliness
4384b88c807SRodney W. Grimes  * here than taint the main code.
4394b88c807SRodney W. Grimes  *
4404b88c807SRodney W. Grimes  *  ps foo -> ps -foo
4414b88c807SRodney W. Grimes  *  ps 34 -> ps -p34
4424b88c807SRodney W. Grimes  *
4434b88c807SRodney W. Grimes  * The old convention that 't' with no trailing tty arg means the users
4444b88c807SRodney W. Grimes  * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
4454b88c807SRodney W. Grimes  * feature is available with the option 'T', which takes no argument.
4464b88c807SRodney W. Grimes  */
4474b88c807SRodney W. Grimes static char *
4484b88c807SRodney W. Grimes kludge_oldps_options(s)
4494b88c807SRodney W. Grimes 	char *s;
4504b88c807SRodney W. Grimes {
4514b88c807SRodney W. Grimes 	size_t len;
4524b88c807SRodney W. Grimes 	char *newopts, *ns, *cp;
4534b88c807SRodney W. Grimes 
4544b88c807SRodney W. Grimes 	len = strlen(s);
4554b88c807SRodney W. Grimes 	if ((newopts = ns = malloc(len + 2)) == NULL)
4564b88c807SRodney W. Grimes 		err(1, NULL);
4574b88c807SRodney W. Grimes 	/*
4584b88c807SRodney W. Grimes 	 * options begin with '-'
4594b88c807SRodney W. Grimes 	 */
4604b88c807SRodney W. Grimes 	if (*s != '-')
4614b88c807SRodney W. Grimes 		*ns++ = '-';	/* add option flag */
4624b88c807SRodney W. Grimes 	/*
4634b88c807SRodney W. Grimes 	 * gaze to end of argv[1]
4644b88c807SRodney W. Grimes 	 */
4654b88c807SRodney W. Grimes 	cp = s + len - 1;
4664b88c807SRodney W. Grimes 	/*
4674b88c807SRodney W. Grimes 	 * if last letter is a 't' flag with no argument (in the context
4684b88c807SRodney W. Grimes 	 * of the oldps options -- option string NOT starting with a '-' --
4694b88c807SRodney W. Grimes 	 * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
4704b88c807SRodney W. Grimes 	 */
4714b88c807SRodney W. Grimes 	if (*cp == 't' && *s != '-')
4724b88c807SRodney W. Grimes 		*cp = 'T';
4734b88c807SRodney W. Grimes 	else {
4744b88c807SRodney W. Grimes 		/*
4754b88c807SRodney W. Grimes 		 * otherwise check for trailing number, which *may* be a
4764b88c807SRodney W. Grimes 		 * pid.
4774b88c807SRodney W. Grimes 		 */
4784b88c807SRodney W. Grimes 		while (cp >= s && isdigit(*cp))
4794b88c807SRodney W. Grimes 			--cp;
4804b88c807SRodney W. Grimes 	}
4814b88c807SRodney W. Grimes 	cp++;
4824b88c807SRodney W. Grimes 	memmove(ns, s, (size_t)(cp - s));	/* copy up to trailing number */
4834b88c807SRodney W. Grimes 	ns += cp - s;
4844b88c807SRodney W. Grimes 	/*
4854b88c807SRodney W. Grimes 	 * if there's a trailing number, and not a preceding 'p' (pid) or
4864b88c807SRodney W. Grimes 	 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
4874b88c807SRodney W. Grimes 	 */
4884b88c807SRodney W. Grimes 	if (isdigit(*cp) && (cp == s || cp[-1] != 't' && cp[-1] != 'p' &&
4894b88c807SRodney W. Grimes 	    (cp - 1 == s || cp[-2] != 't')))
4904b88c807SRodney W. Grimes 		*ns++ = 'p';
4914b88c807SRodney W. Grimes 	(void)strcpy(ns, cp);		/* and append the number */
4924b88c807SRodney W. Grimes 
4934b88c807SRodney W. Grimes 	return (newopts);
4944b88c807SRodney W. Grimes }
4954b88c807SRodney W. Grimes 
4964b88c807SRodney W. Grimes static void
4974b88c807SRodney W. Grimes usage()
4984b88c807SRodney W. Grimes {
4994b88c807SRodney W. Grimes 
5004b88c807SRodney W. Grimes 	(void)fprintf(stderr,
5014b88c807SRodney W. Grimes 	    "usage:\t%s\n\t   %s\n\t%s\n",
5024b88c807SRodney W. Grimes 	    "ps [-aChjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty]",
5034b88c807SRodney W. Grimes 	    "[-M core] [-N system] [-W swap]",
5044b88c807SRodney W. Grimes 	    "ps [-L]");
5054b88c807SRodney W. Grimes 	exit(1);
5064b88c807SRodney W. Grimes }
507