xref: /freebsd/bin/ps/ps.c (revision d822163f3b7abc52afea78fb2bfbc3d04168d197)
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>
57c7910c3aSGarance A Drosehn #include <sys/proc.h>
588b6f5f5fSDavid Greenman #include <sys/user.h>
594b88c807SRodney W. Grimes #include <sys/stat.h>
604b88c807SRodney W. Grimes #include <sys/ioctl.h>
614b88c807SRodney W. Grimes #include <sys/sysctl.h>
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes #include <ctype.h>
644b88c807SRodney W. Grimes #include <err.h>
654e8b6a6fSGarance A Drosehn #include <errno.h>
664b88c807SRodney W. Grimes #include <fcntl.h>
67a4c8a745SGarance A Drosehn #include <grp.h>
684b88c807SRodney W. Grimes #include <kvm.h>
69b2496d93SMike Pritchard #include <limits.h>
7008017519SAndrey A. Chernov #include <locale.h>
714b88c807SRodney W. Grimes #include <paths.h>
72871e8d8cSMark Murray #include <pwd.h>
73a4c8a745SGarance A Drosehn #include <stdint.h>
744b88c807SRodney W. Grimes #include <stdio.h>
754b88c807SRodney W. Grimes #include <stdlib.h>
764b88c807SRodney W. Grimes #include <string.h>
774b88c807SRodney W. Grimes #include <unistd.h>
784b88c807SRodney W. Grimes 
794b88c807SRodney W. Grimes #include "ps.h"
804b88c807SRodney W. Grimes 
81a4c8a745SGarance A Drosehn #define	W_SEP	" \t"		/* "Whitespace" list separators */
82a4c8a745SGarance A Drosehn #define	T_SEP	","		/* "Terminate-element" list separators */
83cf22dcfcSBrian Somers 
84de800cd4SGarance A Drosehn #ifdef LAZY_PS
85c46bb4b3SGarance A Drosehn #define	DEF_UREAD	0
86de800cd4SGarance A Drosehn #define	OPT_LAZY_f	"f"
87de800cd4SGarance A Drosehn #else
88c46bb4b3SGarance A Drosehn #define	DEF_UREAD	1	/* Always do the more-expensive read. */
89de800cd4SGarance A Drosehn #define	OPT_LAZY_f		/* I.e., the `-f' option is not added. */
90de800cd4SGarance A Drosehn #endif
914b88c807SRodney W. Grimes 
92db91faacSPeter Wemm int	 cflag;			/* -c */
93de800cd4SGarance A Drosehn int	 eval;			/* Exit value */
94de800cd4SGarance A Drosehn time_t	 now;			/* Current time(3) value */
954b88c807SRodney W. Grimes int	 rawcpu;		/* -C */
964b88c807SRodney W. Grimes int	 sumrusage;		/* -S */
97de800cd4SGarance A Drosehn int	 termwidth;		/* Width of the screen (0 == infinity). */
98de800cd4SGarance A Drosehn int	 totwidth;		/* Calculated-width of requested variables. */
994b88c807SRodney W. Grimes 
100de800cd4SGarance A Drosehn struct varent *vhead;
101de800cd4SGarance A Drosehn 
102de800cd4SGarance A Drosehn static int	 forceuread = DEF_UREAD; /* Do extra work to get u-area. */
103de800cd4SGarance A Drosehn static kvm_t	*kd;
104de800cd4SGarance A Drosehn static KINFO	*kinfo;
105de800cd4SGarance A Drosehn static int	 needcomm;	/* -o "command" */
106de800cd4SGarance A Drosehn static int	 needenv;	/* -e */
107de800cd4SGarance A Drosehn static int	 needuser;	/* -o "user" */
108de800cd4SGarance A Drosehn static int	 optfatal;	/* Fatal error parsing some list-option. */
109de800cd4SGarance A Drosehn 
110de800cd4SGarance A Drosehn static enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
111ba2cd770SJuli Mallett 
112a4c8a745SGarance A Drosehn struct listinfo;
113de800cd4SGarance A Drosehn typedef	int	addelem_rtn(struct listinfo *_inf, const char *_elem);
114a4c8a745SGarance A Drosehn 
115a4c8a745SGarance A Drosehn struct listinfo {
116a4c8a745SGarance A Drosehn 	int		 count;
117a4c8a745SGarance A Drosehn 	int		 maxcount;
118a4c8a745SGarance A Drosehn 	int		 elemsize;
119a4c8a745SGarance A Drosehn 	addelem_rtn	*addelem;
120a4c8a745SGarance A Drosehn 	const char	*lname;
121a4c8a745SGarance A Drosehn 	union {
122a4c8a745SGarance A Drosehn 		gid_t	*gids;
123a4c8a745SGarance A Drosehn 		pid_t	*pids;
124a4c8a745SGarance A Drosehn 		dev_t	*ttys;
125a4c8a745SGarance A Drosehn 		uid_t	*uids;
126a4c8a745SGarance A Drosehn 		void	*ptr;
127d822163fSGarance A Drosehn 	} l;
128a4c8a745SGarance A Drosehn };
129a4c8a745SGarance A Drosehn 
130a4c8a745SGarance A Drosehn static int	 addelem_gid(struct listinfo *, const char *);
131a4c8a745SGarance A Drosehn static int	 addelem_pid(struct listinfo *, const char *);
132a4c8a745SGarance A Drosehn static int	 addelem_tty(struct listinfo *, const char *);
133a4c8a745SGarance A Drosehn static int	 addelem_uid(struct listinfo *, const char *);
134a4c8a745SGarance A Drosehn static void	 add_list(struct listinfo *, const char *);
1354857f240SGarance A Drosehn static void	 dynsizevars(KINFO *);
136a4c8a745SGarance A Drosehn static void	*expand_list(struct listinfo *);
137f35e0715SGarance A Drosehn static const char *
138f35e0715SGarance A Drosehn 		 fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int),
1394857f240SGarance A Drosehn 		    KINFO *, char *, int);
140a4c8a745SGarance A Drosehn static void	 free_list(struct listinfo *);
141a4c8a745SGarance A Drosehn static void	 init_list(struct listinfo *, addelem_rtn, int, const char *);
1424857f240SGarance A Drosehn static char	*kludge_oldps_options(char *);
1434857f240SGarance A Drosehn static int	 pscomp(const void *, const void *);
1444857f240SGarance A Drosehn static void	 saveuser(KINFO *);
1454857f240SGarance A Drosehn static void	 scanvars(void);
1464857f240SGarance A Drosehn static void	 sizevars(void);
1474857f240SGarance A Drosehn static void	 usage(void);
1484b88c807SRodney W. Grimes 
149c0716492SJuli Mallett static char dfmt[] = "pid,tt,state,time,command";
150c0716492SJuli Mallett static char jfmt[] = "user,pid,ppid,pgid,jobc,state,tt,time,command";
1511d2324f4SGarance A Drosehn static char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state,"
1521d2324f4SGarance A Drosehn 			"tt,time,command";
153871e8d8cSMark Murray static char   o1[] = "pid";
154c0716492SJuli Mallett static char   o2[] = "tt,state,time,command";
155c0716492SJuli Mallett static char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command";
1561d2324f4SGarance A Drosehn static char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz,"
1571d2324f4SGarance A Drosehn 			"%cpu,%mem,command";
1582af538ebSRobert Watson static char Zfmt[] = "label";
1594b88c807SRodney W. Grimes 
160b34f38aeSGarance A Drosehn #define	PS_ARGS	"AaCc" OPT_LAZY_f "G:gHhjLlM:mN:O:o:p:rSTt:U:uvwXxZ"
16141623b2dSMaxim Sobolev 
1624b88c807SRodney W. Grimes int
16346251ddeSWarner Losh main(int argc, char *argv[])
1644b88c807SRodney W. Grimes {
165a4c8a745SGarance A Drosehn 	struct listinfo gidlist, pgrplist, pidlist;
166a4c8a745SGarance A Drosehn 	struct listinfo ruidlist, sesslist, ttylist, uidlist;
1674b88c807SRodney W. Grimes 	struct kinfo_proc *kp;
1684b88c807SRodney W. Grimes 	struct varent *vent;
1694b88c807SRodney W. Grimes 	struct winsize ws;
170ca62e195SGarance A Drosehn 	const char *cp, *nlistf, *memf;
171ca62e195SGarance A Drosehn 	char *cols;
172a4c8a745SGarance A Drosehn 	int all, ch, dropgid, elem, flag, _fmt, i, lineno;
173a4c8a745SGarance A Drosehn 	int nentries, nocludge, nkept, nselectors;
174a4c8a745SGarance A Drosehn 	int prtheader, showthreads, wflag, what, xkeep, xkeep_implied;
175871e8d8cSMark Murray 	char errbuf[_POSIX2_LINE_MAX];
1764b88c807SRodney W. Grimes 
1772bf4b9cfSAndrey A. Chernov 	(void) setlocale(LC_ALL, "");
178352b6523SGarance A Drosehn 	time(&now);			/* Used by routines in print.c. */
1792bf4b9cfSAndrey A. Chernov 
1804f18100dSTim J. Robbins 	if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0')
1814f18100dSTim J. Robbins 		termwidth = atoi(cols);
1824f18100dSTim J. Robbins 	else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1834b88c807SRodney W. Grimes 	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1844b88c807SRodney W. Grimes 	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
1854b88c807SRodney W. Grimes 	     ws.ws_col == 0)
1864b88c807SRodney W. Grimes 		termwidth = 79;
1874b88c807SRodney W. Grimes 	else
1884b88c807SRodney W. Grimes 		termwidth = ws.ws_col - 1;
1894b88c807SRodney W. Grimes 
19041623b2dSMaxim Sobolev 	/*
19141623b2dSMaxim Sobolev 	 * Don't apply a kludge if the first argument is an option taking an
19241623b2dSMaxim Sobolev 	 * argument
19341623b2dSMaxim Sobolev 	 */
19441623b2dSMaxim Sobolev 	if (argc > 1) {
19541623b2dSMaxim Sobolev 		nocludge = 0;
19641623b2dSMaxim Sobolev 		if (argv[1][0] == '-') {
19741623b2dSMaxim Sobolev 			for (cp = PS_ARGS; *cp != '\0'; cp++) {
19841623b2dSMaxim Sobolev 				if (*cp != ':')
19941623b2dSMaxim Sobolev 					continue;
20041623b2dSMaxim Sobolev 				if (*(cp - 1) == argv[1][1]) {
20141623b2dSMaxim Sobolev 					nocludge = 1;
20241623b2dSMaxim Sobolev 					break;
20341623b2dSMaxim Sobolev 				}
20441623b2dSMaxim Sobolev 			}
20541623b2dSMaxim Sobolev 		}
20641623b2dSMaxim Sobolev 		if (nocludge == 0)
2074b88c807SRodney W. Grimes 			argv[1] = kludge_oldps_options(argv[1]);
20841623b2dSMaxim Sobolev 	}
2094b88c807SRodney W. Grimes 
210352b6523SGarance A Drosehn 	all = dropgid = _fmt = nselectors = optfatal = 0;
211352b6523SGarance A Drosehn 	prtheader = showthreads = wflag = xkeep_implied = 0;
212352b6523SGarance A Drosehn 	xkeep = -1;			/* Neither -x nor -X. */
213a4c8a745SGarance A Drosehn 	init_list(&gidlist, addelem_gid, sizeof(gid_t), "group");
214a4c8a745SGarance A Drosehn 	init_list(&pgrplist, addelem_pid, sizeof(pid_t), "process group");
215a4c8a745SGarance A Drosehn 	init_list(&pidlist, addelem_pid, sizeof(pid_t), "process id");
216a4c8a745SGarance A Drosehn 	init_list(&ruidlist, addelem_uid, sizeof(uid_t), "ruser");
217a4c8a745SGarance A Drosehn 	init_list(&sesslist, addelem_pid, sizeof(pid_t), "session id");
218a4c8a745SGarance A Drosehn 	init_list(&ttylist, addelem_tty, sizeof(dev_t), "tty");
219a4c8a745SGarance A Drosehn 	init_list(&uidlist, addelem_uid, sizeof(uid_t), "user");
220831c910aSRuslan Ermilov 	memf = nlistf = _PATH_DEVNULL;
22141623b2dSMaxim Sobolev 	while ((ch = getopt(argc, argv, PS_ARGS)) != -1)
2224b88c807SRodney W. Grimes 		switch((char)ch) {
223a4c8a745SGarance A Drosehn 		case 'A':
224a4c8a745SGarance A Drosehn 			/*
225a4c8a745SGarance A Drosehn 			 * Exactly the same as `-ax'.   This has been
226a4c8a745SGarance A Drosehn 			 * added for compatability with SUSv3, but for
227a4c8a745SGarance A Drosehn 			 * now it will not be described in the man page.
228a4c8a745SGarance A Drosehn 			 */
229a4c8a745SGarance A Drosehn 			nselectors++;
230a4c8a745SGarance A Drosehn 			all = xkeep = 1;
231a4c8a745SGarance A Drosehn 			break;
2324b88c807SRodney W. Grimes 		case 'a':
233a4c8a745SGarance A Drosehn 			nselectors++;
2344b88c807SRodney W. Grimes 			all = 1;
2354b88c807SRodney W. Grimes 			break;
2364b88c807SRodney W. Grimes 		case 'C':
2374b88c807SRodney W. Grimes 			rawcpu = 1;
2384b88c807SRodney W. Grimes 			break;
239db91faacSPeter Wemm 		case 'c':
240db91faacSPeter Wemm 			cflag = 1;
241db91faacSPeter Wemm 			break;
242db91faacSPeter Wemm 		case 'e':			/* XXX set ufmt */
243db91faacSPeter Wemm 			needenv = 1;
244db91faacSPeter Wemm 			break;
2454a355d17SGarance A Drosehn #ifdef LAZY_PS
2464a355d17SGarance A Drosehn 		case 'f':
2474a355d17SGarance A Drosehn 			if (getuid() == 0 || getgid() == 0)
2484a355d17SGarance A Drosehn 				forceuread = 1;
2494a355d17SGarance A Drosehn 			break;
2504a355d17SGarance A Drosehn #endif
251a4c8a745SGarance A Drosehn 		case 'G':
252a4c8a745SGarance A Drosehn 			add_list(&gidlist, optarg);
253a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
254a4c8a745SGarance A Drosehn 			nselectors++;
255a4c8a745SGarance A Drosehn 			break;
2564b88c807SRodney W. Grimes 		case 'g':
257352b6523SGarance A Drosehn #if 0
258ba50b0e0SGarance A Drosehn 			/*-
259352b6523SGarance A Drosehn 			 * XXX - This SUSv3 behavior is still under debate
260352b6523SGarance A Drosehn 			 *	since it conflicts with the (undocumented)
261352b6523SGarance A Drosehn 			 *	`-g' option.  So we skip it for now.
262352b6523SGarance A Drosehn 			 */
263a4c8a745SGarance A Drosehn 			add_list(&pgrplist, optarg);
264a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
265a4c8a745SGarance A Drosehn 			nselectors++;
266a4c8a745SGarance A Drosehn 			break;
267a4c8a745SGarance A Drosehn #else
268352b6523SGarance A Drosehn 			/* The historical BSD-ish (from SunOS) behavior. */
2694b88c807SRodney W. Grimes 			break;			/* no-op */
270a4c8a745SGarance A Drosehn #endif
27148b8c0deSScott Long 		case 'H':
272d75c1d83SDaniel Eischen 			showthreads = KERN_PROC_INC_THREAD;
27348b8c0deSScott Long 			break;
2744b88c807SRodney W. Grimes 		case 'h':
2754b88c807SRodney W. Grimes 			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
2764b88c807SRodney W. Grimes 			break;
2774b88c807SRodney W. Grimes 		case 'j':
278fde411d5SJuli Mallett 			parsefmt(jfmt, 0);
279871e8d8cSMark Murray 			_fmt = 1;
2804b88c807SRodney W. Grimes 			jfmt[0] = '\0';
2814b88c807SRodney W. Grimes 			break;
2824b88c807SRodney W. Grimes 		case 'L':
2834b88c807SRodney W. Grimes 			showkey();
2844b88c807SRodney W. Grimes 			exit(0);
2854b88c807SRodney W. Grimes 		case 'l':
286fde411d5SJuli Mallett 			parsefmt(lfmt, 0);
287871e8d8cSMark Murray 			_fmt = 1;
2884b88c807SRodney W. Grimes 			lfmt[0] = '\0';
2894b88c807SRodney W. Grimes 			break;
2904b88c807SRodney W. Grimes 		case 'M':
2914b88c807SRodney W. Grimes 			memf = optarg;
29285082fc3SPoul-Henning Kamp 			dropgid = 1;
2934b88c807SRodney W. Grimes 			break;
2944b88c807SRodney W. Grimes 		case 'm':
2954b88c807SRodney W. Grimes 			sortby = SORTMEM;
2964b88c807SRodney W. Grimes 			break;
2974b88c807SRodney W. Grimes 		case 'N':
2984b88c807SRodney W. Grimes 			nlistf = optarg;
29985082fc3SPoul-Henning Kamp 			dropgid = 1;
3004b88c807SRodney W. Grimes 			break;
3014b88c807SRodney W. Grimes 		case 'O':
302fde411d5SJuli Mallett 			parsefmt(o1, 1);
303fde411d5SJuli Mallett 			parsefmt(optarg, 1);
304fde411d5SJuli Mallett 			parsefmt(o2, 1);
3054b88c807SRodney W. Grimes 			o1[0] = o2[0] = '\0';
306871e8d8cSMark Murray 			_fmt = 1;
3074b88c807SRodney W. Grimes 			break;
3084b88c807SRodney W. Grimes 		case 'o':
309fde411d5SJuli Mallett 			parsefmt(optarg, 1);
310871e8d8cSMark Murray 			_fmt = 1;
3114b88c807SRodney W. Grimes 			break;
3124b88c807SRodney W. Grimes 		case 'p':
313a4c8a745SGarance A Drosehn 			add_list(&pidlist, optarg);
314a4c8a745SGarance A Drosehn 			/*
315a4c8a745SGarance A Drosehn 			 * Note: `-p' does not *set* xkeep, but any values
316a4c8a745SGarance A Drosehn 			 * from pidlist are checked before xkeep is.  That
317a4c8a745SGarance A Drosehn 			 * way they are always matched, even if the user
318a4c8a745SGarance A Drosehn 			 * specifies `-X'.
319a4c8a745SGarance A Drosehn 			 */
320a4c8a745SGarance A Drosehn 			nselectors++;
3214b88c807SRodney W. Grimes 			break;
322a4c8a745SGarance A Drosehn #if 0
323a4c8a745SGarance A Drosehn 		case 'R':
324ba50b0e0SGarance A Drosehn 			/*-
325352b6523SGarance A Drosehn 			 * XXX - This un-standard option is still under
326352b6523SGarance A Drosehn 			 *	debate.  This is what SUSv3 defines as
327352b6523SGarance A Drosehn 			 *	the `-U' option, and while it would be
328352b6523SGarance A Drosehn 			 *	nice to have, it could cause even more
329352b6523SGarance A Drosehn 			 *	confusion to implement it as `-R'.
330352b6523SGarance A Drosehn 			 */
331a4c8a745SGarance A Drosehn 			add_list(&ruidlist, optarg);
332a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
333a4c8a745SGarance A Drosehn 			nselectors++;
334a4c8a745SGarance A Drosehn 			break;
335a4c8a745SGarance A Drosehn #endif
3364b88c807SRodney W. Grimes 		case 'r':
3374b88c807SRodney W. Grimes 			sortby = SORTCPU;
3384b88c807SRodney W. Grimes 			break;
3394b88c807SRodney W. Grimes 		case 'S':
3404b88c807SRodney W. Grimes 			sumrusage = 1;
3414b88c807SRodney W. Grimes 			break;
342a4c8a745SGarance A Drosehn #if 0
343a4c8a745SGarance A Drosehn 		case 's':
344ba50b0e0SGarance A Drosehn 			/*-
345352b6523SGarance A Drosehn 			 * XXX - This non-standard option is still under
346352b6523SGarance A Drosehn 			 *	debate.  This *is* supported on Solaris,
347352b6523SGarance A Drosehn 			 *	Linux, and IRIX, but conflicts with `-s'
348352b6523SGarance A Drosehn 			 *	on NetBSD and maybe some older BSD's.
349352b6523SGarance A Drosehn 			 */
350a4c8a745SGarance A Drosehn 			add_list(&sesslist, optarg);
351a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
352a4c8a745SGarance A Drosehn 			nselectors++;
353a4c8a745SGarance A Drosehn 			break;
354a4c8a745SGarance A Drosehn #endif
3554b88c807SRodney W. Grimes 		case 'T':
3564b88c807SRodney W. Grimes 			if ((optarg = ttyname(STDIN_FILENO)) == NULL)
3574b88c807SRodney W. Grimes 				errx(1, "stdin: not a terminal");
3584b88c807SRodney W. Grimes 			/* FALLTHROUGH */
359a4c8a745SGarance A Drosehn 		case 't':
360a4c8a745SGarance A Drosehn 			add_list(&ttylist, optarg);
361a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
362a4c8a745SGarance A Drosehn 			nselectors++;
3634b88c807SRodney W. Grimes 			break;
36473eb8310SPeter Wemm 		case 'U':
365a4c8a745SGarance A Drosehn 			/* This is what SUSv3 defines as the `-u' option. */
366a4c8a745SGarance A Drosehn 			add_list(&uidlist, optarg);
367a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
368a4c8a745SGarance A Drosehn 			nselectors++;
36973eb8310SPeter Wemm 			break;
3704b88c807SRodney W. Grimes 		case 'u':
371fde411d5SJuli Mallett 			parsefmt(ufmt, 0);
3724b88c807SRodney W. Grimes 			sortby = SORTCPU;
373871e8d8cSMark Murray 			_fmt = 1;
3744b88c807SRodney W. Grimes 			ufmt[0] = '\0';
3754b88c807SRodney W. Grimes 			break;
3764b88c807SRodney W. Grimes 		case 'v':
377fde411d5SJuli Mallett 			parsefmt(vfmt, 0);
3784b88c807SRodney W. Grimes 			sortby = SORTMEM;
379871e8d8cSMark Murray 			_fmt = 1;
3804b88c807SRodney W. Grimes 			vfmt[0] = '\0';
3814b88c807SRodney W. Grimes 			break;
3824b88c807SRodney W. Grimes 		case 'w':
3834b88c807SRodney W. Grimes 			if (wflag)
3844b88c807SRodney W. Grimes 				termwidth = UNLIMITED;
3854b88c807SRodney W. Grimes 			else if (termwidth < 131)
3864b88c807SRodney W. Grimes 				termwidth = 131;
3874b88c807SRodney W. Grimes 			wflag++;
3884b88c807SRodney W. Grimes 			break;
389a4c8a745SGarance A Drosehn 		case 'X':
390a4c8a745SGarance A Drosehn 			/*
391a4c8a745SGarance A Drosehn 			 * Note that `-X' and `-x' are not standard "selector"
392a4c8a745SGarance A Drosehn 			 * options. For most selector-options, we check *all*
393a4c8a745SGarance A Drosehn 			 * processes to see if any are matched by the given
394a4c8a745SGarance A Drosehn 			 * value(s).  After we have a set of all the matched
395a4c8a745SGarance A Drosehn 			 * processes, then `-X' and `-x' govern whether we
396a4c8a745SGarance A Drosehn 			 * modify that *matched* set for processes which do
397a4c8a745SGarance A Drosehn 			 * not have a controlling terminal.  `-X' causes
398a4c8a745SGarance A Drosehn 			 * those processes to be deleted from the matched
399a4c8a745SGarance A Drosehn 			 * set, while `-x' causes them to be kept.
400a4c8a745SGarance A Drosehn 			 */
401a4c8a745SGarance A Drosehn 			xkeep = 0;
402a4c8a745SGarance A Drosehn 			break;
4034b88c807SRodney W. Grimes 		case 'x':
404a4c8a745SGarance A Drosehn 			xkeep = 1;
4054b88c807SRodney W. Grimes 			break;
4067304f61fSBrian Feldman 		case 'Z':
407fde411d5SJuli Mallett 			parsefmt(Zfmt, 0);
4087304f61fSBrian Feldman 			Zfmt[0] = '\0';
4097304f61fSBrian Feldman 			break;
4104b88c807SRodney W. Grimes 		case '?':
4114b88c807SRodney W. Grimes 		default:
4124b88c807SRodney W. Grimes 			usage();
4134b88c807SRodney W. Grimes 		}
4144b88c807SRodney W. Grimes 	argc -= optind;
4154b88c807SRodney W. Grimes 	argv += optind;
416a4c8a745SGarance A Drosehn 	if (optfatal)
417352b6523SGarance A Drosehn 		exit(1);		/* Error messages already printed. */
418352b6523SGarance A Drosehn 	if (xkeep < 0)			/* Neither -X nor -x was specified. */
419a4c8a745SGarance A Drosehn 		xkeep = xkeep_implied;
420a4c8a745SGarance A Drosehn 
4214b88c807SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4224b88c807SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4234b88c807SRodney W. Grimes 	if (*argv) {
4244b88c807SRodney W. Grimes 		nlistf = *argv;
425352b6523SGarance A Drosehn 		if (*++argv)
4264b88c807SRodney W. Grimes 			memf = *argv;
4274b88c807SRodney W. Grimes 	}
4284b88c807SRodney W. Grimes #endif
4294b88c807SRodney W. Grimes 	/*
4304b88c807SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4314b88c807SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4324b88c807SRodney W. Grimes 	 */
43385082fc3SPoul-Henning Kamp 	if (dropgid) {
4344b88c807SRodney W. Grimes 		setgid(getgid());
43585082fc3SPoul-Henning Kamp 		setuid(getuid());
43685082fc3SPoul-Henning Kamp 	}
4374b88c807SRodney W. Grimes 
438831c910aSRuslan Ermilov 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
4394b88c807SRodney W. Grimes 	if (kd == 0)
4404b88c807SRodney W. Grimes 		errx(1, "%s", errbuf);
4414b88c807SRodney W. Grimes 
442871e8d8cSMark Murray 	if (!_fmt)
443fde411d5SJuli Mallett 		parsefmt(dfmt, 0);
4444b88c807SRodney W. Grimes 
445a4c8a745SGarance A Drosehn 	if (nselectors == 0) {
446d822163fSGarance A Drosehn 		uidlist.l.ptr = malloc(sizeof(uid_t));
447d822163fSGarance A Drosehn 		if (uidlist.l.ptr == NULL)
4484fa7d788SJuli Mallett 			errx(1, "malloc failed");
449a4c8a745SGarance A Drosehn 		nselectors = 1;
450a4c8a745SGarance A Drosehn 		uidlist.count = uidlist.maxcount = 1;
451d822163fSGarance A Drosehn 		*uidlist.l.uids = getuid();
452cf22dcfcSBrian Somers 	}
4534b88c807SRodney W. Grimes 
4544b88c807SRodney W. Grimes 	/*
4554b88c807SRodney W. Grimes 	 * scan requested variables, noting what structures are needed,
456bdfebd84SKris Kennaway 	 * and adjusting header widths as appropriate.
4574b88c807SRodney W. Grimes 	 */
4584b88c807SRodney W. Grimes 	scanvars();
459a4c8a745SGarance A Drosehn 
4604b88c807SRodney W. Grimes 	/*
461a4c8a745SGarance A Drosehn 	 * Get process list.  If the user requested just one selector-
462a4c8a745SGarance A Drosehn 	 * option, then kvm_getprocs can be asked to return just those
463a4c8a745SGarance A Drosehn 	 * processes.  Otherwise, have it return all processes, and
464a4c8a745SGarance A Drosehn 	 * then this routine will search that full list and select the
465a4c8a745SGarance A Drosehn 	 * processes which match any of the user's selector-options.
4664b88c807SRodney W. Grimes 	 */
467d75c1d83SDaniel Eischen 	what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC;
46848b8c0deSScott Long 	flag = 0;
469a4c8a745SGarance A Drosehn 	if (nselectors == 1) {
470a4c8a745SGarance A Drosehn 		/* XXX - Apparently there's no KERN_PROC_GID flag. */
471a4c8a745SGarance A Drosehn 		if (pgrplist.count == 1) {
472a4c8a745SGarance A Drosehn 			what = KERN_PROC_PGRP | showthreads;
473d822163fSGarance A Drosehn 			flag = *pgrplist.l.pids;
474a4c8a745SGarance A Drosehn 			nselectors = 0;
475a4c8a745SGarance A Drosehn 		} else if (pidlist.count == 1) {
476a4c8a745SGarance A Drosehn 			what = KERN_PROC_PID | showthreads;
477d822163fSGarance A Drosehn 			flag = *pidlist.l.pids;
478a4c8a745SGarance A Drosehn 			nselectors = 0;
479a4c8a745SGarance A Drosehn 		} else if (ruidlist.count == 1) {
480a4c8a745SGarance A Drosehn 			what = KERN_PROC_RUID | showthreads;
481d822163fSGarance A Drosehn 			flag = *ruidlist.l.uids;
482a4c8a745SGarance A Drosehn 			nselectors = 0;
483352b6523SGarance A Drosehn #if 0
484ba50b0e0SGarance A Drosehn 		/*-
485352b6523SGarance A Drosehn 		 * XXX - KERN_PROC_SESSION causes error in kvm_getprocs?
486352b6523SGarance A Drosehn 		 *	For now, always do sid-matching in this routine.
487352b6523SGarance A Drosehn 		 */
488a4c8a745SGarance A Drosehn 		} else if (sesslist.count == 1) {
489a4c8a745SGarance A Drosehn 			what = KERN_PROC_SESSION | showthreads;
490d822163fSGarance A Drosehn 			flag = *sesslist.l.pids;
491a4c8a745SGarance A Drosehn 			nselectors = 0;
492a4c8a745SGarance A Drosehn #endif
493a4c8a745SGarance A Drosehn 		} else if (ttylist.count == 1) {
494a4c8a745SGarance A Drosehn 			what = KERN_PROC_TTY | showthreads;
495d822163fSGarance A Drosehn 			flag = *ttylist.l.ttys;
496a4c8a745SGarance A Drosehn 			nselectors = 0;
497a4c8a745SGarance A Drosehn 		} else if (uidlist.count == 1) {
498a4c8a745SGarance A Drosehn 			what = KERN_PROC_UID | showthreads;
499d822163fSGarance A Drosehn 			flag = *uidlist.l.uids;
500a4c8a745SGarance A Drosehn 			nselectors = 0;
501a4c8a745SGarance A Drosehn 		} else if (all) {
502a4c8a745SGarance A Drosehn 			/* No need for this routine to select processes. */
503a4c8a745SGarance A Drosehn 			nselectors = 0;
504a4c8a745SGarance A Drosehn 		}
5054b88c807SRodney W. Grimes 	}
506d75c1d83SDaniel Eischen 
5074b88c807SRodney W. Grimes 	/*
5084b88c807SRodney W. Grimes 	 * select procs
5094b88c807SRodney W. Grimes 	 */
510a4c8a745SGarance A Drosehn 	nentries = -1;
5114e8b6a6fSGarance A Drosehn 	kp = kvm_getprocs(kd, what, flag, &nentries);
51287e47750SGarance A Drosehn 	if ((kp == NULL && nentries > 0) || (kp != NULL && nentries < 0))
5134b88c807SRodney W. Grimes 		errx(1, "%s", kvm_geterr(kd));
514a4c8a745SGarance A Drosehn 	nkept = 0;
5154e8b6a6fSGarance A Drosehn 	if (nentries > 0) {
5164b88c807SRodney W. Grimes 		if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL)
5174fa7d788SJuli Mallett 			errx(1, "malloc failed");
5184b88c807SRodney W. Grimes 		for (i = nentries; --i >= 0; ++kp) {
519a4c8a745SGarance A Drosehn 			/*
520a4c8a745SGarance A Drosehn 			 * If the user specified multiple selection-criteria,
521a4c8a745SGarance A Drosehn 			 * then keep any process matched by the inclusive OR
522a4c8a745SGarance A Drosehn 			 * of all the selection-criteria given.
523a4c8a745SGarance A Drosehn 			 */
524a4c8a745SGarance A Drosehn 			if (pidlist.count > 0) {
525a4c8a745SGarance A Drosehn 				for (elem = 0; elem < pidlist.count; elem++)
526d822163fSGarance A Drosehn 					if (kp->ki_pid == pidlist.l.pids[elem])
527a4c8a745SGarance A Drosehn 						goto keepit;
528a4c8a745SGarance A Drosehn 			}
529a4c8a745SGarance A Drosehn 			/*
530a4c8a745SGarance A Drosehn 			 * Note that we had to process pidlist before
531a4c8a745SGarance A Drosehn 			 * filtering out processes which do not have
532a4c8a745SGarance A Drosehn 			 * a controlling terminal.
533a4c8a745SGarance A Drosehn 			 */
534a4c8a745SGarance A Drosehn 			if (xkeep == 0) {
535a4c8a745SGarance A Drosehn 				if ((kp->ki_tdev == NODEV ||
536a4c8a745SGarance A Drosehn 				    (kp->ki_flag & P_CONTROLT) == 0))
537a4c8a745SGarance A Drosehn 					continue;
538a4c8a745SGarance A Drosehn 			}
539a4c8a745SGarance A Drosehn 			if (nselectors == 0)
540a4c8a745SGarance A Drosehn 				goto keepit;
541a4c8a745SGarance A Drosehn 			if (gidlist.count > 0) {
542a4c8a745SGarance A Drosehn 				for (elem = 0; elem < gidlist.count; elem++)
543d822163fSGarance A Drosehn 					if (kp->ki_rgid == gidlist.l.gids[elem])
544a4c8a745SGarance A Drosehn 						goto keepit;
545a4c8a745SGarance A Drosehn 			}
546a4c8a745SGarance A Drosehn 			if (pgrplist.count > 0) {
547a4c8a745SGarance A Drosehn 				for (elem = 0; elem < pgrplist.count; elem++)
548d822163fSGarance A Drosehn 					if (kp->ki_pgid ==
549d822163fSGarance A Drosehn 					    pgrplist.l.pids[elem])
550a4c8a745SGarance A Drosehn 						goto keepit;
551a4c8a745SGarance A Drosehn 			}
552a4c8a745SGarance A Drosehn 			if (ruidlist.count > 0) {
553a4c8a745SGarance A Drosehn 				for (elem = 0; elem < ruidlist.count; elem++)
554d822163fSGarance A Drosehn 					if (kp->ki_ruid ==
555d822163fSGarance A Drosehn 					    ruidlist.l.uids[elem])
556a4c8a745SGarance A Drosehn 						goto keepit;
557a4c8a745SGarance A Drosehn 			}
558a4c8a745SGarance A Drosehn 			if (sesslist.count > 0) {
559a4c8a745SGarance A Drosehn 				for (elem = 0; elem < sesslist.count; elem++)
560d822163fSGarance A Drosehn 					if (kp->ki_sid == sesslist.l.pids[elem])
561a4c8a745SGarance A Drosehn 						goto keepit;
562a4c8a745SGarance A Drosehn 			}
563a4c8a745SGarance A Drosehn 			if (ttylist.count > 0) {
564a4c8a745SGarance A Drosehn 				for (elem = 0; elem < ttylist.count; elem++)
565d822163fSGarance A Drosehn 					if (kp->ki_tdev == ttylist.l.ttys[elem])
566a4c8a745SGarance A Drosehn 						goto keepit;
567a4c8a745SGarance A Drosehn 			}
568a4c8a745SGarance A Drosehn 			if (uidlist.count > 0) {
569a4c8a745SGarance A Drosehn 				for (elem = 0; elem < uidlist.count; elem++)
570d822163fSGarance A Drosehn 					if (kp->ki_uid == uidlist.l.uids[elem])
571a4c8a745SGarance A Drosehn 						goto keepit;
572a4c8a745SGarance A Drosehn 			}
573a4c8a745SGarance A Drosehn 			/*
574a4c8a745SGarance A Drosehn 			 * This process did not match any of the user's
575a4c8a745SGarance A Drosehn 			 * selector-options, so skip the process.
576a4c8a745SGarance A Drosehn 			 */
577a4c8a745SGarance A Drosehn 			continue;
578a4c8a745SGarance A Drosehn 
579a4c8a745SGarance A Drosehn 		keepit:
580a4c8a745SGarance A Drosehn 			kinfo[nkept].ki_p = kp;
5814b88c807SRodney W. Grimes 			if (needuser)
582a4c8a745SGarance A Drosehn 				saveuser(&kinfo[nkept]);
583a4c8a745SGarance A Drosehn 			dynsizevars(&kinfo[nkept]);
584a4c8a745SGarance A Drosehn 			nkept++;
5854b88c807SRodney W. Grimes 		}
5864e8b6a6fSGarance A Drosehn 	}
5876a2d726bSJordan K. Hubbard 
5886a2d726bSJordan K. Hubbard 	sizevars();
5896a2d726bSJordan K. Hubbard 
5904b88c807SRodney W. Grimes 	/*
5914b88c807SRodney W. Grimes 	 * print header
5924b88c807SRodney W. Grimes 	 */
5934b88c807SRodney W. Grimes 	printheader();
594a4c8a745SGarance A Drosehn 	if (nkept == 0)
595f8c9c11cSWill Andrews 		exit(1);
596a4c8a745SGarance A Drosehn 
5974b88c807SRodney W. Grimes 	/*
5984b88c807SRodney W. Grimes 	 * sort proc list
5994b88c807SRodney W. Grimes 	 */
600a4c8a745SGarance A Drosehn 	qsort(kinfo, nkept, sizeof(KINFO), pscomp);
6014b88c807SRodney W. Grimes 	/*
602a4c8a745SGarance A Drosehn 	 * For each process, call each variable output function.
6034b88c807SRodney W. Grimes 	 */
604a4c8a745SGarance A Drosehn 	for (i = lineno = 0; i < nkept; i++) {
6054b88c807SRodney W. Grimes 		for (vent = vhead; vent; vent = vent->next) {
6064b88c807SRodney W. Grimes 			(vent->var->oproc)(&kinfo[i], vent);
6074b88c807SRodney W. Grimes 			if (vent->next != NULL)
6084b88c807SRodney W. Grimes 				(void)putchar(' ');
6094b88c807SRodney W. Grimes 		}
6104b88c807SRodney W. Grimes 		(void)putchar('\n');
6114b88c807SRodney W. Grimes 		if (prtheader && lineno++ == prtheader - 4) {
6124b88c807SRodney W. Grimes 			(void)putchar('\n');
6134b88c807SRodney W. Grimes 			printheader();
6144b88c807SRodney W. Grimes 			lineno = 0;
6154b88c807SRodney W. Grimes 		}
6164b88c807SRodney W. Grimes 	}
617a4c8a745SGarance A Drosehn 	free_list(&gidlist);
618a4c8a745SGarance A Drosehn 	free_list(&pidlist);
619a4c8a745SGarance A Drosehn 	free_list(&pgrplist);
620a4c8a745SGarance A Drosehn 	free_list(&ruidlist);
621a4c8a745SGarance A Drosehn 	free_list(&sesslist);
622a4c8a745SGarance A Drosehn 	free_list(&ttylist);
623a4c8a745SGarance A Drosehn 	free_list(&uidlist);
6242e6c6ac4SGarance A Drosehn 
6254b88c807SRodney W. Grimes 	exit(eval);
6264b88c807SRodney W. Grimes }
6274b88c807SRodney W. Grimes 
628a4c8a745SGarance A Drosehn static int
629a4c8a745SGarance A Drosehn addelem_gid(struct listinfo *inf, const char *elem)
6304e8b6a6fSGarance A Drosehn {
631a4c8a745SGarance A Drosehn 	struct group *grp;
632a4c8a745SGarance A Drosehn 	const char *nameorID;
633a4c8a745SGarance A Drosehn 	char *endp;
6340b42be7cSGarance A Drosehn 	u_long bigtemp;
6354e8b6a6fSGarance A Drosehn 
636a4c8a745SGarance A Drosehn 	if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
637a4c8a745SGarance A Drosehn 		if (*elem == '\0')
638a4c8a745SGarance A Drosehn 			warnx("Invalid (zero-length) %s name", inf->lname);
639a4c8a745SGarance A Drosehn 		else
640a4c8a745SGarance A Drosehn 			warnx("%s name too long: %s", inf->lname, elem);
641a4c8a745SGarance A Drosehn 		optfatal = 1;
642352b6523SGarance A Drosehn 		return (0);		/* Do not add this value. */
6434e8b6a6fSGarance A Drosehn 	}
644a4c8a745SGarance A Drosehn 
6454e8b6a6fSGarance A Drosehn 	/*
646a4c8a745SGarance A Drosehn 	 * SUSv3 states that `ps -G grouplist' should match "real-group
647a4c8a745SGarance A Drosehn 	 * ID numbers", and does not mention group-names.  I do want to
648a4c8a745SGarance A Drosehn 	 * also support group-names, so this tries for a group-id first,
649a4c8a745SGarance A Drosehn 	 * and only tries for a name if that doesn't work.  This is the
650a4c8a745SGarance A Drosehn 	 * opposite order of what is done in addelem_uid(), but in
651a4c8a745SGarance A Drosehn 	 * practice the order would only matter for group-names which
652a4c8a745SGarance A Drosehn 	 * are all-numeric.
6534e8b6a6fSGarance A Drosehn 	 */
654a4c8a745SGarance A Drosehn 	grp = NULL;
655a4c8a745SGarance A Drosehn 	nameorID = "named";
656a4c8a745SGarance A Drosehn 	errno = 0;
6570b42be7cSGarance A Drosehn 	bigtemp = strtoul(elem, &endp, 10);
6580b42be7cSGarance A Drosehn 	if (errno == 0 && *endp == '\0' && bigtemp <= GID_MAX) {
659a4c8a745SGarance A Drosehn 		nameorID = "name or ID matches";
6600b42be7cSGarance A Drosehn 		grp = getgrgid((gid_t)bigtemp);
661a4c8a745SGarance A Drosehn 	}
662a4c8a745SGarance A Drosehn 	if (grp == NULL)
663a4c8a745SGarance A Drosehn 		grp = getgrnam(elem);
664a4c8a745SGarance A Drosehn 	if (grp == NULL) {
665a4c8a745SGarance A Drosehn 		warnx("No %s %s '%s'", inf->lname, nameorID, elem);
666a4c8a745SGarance A Drosehn 		optfatal = 1;
667352b6523SGarance A Drosehn 		return (0);		/* Do not add this value. */
668a4c8a745SGarance A Drosehn 	}
669a4c8a745SGarance A Drosehn 
670a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
671a4c8a745SGarance A Drosehn 		expand_list(inf);
672d822163fSGarance A Drosehn 	inf->l.gids[(inf->count)++] = grp->gr_gid;
673a4c8a745SGarance A Drosehn 	return (1);
674a4c8a745SGarance A Drosehn }
675a4c8a745SGarance A Drosehn 
676352b6523SGarance A Drosehn #define	BSD_PID_MAX	99999		/* Copy of PID_MAX from sys/proc.h. */
677a4c8a745SGarance A Drosehn static int
678a4c8a745SGarance A Drosehn addelem_pid(struct listinfo *inf, const char *elem)
679a4c8a745SGarance A Drosehn {
680a4c8a745SGarance A Drosehn 	char *endp;
681ca62e195SGarance A Drosehn 	long tempid;
682a4c8a745SGarance A Drosehn 
683a4c8a745SGarance A Drosehn 	if (*elem == '\0')
684a4c8a745SGarance A Drosehn 		tempid = 0L;
685a4c8a745SGarance A Drosehn 	else {
686a4c8a745SGarance A Drosehn 		errno = 0;
687a4c8a745SGarance A Drosehn 		tempid = strtol(elem, &endp, 10);
688a4c8a745SGarance A Drosehn 		if (*endp != '\0' || tempid < 0 || elem == endp) {
689a4c8a745SGarance A Drosehn 			warnx("Invalid %s: %s", inf->lname, elem);
6904e8b6a6fSGarance A Drosehn 			errno = ERANGE;
6914e8b6a6fSGarance A Drosehn 		} else if (errno != 0 || tempid > BSD_PID_MAX) {
692a4c8a745SGarance A Drosehn 			warnx("%s too large: %s", inf->lname, elem);
6934e8b6a6fSGarance A Drosehn 			errno = ERANGE;
6944e8b6a6fSGarance A Drosehn 		}
6954e8b6a6fSGarance A Drosehn 		if (errno == ERANGE) {
696a4c8a745SGarance A Drosehn 			optfatal = 1;
697352b6523SGarance A Drosehn 			return (0);	/* Do not add this value. */
6984e8b6a6fSGarance A Drosehn 		}
6994e8b6a6fSGarance A Drosehn 	}
7004e8b6a6fSGarance A Drosehn 
701a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
702a4c8a745SGarance A Drosehn 		expand_list(inf);
703d822163fSGarance A Drosehn 	inf->l.pids[(inf->count)++] = tempid;
704a4c8a745SGarance A Drosehn 	return (1);
7054e8b6a6fSGarance A Drosehn }
706a4c8a745SGarance A Drosehn #undef	BSD_PID_MAX
7074e8b6a6fSGarance A Drosehn 
708a4c8a745SGarance A Drosehn static int
709a4c8a745SGarance A Drosehn addelem_tty(struct listinfo *inf, const char *elem)
710cf22dcfcSBrian Somers {
711a4c8a745SGarance A Drosehn 	const char *ttypath;
712ca62e195SGarance A Drosehn 	struct stat sb;
713ca62e195SGarance A Drosehn 	char pathbuf[PATH_MAX];
714a4c8a745SGarance A Drosehn 
715a4c8a745SGarance A Drosehn 	if (strcmp(elem, "co") == 0)
716a4c8a745SGarance A Drosehn 		ttypath = strdup(_PATH_CONSOLE);
717a4c8a745SGarance A Drosehn 	else if (*elem == '/')
718a4c8a745SGarance A Drosehn 		ttypath = elem;
719a4c8a745SGarance A Drosehn 	else {
720a4c8a745SGarance A Drosehn 		strlcpy(pathbuf, _PATH_TTY, sizeof(pathbuf));
721a4c8a745SGarance A Drosehn 		strlcat(pathbuf, elem, sizeof(pathbuf));
722a4c8a745SGarance A Drosehn 		ttypath = pathbuf;
723a4c8a745SGarance A Drosehn 	}
724a4c8a745SGarance A Drosehn 
725a4c8a745SGarance A Drosehn 	if (stat(ttypath, &sb) == -1) {
726a4c8a745SGarance A Drosehn 		warn("%s", ttypath);
727a4c8a745SGarance A Drosehn 		optfatal = 1;
728352b6523SGarance A Drosehn 		return (0);		/* Do not add this value. */
729a4c8a745SGarance A Drosehn 	}
730a4c8a745SGarance A Drosehn 	if (!S_ISCHR(sb.st_mode)) {
731a4c8a745SGarance A Drosehn 		warn("%s: Not a terminal", ttypath);
732a4c8a745SGarance A Drosehn 		optfatal = 1;
733352b6523SGarance A Drosehn 		return (0);		/* Do not add this value. */
734a4c8a745SGarance A Drosehn 	}
735a4c8a745SGarance A Drosehn 
736a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
737a4c8a745SGarance A Drosehn 		expand_list(inf);
738d822163fSGarance A Drosehn 	inf->l.ttys[(inf->count)++] = sb.st_rdev;
739a4c8a745SGarance A Drosehn 	return (1);
740a4c8a745SGarance A Drosehn }
741a4c8a745SGarance A Drosehn 
742a4c8a745SGarance A Drosehn static int
743a4c8a745SGarance A Drosehn addelem_uid(struct listinfo *inf, const char *elem)
744a4c8a745SGarance A Drosehn {
745cf22dcfcSBrian Somers 	struct passwd *pwd;
746a4c8a745SGarance A Drosehn 	char *endp;
7470b42be7cSGarance A Drosehn 	u_long bigtemp;
748cf22dcfcSBrian Somers 
749a4c8a745SGarance A Drosehn 	if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
750a4c8a745SGarance A Drosehn 		if (*elem == '\0')
751a4c8a745SGarance A Drosehn 			warnx("Invalid (zero-length) %s name", inf->lname);
752a4c8a745SGarance A Drosehn 		else
753a4c8a745SGarance A Drosehn 			warnx("%s name too long: %s", inf->lname, elem);
754a4c8a745SGarance A Drosehn 		optfatal = 1;
755352b6523SGarance A Drosehn 		return (0);		/* Do not add this value. */
756a4c8a745SGarance A Drosehn 	}
757cf22dcfcSBrian Somers 
758a4c8a745SGarance A Drosehn 	pwd = getpwnam(elem);
759a4c8a745SGarance A Drosehn 	if (pwd == NULL) {
760a4c8a745SGarance A Drosehn 		errno = 0;
7610b42be7cSGarance A Drosehn 		bigtemp = strtoul(elem, &endp, 10);
7620b42be7cSGarance A Drosehn 		if (errno != 0 || *endp != '\0' || bigtemp > UID_MAX)
763a4c8a745SGarance A Drosehn 			warnx("No %s named '%s'", inf->lname, elem);
764a4c8a745SGarance A Drosehn 		else {
765a4c8a745SGarance A Drosehn 			/* The string is all digits, so it might be a userID. */
7660b42be7cSGarance A Drosehn 			pwd = getpwuid((uid_t)bigtemp);
767a4c8a745SGarance A Drosehn 			if (pwd == NULL)
768a4c8a745SGarance A Drosehn 				warnx("No %s name or ID matches '%s'",
769a4c8a745SGarance A Drosehn 				    inf->lname, elem);
770cf22dcfcSBrian Somers 		}
771cf22dcfcSBrian Somers 	}
772a4c8a745SGarance A Drosehn 	if (pwd == NULL) {
773e3c4e1ddSGarance A Drosehn 		/*
774e3c4e1ddSGarance A Drosehn 		 * These used to be treated as minor warnings (and the
775e3c4e1ddSGarance A Drosehn 		 * option was simply ignored), but now they are fatal
776e3c4e1ddSGarance A Drosehn 		 * errors (and the command will be aborted).
777e3c4e1ddSGarance A Drosehn 		 */
778e3c4e1ddSGarance A Drosehn 		optfatal = 1;
779352b6523SGarance A Drosehn 		return (0);		/* Do not add this value. */
780cf22dcfcSBrian Somers 	}
781cf22dcfcSBrian Somers 
782a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
783a4c8a745SGarance A Drosehn 		expand_list(inf);
784d822163fSGarance A Drosehn 	inf->l.uids[(inf->count)++] = pwd->pw_uid;
785a4c8a745SGarance A Drosehn 	return (1);
786a4c8a745SGarance A Drosehn }
787cf22dcfcSBrian Somers 
788a4c8a745SGarance A Drosehn static void
789a4c8a745SGarance A Drosehn add_list(struct listinfo *inf, const char *argp)
790a4c8a745SGarance A Drosehn {
791a4c8a745SGarance A Drosehn 	const char *savep;
792a4c8a745SGarance A Drosehn 	char *cp, *endp;
793a4c8a745SGarance A Drosehn 	int toolong;
794ca62e195SGarance A Drosehn 	char elemcopy[PATH_MAX];
795a4c8a745SGarance A Drosehn 
796a4c8a745SGarance A Drosehn 	while (*argp != '\0') {
797a4c8a745SGarance A Drosehn 		while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
798a4c8a745SGarance A Drosehn 			argp++;
799a4c8a745SGarance A Drosehn 		savep = argp;
800a4c8a745SGarance A Drosehn 		toolong = 0;
801a4c8a745SGarance A Drosehn 		cp = elemcopy;
802a4c8a745SGarance A Drosehn 		if (strchr(T_SEP, *argp) == NULL) {
803a4c8a745SGarance A Drosehn 			endp = elemcopy + sizeof(elemcopy) - 1;
804a4c8a745SGarance A Drosehn 			while (*argp != '\0' && cp <= endp &&
805a4c8a745SGarance A Drosehn 			    strchr(W_SEP T_SEP, *argp) == NULL)
806a4c8a745SGarance A Drosehn 				*cp++ = *argp++;
807a4c8a745SGarance A Drosehn 			if (cp > endp)
808a4c8a745SGarance A Drosehn 				toolong = 1;
809a4c8a745SGarance A Drosehn 		}
810a4c8a745SGarance A Drosehn 		if (!toolong) {
811a4c8a745SGarance A Drosehn 			*cp = '\0';
812a4c8a745SGarance A Drosehn #ifndef ADD_PS_LISTRESET
813352b6523SGarance A Drosehn 			/*
814352b6523SGarance A Drosehn 			 * This is how the standard expects lists to
815352b6523SGarance A Drosehn 			 * be handled.
816352b6523SGarance A Drosehn 			 */
817a4c8a745SGarance A Drosehn 			inf->addelem(inf, elemcopy);
818a4c8a745SGarance A Drosehn #else
819ba50b0e0SGarance A Drosehn 			/*-
820352b6523SGarance A Drosehn 			 * This would add a simple non-standard-but-convienent
821352b6523SGarance A Drosehn 			 * feature.
822a4c8a745SGarance A Drosehn 			 *
823352b6523SGarance A Drosehn 			 * XXX - The first time I tried to add this check,
824352b6523SGarance A Drosehn 			 *	it increased the total size of `ps' by 3940
825352b6523SGarance A Drosehn 			 *	bytes on i386!  That's 12% of the entire
826352b6523SGarance A Drosehn 			 *	program!  The `ps.o' file grew by only about
827352b6523SGarance A Drosehn 			 *	40 bytes, but the final (stripped) executable
828352b6523SGarance A Drosehn 			 *	in /bin/ps grew by 12%.  I have not had time
829352b6523SGarance A Drosehn 			 *	to investigate, so skip the feature for now.
830a4c8a745SGarance A Drosehn 			 */
831a4c8a745SGarance A Drosehn 			/*
832a4c8a745SGarance A Drosehn 			 * We now have a single element.  Add it to the
833a4c8a745SGarance A Drosehn 			 * list, unless the element is ":".  In that case,
834a4c8a745SGarance A Drosehn 			 * reset the list so previous entries are ignored.
835a4c8a745SGarance A Drosehn 			 */
836a4c8a745SGarance A Drosehn 			if (strcmp(elemcopy, ":") == 0)
837a4c8a745SGarance A Drosehn 				inf->count = 0;
838a4c8a745SGarance A Drosehn 			else
839a4c8a745SGarance A Drosehn 				inf->addelem(inf, elemcopy);
840a4c8a745SGarance A Drosehn #endif
841a4c8a745SGarance A Drosehn 		} else {
842a4c8a745SGarance A Drosehn 			/*
843a4c8a745SGarance A Drosehn 			 * The string is too long to copy.  Find the end
844a4c8a745SGarance A Drosehn 			 * of the string to print out the warning message.
845a4c8a745SGarance A Drosehn 			 */
846a4c8a745SGarance A Drosehn 			while (*argp != '\0' && strchr(W_SEP T_SEP,
847a4c8a745SGarance A Drosehn 			    *argp) == NULL)
848a4c8a745SGarance A Drosehn 				argp++;
849a4c8a745SGarance A Drosehn 			warnx("Value too long: %.*s", (int)(argp - savep),
850a4c8a745SGarance A Drosehn 			    savep);
851a4c8a745SGarance A Drosehn 			optfatal = 1;
852a4c8a745SGarance A Drosehn 		}
853a4c8a745SGarance A Drosehn 		/*
854a4c8a745SGarance A Drosehn 		 * Skip over any number of trailing whitespace characters,
855a4c8a745SGarance A Drosehn 		 * but only one (at most) trailing element-terminating
856a4c8a745SGarance A Drosehn 		 * character.
857a4c8a745SGarance A Drosehn 		 */
858a4c8a745SGarance A Drosehn 		while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
859a4c8a745SGarance A Drosehn 			argp++;
860a4c8a745SGarance A Drosehn 		if (*argp != '\0' && strchr(T_SEP, *argp) != NULL) {
861a4c8a745SGarance A Drosehn 			argp++;
862a4c8a745SGarance A Drosehn 			/* Catch case where string ended with a comma. */
863a4c8a745SGarance A Drosehn 			if (*argp == '\0')
864a4c8a745SGarance A Drosehn 				inf->addelem(inf, argp);
865a4c8a745SGarance A Drosehn 		}
866a4c8a745SGarance A Drosehn 	}
867a4c8a745SGarance A Drosehn }
868a4c8a745SGarance A Drosehn 
869a4c8a745SGarance A Drosehn static void *
870a4c8a745SGarance A Drosehn expand_list(struct listinfo *inf)
871a4c8a745SGarance A Drosehn {
872a4c8a745SGarance A Drosehn 	void *newlist;
873ca62e195SGarance A Drosehn 	int newmax;
874a4c8a745SGarance A Drosehn 
875a4c8a745SGarance A Drosehn 	newmax = (inf->maxcount + 1) << 1;
876d822163fSGarance A Drosehn 	newlist = realloc(inf->l.ptr, newmax * inf->elemsize);
877a4c8a745SGarance A Drosehn 	if (newlist == NULL) {
878d822163fSGarance A Drosehn 		free(inf->l.ptr);
879a4c8a745SGarance A Drosehn 		errx(1, "realloc to %d %ss failed", newmax,
880a4c8a745SGarance A Drosehn 		    inf->lname);
881a4c8a745SGarance A Drosehn 	}
882a4c8a745SGarance A Drosehn 	inf->maxcount = newmax;
883d822163fSGarance A Drosehn 	inf->l.ptr = newlist;
884a4c8a745SGarance A Drosehn 
885a4c8a745SGarance A Drosehn 	return (newlist);
886a4c8a745SGarance A Drosehn }
887a4c8a745SGarance A Drosehn 
888a4c8a745SGarance A Drosehn static void
889a4c8a745SGarance A Drosehn free_list(struct listinfo *inf)
890a4c8a745SGarance A Drosehn {
891a4c8a745SGarance A Drosehn 
892a4c8a745SGarance A Drosehn 	inf->count = inf->elemsize = inf->maxcount = 0;
893d822163fSGarance A Drosehn 	if (inf->l.ptr != NULL)
894d822163fSGarance A Drosehn 		free(inf->l.ptr);
895a4c8a745SGarance A Drosehn 	inf->addelem = NULL;
896a4c8a745SGarance A Drosehn 	inf->lname = NULL;
897d822163fSGarance A Drosehn 	inf->l.ptr = NULL;
898a4c8a745SGarance A Drosehn }
899a4c8a745SGarance A Drosehn 
900a4c8a745SGarance A Drosehn static void
901a4c8a745SGarance A Drosehn init_list(struct listinfo *inf, addelem_rtn artn, int elemsize,
902a4c8a745SGarance A Drosehn     const char *lname)
903a4c8a745SGarance A Drosehn {
904a4c8a745SGarance A Drosehn 
905a4c8a745SGarance A Drosehn 	inf->count = inf->maxcount = 0;
906a4c8a745SGarance A Drosehn 	inf->elemsize = elemsize;
907a4c8a745SGarance A Drosehn 	inf->addelem = artn;
908a4c8a745SGarance A Drosehn 	inf->lname = lname;
909d822163fSGarance A Drosehn 	inf->l.ptr = NULL;
910cf22dcfcSBrian Somers }
911cf22dcfcSBrian Somers 
912fde411d5SJuli Mallett VARENT *
913fde411d5SJuli Mallett find_varentry(VAR *v)
914fde411d5SJuli Mallett {
915fde411d5SJuli Mallett 	struct varent *vent;
916fde411d5SJuli Mallett 
917fde411d5SJuli Mallett 	for (vent = vhead; vent; vent = vent->next) {
918fde411d5SJuli Mallett 		if (strcmp(vent->var->name, v->name) == 0)
919fde411d5SJuli Mallett 			return vent;
920fde411d5SJuli Mallett 	}
921fde411d5SJuli Mallett 	return NULL;
922fde411d5SJuli Mallett }
923fde411d5SJuli Mallett 
9244b88c807SRodney W. Grimes static void
92546251ddeSWarner Losh scanvars(void)
9264b88c807SRodney W. Grimes {
9274b88c807SRodney W. Grimes 	struct varent *vent;
9284b88c807SRodney W. Grimes 	VAR *v;
9296a2d726bSJordan K. Hubbard 
9306a2d726bSJordan K. Hubbard 	for (vent = vhead; vent; vent = vent->next) {
9316a2d726bSJordan K. Hubbard 		v = vent->var;
9326a2d726bSJordan K. Hubbard 		if (v->flag & DSIZ) {
9336a2d726bSJordan K. Hubbard 			v->dwidth = v->width;
9346a2d726bSJordan K. Hubbard 			v->width = 0;
9356a2d726bSJordan K. Hubbard 		}
9366a2d726bSJordan K. Hubbard 		if (v->flag & USER)
9376a2d726bSJordan K. Hubbard 			needuser = 1;
9386a2d726bSJordan K. Hubbard 		if (v->flag & COMM)
9396a2d726bSJordan K. Hubbard 			needcomm = 1;
9406a2d726bSJordan K. Hubbard 	}
9416a2d726bSJordan K. Hubbard }
9426a2d726bSJordan K. Hubbard 
9436a2d726bSJordan K. Hubbard static void
94446251ddeSWarner Losh dynsizevars(KINFO *ki)
9456a2d726bSJordan K. Hubbard {
9466a2d726bSJordan K. Hubbard 	struct varent *vent;
9476a2d726bSJordan K. Hubbard 	VAR *v;
9486a2d726bSJordan K. Hubbard 	int i;
9496a2d726bSJordan K. Hubbard 
9506a2d726bSJordan K. Hubbard 	for (vent = vhead; vent; vent = vent->next) {
9516a2d726bSJordan K. Hubbard 		v = vent->var;
9526a2d726bSJordan K. Hubbard 		if (!(v->flag & DSIZ))
9536a2d726bSJordan K. Hubbard 			continue;
9546a2d726bSJordan K. Hubbard 		i = (v->sproc)( ki);
9556a2d726bSJordan K. Hubbard 		if (v->width < i)
9566a2d726bSJordan K. Hubbard 			v->width = i;
9576a2d726bSJordan K. Hubbard 		if (v->width > v->dwidth)
9586a2d726bSJordan K. Hubbard 			v->width = v->dwidth;
9596a2d726bSJordan K. Hubbard 	}
9606a2d726bSJordan K. Hubbard }
9616a2d726bSJordan K. Hubbard 
9626a2d726bSJordan K. Hubbard static void
96346251ddeSWarner Losh sizevars(void)
9646a2d726bSJordan K. Hubbard {
9656a2d726bSJordan K. Hubbard 	struct varent *vent;
9666a2d726bSJordan K. Hubbard 	VAR *v;
9674b88c807SRodney W. Grimes 	int i;
9684b88c807SRodney W. Grimes 
9694b88c807SRodney W. Grimes 	for (vent = vhead; vent; vent = vent->next) {
9704b88c807SRodney W. Grimes 		v = vent->var;
97178b1878aSJuli Mallett 		i = strlen(vent->header);
9724b88c807SRodney W. Grimes 		if (v->width < i)
9734b88c807SRodney W. Grimes 			v->width = i;
9744b88c807SRodney W. Grimes 		totwidth += v->width + 1;	/* +1 for space */
9754b88c807SRodney W. Grimes 	}
9764b88c807SRodney W. Grimes 	totwidth--;
9774b88c807SRodney W. Grimes }
9784b88c807SRodney W. Grimes 
979871e8d8cSMark Murray static const char *
98046251ddeSWarner Losh fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki,
98146251ddeSWarner Losh     char *comm, int maxlen)
9824b88c807SRodney W. Grimes {
983871e8d8cSMark Murray 	const char *s;
9844b88c807SRodney W. Grimes 
985871e8d8cSMark Murray 	s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen);
9864b88c807SRodney W. Grimes 	return (s);
9874b88c807SRodney W. Grimes }
9884b88c807SRodney W. Grimes 
989e0aa5ab7SJohn Baldwin #define UREADOK(ki)	(forceuread || (ki->ki_p->ki_sflag & PS_INMEM))
9903ac5e955SJohn Dyson 
9914b88c807SRodney W. Grimes static void
99246251ddeSWarner Losh saveuser(KINFO *ki)
9934b88c807SRodney W. Grimes {
9944b88c807SRodney W. Grimes 
995e0aa5ab7SJohn Baldwin 	if (ki->ki_p->ki_sflag & PS_INMEM) {
9964b88c807SRodney W. Grimes 		/*
9974b88c807SRodney W. Grimes 		 * The u-area might be swapped out, and we can't get
9984b88c807SRodney W. Grimes 		 * at it because we have a crashdump and no swap.
9994b88c807SRodney W. Grimes 		 * If it's here fill in these fields, otherwise, just
10004b88c807SRodney W. Grimes 		 * leave them 0.
10014b88c807SRodney W. Grimes 		 */
10021f7d2501SKirk McKusick 		ki->ki_valid = 1;
10034b88c807SRodney W. Grimes 	} else
10041f7d2501SKirk McKusick 		ki->ki_valid = 0;
10054b88c807SRodney W. Grimes 	/*
10064b88c807SRodney W. Grimes 	 * save arguments if needed
10074b88c807SRodney W. Grimes 	 */
10081f7d2501SKirk McKusick 	if (needcomm && (UREADOK(ki) || (ki->ki_p->ki_args != NULL))) {
1009871e8d8cSMark Murray 		ki->ki_args = strdup(fmt(kvm_getargv, ki, ki->ki_p->ki_comm,
1010871e8d8cSMark Murray 		    MAXCOMLEN));
10113ac5e955SJohn Dyson 	} else if (needcomm) {
1012871e8d8cSMark Murray 		asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm);
10133ac5e955SJohn Dyson 	} else {
10144b88c807SRodney W. Grimes 		ki->ki_args = NULL;
10153ac5e955SJohn Dyson 	}
10163ac5e955SJohn Dyson 	if (needenv && UREADOK(ki)) {
1017871e8d8cSMark Murray 		ki->ki_env = strdup(fmt(kvm_getenvv, ki, (char *)NULL, 0));
10183ac5e955SJohn Dyson 	} else if (needenv) {
10193ac5e955SJohn Dyson 		ki->ki_env = malloc(3);
10203ac5e955SJohn Dyson 		strcpy(ki->ki_env, "()");
10213ac5e955SJohn Dyson 	} else {
10224b88c807SRodney W. Grimes 		ki->ki_env = NULL;
10234b88c807SRodney W. Grimes 	}
10243ac5e955SJohn Dyson }
10254b88c807SRodney W. Grimes 
10264b88c807SRodney W. Grimes static int
102746251ddeSWarner Losh pscomp(const void *a, const void *b)
10284b88c807SRodney W. Grimes {
10295bd7b1f3SGarance A Drosehn 	const KINFO *ka, *kb;
10305bd7b1f3SGarance A Drosehn 	double cpua, cpub;
10315bd7b1f3SGarance A Drosehn 	segsz_t sizea, sizeb;
10324b88c807SRodney W. Grimes 
10335bd7b1f3SGarance A Drosehn 	ka = a;
10345bd7b1f3SGarance A Drosehn 	kb = b;
10355bd7b1f3SGarance A Drosehn 	/* SORTCPU and SORTMEM are sorted in descending order. */
10365bd7b1f3SGarance A Drosehn 	if (sortby == SORTCPU) {
10375bd7b1f3SGarance A Drosehn 		cpua = getpcpu(ka);
10385bd7b1f3SGarance A Drosehn 		cpub = getpcpu(kb);
10395bd7b1f3SGarance A Drosehn 		if (cpua < cpub)
10405bd7b1f3SGarance A Drosehn 			return (1);
10415bd7b1f3SGarance A Drosehn 		if (cpua > cpub)
10425bd7b1f3SGarance A Drosehn 			return (-1);
10435bd7b1f3SGarance A Drosehn 	}
10445bd7b1f3SGarance A Drosehn 	if (sortby == SORTMEM) {
10455bd7b1f3SGarance A Drosehn 		sizea = ka->ki_p->ki_tsize + ka->ki_p->ki_dsize +
10465bd7b1f3SGarance A Drosehn 		    ka->ki_p->ki_ssize;
10475bd7b1f3SGarance A Drosehn 		sizeb = kb->ki_p->ki_tsize + kb->ki_p->ki_dsize +
10485bd7b1f3SGarance A Drosehn 		    kb->ki_p->ki_ssize;
10495bd7b1f3SGarance A Drosehn 		if (sizea < sizeb)
10505bd7b1f3SGarance A Drosehn 			return (1);
10515bd7b1f3SGarance A Drosehn 		if (sizea > sizeb)
10525bd7b1f3SGarance A Drosehn 			return (-1);
10535bd7b1f3SGarance A Drosehn 	}
10545bd7b1f3SGarance A Drosehn 	/*
10555bd7b1f3SGarance A Drosehn 	 * TTY's are sorted in ascending order, except that all NODEV
10565bd7b1f3SGarance A Drosehn 	 * processes come before all processes with a device.
10575bd7b1f3SGarance A Drosehn 	 */
10585bd7b1f3SGarance A Drosehn 	if (ka->ki_p->ki_tdev == NODEV && kb->ki_p->ki_tdev != NODEV)
10595bd7b1f3SGarance A Drosehn 		return (-1);
10605bd7b1f3SGarance A Drosehn 	if (ka->ki_p->ki_tdev != NODEV && kb->ki_p->ki_tdev == NODEV)
10615bd7b1f3SGarance A Drosehn 		return (1);
10625bd7b1f3SGarance A Drosehn 	if (ka->ki_p->ki_tdev < kb->ki_p->ki_tdev)
10635bd7b1f3SGarance A Drosehn 		return (-1);
10645bd7b1f3SGarance A Drosehn 	if (ka->ki_p->ki_tdev > kb->ki_p->ki_tdev)
10655bd7b1f3SGarance A Drosehn 		return (1);
10665bd7b1f3SGarance A Drosehn 	/* PID's are sorted in ascending order. */
10675bd7b1f3SGarance A Drosehn 	if (ka->ki_p->ki_pid < kb->ki_p->ki_pid)
10685bd7b1f3SGarance A Drosehn 		return (-1);
10695bd7b1f3SGarance A Drosehn 	if (ka->ki_p->ki_pid > kb->ki_p->ki_pid)
10705bd7b1f3SGarance A Drosehn 		return (1);
10715bd7b1f3SGarance A Drosehn 	return (0);
10724b88c807SRodney W. Grimes }
10734b88c807SRodney W. Grimes 
10744b88c807SRodney W. Grimes /*
10754b88c807SRodney W. Grimes  * ICK (all for getopt), would rather hide the ugliness
10764b88c807SRodney W. Grimes  * here than taint the main code.
10774b88c807SRodney W. Grimes  *
10784b88c807SRodney W. Grimes  *  ps foo -> ps -foo
10794b88c807SRodney W. Grimes  *  ps 34 -> ps -p34
10804b88c807SRodney W. Grimes  *
10814b88c807SRodney W. Grimes  * The old convention that 't' with no trailing tty arg means the users
10824b88c807SRodney W. Grimes  * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
10834b88c807SRodney W. Grimes  * feature is available with the option 'T', which takes no argument.
10844b88c807SRodney W. Grimes  */
10854b88c807SRodney W. Grimes static char *
108646251ddeSWarner Losh kludge_oldps_options(char *s)
10874b88c807SRodney W. Grimes {
1088daed3ad6SJuli Mallett 	int have_fmt;
10894b88c807SRodney W. Grimes 	size_t len;
10904b88c807SRodney W. Grimes 	char *newopts, *ns, *cp;
10914b88c807SRodney W. Grimes 
1092daed3ad6SJuli Mallett 	/*
1093daed3ad6SJuli Mallett 	 * If we have an 'o' option, then note it, since we don't want to do
1094daed3ad6SJuli Mallett 	 * some types of munging.
1095daed3ad6SJuli Mallett 	 */
1096daed3ad6SJuli Mallett 	have_fmt = index(s, 'o') != NULL;
1097daed3ad6SJuli Mallett 
10984b88c807SRodney W. Grimes 	len = strlen(s);
10994b88c807SRodney W. Grimes 	if ((newopts = ns = malloc(len + 2)) == NULL)
11004fa7d788SJuli Mallett 		errx(1, "malloc failed");
11014b88c807SRodney W. Grimes 	/*
11024b88c807SRodney W. Grimes 	 * options begin with '-'
11034b88c807SRodney W. Grimes 	 */
11044b88c807SRodney W. Grimes 	if (*s != '-')
11054b88c807SRodney W. Grimes 		*ns++ = '-';	/* add option flag */
11064b88c807SRodney W. Grimes 	/*
11074b88c807SRodney W. Grimes 	 * gaze to end of argv[1]
11084b88c807SRodney W. Grimes 	 */
11094b88c807SRodney W. Grimes 	cp = s + len - 1;
11104b88c807SRodney W. Grimes 	/*
11114b88c807SRodney W. Grimes 	 * if last letter is a 't' flag with no argument (in the context
11124b88c807SRodney W. Grimes 	 * of the oldps options -- option string NOT starting with a '-' --
11134b88c807SRodney W. Grimes 	 * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
1114380434d4SBrian Somers 	 *
1115380434d4SBrian Somers 	 * However, if a flag accepting a string argument is found in the
1116380434d4SBrian Somers 	 * option string, the remainder of the string is the argument to
1117380434d4SBrian Somers 	 * that flag; do not modify that argument.
11184b88c807SRodney W. Grimes 	 */
1119831c910aSRuslan Ermilov 	if (strcspn(s, "MNOoU") == len && *cp == 't' && *s != '-')
11204b88c807SRodney W. Grimes 		*cp = 'T';
11214b88c807SRodney W. Grimes 	else {
11224b88c807SRodney W. Grimes 		/*
11234b88c807SRodney W. Grimes 		 * otherwise check for trailing number, which *may* be a
11244b88c807SRodney W. Grimes 		 * pid.
11254b88c807SRodney W. Grimes 		 */
11264b88c807SRodney W. Grimes 		while (cp >= s && isdigit(*cp))
11274b88c807SRodney W. Grimes 			--cp;
11284b88c807SRodney W. Grimes 	}
11294b88c807SRodney W. Grimes 	cp++;
11304b88c807SRodney W. Grimes 	memmove(ns, s, (size_t)(cp - s));	/* copy up to trailing number */
11314b88c807SRodney W. Grimes 	ns += cp - s;
11324b88c807SRodney W. Grimes 	/*
11334b88c807SRodney W. Grimes 	 * if there's a trailing number, and not a preceding 'p' (pid) or
11344b88c807SRodney W. Grimes 	 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
11354b88c807SRodney W. Grimes 	 */
11360fd510b7SJoerg Wunsch 	if (isdigit(*cp) &&
11370fd510b7SJoerg Wunsch 	    (cp == s || (cp[-1] != 't' && cp[-1] != 'p')) &&
1138daed3ad6SJuli Mallett 	    (cp - 1 == s || cp[-2] != 't') && !have_fmt)
11394b88c807SRodney W. Grimes 		*ns++ = 'p';
11404b88c807SRodney W. Grimes 	(void)strcpy(ns, cp);		/* and append the number */
11414b88c807SRodney W. Grimes 
11424b88c807SRodney W. Grimes 	return (newopts);
11434b88c807SRodney W. Grimes }
11444b88c807SRodney W. Grimes 
11454b88c807SRodney W. Grimes static void
114646251ddeSWarner Losh usage(void)
11474b88c807SRodney W. Grimes {
1148b34f38aeSGarance A Drosehn #define	SINGLE_OPTS	"[-aC" OPT_LAZY_f "HhjlmrSTuvwXxZ]"
11494b88c807SRodney W. Grimes 
1150a4c8a745SGarance A Drosehn 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
1151b34f38aeSGarance A Drosehn 	    "usage: ps " SINGLE_OPTS " [-G gid[,gid]] [-O|o fmt]",
1152a4c8a745SGarance A Drosehn 	    "          [-p pid[,pid]] [-t tty[,tty]] [-U user[,user]]",
1153a4c8a745SGarance A Drosehn 	    "          [-M core] [-N system]",
11544b88c807SRodney W. Grimes 	    "       ps [-L]");
11554b88c807SRodney W. Grimes 	exit(1);
11564b88c807SRodney W. Grimes }
1157