xref: /freebsd/bin/ps/ps.c (revision ca62e195dda11047a04335041811c4a8a6778d2b)
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.
32a4c8a745SGarance A Drosehn  * ------+---------+---------+-------- + --------+---------+---------+---------*
33a4c8a745SGarance A Drosehn  * Copyright (c) 2004  - Garance Alistair Drosehn <gad@FreeBSD.org>.
34a4c8a745SGarance A Drosehn  * All rights reserved.
35a4c8a745SGarance A Drosehn  *
36a4c8a745SGarance A Drosehn  * Significant modifications made to bring `ps' options somewhat closer
37a4c8a745SGarance A Drosehn  * to the standard for `ps' as described in SingleUnixSpec-v3.
38a4c8a745SGarance A Drosehn  * ------+---------+---------+-------- + --------+---------+---------+---------*
394b88c807SRodney W. Grimes  */
404b88c807SRodney W. Grimes 
414b88c807SRodney W. Grimes #ifndef lint
42871e8d8cSMark Murray static const char copyright[] =
434b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\
444b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
454b88c807SRodney W. Grimes #endif /* not lint */
464b88c807SRodney W. Grimes 
47c9a8d1f4SPhilippe Charnier #if 0
48871e8d8cSMark Murray #ifndef lint
49c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)ps.c	8.4 (Berkeley) 4/2/94";
504b88c807SRodney W. Grimes #endif /* not lint */
51871e8d8cSMark Murray #endif
52eaed5652SPhilippe Charnier 
532749b141SDavid E. O'Brien #include <sys/cdefs.h>
542749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
554b88c807SRodney W. Grimes 
564b88c807SRodney W. Grimes #include <sys/param.h>
578b6f5f5fSDavid Greenman #include <sys/user.h>
584b88c807SRodney W. Grimes #include <sys/stat.h>
594b88c807SRodney W. Grimes #include <sys/ioctl.h>
604b88c807SRodney W. Grimes #include <sys/sysctl.h>
614b88c807SRodney W. Grimes 
624b88c807SRodney W. Grimes #include <ctype.h>
634b88c807SRodney W. Grimes #include <err.h>
644e8b6a6fSGarance A Drosehn #include <errno.h>
654b88c807SRodney W. Grimes #include <fcntl.h>
66a4c8a745SGarance A Drosehn #include <grp.h>
674b88c807SRodney W. Grimes #include <kvm.h>
68b2496d93SMike Pritchard #include <limits.h>
6908017519SAndrey A. Chernov #include <locale.h>
704b88c807SRodney W. Grimes #include <paths.h>
71871e8d8cSMark Murray #include <pwd.h>
72a4c8a745SGarance A Drosehn #include <stdint.h>
734b88c807SRodney W. Grimes #include <stdio.h>
744b88c807SRodney W. Grimes #include <stdlib.h>
754b88c807SRodney W. Grimes #include <string.h>
764b88c807SRodney W. Grimes #include <unistd.h>
774b88c807SRodney W. Grimes 
784b88c807SRodney W. Grimes #include "ps.h"
794b88c807SRodney W. Grimes 
80a4c8a745SGarance A Drosehn #define	W_SEP	" \t"		/* "Whitespace" list separators */
81a4c8a745SGarance A Drosehn #define	T_SEP	","		/* "Terminate-element" list separators */
82cf22dcfcSBrian Somers 
83de800cd4SGarance A Drosehn #ifdef LAZY_PS
84de800cd4SGarance A Drosehn #define	DEF_UREAD	0;
85de800cd4SGarance A Drosehn #define	OPT_LAZY_f	"f"
86de800cd4SGarance A Drosehn #else
87de800cd4SGarance A Drosehn #define	DEF_UREAD	1;	/* Always do the more-expensive read. */
88de800cd4SGarance A Drosehn #define	OPT_LAZY_f		/* I.e., the `-f' option is not added. */
89de800cd4SGarance A Drosehn #endif
904b88c807SRodney W. Grimes 
91db91faacSPeter Wemm int	 cflag;			/* -c */
92de800cd4SGarance A Drosehn int	 eval;			/* Exit value */
93de800cd4SGarance A Drosehn time_t	 now;			/* Current time(3) value */
944b88c807SRodney W. Grimes int	 rawcpu;		/* -C */
954b88c807SRodney W. Grimes int	 sumrusage;		/* -S */
96de800cd4SGarance A Drosehn int	 termwidth;		/* Width of the screen (0 == infinity). */
97de800cd4SGarance A Drosehn int	 totwidth;		/* Calculated-width of requested variables. */
984b88c807SRodney W. Grimes 
99de800cd4SGarance A Drosehn struct varent *vhead;
100de800cd4SGarance A Drosehn 
101de800cd4SGarance A Drosehn static int	 forceuread = DEF_UREAD; /* Do extra work to get u-area. */
102de800cd4SGarance A Drosehn static kvm_t	*kd;
103de800cd4SGarance A Drosehn static KINFO	*kinfo;
104de800cd4SGarance A Drosehn static int	 needcomm;	/* -o "command" */
105de800cd4SGarance A Drosehn static int	 needenv;	/* -e */
106de800cd4SGarance A Drosehn static int	 needuser;	/* -o "user" */
107de800cd4SGarance A Drosehn static int	 optfatal;	/* Fatal error parsing some list-option. */
108de800cd4SGarance A Drosehn 
109de800cd4SGarance A Drosehn static enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
110ba2cd770SJuli Mallett 
111a4c8a745SGarance A Drosehn struct listinfo;
112de800cd4SGarance A Drosehn typedef	int	addelem_rtn(struct listinfo *_inf, const char *_elem);
113a4c8a745SGarance A Drosehn 
114a4c8a745SGarance A Drosehn struct listinfo {
115a4c8a745SGarance A Drosehn 	int		 count;
116a4c8a745SGarance A Drosehn 	int		 maxcount;
117a4c8a745SGarance A Drosehn 	int		 elemsize;
118a4c8a745SGarance A Drosehn 	addelem_rtn	*addelem;
119a4c8a745SGarance A Drosehn 	const char	*lname;
120a4c8a745SGarance A Drosehn 	union {
121a4c8a745SGarance A Drosehn 		gid_t	*gids;
122a4c8a745SGarance A Drosehn 		pid_t	*pids;
123a4c8a745SGarance A Drosehn 		dev_t	*ttys;
124a4c8a745SGarance A Drosehn 		uid_t	*uids;
125a4c8a745SGarance A Drosehn 		void	*ptr;
126a4c8a745SGarance A Drosehn 	};
127a4c8a745SGarance A Drosehn };
128a4c8a745SGarance A Drosehn 
129a4c8a745SGarance A Drosehn static int	 addelem_gid(struct listinfo *, const char *);
130a4c8a745SGarance A Drosehn static int	 addelem_pid(struct listinfo *, const char *);
131a4c8a745SGarance A Drosehn static int	 addelem_tty(struct listinfo *, const char *);
132a4c8a745SGarance A Drosehn static int	 addelem_uid(struct listinfo *, const char *);
133a4c8a745SGarance A Drosehn static void	 add_list(struct listinfo *, const char *);
1344857f240SGarance A Drosehn static void	 dynsizevars(KINFO *);
135a4c8a745SGarance A Drosehn static void	*expand_list(struct listinfo *);
1364857f240SGarance A Drosehn static const char *fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int),
1374857f240SGarance A Drosehn 		    KINFO *, char *, int);
138a4c8a745SGarance A Drosehn static void	 free_list(struct listinfo *);
139a4c8a745SGarance A Drosehn static void	 init_list(struct listinfo *, addelem_rtn, int, const char *);
1404857f240SGarance A Drosehn static char	*kludge_oldps_options(char *);
1414857f240SGarance A Drosehn static int	 pscomp(const void *, const void *);
1424857f240SGarance A Drosehn static void	 saveuser(KINFO *);
1434857f240SGarance A Drosehn static void	 scanvars(void);
1444857f240SGarance A Drosehn static void	 sizevars(void);
1454857f240SGarance A Drosehn static void	 usage(void);
1464b88c807SRodney W. Grimes 
147c0716492SJuli Mallett static char dfmt[] = "pid,tt,state,time,command";
148c0716492SJuli Mallett static char jfmt[] = "user,pid,ppid,pgid,jobc,state,tt,time,command";
1491d2324f4SGarance A Drosehn static char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state,"
1501d2324f4SGarance A Drosehn 			"tt,time,command";
151871e8d8cSMark Murray static char   o1[] = "pid";
152c0716492SJuli Mallett static char   o2[] = "tt,state,time,command";
153c0716492SJuli Mallett static char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command";
1541d2324f4SGarance A Drosehn static char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz,"
1551d2324f4SGarance A Drosehn 			"%cpu,%mem,command";
1562af538ebSRobert Watson static char Zfmt[] = "label";
1574b88c807SRodney W. Grimes 
158b34f38aeSGarance A Drosehn #define	PS_ARGS	"AaCc" OPT_LAZY_f "G:gHhjLlM:mN:O:o:p:rSTt:U:uvwXxZ"
15941623b2dSMaxim Sobolev 
1604b88c807SRodney W. Grimes int
16146251ddeSWarner Losh main(int argc, char *argv[])
1624b88c807SRodney W. Grimes {
163a4c8a745SGarance A Drosehn 	struct listinfo gidlist, pgrplist, pidlist;
164a4c8a745SGarance A Drosehn 	struct listinfo ruidlist, sesslist, ttylist, uidlist;
1654b88c807SRodney W. Grimes 	struct kinfo_proc *kp;
1664b88c807SRodney W. Grimes 	struct varent *vent;
1674b88c807SRodney W. Grimes 	struct winsize ws;
168ca62e195SGarance A Drosehn 	const char *cp, *nlistf, *memf;
169ca62e195SGarance A Drosehn 	char *cols;
170a4c8a745SGarance A Drosehn 	int all, ch, dropgid, elem, flag, _fmt, i, lineno;
171a4c8a745SGarance A Drosehn 	int nentries, nocludge, nkept, nselectors;
172a4c8a745SGarance A Drosehn 	int prtheader, showthreads, wflag, what, xkeep, xkeep_implied;
173871e8d8cSMark Murray 	char errbuf[_POSIX2_LINE_MAX];
1744b88c807SRodney W. Grimes 
1752bf4b9cfSAndrey A. Chernov 	(void) setlocale(LC_ALL, "");
176ba2cd770SJuli Mallett 	/* Set the time to what it is right now. */
177ba2cd770SJuli Mallett 	time(&now);
1782bf4b9cfSAndrey A. Chernov 
1794f18100dSTim J. Robbins 	if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0')
1804f18100dSTim J. Robbins 		termwidth = atoi(cols);
1814f18100dSTim J. Robbins 	else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1824b88c807SRodney W. Grimes 	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1834b88c807SRodney W. Grimes 	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
1844b88c807SRodney W. Grimes 	     ws.ws_col == 0)
1854b88c807SRodney W. Grimes 		termwidth = 79;
1864b88c807SRodney W. Grimes 	else
1874b88c807SRodney W. Grimes 		termwidth = ws.ws_col - 1;
1884b88c807SRodney W. Grimes 
18941623b2dSMaxim Sobolev 	/*
19041623b2dSMaxim Sobolev 	 * Don't apply a kludge if the first argument is an option taking an
19141623b2dSMaxim Sobolev 	 * argument
19241623b2dSMaxim Sobolev 	 */
19341623b2dSMaxim Sobolev 	if (argc > 1) {
19441623b2dSMaxim Sobolev 		nocludge = 0;
19541623b2dSMaxim Sobolev 		if (argv[1][0] == '-') {
19641623b2dSMaxim Sobolev 			for (cp = PS_ARGS; *cp != '\0'; cp++) {
19741623b2dSMaxim Sobolev 				if (*cp != ':')
19841623b2dSMaxim Sobolev 					continue;
19941623b2dSMaxim Sobolev 				if (*(cp - 1) == argv[1][1]) {
20041623b2dSMaxim Sobolev 					nocludge = 1;
20141623b2dSMaxim Sobolev 					break;
20241623b2dSMaxim Sobolev 				}
20341623b2dSMaxim Sobolev 			}
20441623b2dSMaxim Sobolev 		}
20541623b2dSMaxim Sobolev 		if (nocludge == 0)
2064b88c807SRodney W. Grimes 			argv[1] = kludge_oldps_options(argv[1]);
20741623b2dSMaxim Sobolev 	}
2084b88c807SRodney W. Grimes 
209a4c8a745SGarance A Drosehn 	xkeep = -1;				/* Neither -x nor -X */
210a4c8a745SGarance A Drosehn 	all = _fmt = nselectors = prtheader = wflag = xkeep_implied = 0;
211a4c8a745SGarance A Drosehn 	init_list(&gidlist, addelem_gid, sizeof(gid_t), "group");
212a4c8a745SGarance A Drosehn 	init_list(&pgrplist, addelem_pid, sizeof(pid_t), "process group");
213a4c8a745SGarance A Drosehn 	init_list(&pidlist, addelem_pid, sizeof(pid_t), "process id");
214a4c8a745SGarance A Drosehn 	init_list(&ruidlist, addelem_uid, sizeof(uid_t), "ruser");
215a4c8a745SGarance A Drosehn 	init_list(&sesslist, addelem_pid, sizeof(pid_t), "session id");
216a4c8a745SGarance A Drosehn 	init_list(&ttylist, addelem_tty, sizeof(dev_t), "tty");
217a4c8a745SGarance A Drosehn 	init_list(&uidlist, addelem_uid, sizeof(uid_t), "user");
21885082fc3SPoul-Henning Kamp 	dropgid = 0;
219a4c8a745SGarance A Drosehn 	optfatal = 0;
220831c910aSRuslan Ermilov 	memf = nlistf = _PATH_DEVNULL;
22148b8c0deSScott Long 	showthreads = 0;
22241623b2dSMaxim Sobolev 	while ((ch = getopt(argc, argv, PS_ARGS)) != -1)
2234b88c807SRodney W. Grimes 		switch((char)ch) {
224a4c8a745SGarance A Drosehn 		case 'A':
225a4c8a745SGarance A Drosehn 			/*
226a4c8a745SGarance A Drosehn 			 * Exactly the same as `-ax'.   This has been
227a4c8a745SGarance A Drosehn 			 * added for compatability with SUSv3, but for
228a4c8a745SGarance A Drosehn 			 * now it will not be described in the man page.
229a4c8a745SGarance A Drosehn 			 */
230a4c8a745SGarance A Drosehn 			nselectors++;
231a4c8a745SGarance A Drosehn 			all = xkeep = 1;
232a4c8a745SGarance A Drosehn 			break;
2334b88c807SRodney W. Grimes 		case 'a':
234a4c8a745SGarance A Drosehn 			nselectors++;
2354b88c807SRodney W. Grimes 			all = 1;
2364b88c807SRodney W. Grimes 			break;
2374b88c807SRodney W. Grimes 		case 'C':
2384b88c807SRodney W. Grimes 			rawcpu = 1;
2394b88c807SRodney W. Grimes 			break;
240db91faacSPeter Wemm 		case 'c':
241db91faacSPeter Wemm 			cflag = 1;
242db91faacSPeter Wemm 			break;
243db91faacSPeter Wemm 		case 'e':			/* XXX set ufmt */
244db91faacSPeter Wemm 			needenv = 1;
245db91faacSPeter Wemm 			break;
2464a355d17SGarance A Drosehn #ifdef LAZY_PS
2474a355d17SGarance A Drosehn 		case 'f':
2484a355d17SGarance A Drosehn 			if (getuid() == 0 || getgid() == 0)
2494a355d17SGarance A Drosehn 			    forceuread = 1;
2504a355d17SGarance A Drosehn 			break;
2514a355d17SGarance A Drosehn #endif
252a4c8a745SGarance A Drosehn 		case 'G':
253a4c8a745SGarance A Drosehn 			add_list(&gidlist, optarg);
254a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
255a4c8a745SGarance A Drosehn 			nselectors++;
256a4c8a745SGarance A Drosehn 			break;
257a4c8a745SGarance A Drosehn #if 0
258a4c8a745SGarance A Drosehn 		/* XXX - This SUSv3 option is still under debate. */
259a4c8a745SGarance A Drosehn 		/* (it conflicts with the undocumented `-g' option) */
2604b88c807SRodney W. Grimes 		case 'g':
261a4c8a745SGarance A Drosehn 			add_list(&pgrplist, optarg);
262a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
263a4c8a745SGarance A Drosehn 			nselectors++;
264a4c8a745SGarance A Drosehn 			break;
265a4c8a745SGarance A Drosehn #else
266a4c8a745SGarance A Drosehn 		case 'g':
267a4c8a745SGarance A Drosehn 			/* Historical BSD-ish (from SunOS) option */
2684b88c807SRodney W. Grimes 			break;			/* no-op */
269a4c8a745SGarance A Drosehn #endif
27048b8c0deSScott Long 		case 'H':
271d75c1d83SDaniel Eischen 			showthreads = KERN_PROC_INC_THREAD;
27248b8c0deSScott Long 			break;
2734b88c807SRodney W. Grimes 		case 'h':
2744b88c807SRodney W. Grimes 			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
2754b88c807SRodney W. Grimes 			break;
2764b88c807SRodney W. Grimes 		case 'j':
277fde411d5SJuli Mallett 			parsefmt(jfmt, 0);
278871e8d8cSMark Murray 			_fmt = 1;
2794b88c807SRodney W. Grimes 			jfmt[0] = '\0';
2804b88c807SRodney W. Grimes 			break;
2814b88c807SRodney W. Grimes 		case 'L':
2824b88c807SRodney W. Grimes 			showkey();
2834b88c807SRodney W. Grimes 			exit(0);
2844b88c807SRodney W. Grimes 		case 'l':
285fde411d5SJuli Mallett 			parsefmt(lfmt, 0);
286871e8d8cSMark Murray 			_fmt = 1;
2874b88c807SRodney W. Grimes 			lfmt[0] = '\0';
2884b88c807SRodney W. Grimes 			break;
2894b88c807SRodney W. Grimes 		case 'M':
2904b88c807SRodney W. Grimes 			memf = optarg;
29185082fc3SPoul-Henning Kamp 			dropgid = 1;
2924b88c807SRodney W. Grimes 			break;
2934b88c807SRodney W. Grimes 		case 'm':
2944b88c807SRodney W. Grimes 			sortby = SORTMEM;
2954b88c807SRodney W. Grimes 			break;
2964b88c807SRodney W. Grimes 		case 'N':
2974b88c807SRodney W. Grimes 			nlistf = optarg;
29885082fc3SPoul-Henning Kamp 			dropgid = 1;
2994b88c807SRodney W. Grimes 			break;
3004b88c807SRodney W. Grimes 		case 'O':
301fde411d5SJuli Mallett 			parsefmt(o1, 1);
302fde411d5SJuli Mallett 			parsefmt(optarg, 1);
303fde411d5SJuli Mallett 			parsefmt(o2, 1);
3044b88c807SRodney W. Grimes 			o1[0] = o2[0] = '\0';
305871e8d8cSMark Murray 			_fmt = 1;
3064b88c807SRodney W. Grimes 			break;
3074b88c807SRodney W. Grimes 		case 'o':
308fde411d5SJuli Mallett 			parsefmt(optarg, 1);
309871e8d8cSMark Murray 			_fmt = 1;
3104b88c807SRodney W. Grimes 			break;
3114b88c807SRodney W. Grimes 		case 'p':
312a4c8a745SGarance A Drosehn 			add_list(&pidlist, optarg);
313a4c8a745SGarance A Drosehn 			/*
314a4c8a745SGarance A Drosehn 			 * Note: `-p' does not *set* xkeep, but any values
315a4c8a745SGarance A Drosehn 			 * from pidlist are checked before xkeep is.  That
316a4c8a745SGarance A Drosehn 			 * way they are always matched, even if the user
317a4c8a745SGarance A Drosehn 			 * specifies `-X'.
318a4c8a745SGarance A Drosehn 			 */
319a4c8a745SGarance A Drosehn 			nselectors++;
3204b88c807SRodney W. Grimes 			break;
321a4c8a745SGarance A Drosehn #if 0
322a4c8a745SGarance A Drosehn 		/* XXX - This un-standard option is still under debate. */
323a4c8a745SGarance A Drosehn 		case 'R':
324a4c8a745SGarance A Drosehn 			/* This is what SUSv3 defines as the `-U' option. */
325a4c8a745SGarance A Drosehn 			add_list(&ruidlist, optarg);
326a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
327a4c8a745SGarance A Drosehn 			nselectors++;
328a4c8a745SGarance A Drosehn 			break;
329a4c8a745SGarance A Drosehn #endif
3304b88c807SRodney W. Grimes 		case 'r':
3314b88c807SRodney W. Grimes 			sortby = SORTCPU;
3324b88c807SRodney W. Grimes 			break;
3334b88c807SRodney W. Grimes 		case 'S':
3344b88c807SRodney W. Grimes 			sumrusage = 1;
3354b88c807SRodney W. Grimes 			break;
336a4c8a745SGarance A Drosehn #if 0
337a4c8a745SGarance A Drosehn 		/* XXX - This non-standard option is still under debate. */
338a4c8a745SGarance A Drosehn 		/* (it conflicts with `-s' in NetBSD) */
339a4c8a745SGarance A Drosehn 		case 's':
340a4c8a745SGarance A Drosehn 			/* As seen on Solaris, Linux, IRIX. */
341a4c8a745SGarance A Drosehn 			add_list(&sesslist, optarg);
342a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
343a4c8a745SGarance A Drosehn 			nselectors++;
344a4c8a745SGarance A Drosehn 			break;
345a4c8a745SGarance A Drosehn #endif
3464b88c807SRodney W. Grimes 		case 'T':
3474b88c807SRodney W. Grimes 			if ((optarg = ttyname(STDIN_FILENO)) == NULL)
3484b88c807SRodney W. Grimes 				errx(1, "stdin: not a terminal");
3494b88c807SRodney W. Grimes 			/* FALLTHROUGH */
350a4c8a745SGarance A Drosehn 		case 't':
351a4c8a745SGarance A Drosehn 			add_list(&ttylist, optarg);
352a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
353a4c8a745SGarance A Drosehn 			nselectors++;
3544b88c807SRodney W. Grimes 			break;
35573eb8310SPeter Wemm 		case 'U':
356a4c8a745SGarance A Drosehn 			/* This is what SUSv3 defines as the `-u' option. */
357a4c8a745SGarance A Drosehn 			add_list(&uidlist, optarg);
358a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
359a4c8a745SGarance A Drosehn 			nselectors++;
36073eb8310SPeter Wemm 			break;
3614b88c807SRodney W. Grimes 		case 'u':
362fde411d5SJuli Mallett 			parsefmt(ufmt, 0);
3634b88c807SRodney W. Grimes 			sortby = SORTCPU;
364871e8d8cSMark Murray 			_fmt = 1;
3654b88c807SRodney W. Grimes 			ufmt[0] = '\0';
3664b88c807SRodney W. Grimes 			break;
3674b88c807SRodney W. Grimes 		case 'v':
368fde411d5SJuli Mallett 			parsefmt(vfmt, 0);
3694b88c807SRodney W. Grimes 			sortby = SORTMEM;
370871e8d8cSMark Murray 			_fmt = 1;
3714b88c807SRodney W. Grimes 			vfmt[0] = '\0';
3724b88c807SRodney W. Grimes 			break;
3734b88c807SRodney W. Grimes 		case 'w':
3744b88c807SRodney W. Grimes 			if (wflag)
3754b88c807SRodney W. Grimes 				termwidth = UNLIMITED;
3764b88c807SRodney W. Grimes 			else if (termwidth < 131)
3774b88c807SRodney W. Grimes 				termwidth = 131;
3784b88c807SRodney W. Grimes 			wflag++;
3794b88c807SRodney W. Grimes 			break;
380a4c8a745SGarance A Drosehn 		case 'X':
381a4c8a745SGarance A Drosehn 			/*
382a4c8a745SGarance A Drosehn 			 * Note that `-X' and `-x' are not standard "selector"
383a4c8a745SGarance A Drosehn 			 * options. For most selector-options, we check *all*
384a4c8a745SGarance A Drosehn 			 * processes to see if any are matched by the given
385a4c8a745SGarance A Drosehn 			 * value(s).  After we have a set of all the matched
386a4c8a745SGarance A Drosehn 			 * processes, then `-X' and `-x' govern whether we
387a4c8a745SGarance A Drosehn 			 * modify that *matched* set for processes which do
388a4c8a745SGarance A Drosehn 			 * not have a controlling terminal.  `-X' causes
389a4c8a745SGarance A Drosehn 			 * those processes to be deleted from the matched
390a4c8a745SGarance A Drosehn 			 * set, while `-x' causes them to be kept.
391a4c8a745SGarance A Drosehn 			 */
392a4c8a745SGarance A Drosehn 			xkeep = 0;
393a4c8a745SGarance A Drosehn 			break;
3944b88c807SRodney W. Grimes 		case 'x':
395a4c8a745SGarance A Drosehn 			xkeep = 1;
3964b88c807SRodney W. Grimes 			break;
3977304f61fSBrian Feldman 		case 'Z':
398fde411d5SJuli Mallett 			parsefmt(Zfmt, 0);
3997304f61fSBrian Feldman 			Zfmt[0] = '\0';
4007304f61fSBrian Feldman 			break;
4014b88c807SRodney W. Grimes 		case '?':
4024b88c807SRodney W. Grimes 		default:
4034b88c807SRodney W. Grimes 			usage();
4044b88c807SRodney W. Grimes 		}
4054b88c807SRodney W. Grimes 	argc -= optind;
4064b88c807SRodney W. Grimes 	argv += optind;
4074b88c807SRodney W. Grimes 
408a4c8a745SGarance A Drosehn 	if (optfatal)
409a4c8a745SGarance A Drosehn 		exit(1);		/* Error messages already printed */
410a4c8a745SGarance A Drosehn 
411a4c8a745SGarance A Drosehn 	if (xkeep < 0)			/* Neither -X nor -x was specified */
412a4c8a745SGarance A Drosehn 		xkeep = xkeep_implied;
413a4c8a745SGarance A Drosehn 
4144b88c807SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4154b88c807SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4164b88c807SRodney W. Grimes 	if (*argv) {
4174b88c807SRodney W. Grimes 		nlistf = *argv;
4184b88c807SRodney W. Grimes 		if (*++argv) {
4194b88c807SRodney W. Grimes 			memf = *argv;
4204b88c807SRodney W. Grimes 		}
4214b88c807SRodney W. Grimes 	}
4224b88c807SRodney W. Grimes #endif
4234b88c807SRodney W. Grimes 	/*
4244b88c807SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4254b88c807SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4264b88c807SRodney W. Grimes 	 */
42785082fc3SPoul-Henning Kamp 	if (dropgid) {
4284b88c807SRodney W. Grimes 		setgid(getgid());
42985082fc3SPoul-Henning Kamp 		setuid(getuid());
43085082fc3SPoul-Henning Kamp 	}
4314b88c807SRodney W. Grimes 
432831c910aSRuslan Ermilov 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
4334b88c807SRodney W. Grimes 	if (kd == 0)
4344b88c807SRodney W. Grimes 		errx(1, "%s", errbuf);
4354b88c807SRodney W. Grimes 
436871e8d8cSMark Murray 	if (!_fmt)
437fde411d5SJuli Mallett 		parsefmt(dfmt, 0);
4384b88c807SRodney W. Grimes 
439a4c8a745SGarance A Drosehn 	if (nselectors == 0) {
440a4c8a745SGarance A Drosehn 		uidlist.ptr = malloc(sizeof(uid_t));
441a4c8a745SGarance A Drosehn 		if (uidlist.ptr == NULL)
4424fa7d788SJuli Mallett 			errx(1, "malloc failed");
443a4c8a745SGarance A Drosehn 		nselectors = 1;
444a4c8a745SGarance A Drosehn 		uidlist.count = uidlist.maxcount = 1;
445a4c8a745SGarance A Drosehn 		*uidlist.uids = getuid();
446cf22dcfcSBrian Somers 	}
4474b88c807SRodney W. Grimes 
4484b88c807SRodney W. Grimes 	/*
4494b88c807SRodney W. Grimes 	 * scan requested variables, noting what structures are needed,
450bdfebd84SKris Kennaway 	 * and adjusting header widths as appropriate.
4514b88c807SRodney W. Grimes 	 */
4524b88c807SRodney W. Grimes 	scanvars();
453a4c8a745SGarance A Drosehn 
4544b88c807SRodney W. Grimes 	/*
455a4c8a745SGarance A Drosehn 	 * Get process list.  If the user requested just one selector-
456a4c8a745SGarance A Drosehn 	 * option, then kvm_getprocs can be asked to return just those
457a4c8a745SGarance A Drosehn 	 * processes.  Otherwise, have it return all processes, and
458a4c8a745SGarance A Drosehn 	 * then this routine will search that full list and select the
459a4c8a745SGarance A Drosehn 	 * processes which match any of the user's selector-options.
4604b88c807SRodney W. Grimes 	 */
461d75c1d83SDaniel Eischen 	what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC;
46248b8c0deSScott Long 	flag = 0;
463a4c8a745SGarance A Drosehn 	if (nselectors == 1) {
464a4c8a745SGarance A Drosehn 		/* XXX - Apparently there's no KERN_PROC_GID flag. */
465a4c8a745SGarance A Drosehn 		if (pgrplist.count == 1) {
466a4c8a745SGarance A Drosehn 			what = KERN_PROC_PGRP | showthreads;
467a4c8a745SGarance A Drosehn 			flag = *pgrplist.pids;
468a4c8a745SGarance A Drosehn 			nselectors = 0;
469a4c8a745SGarance A Drosehn 		} else if (pidlist.count == 1) {
470a4c8a745SGarance A Drosehn 			what = KERN_PROC_PID | showthreads;
471a4c8a745SGarance A Drosehn 			flag = *pidlist.pids;
472a4c8a745SGarance A Drosehn 			nselectors = 0;
473a4c8a745SGarance A Drosehn 		} else if (ruidlist.count == 1) {
474a4c8a745SGarance A Drosehn 			what = KERN_PROC_RUID | showthreads;
475a4c8a745SGarance A Drosehn 			flag = *ruidlist.uids;
476a4c8a745SGarance A Drosehn 			nselectors = 0;
477a4c8a745SGarance A Drosehn #if 0		/* XXX - KERN_PROC_SESSION causes error in kvm_getprocs? */
478a4c8a745SGarance A Drosehn 		} else if (sesslist.count == 1) {
479a4c8a745SGarance A Drosehn 			what = KERN_PROC_SESSION | showthreads;
480a4c8a745SGarance A Drosehn 			flag = *sesslist.pids;
481a4c8a745SGarance A Drosehn 			nselectors = 0;
482a4c8a745SGarance A Drosehn #endif
483a4c8a745SGarance A Drosehn 		} else if (ttylist.count == 1) {
484a4c8a745SGarance A Drosehn 			what = KERN_PROC_TTY | showthreads;
485a4c8a745SGarance A Drosehn 			flag = *ttylist.ttys;
486a4c8a745SGarance A Drosehn 			nselectors = 0;
487a4c8a745SGarance A Drosehn 		} else if (uidlist.count == 1) {
488a4c8a745SGarance A Drosehn 			what = KERN_PROC_UID | showthreads;
489a4c8a745SGarance A Drosehn 			flag = *uidlist.uids;
490a4c8a745SGarance A Drosehn 			nselectors = 0;
491a4c8a745SGarance A Drosehn 		} else if (all) {
492a4c8a745SGarance A Drosehn 			/* No need for this routine to select processes. */
493a4c8a745SGarance A Drosehn 			nselectors = 0;
494a4c8a745SGarance A Drosehn 		}
4954b88c807SRodney W. Grimes 	}
496d75c1d83SDaniel Eischen 
4974b88c807SRodney W. Grimes 	/*
4984b88c807SRodney W. Grimes 	 * select procs
4994b88c807SRodney W. Grimes 	 */
500a4c8a745SGarance A Drosehn 	nentries = -1;
5014e8b6a6fSGarance A Drosehn 	kp = kvm_getprocs(kd, what, flag, &nentries);
5025dfd7724SGarance A Drosehn 	if ((kp == 0 && nentries > 0) || (kp != 0 && nentries < 0))
5034b88c807SRodney W. Grimes 		errx(1, "%s", kvm_geterr(kd));
504a4c8a745SGarance A Drosehn 	nkept = 0;
5054e8b6a6fSGarance A Drosehn 	if (nentries > 0) {
5064b88c807SRodney W. Grimes 		if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL)
5074fa7d788SJuli Mallett 			errx(1, "malloc failed");
5084b88c807SRodney W. Grimes 		for (i = nentries; --i >= 0; ++kp) {
509a4c8a745SGarance A Drosehn 			/*
510a4c8a745SGarance A Drosehn 			 * If the user specified multiple selection-criteria,
511a4c8a745SGarance A Drosehn 			 * then keep any process matched by the inclusive OR
512a4c8a745SGarance A Drosehn 			 * of all the selection-criteria given.
513a4c8a745SGarance A Drosehn 			 */
514a4c8a745SGarance A Drosehn 			if (pidlist.count > 0) {
515a4c8a745SGarance A Drosehn 				for (elem = 0; elem < pidlist.count; elem++)
516a4c8a745SGarance A Drosehn 					if (kp->ki_pid == pidlist.pids[elem])
517a4c8a745SGarance A Drosehn 						goto keepit;
518a4c8a745SGarance A Drosehn 			}
519a4c8a745SGarance A Drosehn 			/*
520a4c8a745SGarance A Drosehn 			 * Note that we had to process pidlist before
521a4c8a745SGarance A Drosehn 			 * filtering out processes which do not have
522a4c8a745SGarance A Drosehn 			 * a controlling terminal.
523a4c8a745SGarance A Drosehn 			 */
524a4c8a745SGarance A Drosehn 			if (xkeep == 0) {
525a4c8a745SGarance A Drosehn 				if ((kp->ki_tdev == NODEV ||
526a4c8a745SGarance A Drosehn 				    (kp->ki_flag & P_CONTROLT) == 0))
527a4c8a745SGarance A Drosehn 					continue;
528a4c8a745SGarance A Drosehn 			}
529a4c8a745SGarance A Drosehn 			if (nselectors == 0)
530a4c8a745SGarance A Drosehn 				goto keepit;
531a4c8a745SGarance A Drosehn 			if (gidlist.count > 0) {
532a4c8a745SGarance A Drosehn 				for (elem = 0; elem < gidlist.count; elem++)
533a4c8a745SGarance A Drosehn 					if (kp->ki_rgid == gidlist.gids[elem])
534a4c8a745SGarance A Drosehn 						goto keepit;
535a4c8a745SGarance A Drosehn 			}
536a4c8a745SGarance A Drosehn 			if (pgrplist.count > 0) {
537a4c8a745SGarance A Drosehn 				for (elem = 0; elem < pgrplist.count; elem++)
538a4c8a745SGarance A Drosehn 					if (kp->ki_pgid == pgrplist.pids[elem])
539a4c8a745SGarance A Drosehn 						goto keepit;
540a4c8a745SGarance A Drosehn 			}
541a4c8a745SGarance A Drosehn 			if (ruidlist.count > 0) {
542a4c8a745SGarance A Drosehn 				for (elem = 0; elem < ruidlist.count; elem++)
543a4c8a745SGarance A Drosehn 					if (kp->ki_ruid == ruidlist.uids[elem])
544a4c8a745SGarance A Drosehn 						goto keepit;
545a4c8a745SGarance A Drosehn 			}
546a4c8a745SGarance A Drosehn 			if (sesslist.count > 0) {
547a4c8a745SGarance A Drosehn 				for (elem = 0; elem < sesslist.count; elem++)
548a4c8a745SGarance A Drosehn 					if (kp->ki_sid == sesslist.pids[elem])
549a4c8a745SGarance A Drosehn 						goto keepit;
550a4c8a745SGarance A Drosehn 			}
551a4c8a745SGarance A Drosehn 			if (ttylist.count > 0) {
552a4c8a745SGarance A Drosehn 				for (elem = 0; elem < ttylist.count; elem++)
553a4c8a745SGarance A Drosehn 					if (kp->ki_tdev == ttylist.ttys[elem])
554a4c8a745SGarance A Drosehn 						goto keepit;
555a4c8a745SGarance A Drosehn 			}
556a4c8a745SGarance A Drosehn 			if (uidlist.count > 0) {
557a4c8a745SGarance A Drosehn 				for (elem = 0; elem < uidlist.count; elem++)
558a4c8a745SGarance A Drosehn 					if (kp->ki_uid == uidlist.uids[elem])
559a4c8a745SGarance A Drosehn 						goto keepit;
560a4c8a745SGarance A Drosehn 			}
561a4c8a745SGarance A Drosehn 			/*
562a4c8a745SGarance A Drosehn 			 * This process did not match any of the user's
563a4c8a745SGarance A Drosehn 			 * selector-options, so skip the process.
564a4c8a745SGarance A Drosehn 			 */
565a4c8a745SGarance A Drosehn 			continue;
566a4c8a745SGarance A Drosehn 
567a4c8a745SGarance A Drosehn 		keepit:
568a4c8a745SGarance A Drosehn 			kinfo[nkept].ki_p = kp;
5694b88c807SRodney W. Grimes 			if (needuser)
570a4c8a745SGarance A Drosehn 				saveuser(&kinfo[nkept]);
571a4c8a745SGarance A Drosehn 			dynsizevars(&kinfo[nkept]);
572a4c8a745SGarance A Drosehn 			nkept++;
5734b88c807SRodney W. Grimes 		}
5744e8b6a6fSGarance A Drosehn 	}
5756a2d726bSJordan K. Hubbard 
5766a2d726bSJordan K. Hubbard 	sizevars();
5776a2d726bSJordan K. Hubbard 
5784b88c807SRodney W. Grimes 	/*
5794b88c807SRodney W. Grimes 	 * print header
5804b88c807SRodney W. Grimes 	 */
5814b88c807SRodney W. Grimes 	printheader();
582a4c8a745SGarance A Drosehn 	if (nkept == 0)
583f8c9c11cSWill Andrews 		exit(1);
584a4c8a745SGarance A Drosehn 
5854b88c807SRodney W. Grimes 	/*
5864b88c807SRodney W. Grimes 	 * sort proc list
5874b88c807SRodney W. Grimes 	 */
588a4c8a745SGarance A Drosehn 	qsort(kinfo, nkept, sizeof(KINFO), pscomp);
5894b88c807SRodney W. Grimes 	/*
590a4c8a745SGarance A Drosehn 	 * For each process, call each variable output function.
5914b88c807SRodney W. Grimes 	 */
592a4c8a745SGarance A Drosehn 	for (i = lineno = 0; i < nkept; i++) {
5934b88c807SRodney W. Grimes 		for (vent = vhead; vent; vent = vent->next) {
5944b88c807SRodney W. Grimes 			(vent->var->oproc)(&kinfo[i], vent);
5954b88c807SRodney W. Grimes 			if (vent->next != NULL)
5964b88c807SRodney W. Grimes 				(void)putchar(' ');
5974b88c807SRodney W. Grimes 		}
5984b88c807SRodney W. Grimes 		(void)putchar('\n');
5994b88c807SRodney W. Grimes 		if (prtheader && lineno++ == prtheader - 4) {
6004b88c807SRodney W. Grimes 			(void)putchar('\n');
6014b88c807SRodney W. Grimes 			printheader();
6024b88c807SRodney W. Grimes 			lineno = 0;
6034b88c807SRodney W. Grimes 		}
6044b88c807SRodney W. Grimes 	}
605a4c8a745SGarance A Drosehn 	free_list(&gidlist);
606a4c8a745SGarance A Drosehn 	free_list(&pidlist);
607a4c8a745SGarance A Drosehn 	free_list(&pgrplist);
608a4c8a745SGarance A Drosehn 	free_list(&ruidlist);
609a4c8a745SGarance A Drosehn 	free_list(&sesslist);
610a4c8a745SGarance A Drosehn 	free_list(&ttylist);
611a4c8a745SGarance A Drosehn 	free_list(&uidlist);
6122e6c6ac4SGarance A Drosehn 
6134b88c807SRodney W. Grimes 	exit(eval);
6144b88c807SRodney W. Grimes }
6154b88c807SRodney W. Grimes 
616a4c8a745SGarance A Drosehn static int
617a4c8a745SGarance A Drosehn addelem_gid(struct listinfo *inf, const char *elem)
6184e8b6a6fSGarance A Drosehn {
619a4c8a745SGarance A Drosehn 	struct group *grp;
620a4c8a745SGarance A Drosehn 	const char *nameorID;
621a4c8a745SGarance A Drosehn 	char *endp;
622ca62e195SGarance A Drosehn 	intmax_t ltemp;
6234e8b6a6fSGarance A Drosehn 
624a4c8a745SGarance A Drosehn 	if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
625a4c8a745SGarance A Drosehn 		if (*elem == '\0')
626a4c8a745SGarance A Drosehn 			warnx("Invalid (zero-length) %s name", inf->lname);
627a4c8a745SGarance A Drosehn 		else
628a4c8a745SGarance A Drosehn 			warnx("%s name too long: %s", inf->lname, elem);
629a4c8a745SGarance A Drosehn 		optfatal = 1;
630a4c8a745SGarance A Drosehn 		return (0);			/* Do not add this value */
6314e8b6a6fSGarance A Drosehn 	}
632a4c8a745SGarance A Drosehn 
6334e8b6a6fSGarance A Drosehn 	/*
634a4c8a745SGarance A Drosehn 	 * SUSv3 states that `ps -G grouplist' should match "real-group
635a4c8a745SGarance A Drosehn 	 * ID numbers", and does not mention group-names.  I do want to
636a4c8a745SGarance A Drosehn 	 * also support group-names, so this tries for a group-id first,
637a4c8a745SGarance A Drosehn 	 * and only tries for a name if that doesn't work.  This is the
638a4c8a745SGarance A Drosehn 	 * opposite order of what is done in addelem_uid(), but in
639a4c8a745SGarance A Drosehn 	 * practice the order would only matter for group-names which
640a4c8a745SGarance A Drosehn 	 * are all-numeric.
6414e8b6a6fSGarance A Drosehn 	 */
642a4c8a745SGarance A Drosehn 	grp = NULL;
643a4c8a745SGarance A Drosehn 	nameorID = "named";
644a4c8a745SGarance A Drosehn 	errno = 0;
645a4c8a745SGarance A Drosehn 	ltemp = strtol(elem, &endp, 10);
646a4c8a745SGarance A Drosehn 	if (errno == 0 && *endp == '\0' && ltemp >= 0 && ltemp <= GID_MAX) {
647a4c8a745SGarance A Drosehn 		nameorID = "name or ID matches";
648a4c8a745SGarance A Drosehn 		grp = getgrgid((gid_t)ltemp);
649a4c8a745SGarance A Drosehn 	}
650a4c8a745SGarance A Drosehn 	if (grp == NULL)
651a4c8a745SGarance A Drosehn 		grp = getgrnam(elem);
652a4c8a745SGarance A Drosehn 	if (grp == NULL) {
653a4c8a745SGarance A Drosehn 		warnx("No %s %s '%s'", inf->lname, nameorID, elem);
654a4c8a745SGarance A Drosehn 		optfatal = 1;
655a4c8a745SGarance A Drosehn 		return (0);			/* Do not add this value */
656a4c8a745SGarance A Drosehn 	}
657a4c8a745SGarance A Drosehn 
658a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
659a4c8a745SGarance A Drosehn 		expand_list(inf);
660a4c8a745SGarance A Drosehn 	inf->gids[(inf->count)++] = grp->gr_gid;
661a4c8a745SGarance A Drosehn 	return (1);
662a4c8a745SGarance A Drosehn }
663a4c8a745SGarance A Drosehn 
664a4c8a745SGarance A Drosehn #define	BSD_PID_MAX	99999		/* Copy of PID_MAX from sys/proc.h */
665a4c8a745SGarance A Drosehn static int
666a4c8a745SGarance A Drosehn addelem_pid(struct listinfo *inf, const char *elem)
667a4c8a745SGarance A Drosehn {
668a4c8a745SGarance A Drosehn 	char *endp;
669ca62e195SGarance A Drosehn 	long tempid;
670a4c8a745SGarance A Drosehn 
671a4c8a745SGarance A Drosehn 	if (*elem == '\0')
672a4c8a745SGarance A Drosehn 		tempid = 0L;
673a4c8a745SGarance A Drosehn 	else {
674a4c8a745SGarance A Drosehn 		errno = 0;
675a4c8a745SGarance A Drosehn 		tempid = strtol(elem, &endp, 10);
676a4c8a745SGarance A Drosehn 		if (*endp != '\0' || tempid < 0 || elem == endp) {
677a4c8a745SGarance A Drosehn 			warnx("Invalid %s: %s", inf->lname, elem);
6784e8b6a6fSGarance A Drosehn 			errno = ERANGE;
6794e8b6a6fSGarance A Drosehn 		} else if (errno != 0 || tempid > BSD_PID_MAX) {
680a4c8a745SGarance A Drosehn 			warnx("%s too large: %s", inf->lname, elem);
6814e8b6a6fSGarance A Drosehn 			errno = ERANGE;
6824e8b6a6fSGarance A Drosehn 		}
6834e8b6a6fSGarance A Drosehn 		if (errno == ERANGE) {
684a4c8a745SGarance A Drosehn 			optfatal = 1;
685a4c8a745SGarance A Drosehn 			return (0);		/* Do not add this value */
6864e8b6a6fSGarance A Drosehn 		}
6874e8b6a6fSGarance A Drosehn 	}
6884e8b6a6fSGarance A Drosehn 
689a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
690a4c8a745SGarance A Drosehn 		expand_list(inf);
691a4c8a745SGarance A Drosehn 	inf->pids[(inf->count)++] = tempid;
692a4c8a745SGarance A Drosehn 	return (1);
6934e8b6a6fSGarance A Drosehn }
694a4c8a745SGarance A Drosehn #undef	BSD_PID_MAX
6954e8b6a6fSGarance A Drosehn 
696a4c8a745SGarance A Drosehn static int
697a4c8a745SGarance A Drosehn addelem_tty(struct listinfo *inf, const char *elem)
698cf22dcfcSBrian Somers {
699a4c8a745SGarance A Drosehn 	const char *ttypath;
700ca62e195SGarance A Drosehn 	struct stat sb;
701ca62e195SGarance A Drosehn 	char pathbuf[PATH_MAX];
702a4c8a745SGarance A Drosehn 
703a4c8a745SGarance A Drosehn 	if (strcmp(elem, "co") == 0)
704a4c8a745SGarance A Drosehn 		ttypath = strdup(_PATH_CONSOLE);
705a4c8a745SGarance A Drosehn 	else if (*elem == '/')
706a4c8a745SGarance A Drosehn 		ttypath = elem;
707a4c8a745SGarance A Drosehn 	else {
708a4c8a745SGarance A Drosehn 		strlcpy(pathbuf, _PATH_TTY, sizeof(pathbuf));
709a4c8a745SGarance A Drosehn 		strlcat(pathbuf, elem, sizeof(pathbuf));
710a4c8a745SGarance A Drosehn 		ttypath = pathbuf;
711a4c8a745SGarance A Drosehn 	}
712a4c8a745SGarance A Drosehn 
713a4c8a745SGarance A Drosehn 	if (stat(ttypath, &sb) == -1) {
714a4c8a745SGarance A Drosehn 		warn("%s", ttypath);
715a4c8a745SGarance A Drosehn 		optfatal = 1;
716a4c8a745SGarance A Drosehn 		return (0);			/* Do not add this value */
717a4c8a745SGarance A Drosehn 	}
718a4c8a745SGarance A Drosehn 	if (!S_ISCHR(sb.st_mode)) {
719a4c8a745SGarance A Drosehn 		warn("%s: Not a terminal", ttypath);
720a4c8a745SGarance A Drosehn 		optfatal = 1;
721a4c8a745SGarance A Drosehn 		return (0);			/* Do not add this value */
722a4c8a745SGarance A Drosehn 	}
723a4c8a745SGarance A Drosehn 
724a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
725a4c8a745SGarance A Drosehn 		expand_list(inf);
726a4c8a745SGarance A Drosehn 	inf->ttys[(inf->count)++] = sb.st_rdev;
727a4c8a745SGarance A Drosehn 	return (1);
728a4c8a745SGarance A Drosehn }
729a4c8a745SGarance A Drosehn 
730a4c8a745SGarance A Drosehn static int
731a4c8a745SGarance A Drosehn addelem_uid(struct listinfo *inf, const char *elem)
732a4c8a745SGarance A Drosehn {
733cf22dcfcSBrian Somers 	struct passwd *pwd;
734a4c8a745SGarance A Drosehn 	char *endp;
735ca62e195SGarance A Drosehn 	intmax_t ltemp;
736cf22dcfcSBrian Somers 
737a4c8a745SGarance A Drosehn 	if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
738a4c8a745SGarance A Drosehn 		if (*elem == '\0')
739a4c8a745SGarance A Drosehn 			warnx("Invalid (zero-length) %s name", inf->lname);
740a4c8a745SGarance A Drosehn 		else
741a4c8a745SGarance A Drosehn 			warnx("%s name too long: %s", inf->lname, elem);
742a4c8a745SGarance A Drosehn 		optfatal = 1;
743a4c8a745SGarance A Drosehn 		return (0);			/* Do not add this value */
744a4c8a745SGarance A Drosehn 	}
745cf22dcfcSBrian Somers 
746a4c8a745SGarance A Drosehn 	pwd = getpwnam(elem);
747a4c8a745SGarance A Drosehn 	if (pwd == NULL) {
748a4c8a745SGarance A Drosehn 		errno = 0;
749a4c8a745SGarance A Drosehn 		ltemp = strtol(elem, &endp, 10);
750a4c8a745SGarance A Drosehn 		if (errno != 0 || *endp != '\0' || ltemp < 0 ||
751a4c8a745SGarance A Drosehn 		    ltemp > UID_MAX)
752a4c8a745SGarance A Drosehn 			warnx("No %s named '%s'", inf->lname, elem);
753a4c8a745SGarance A Drosehn 		else {
754a4c8a745SGarance A Drosehn 			/* The string is all digits, so it might be a userID. */
755a4c8a745SGarance A Drosehn 			pwd = getpwuid((uid_t)ltemp);
756a4c8a745SGarance A Drosehn 			if (pwd == NULL)
757a4c8a745SGarance A Drosehn 				warnx("No %s name or ID matches '%s'",
758a4c8a745SGarance A Drosehn 				    inf->lname, elem);
759cf22dcfcSBrian Somers 		}
760cf22dcfcSBrian Somers 	}
761a4c8a745SGarance A Drosehn 	if (pwd == NULL) {
762e3c4e1ddSGarance A Drosehn 		/*
763e3c4e1ddSGarance A Drosehn 		 * These used to be treated as minor warnings (and the
764e3c4e1ddSGarance A Drosehn 		 * option was simply ignored), but now they are fatal
765e3c4e1ddSGarance A Drosehn 		 * errors (and the command will be aborted).
766e3c4e1ddSGarance A Drosehn 		 */
767e3c4e1ddSGarance A Drosehn 		optfatal = 1;
768a4c8a745SGarance A Drosehn 		return (0);			/* Do not add this value */
769cf22dcfcSBrian Somers 	}
770cf22dcfcSBrian Somers 
771a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
772a4c8a745SGarance A Drosehn 		expand_list(inf);
773a4c8a745SGarance A Drosehn 	inf->uids[(inf->count)++] = pwd->pw_uid;
774a4c8a745SGarance A Drosehn 	return (1);
775a4c8a745SGarance A Drosehn }
776cf22dcfcSBrian Somers 
777a4c8a745SGarance A Drosehn static void
778a4c8a745SGarance A Drosehn add_list(struct listinfo *inf, const char *argp)
779a4c8a745SGarance A Drosehn {
780a4c8a745SGarance A Drosehn 	const char *savep;
781a4c8a745SGarance A Drosehn 	char *cp, *endp;
782a4c8a745SGarance A Drosehn 	int toolong;
783ca62e195SGarance A Drosehn 	char elemcopy[PATH_MAX];
784a4c8a745SGarance A Drosehn 
785a4c8a745SGarance A Drosehn 	while (*argp != '\0') {
786a4c8a745SGarance A Drosehn 		while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
787a4c8a745SGarance A Drosehn 			argp++;
788a4c8a745SGarance A Drosehn 		savep = argp;
789a4c8a745SGarance A Drosehn 		toolong = 0;
790a4c8a745SGarance A Drosehn 		cp = elemcopy;
791a4c8a745SGarance A Drosehn 		if (strchr(T_SEP, *argp) == NULL) {
792a4c8a745SGarance A Drosehn 			endp = elemcopy + sizeof(elemcopy) - 1;
793a4c8a745SGarance A Drosehn 			while (*argp != '\0' && cp <= endp &&
794a4c8a745SGarance A Drosehn 			    strchr(W_SEP T_SEP, *argp) == NULL)
795a4c8a745SGarance A Drosehn 				*cp++ = *argp++;
796a4c8a745SGarance A Drosehn 			if (cp > endp)
797a4c8a745SGarance A Drosehn 				toolong = 1;
798a4c8a745SGarance A Drosehn 		}
799a4c8a745SGarance A Drosehn 		if (!toolong) {
800a4c8a745SGarance A Drosehn 			*cp = '\0';
801a4c8a745SGarance A Drosehn #ifndef ADD_PS_LISTRESET
802a4c8a745SGarance A Drosehn 	/* This is how the standard expects lists to be handled. */
803a4c8a745SGarance A Drosehn 			inf->addelem(inf, elemcopy);
804a4c8a745SGarance A Drosehn #else
805a4c8a745SGarance A Drosehn 	/*
806a4c8a745SGarance A Drosehn 	 * This would add a simple non-standard-but-convienent feature.
807a4c8a745SGarance A Drosehn 	 *
808a4c8a745SGarance A Drosehn 	 * XXX - Adding this check increased the total size of `ps' by
809a4c8a745SGarance A Drosehn 	 *	3940 bytes on i386!  That's 12% of the entire program!
810a4c8a745SGarance A Drosehn 	 *	The `ps.o' file grew by only about 40 bytes, but the
811a4c8a745SGarance A Drosehn 	 *	final (stripped) executable in /bin/ps grew by 12%.
812a4c8a745SGarance A Drosehn 	 */
813a4c8a745SGarance A Drosehn 			/*
814a4c8a745SGarance A Drosehn 			 * We now have a single element.  Add it to the
815a4c8a745SGarance A Drosehn 			 * list, unless the element is ":".  In that case,
816a4c8a745SGarance A Drosehn 			 * reset the list so previous entries are ignored.
817a4c8a745SGarance A Drosehn 			 */
818a4c8a745SGarance A Drosehn 			if (strcmp(elemcopy, ":") == 0)
819a4c8a745SGarance A Drosehn 				inf->count = 0;
820a4c8a745SGarance A Drosehn 			else
821a4c8a745SGarance A Drosehn 				inf->addelem(inf, elemcopy);
822a4c8a745SGarance A Drosehn #endif
823a4c8a745SGarance A Drosehn 		} else {
824a4c8a745SGarance A Drosehn 			/*
825a4c8a745SGarance A Drosehn 			 * The string is too long to copy.  Find the end
826a4c8a745SGarance A Drosehn 			 * of the string to print out the warning message.
827a4c8a745SGarance A Drosehn 			 */
828a4c8a745SGarance A Drosehn 			while (*argp != '\0' && strchr(W_SEP T_SEP,
829a4c8a745SGarance A Drosehn 			    *argp) == NULL)
830a4c8a745SGarance A Drosehn 				argp++;
831a4c8a745SGarance A Drosehn 			warnx("Value too long: %.*s", (int)(argp - savep),
832a4c8a745SGarance A Drosehn 			    savep);
833a4c8a745SGarance A Drosehn 			optfatal = 1;
834a4c8a745SGarance A Drosehn 		}
835a4c8a745SGarance A Drosehn 		/*
836a4c8a745SGarance A Drosehn 		 * Skip over any number of trailing whitespace characters,
837a4c8a745SGarance A Drosehn 		 * but only one (at most) trailing element-terminating
838a4c8a745SGarance A Drosehn 		 * character.
839a4c8a745SGarance A Drosehn 		 */
840a4c8a745SGarance A Drosehn 		while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
841a4c8a745SGarance A Drosehn 			argp++;
842a4c8a745SGarance A Drosehn 		if (*argp != '\0' && strchr(T_SEP, *argp) != NULL) {
843a4c8a745SGarance A Drosehn 			argp++;
844a4c8a745SGarance A Drosehn 			/* Catch case where string ended with a comma. */
845a4c8a745SGarance A Drosehn 			if (*argp == '\0')
846a4c8a745SGarance A Drosehn 				inf->addelem(inf, argp);
847a4c8a745SGarance A Drosehn 		}
848a4c8a745SGarance A Drosehn 	}
849a4c8a745SGarance A Drosehn }
850a4c8a745SGarance A Drosehn 
851a4c8a745SGarance A Drosehn static void *
852a4c8a745SGarance A Drosehn expand_list(struct listinfo *inf)
853a4c8a745SGarance A Drosehn {
854a4c8a745SGarance A Drosehn 	void *newlist;
855ca62e195SGarance A Drosehn 	int newmax;
856a4c8a745SGarance A Drosehn 
857a4c8a745SGarance A Drosehn 	newmax = (inf->maxcount + 1) << 1;
858a4c8a745SGarance A Drosehn 	newlist = realloc(inf->ptr, newmax * inf->elemsize);
859a4c8a745SGarance A Drosehn 	if (newlist == NULL) {
860a4c8a745SGarance A Drosehn 		free(inf->ptr);
861a4c8a745SGarance A Drosehn 		errx(1, "realloc to %d %ss failed", newmax,
862a4c8a745SGarance A Drosehn 		    inf->lname);
863a4c8a745SGarance A Drosehn 	}
864a4c8a745SGarance A Drosehn 	inf->maxcount = newmax;
865a4c8a745SGarance A Drosehn 	inf->ptr = newlist;
866a4c8a745SGarance A Drosehn 
867a4c8a745SGarance A Drosehn 	return (newlist);
868a4c8a745SGarance A Drosehn }
869a4c8a745SGarance A Drosehn 
870a4c8a745SGarance A Drosehn static void
871a4c8a745SGarance A Drosehn free_list(struct listinfo *inf)
872a4c8a745SGarance A Drosehn {
873a4c8a745SGarance A Drosehn 
874a4c8a745SGarance A Drosehn 	inf->count = inf->elemsize = inf->maxcount = 0;
875a4c8a745SGarance A Drosehn 	if (inf->ptr != NULL)
876a4c8a745SGarance A Drosehn 		free(inf->ptr);
877a4c8a745SGarance A Drosehn 	inf->addelem = NULL;
878a4c8a745SGarance A Drosehn 	inf->lname = NULL;
879a4c8a745SGarance A Drosehn 	inf->ptr = NULL;
880a4c8a745SGarance A Drosehn }
881a4c8a745SGarance A Drosehn 
882a4c8a745SGarance A Drosehn static void
883a4c8a745SGarance A Drosehn init_list(struct listinfo *inf, addelem_rtn artn, int elemsize,
884a4c8a745SGarance A Drosehn     const char *lname)
885a4c8a745SGarance A Drosehn {
886a4c8a745SGarance A Drosehn 
887a4c8a745SGarance A Drosehn 	inf->count = inf->maxcount = 0;
888a4c8a745SGarance A Drosehn 	inf->elemsize = elemsize;
889a4c8a745SGarance A Drosehn 	inf->addelem = artn;
890a4c8a745SGarance A Drosehn 	inf->lname = lname;
891a4c8a745SGarance A Drosehn 	inf->ptr = NULL;
892cf22dcfcSBrian Somers }
893cf22dcfcSBrian Somers 
894fde411d5SJuli Mallett VARENT *
895fde411d5SJuli Mallett find_varentry(VAR *v)
896fde411d5SJuli Mallett {
897fde411d5SJuli Mallett 	struct varent *vent;
898fde411d5SJuli Mallett 
899fde411d5SJuli Mallett 	for (vent = vhead; vent; vent = vent->next) {
900fde411d5SJuli Mallett 		if (strcmp(vent->var->name, v->name) == 0)
901fde411d5SJuli Mallett 			return vent;
902fde411d5SJuli Mallett 	}
903fde411d5SJuli Mallett 	return NULL;
904fde411d5SJuli Mallett }
905fde411d5SJuli Mallett 
9064b88c807SRodney W. Grimes static void
90746251ddeSWarner Losh scanvars(void)
9084b88c807SRodney W. Grimes {
9094b88c807SRodney W. Grimes 	struct varent *vent;
9104b88c807SRodney W. Grimes 	VAR *v;
9116a2d726bSJordan K. Hubbard 
9126a2d726bSJordan K. Hubbard 	for (vent = vhead; vent; vent = vent->next) {
9136a2d726bSJordan K. Hubbard 		v = vent->var;
9146a2d726bSJordan K. Hubbard 		if (v->flag & DSIZ) {
9156a2d726bSJordan K. Hubbard 			v->dwidth = v->width;
9166a2d726bSJordan K. Hubbard 			v->width = 0;
9176a2d726bSJordan K. Hubbard 		}
9186a2d726bSJordan K. Hubbard 		if (v->flag & USER)
9196a2d726bSJordan K. Hubbard 			needuser = 1;
9206a2d726bSJordan K. Hubbard 		if (v->flag & COMM)
9216a2d726bSJordan K. Hubbard 			needcomm = 1;
9226a2d726bSJordan K. Hubbard 	}
9236a2d726bSJordan K. Hubbard }
9246a2d726bSJordan K. Hubbard 
9256a2d726bSJordan K. Hubbard static void
92646251ddeSWarner Losh dynsizevars(KINFO *ki)
9276a2d726bSJordan K. Hubbard {
9286a2d726bSJordan K. Hubbard 	struct varent *vent;
9296a2d726bSJordan K. Hubbard 	VAR *v;
9306a2d726bSJordan K. Hubbard 	int i;
9316a2d726bSJordan K. Hubbard 
9326a2d726bSJordan K. Hubbard 	for (vent = vhead; vent; vent = vent->next) {
9336a2d726bSJordan K. Hubbard 		v = vent->var;
9346a2d726bSJordan K. Hubbard 		if (!(v->flag & DSIZ))
9356a2d726bSJordan K. Hubbard 			continue;
9366a2d726bSJordan K. Hubbard 		i = (v->sproc)( ki);
9376a2d726bSJordan K. Hubbard 		if (v->width < i)
9386a2d726bSJordan K. Hubbard 			v->width = i;
9396a2d726bSJordan K. Hubbard 		if (v->width > v->dwidth)
9406a2d726bSJordan K. Hubbard 			v->width = v->dwidth;
9416a2d726bSJordan K. Hubbard 	}
9426a2d726bSJordan K. Hubbard }
9436a2d726bSJordan K. Hubbard 
9446a2d726bSJordan K. Hubbard static void
94546251ddeSWarner Losh sizevars(void)
9466a2d726bSJordan K. Hubbard {
9476a2d726bSJordan K. Hubbard 	struct varent *vent;
9486a2d726bSJordan K. Hubbard 	VAR *v;
9494b88c807SRodney W. Grimes 	int i;
9504b88c807SRodney W. Grimes 
9514b88c807SRodney W. Grimes 	for (vent = vhead; vent; vent = vent->next) {
9524b88c807SRodney W. Grimes 		v = vent->var;
95378b1878aSJuli Mallett 		i = strlen(vent->header);
9544b88c807SRodney W. Grimes 		if (v->width < i)
9554b88c807SRodney W. Grimes 			v->width = i;
9564b88c807SRodney W. Grimes 		totwidth += v->width + 1;	/* +1 for space */
9574b88c807SRodney W. Grimes 	}
9584b88c807SRodney W. Grimes 	totwidth--;
9594b88c807SRodney W. Grimes }
9604b88c807SRodney W. Grimes 
961871e8d8cSMark Murray static const char *
96246251ddeSWarner Losh fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki,
96346251ddeSWarner Losh   char *comm, int maxlen)
9644b88c807SRodney W. Grimes {
965871e8d8cSMark Murray 	const char *s;
9664b88c807SRodney W. Grimes 
967871e8d8cSMark Murray 	s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen);
9684b88c807SRodney W. Grimes 	return (s);
9694b88c807SRodney W. Grimes }
9704b88c807SRodney W. Grimes 
971e0aa5ab7SJohn Baldwin #define UREADOK(ki)	(forceuread || (ki->ki_p->ki_sflag & PS_INMEM))
9723ac5e955SJohn Dyson 
9734b88c807SRodney W. Grimes static void
97446251ddeSWarner Losh saveuser(KINFO *ki)
9754b88c807SRodney W. Grimes {
9764b88c807SRodney W. Grimes 
977e0aa5ab7SJohn Baldwin 	if (ki->ki_p->ki_sflag & PS_INMEM) {
9784b88c807SRodney W. Grimes 		/*
9794b88c807SRodney W. Grimes 		 * The u-area might be swapped out, and we can't get
9804b88c807SRodney W. Grimes 		 * at it because we have a crashdump and no swap.
9814b88c807SRodney W. Grimes 		 * If it's here fill in these fields, otherwise, just
9824b88c807SRodney W. Grimes 		 * leave them 0.
9834b88c807SRodney W. Grimes 		 */
9841f7d2501SKirk McKusick 		ki->ki_valid = 1;
9854b88c807SRodney W. Grimes 	} else
9861f7d2501SKirk McKusick 		ki->ki_valid = 0;
9874b88c807SRodney W. Grimes 	/*
9884b88c807SRodney W. Grimes 	 * save arguments if needed
9894b88c807SRodney W. Grimes 	 */
9901f7d2501SKirk McKusick 	if (needcomm && (UREADOK(ki) || (ki->ki_p->ki_args != NULL))) {
991871e8d8cSMark Murray 		ki->ki_args = strdup(fmt(kvm_getargv, ki, ki->ki_p->ki_comm,
992871e8d8cSMark Murray 		    MAXCOMLEN));
9933ac5e955SJohn Dyson 	} else if (needcomm) {
994871e8d8cSMark Murray 		asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm);
9953ac5e955SJohn Dyson 	} else {
9964b88c807SRodney W. Grimes 		ki->ki_args = NULL;
9973ac5e955SJohn Dyson 	}
9983ac5e955SJohn Dyson 	if (needenv && UREADOK(ki)) {
999871e8d8cSMark Murray 		ki->ki_env = strdup(fmt(kvm_getenvv, ki, (char *)NULL, 0));
10003ac5e955SJohn Dyson 	} else if (needenv) {
10013ac5e955SJohn Dyson 		ki->ki_env = malloc(3);
10023ac5e955SJohn Dyson 		strcpy(ki->ki_env, "()");
10033ac5e955SJohn Dyson 	} else {
10044b88c807SRodney W. Grimes 		ki->ki_env = NULL;
10054b88c807SRodney W. Grimes 	}
10063ac5e955SJohn Dyson }
10074b88c807SRodney W. Grimes 
10084b88c807SRodney W. Grimes static int
100946251ddeSWarner Losh pscomp(const void *a, const void *b)
10104b88c807SRodney W. Grimes {
10114b88c807SRodney W. Grimes 	int i;
10121f7d2501SKirk McKusick #define VSIZE(k) ((k)->ki_p->ki_dsize + (k)->ki_p->ki_ssize + \
10131f7d2501SKirk McKusick 		  (k)->ki_p->ki_tsize)
10144b88c807SRodney W. Grimes 
10154b88c807SRodney W. Grimes 	if (sortby == SORTCPU)
1016871e8d8cSMark Murray 		return (getpcpu((const KINFO *)b) - getpcpu((const KINFO *)a));
10174b88c807SRodney W. Grimes 	if (sortby == SORTMEM)
1018871e8d8cSMark Murray 		return (VSIZE((const KINFO *)b) - VSIZE((const KINFO *)a));
101929d58b10SGarance A Drosehn 	i =  (int)((const KINFO *)a)->ki_p->ki_tdev -
102029d58b10SGarance A Drosehn 	    (int)((const KINFO *)b)->ki_p->ki_tdev;
10214b88c807SRodney W. Grimes 	if (i == 0)
102229d58b10SGarance A Drosehn 		i = ((const KINFO *)a)->ki_p->ki_pid -
102329d58b10SGarance A Drosehn 		    ((const KINFO *)b)->ki_p->ki_pid;
10244b88c807SRodney W. Grimes 	return (i);
10254b88c807SRodney W. Grimes }
10264b88c807SRodney W. Grimes 
10274b88c807SRodney W. Grimes /*
10284b88c807SRodney W. Grimes  * ICK (all for getopt), would rather hide the ugliness
10294b88c807SRodney W. Grimes  * here than taint the main code.
10304b88c807SRodney W. Grimes  *
10314b88c807SRodney W. Grimes  *  ps foo -> ps -foo
10324b88c807SRodney W. Grimes  *  ps 34 -> ps -p34
10334b88c807SRodney W. Grimes  *
10344b88c807SRodney W. Grimes  * The old convention that 't' with no trailing tty arg means the users
10354b88c807SRodney W. Grimes  * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
10364b88c807SRodney W. Grimes  * feature is available with the option 'T', which takes no argument.
10374b88c807SRodney W. Grimes  */
10384b88c807SRodney W. Grimes static char *
103946251ddeSWarner Losh kludge_oldps_options(char *s)
10404b88c807SRodney W. Grimes {
1041daed3ad6SJuli Mallett 	int have_fmt;
10424b88c807SRodney W. Grimes 	size_t len;
10434b88c807SRodney W. Grimes 	char *newopts, *ns, *cp;
10444b88c807SRodney W. Grimes 
1045daed3ad6SJuli Mallett 	/*
1046daed3ad6SJuli Mallett 	 * If we have an 'o' option, then note it, since we don't want to do
1047daed3ad6SJuli Mallett 	 * some types of munging.
1048daed3ad6SJuli Mallett 	 */
1049daed3ad6SJuli Mallett 	have_fmt = index(s, 'o') != NULL;
1050daed3ad6SJuli Mallett 
10514b88c807SRodney W. Grimes 	len = strlen(s);
10524b88c807SRodney W. Grimes 	if ((newopts = ns = malloc(len + 2)) == NULL)
10534fa7d788SJuli Mallett 		errx(1, "malloc failed");
10544b88c807SRodney W. Grimes 	/*
10554b88c807SRodney W. Grimes 	 * options begin with '-'
10564b88c807SRodney W. Grimes 	 */
10574b88c807SRodney W. Grimes 	if (*s != '-')
10584b88c807SRodney W. Grimes 		*ns++ = '-';	/* add option flag */
10594b88c807SRodney W. Grimes 	/*
10604b88c807SRodney W. Grimes 	 * gaze to end of argv[1]
10614b88c807SRodney W. Grimes 	 */
10624b88c807SRodney W. Grimes 	cp = s + len - 1;
10634b88c807SRodney W. Grimes 	/*
10644b88c807SRodney W. Grimes 	 * if last letter is a 't' flag with no argument (in the context
10654b88c807SRodney W. Grimes 	 * of the oldps options -- option string NOT starting with a '-' --
10664b88c807SRodney W. Grimes 	 * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
1067380434d4SBrian Somers 	 *
1068380434d4SBrian Somers 	 * However, if a flag accepting a string argument is found in the
1069380434d4SBrian Somers 	 * option string, the remainder of the string is the argument to
1070380434d4SBrian Somers 	 * that flag; do not modify that argument.
10714b88c807SRodney W. Grimes 	 */
1072831c910aSRuslan Ermilov 	if (strcspn(s, "MNOoU") == len && *cp == 't' && *s != '-')
10734b88c807SRodney W. Grimes 		*cp = 'T';
10744b88c807SRodney W. Grimes 	else {
10754b88c807SRodney W. Grimes 		/*
10764b88c807SRodney W. Grimes 		 * otherwise check for trailing number, which *may* be a
10774b88c807SRodney W. Grimes 		 * pid.
10784b88c807SRodney W. Grimes 		 */
10794b88c807SRodney W. Grimes 		while (cp >= s && isdigit(*cp))
10804b88c807SRodney W. Grimes 			--cp;
10814b88c807SRodney W. Grimes 	}
10824b88c807SRodney W. Grimes 	cp++;
10834b88c807SRodney W. Grimes 	memmove(ns, s, (size_t)(cp - s));	/* copy up to trailing number */
10844b88c807SRodney W. Grimes 	ns += cp - s;
10854b88c807SRodney W. Grimes 	/*
10864b88c807SRodney W. Grimes 	 * if there's a trailing number, and not a preceding 'p' (pid) or
10874b88c807SRodney W. Grimes 	 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
10884b88c807SRodney W. Grimes 	 */
10890fd510b7SJoerg Wunsch 	if (isdigit(*cp) &&
10900fd510b7SJoerg Wunsch 	    (cp == s || (cp[-1] != 't' && cp[-1] != 'p')) &&
1091daed3ad6SJuli Mallett 	    (cp - 1 == s || cp[-2] != 't') && !have_fmt)
10924b88c807SRodney W. Grimes 		*ns++ = 'p';
10934b88c807SRodney W. Grimes 	(void)strcpy(ns, cp);		/* and append the number */
10944b88c807SRodney W. Grimes 
10954b88c807SRodney W. Grimes 	return (newopts);
10964b88c807SRodney W. Grimes }
10974b88c807SRodney W. Grimes 
10984b88c807SRodney W. Grimes static void
109946251ddeSWarner Losh usage(void)
11004b88c807SRodney W. Grimes {
1101b34f38aeSGarance A Drosehn #define	SINGLE_OPTS	"[-aC" OPT_LAZY_f "HhjlmrSTuvwXxZ]"
11024b88c807SRodney W. Grimes 
1103a4c8a745SGarance A Drosehn 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
1104b34f38aeSGarance A Drosehn 	    "usage: ps " SINGLE_OPTS " [-G gid[,gid]] [-O|o fmt]",
1105a4c8a745SGarance A Drosehn 	    "          [-p pid[,pid]] [-t tty[,tty]] [-U user[,user]]",
1106a4c8a745SGarance A Drosehn 	    "          [-M core] [-N system]",
11074b88c807SRodney W. Grimes 	    "       ps [-L]");
11084b88c807SRodney W. Grimes 	exit(1);
11094b88c807SRodney W. Grimes }
1110