xref: /freebsd/bin/ps/ps.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
14b88c807SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
84b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
94b88c807SRodney W. Grimes  * are met:
104b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
124b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
144b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
164b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
174b88c807SRodney W. Grimes  *    without specific prior written permission.
184b88c807SRodney W. Grimes  *
194b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
204b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
234b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294b88c807SRodney W. Grimes  * SUCH DAMAGE.
30a4c8a745SGarance A Drosehn  * ------+---------+---------+-------- + --------+---------+---------+---------*
31a4c8a745SGarance A Drosehn  * Copyright (c) 2004  - Garance Alistair Drosehn <gad@FreeBSD.org>.
32a4c8a745SGarance A Drosehn  * All rights reserved.
33a4c8a745SGarance A Drosehn  *
34a4c8a745SGarance A Drosehn  * Significant modifications made to bring `ps' options somewhat closer
35a4c8a745SGarance A Drosehn  * to the standard for `ps' as described in SingleUnixSpec-v3.
36a4c8a745SGarance A Drosehn  * ------+---------+---------+-------- + --------+---------+---------+---------*
374b88c807SRodney W. Grimes  */
384b88c807SRodney W. Grimes 
394b88c807SRodney W. Grimes #ifndef lint
40871e8d8cSMark Murray static const char copyright[] =
414b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\
424b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
434b88c807SRodney W. Grimes #endif /* not lint */
444b88c807SRodney W. Grimes 
45c9a8d1f4SPhilippe Charnier #if 0
46871e8d8cSMark Murray #ifndef lint
47c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)ps.c	8.4 (Berkeley) 4/2/94";
484b88c807SRodney W. Grimes #endif /* not lint */
49871e8d8cSMark Murray #endif
50eaed5652SPhilippe Charnier 
512749b141SDavid E. O'Brien #include <sys/cdefs.h>
522749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
534b88c807SRodney W. Grimes 
544b88c807SRodney W. Grimes #include <sys/param.h>
5513767130SBryan Drewery #include <sys/jail.h>
56c7910c3aSGarance A Drosehn #include <sys/proc.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>
61a951d1f8SChristian S.J. Peron #include <sys/mount.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>
6813767130SBryan Drewery #include <jail.h>
694b88c807SRodney W. Grimes #include <kvm.h>
70b2496d93SMike Pritchard #include <limits.h>
7108017519SAndrey A. Chernov #include <locale.h>
724b88c807SRodney W. Grimes #include <paths.h>
73871e8d8cSMark Murray #include <pwd.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>
788beb1a2fSMarcel Moolenaar #include <libxo/xo.h>
794b88c807SRodney W. Grimes 
804b88c807SRodney W. Grimes #include "ps.h"
814b88c807SRodney W. Grimes 
828a529f59SJohn Baldwin #define	_PATH_PTS	"/dev/pts/"
838a529f59SJohn Baldwin 
84a4c8a745SGarance A Drosehn #define	W_SEP	" \t"		/* "Whitespace" list separators */
85a4c8a745SGarance A Drosehn #define	T_SEP	","		/* "Terminate-element" list separators */
86cf22dcfcSBrian Somers 
87de800cd4SGarance A Drosehn #ifdef LAZY_PS
88c46bb4b3SGarance A Drosehn #define	DEF_UREAD	0
89de800cd4SGarance A Drosehn #define	OPT_LAZY_f	"f"
90de800cd4SGarance A Drosehn #else
91c46bb4b3SGarance A Drosehn #define	DEF_UREAD	1	/* Always do the more-expensive read. */
92de800cd4SGarance A Drosehn #define	OPT_LAZY_f		/* I.e., the `-f' option is not added. */
93de800cd4SGarance A Drosehn #endif
944b88c807SRodney W. Grimes 
95c675340aSGarance A Drosehn /*
96ecbb06f8SGarance A Drosehn  * isdigit takes an `int', but expects values in the range of unsigned char.
97ecbb06f8SGarance A Drosehn  * This wrapper ensures that values from a 'char' end up in the correct range.
98c675340aSGarance A Drosehn  */
99ecbb06f8SGarance A Drosehn #define	isdigitch(Anychar) isdigit((u_char)(Anychar))
100c675340aSGarance A Drosehn 
101db91faacSPeter Wemm int	 cflag;			/* -c */
102de800cd4SGarance A Drosehn int	 eval;			/* Exit value */
103de800cd4SGarance A Drosehn time_t	 now;			/* Current time(3) value */
1044b88c807SRodney W. Grimes int	 rawcpu;		/* -C */
1054b88c807SRodney W. Grimes int	 sumrusage;		/* -S */
106de800cd4SGarance A Drosehn int	 termwidth;		/* Width of the screen (0 == infinity). */
1077ab24ea3SJulian Elischer int	 showthreads;		/* will threads be shown? */
1084b88c807SRodney W. Grimes 
109bdf8ab46SGarance A Drosehn struct velisthead varlist = STAILQ_HEAD_INITIALIZER(varlist);
110de800cd4SGarance A Drosehn 
111de800cd4SGarance A Drosehn static int	 forceuread = DEF_UREAD; /* Do extra work to get u-area. */
112de800cd4SGarance A Drosehn static kvm_t	*kd;
113de800cd4SGarance A Drosehn static int	 needcomm;	/* -o "command" */
114de800cd4SGarance A Drosehn static int	 needenv;	/* -e */
115de800cd4SGarance A Drosehn static int	 needuser;	/* -o "user" */
116de800cd4SGarance A Drosehn static int	 optfatal;	/* Fatal error parsing some list-option. */
11764b0bf0bSPawel Jakub Dawidek static int	 pid_max;	/* kern.max_pid */
118de800cd4SGarance A Drosehn 
119de800cd4SGarance A Drosehn static enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
120ba2cd770SJuli Mallett 
121a4c8a745SGarance A Drosehn struct listinfo;
122de800cd4SGarance A Drosehn typedef	int	addelem_rtn(struct listinfo *_inf, const char *_elem);
123a4c8a745SGarance A Drosehn 
124a4c8a745SGarance A Drosehn struct listinfo {
125a4c8a745SGarance A Drosehn 	int		 count;
126a4c8a745SGarance A Drosehn 	int		 maxcount;
127a4c8a745SGarance A Drosehn 	int		 elemsize;
128a4c8a745SGarance A Drosehn 	addelem_rtn	*addelem;
129a4c8a745SGarance A Drosehn 	const char	*lname;
130a4c8a745SGarance A Drosehn 	union {
131a4c8a745SGarance A Drosehn 		gid_t	*gids;
13213767130SBryan Drewery 		int	*jids;
133a4c8a745SGarance A Drosehn 		pid_t	*pids;
134a4c8a745SGarance A Drosehn 		dev_t	*ttys;
135a4c8a745SGarance A Drosehn 		uid_t	*uids;
136a4c8a745SGarance A Drosehn 		void	*ptr;
137d822163fSGarance A Drosehn 	} l;
138a4c8a745SGarance A Drosehn };
139a4c8a745SGarance A Drosehn 
140a4c8a745SGarance A Drosehn static int	 addelem_gid(struct listinfo *, const char *);
14113767130SBryan Drewery static int	 addelem_jid(struct listinfo *, const char *);
142a4c8a745SGarance A Drosehn static int	 addelem_pid(struct listinfo *, const char *);
143a4c8a745SGarance A Drosehn static int	 addelem_tty(struct listinfo *, const char *);
144a4c8a745SGarance A Drosehn static int	 addelem_uid(struct listinfo *, const char *);
145a4c8a745SGarance A Drosehn static void	 add_list(struct listinfo *, const char *);
146044fce53SBrian Somers static void	 descendant_sort(KINFO *, int);
1471d1143ecSEdward Tomasz Napierala static void	 format_output(KINFO *);
148a4c8a745SGarance A Drosehn static void	*expand_list(struct listinfo *);
149f35e0715SGarance A Drosehn static const char *
150f35e0715SGarance A Drosehn 		 fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int),
1511c67ef12SJohn Baldwin 		    KINFO *, char *, char *, int);
152a4c8a745SGarance A Drosehn static void	 free_list(struct listinfo *);
153a4c8a745SGarance A Drosehn static void	 init_list(struct listinfo *, addelem_rtn, int, const char *);
15425113083SGarance A Drosehn static char	*kludge_oldps_options(const char *, char *, const char *);
1554857f240SGarance A Drosehn static int	 pscomp(const void *, const void *);
1564857f240SGarance A Drosehn static void	 saveuser(KINFO *);
1574857f240SGarance A Drosehn static void	 scanvars(void);
1584857f240SGarance A Drosehn static void	 sizevars(void);
15964b0bf0bSPawel Jakub Dawidek static void	 pidmax_init(void);
1604857f240SGarance A Drosehn static void	 usage(void);
1614b88c807SRodney W. Grimes 
162c0716492SJuli Mallett static char dfmt[] = "pid,tt,state,time,command";
163259fcfacSGarance A Drosehn static char jfmt[] = "user,pid,ppid,pgid,sid,jobc,state,tt,time,command";
1641d2324f4SGarance A Drosehn static char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state,"
1651d2324f4SGarance A Drosehn 			"tt,time,command";
166871e8d8cSMark Murray static char   o1[] = "pid";
167c0716492SJuli Mallett static char   o2[] = "tt,state,time,command";
168c0716492SJuli Mallett static char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command";
1691d2324f4SGarance A Drosehn static char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz,"
1701d2324f4SGarance A Drosehn 			"%cpu,%mem,command";
1712af538ebSRobert Watson static char Zfmt[] = "label";
1724b88c807SRodney W. Grimes 
17313767130SBryan Drewery #define	PS_ARGS	"AaCcde" OPT_LAZY_f "G:gHhjJ:LlM:mN:O:o:p:rSTt:U:uvwXxZ"
17441623b2dSMaxim Sobolev 
1754b88c807SRodney W. Grimes int
17646251ddeSWarner Losh main(int argc, char *argv[])
1774b88c807SRodney W. Grimes {
17813767130SBryan Drewery 	struct listinfo gidlist, jidlist, pgrplist, pidlist;
179a4c8a745SGarance A Drosehn 	struct listinfo ruidlist, sesslist, ttylist, uidlist;
1804b88c807SRodney W. Grimes 	struct kinfo_proc *kp;
1811d1143ecSEdward Tomasz Napierala 	KINFO *kinfo = NULL, *next_KINFO;
1821d1143ecSEdward Tomasz Napierala 	KINFO_STR *ks;
1834b88c807SRodney W. Grimes 	struct varent *vent;
184484f5781SPedro F. Giffuni 	struct winsize ws = { .ws_row = 0 };
18516eb3576SMarcelo Araujo 	const char *nlistf, *memf, *str;
186ca62e195SGarance A Drosehn 	char *cols;
1871d1143ecSEdward Tomasz Napierala 	int all, ch, elem, flag, _fmt, i, lineno, linelen, left;
188044fce53SBrian Somers 	int descendancy, nentries, nkept, nselectors;
1897ab24ea3SJulian Elischer 	int prtheader, wflag, what, xkeep, xkeep_implied;
1908beb1a2fSMarcel Moolenaar 	int fwidthmin, fwidthmax;
191871e8d8cSMark Murray 	char errbuf[_POSIX2_LINE_MAX];
1928beb1a2fSMarcel Moolenaar 	char fmtbuf[_POSIX2_LINE_MAX];
1934b88c807SRodney W. Grimes 
1942bf4b9cfSAndrey A. Chernov 	(void) setlocale(LC_ALL, "");
195352b6523SGarance A Drosehn 	time(&now);			/* Used by routines in print.c. */
1962bf4b9cfSAndrey A. Chernov 
1974f18100dSTim J. Robbins 	if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0')
1984f18100dSTim J. Robbins 		termwidth = atoi(cols);
1999c4241c3SConrad Meyer 	else if (!isatty(STDOUT_FILENO))
2009c4241c3SConrad Meyer 		termwidth = UNLIMITED;
2014f18100dSTim J. Robbins 	else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
2024b88c807SRodney W. Grimes 	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
2034b88c807SRodney W. Grimes 	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
2044b88c807SRodney W. Grimes 	     ws.ws_col == 0)
2054b88c807SRodney W. Grimes 		termwidth = 79;
2064b88c807SRodney W. Grimes 	else
2074b88c807SRodney W. Grimes 		termwidth = ws.ws_col - 1;
2084b88c807SRodney W. Grimes 
20941623b2dSMaxim Sobolev 	/*
210c675340aSGarance A Drosehn 	 * Hide a number of option-processing kludges in a separate routine,
211c675340aSGarance A Drosehn 	 * to support some historical BSD behaviors, such as `ps axu'.
21241623b2dSMaxim Sobolev 	 */
213c675340aSGarance A Drosehn 	if (argc > 1)
214bf46a3bfSGarance A Drosehn 		argv[1] = kludge_oldps_options(PS_ARGS, argv[1], argv[2]);
2154b88c807SRodney W. Grimes 
21664b0bf0bSPawel Jakub Dawidek 	pidmax_init();
21764b0bf0bSPawel Jakub Dawidek 
218044fce53SBrian Somers 	all = descendancy = _fmt = nselectors = optfatal = 0;
219352b6523SGarance A Drosehn 	prtheader = showthreads = wflag = xkeep_implied = 0;
220352b6523SGarance A Drosehn 	xkeep = -1;			/* Neither -x nor -X. */
221a4c8a745SGarance A Drosehn 	init_list(&gidlist, addelem_gid, sizeof(gid_t), "group");
22213767130SBryan Drewery 	init_list(&jidlist, addelem_jid, sizeof(int), "jail id");
223a4c8a745SGarance A Drosehn 	init_list(&pgrplist, addelem_pid, sizeof(pid_t), "process group");
224a4c8a745SGarance A Drosehn 	init_list(&pidlist, addelem_pid, sizeof(pid_t), "process id");
225a4c8a745SGarance A Drosehn 	init_list(&ruidlist, addelem_uid, sizeof(uid_t), "ruser");
226a4c8a745SGarance A Drosehn 	init_list(&sesslist, addelem_pid, sizeof(pid_t), "session id");
227a4c8a745SGarance A Drosehn 	init_list(&ttylist, addelem_tty, sizeof(dev_t), "tty");
228a4c8a745SGarance A Drosehn 	init_list(&uidlist, addelem_uid, sizeof(uid_t), "user");
229c08dcaf1SRebecca Cran 	memf = _PATH_DEVNULL;
230c08dcaf1SRebecca Cran 	nlistf = NULL;
2318beb1a2fSMarcel Moolenaar 
2328beb1a2fSMarcel Moolenaar 	argc = xo_parse_args(argc, argv);
2338beb1a2fSMarcel Moolenaar 	if (argc < 0)
2348beb1a2fSMarcel Moolenaar 		exit(1);
2358beb1a2fSMarcel Moolenaar 
23641623b2dSMaxim Sobolev 	while ((ch = getopt(argc, argv, PS_ARGS)) != -1)
2370c025af9SKevin Lo 		switch (ch) {
238a4c8a745SGarance A Drosehn 		case 'A':
239a4c8a745SGarance A Drosehn 			/*
240a4c8a745SGarance A Drosehn 			 * Exactly the same as `-ax'.   This has been
241bf2fe08eSUlrich Spörlein 			 * added for compatibility with SUSv3, but for
242a4c8a745SGarance A Drosehn 			 * now it will not be described in the man page.
243a4c8a745SGarance A Drosehn 			 */
244a4c8a745SGarance A Drosehn 			nselectors++;
245a4c8a745SGarance A Drosehn 			all = xkeep = 1;
246a4c8a745SGarance A Drosehn 			break;
2474b88c807SRodney W. Grimes 		case 'a':
248a4c8a745SGarance A Drosehn 			nselectors++;
2494b88c807SRodney W. Grimes 			all = 1;
2504b88c807SRodney W. Grimes 			break;
2514b88c807SRodney W. Grimes 		case 'C':
2524b88c807SRodney W. Grimes 			rawcpu = 1;
2534b88c807SRodney W. Grimes 			break;
254db91faacSPeter Wemm 		case 'c':
255db91faacSPeter Wemm 			cflag = 1;
256db91faacSPeter Wemm 			break;
257044fce53SBrian Somers 		case 'd':
258044fce53SBrian Somers 			descendancy = 1;
259044fce53SBrian Somers 			break;
260db91faacSPeter Wemm 		case 'e':			/* XXX set ufmt */
261db91faacSPeter Wemm 			needenv = 1;
262db91faacSPeter Wemm 			break;
2634a355d17SGarance A Drosehn #ifdef LAZY_PS
2644a355d17SGarance A Drosehn 		case 'f':
2654a355d17SGarance A Drosehn 			if (getuid() == 0 || getgid() == 0)
2664a355d17SGarance A Drosehn 				forceuread = 1;
2674a355d17SGarance A Drosehn 			break;
2684a355d17SGarance A Drosehn #endif
269a4c8a745SGarance A Drosehn 		case 'G':
270a4c8a745SGarance A Drosehn 			add_list(&gidlist, optarg);
271a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
272a4c8a745SGarance A Drosehn 			nselectors++;
273a4c8a745SGarance A Drosehn 			break;
2744b88c807SRodney W. Grimes 		case 'g':
275352b6523SGarance A Drosehn #if 0
276ba50b0e0SGarance A Drosehn 			/*-
277352b6523SGarance A Drosehn 			 * XXX - This SUSv3 behavior is still under debate
278352b6523SGarance A Drosehn 			 *	since it conflicts with the (undocumented)
279352b6523SGarance A Drosehn 			 *	`-g' option.  So we skip it for now.
280352b6523SGarance A Drosehn 			 */
281a4c8a745SGarance A Drosehn 			add_list(&pgrplist, optarg);
282a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
283a4c8a745SGarance A Drosehn 			nselectors++;
284a4c8a745SGarance A Drosehn 			break;
285a4c8a745SGarance A Drosehn #else
286352b6523SGarance A Drosehn 			/* The historical BSD-ish (from SunOS) behavior. */
2874b88c807SRodney W. Grimes 			break;			/* no-op */
288a4c8a745SGarance A Drosehn #endif
28948b8c0deSScott Long 		case 'H':
290d75c1d83SDaniel Eischen 			showthreads = KERN_PROC_INC_THREAD;
29148b8c0deSScott Long 			break;
2924b88c807SRodney W. Grimes 		case 'h':
2934b88c807SRodney W. Grimes 			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
2944b88c807SRodney W. Grimes 			break;
29513767130SBryan Drewery 		case 'J':
29613767130SBryan Drewery 			add_list(&jidlist, optarg);
29713767130SBryan Drewery 			xkeep_implied = 1;
29813767130SBryan Drewery 			nselectors++;
29913767130SBryan Drewery 			break;
3004b88c807SRodney W. Grimes 		case 'j':
301fde411d5SJuli Mallett 			parsefmt(jfmt, 0);
302871e8d8cSMark Murray 			_fmt = 1;
3034b88c807SRodney W. Grimes 			jfmt[0] = '\0';
3044b88c807SRodney W. Grimes 			break;
3054b88c807SRodney W. Grimes 		case 'L':
3064b88c807SRodney W. Grimes 			showkey();
3074b88c807SRodney W. Grimes 			exit(0);
3084b88c807SRodney W. Grimes 		case 'l':
309fde411d5SJuli Mallett 			parsefmt(lfmt, 0);
310871e8d8cSMark Murray 			_fmt = 1;
3114b88c807SRodney W. Grimes 			lfmt[0] = '\0';
3124b88c807SRodney W. Grimes 			break;
3134b88c807SRodney W. Grimes 		case 'M':
3144b88c807SRodney W. Grimes 			memf = optarg;
3154b88c807SRodney W. Grimes 			break;
3164b88c807SRodney W. Grimes 		case 'm':
3174b88c807SRodney W. Grimes 			sortby = SORTMEM;
3184b88c807SRodney W. Grimes 			break;
3194b88c807SRodney W. Grimes 		case 'N':
3204b88c807SRodney W. Grimes 			nlistf = optarg;
3214b88c807SRodney W. Grimes 			break;
3224b88c807SRodney W. Grimes 		case 'O':
323fde411d5SJuli Mallett 			parsefmt(o1, 1);
324fde411d5SJuli Mallett 			parsefmt(optarg, 1);
325fde411d5SJuli Mallett 			parsefmt(o2, 1);
3264b88c807SRodney W. Grimes 			o1[0] = o2[0] = '\0';
327871e8d8cSMark Murray 			_fmt = 1;
3284b88c807SRodney W. Grimes 			break;
3294b88c807SRodney W. Grimes 		case 'o':
330fde411d5SJuli Mallett 			parsefmt(optarg, 1);
331871e8d8cSMark Murray 			_fmt = 1;
3324b88c807SRodney W. Grimes 			break;
3334b88c807SRodney W. Grimes 		case 'p':
334a4c8a745SGarance A Drosehn 			add_list(&pidlist, optarg);
335a4c8a745SGarance A Drosehn 			/*
336a4c8a745SGarance A Drosehn 			 * Note: `-p' does not *set* xkeep, but any values
337a4c8a745SGarance A Drosehn 			 * from pidlist are checked before xkeep is.  That
338a4c8a745SGarance A Drosehn 			 * way they are always matched, even if the user
339a4c8a745SGarance A Drosehn 			 * specifies `-X'.
340a4c8a745SGarance A Drosehn 			 */
341a4c8a745SGarance A Drosehn 			nselectors++;
3424b88c807SRodney W. Grimes 			break;
343a4c8a745SGarance A Drosehn #if 0
344a4c8a745SGarance A Drosehn 		case 'R':
345ba50b0e0SGarance A Drosehn 			/*-
346352b6523SGarance A Drosehn 			 * XXX - This un-standard option is still under
347352b6523SGarance A Drosehn 			 *	debate.  This is what SUSv3 defines as
348352b6523SGarance A Drosehn 			 *	the `-U' option, and while it would be
349352b6523SGarance A Drosehn 			 *	nice to have, it could cause even more
350352b6523SGarance A Drosehn 			 *	confusion to implement it as `-R'.
351352b6523SGarance A Drosehn 			 */
352a4c8a745SGarance A Drosehn 			add_list(&ruidlist, optarg);
353a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
354a4c8a745SGarance A Drosehn 			nselectors++;
355a4c8a745SGarance A Drosehn 			break;
356a4c8a745SGarance A Drosehn #endif
3574b88c807SRodney W. Grimes 		case 'r':
3584b88c807SRodney W. Grimes 			sortby = SORTCPU;
3594b88c807SRodney W. Grimes 			break;
3604b88c807SRodney W. Grimes 		case 'S':
3614b88c807SRodney W. Grimes 			sumrusage = 1;
3624b88c807SRodney W. Grimes 			break;
363a4c8a745SGarance A Drosehn #if 0
364a4c8a745SGarance A Drosehn 		case 's':
365ba50b0e0SGarance A Drosehn 			/*-
366352b6523SGarance A Drosehn 			 * XXX - This non-standard option is still under
367352b6523SGarance A Drosehn 			 *	debate.  This *is* supported on Solaris,
368352b6523SGarance A Drosehn 			 *	Linux, and IRIX, but conflicts with `-s'
369352b6523SGarance A Drosehn 			 *	on NetBSD and maybe some older BSD's.
370352b6523SGarance A Drosehn 			 */
371a4c8a745SGarance A Drosehn 			add_list(&sesslist, optarg);
372a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
373a4c8a745SGarance A Drosehn 			nselectors++;
374a4c8a745SGarance A Drosehn 			break;
375a4c8a745SGarance A Drosehn #endif
3764b88c807SRodney W. Grimes 		case 'T':
3774b88c807SRodney W. Grimes 			if ((optarg = ttyname(STDIN_FILENO)) == NULL)
3788beb1a2fSMarcel Moolenaar 				xo_errx(1, "stdin: not a terminal");
3794b88c807SRodney W. Grimes 			/* FALLTHROUGH */
380a4c8a745SGarance A Drosehn 		case 't':
381a4c8a745SGarance A Drosehn 			add_list(&ttylist, optarg);
382a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
383a4c8a745SGarance A Drosehn 			nselectors++;
3844b88c807SRodney W. Grimes 			break;
38573eb8310SPeter Wemm 		case 'U':
386a4c8a745SGarance A Drosehn 			/* This is what SUSv3 defines as the `-u' option. */
387a4c8a745SGarance A Drosehn 			add_list(&uidlist, optarg);
388a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
389a4c8a745SGarance A Drosehn 			nselectors++;
39073eb8310SPeter Wemm 			break;
3914b88c807SRodney W. Grimes 		case 'u':
392fde411d5SJuli Mallett 			parsefmt(ufmt, 0);
3934b88c807SRodney W. Grimes 			sortby = SORTCPU;
394871e8d8cSMark Murray 			_fmt = 1;
3954b88c807SRodney W. Grimes 			ufmt[0] = '\0';
3964b88c807SRodney W. Grimes 			break;
3974b88c807SRodney W. Grimes 		case 'v':
398fde411d5SJuli Mallett 			parsefmt(vfmt, 0);
3994b88c807SRodney W. Grimes 			sortby = SORTMEM;
400871e8d8cSMark Murray 			_fmt = 1;
4014b88c807SRodney W. Grimes 			vfmt[0] = '\0';
4024b88c807SRodney W. Grimes 			break;
4034b88c807SRodney W. Grimes 		case 'w':
4044b88c807SRodney W. Grimes 			if (wflag)
4054b88c807SRodney W. Grimes 				termwidth = UNLIMITED;
406ef1d40daSConrad Meyer 			else if (termwidth < 131 && termwidth != UNLIMITED)
4074b88c807SRodney W. Grimes 				termwidth = 131;
4084b88c807SRodney W. Grimes 			wflag++;
4094b88c807SRodney W. Grimes 			break;
410a4c8a745SGarance A Drosehn 		case 'X':
411a4c8a745SGarance A Drosehn 			/*
412a4c8a745SGarance A Drosehn 			 * Note that `-X' and `-x' are not standard "selector"
413a4c8a745SGarance A Drosehn 			 * options. For most selector-options, we check *all*
414a4c8a745SGarance A Drosehn 			 * processes to see if any are matched by the given
415a4c8a745SGarance A Drosehn 			 * value(s).  After we have a set of all the matched
416a4c8a745SGarance A Drosehn 			 * processes, then `-X' and `-x' govern whether we
417a4c8a745SGarance A Drosehn 			 * modify that *matched* set for processes which do
418a4c8a745SGarance A Drosehn 			 * not have a controlling terminal.  `-X' causes
419a4c8a745SGarance A Drosehn 			 * those processes to be deleted from the matched
420a4c8a745SGarance A Drosehn 			 * set, while `-x' causes them to be kept.
421a4c8a745SGarance A Drosehn 			 */
422a4c8a745SGarance A Drosehn 			xkeep = 0;
423a4c8a745SGarance A Drosehn 			break;
4244b88c807SRodney W. Grimes 		case 'x':
425a4c8a745SGarance A Drosehn 			xkeep = 1;
4264b88c807SRodney W. Grimes 			break;
4277304f61fSBrian Feldman 		case 'Z':
428fde411d5SJuli Mallett 			parsefmt(Zfmt, 0);
4297304f61fSBrian Feldman 			Zfmt[0] = '\0';
4307304f61fSBrian Feldman 			break;
4314b88c807SRodney W. Grimes 		case '?':
4324b88c807SRodney W. Grimes 		default:
4334b88c807SRodney W. Grimes 			usage();
4344b88c807SRodney W. Grimes 		}
4354b88c807SRodney W. Grimes 	argc -= optind;
4364b88c807SRodney W. Grimes 	argv += optind;
437c675340aSGarance A Drosehn 
438c675340aSGarance A Drosehn 	/*
439c675340aSGarance A Drosehn 	 * If there arguments after processing all the options, attempt
440c675340aSGarance A Drosehn 	 * to treat them as a list of process ids.
441c675340aSGarance A Drosehn 	 */
442c675340aSGarance A Drosehn 	while (*argv) {
443c675340aSGarance A Drosehn 		if (!isdigitch(**argv))
444c675340aSGarance A Drosehn 			break;
445c675340aSGarance A Drosehn 		add_list(&pidlist, *argv);
446c675340aSGarance A Drosehn 		argv++;
447c675340aSGarance A Drosehn 	}
448c675340aSGarance A Drosehn 	if (*argv) {
4498beb1a2fSMarcel Moolenaar 		xo_warnx("illegal argument: %s\n", *argv);
450c675340aSGarance A Drosehn 		usage();
451c675340aSGarance A Drosehn 	}
452a4c8a745SGarance A Drosehn 	if (optfatal)
453352b6523SGarance A Drosehn 		exit(1);		/* Error messages already printed. */
454352b6523SGarance A Drosehn 	if (xkeep < 0)			/* Neither -X nor -x was specified. */
455a4c8a745SGarance A Drosehn 		xkeep = xkeep_implied;
456a4c8a745SGarance A Drosehn 
457831c910aSRuslan Ermilov 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
458ec23a763SMarcelo Araujo 	if (kd == NULL)
4598beb1a2fSMarcel Moolenaar 		xo_errx(1, "%s", errbuf);
4604b88c807SRodney W. Grimes 
461871e8d8cSMark Murray 	if (!_fmt)
462fde411d5SJuli Mallett 		parsefmt(dfmt, 0);
4634b88c807SRodney W. Grimes 
464a4c8a745SGarance A Drosehn 	if (nselectors == 0) {
465d822163fSGarance A Drosehn 		uidlist.l.ptr = malloc(sizeof(uid_t));
466d822163fSGarance A Drosehn 		if (uidlist.l.ptr == NULL)
4678beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
468a4c8a745SGarance A Drosehn 		nselectors = 1;
469a4c8a745SGarance A Drosehn 		uidlist.count = uidlist.maxcount = 1;
470d822163fSGarance A Drosehn 		*uidlist.l.uids = getuid();
471cf22dcfcSBrian Somers 	}
4724b88c807SRodney W. Grimes 
4734b88c807SRodney W. Grimes 	/*
4744b88c807SRodney W. Grimes 	 * scan requested variables, noting what structures are needed,
475bdfebd84SKris Kennaway 	 * and adjusting header widths as appropriate.
4764b88c807SRodney W. Grimes 	 */
4774b88c807SRodney W. Grimes 	scanvars();
478a4c8a745SGarance A Drosehn 
4794b88c807SRodney W. Grimes 	/*
480a4c8a745SGarance A Drosehn 	 * Get process list.  If the user requested just one selector-
481a4c8a745SGarance A Drosehn 	 * option, then kvm_getprocs can be asked to return just those
482a4c8a745SGarance A Drosehn 	 * processes.  Otherwise, have it return all processes, and
483a4c8a745SGarance A Drosehn 	 * then this routine will search that full list and select the
484a4c8a745SGarance A Drosehn 	 * processes which match any of the user's selector-options.
4854b88c807SRodney W. Grimes 	 */
486d75c1d83SDaniel Eischen 	what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC;
48748b8c0deSScott Long 	flag = 0;
488a4c8a745SGarance A Drosehn 	if (nselectors == 1) {
4897bd42165SGarance A Drosehn 		if (gidlist.count == 1) {
4907bd42165SGarance A Drosehn 			what = KERN_PROC_RGID | showthreads;
4917bd42165SGarance A Drosehn 			flag = *gidlist.l.gids;
4927bd42165SGarance A Drosehn 			nselectors = 0;
4937bd42165SGarance A Drosehn 		} else if (pgrplist.count == 1) {
494a4c8a745SGarance A Drosehn 			what = KERN_PROC_PGRP | showthreads;
495d822163fSGarance A Drosehn 			flag = *pgrplist.l.pids;
496a4c8a745SGarance A Drosehn 			nselectors = 0;
497a4c8a745SGarance A Drosehn 		} else if (pidlist.count == 1) {
498a4c8a745SGarance A Drosehn 			what = KERN_PROC_PID | showthreads;
499d822163fSGarance A Drosehn 			flag = *pidlist.l.pids;
500a4c8a745SGarance A Drosehn 			nselectors = 0;
501a4c8a745SGarance A Drosehn 		} else if (ruidlist.count == 1) {
502a4c8a745SGarance A Drosehn 			what = KERN_PROC_RUID | showthreads;
503d822163fSGarance A Drosehn 			flag = *ruidlist.l.uids;
504a4c8a745SGarance A Drosehn 			nselectors = 0;
505a4c8a745SGarance A Drosehn 		} else if (sesslist.count == 1) {
506a4c8a745SGarance A Drosehn 			what = KERN_PROC_SESSION | showthreads;
507d822163fSGarance A Drosehn 			flag = *sesslist.l.pids;
508a4c8a745SGarance A Drosehn 			nselectors = 0;
509a4c8a745SGarance A Drosehn 		} else if (ttylist.count == 1) {
510a4c8a745SGarance A Drosehn 			what = KERN_PROC_TTY | showthreads;
511d822163fSGarance A Drosehn 			flag = *ttylist.l.ttys;
512a4c8a745SGarance A Drosehn 			nselectors = 0;
513a4c8a745SGarance A Drosehn 		} else if (uidlist.count == 1) {
514a4c8a745SGarance A Drosehn 			what = KERN_PROC_UID | showthreads;
515d822163fSGarance A Drosehn 			flag = *uidlist.l.uids;
516a4c8a745SGarance A Drosehn 			nselectors = 0;
517a4c8a745SGarance A Drosehn 		} else if (all) {
518a4c8a745SGarance A Drosehn 			/* No need for this routine to select processes. */
519a4c8a745SGarance A Drosehn 			nselectors = 0;
520a4c8a745SGarance A Drosehn 		}
5214b88c807SRodney W. Grimes 	}
522d75c1d83SDaniel Eischen 
5234b88c807SRodney W. Grimes 	/*
5244b88c807SRodney W. Grimes 	 * select procs
5254b88c807SRodney W. Grimes 	 */
526a4c8a745SGarance A Drosehn 	nentries = -1;
5274e8b6a6fSGarance A Drosehn 	kp = kvm_getprocs(kd, what, flag, &nentries);
528d7026b96SEdward Tomasz Napierala 	/*
529d7026b96SEdward Tomasz Napierala 	 * Ignore ESRCH to preserve behaviour of "ps -p nonexistent-pid"
530d7026b96SEdward Tomasz Napierala 	 * not reporting an error.
531d7026b96SEdward Tomasz Napierala 	 */
532d7026b96SEdward Tomasz Napierala 	if ((kp == NULL && errno != ESRCH) || (kp != NULL && nentries < 0))
5338beb1a2fSMarcel Moolenaar 		xo_errx(1, "%s", kvm_geterr(kd));
534a4c8a745SGarance A Drosehn 	nkept = 0;
5354e8b6a6fSGarance A Drosehn 	if (nentries > 0) {
5364b88c807SRodney W. Grimes 		if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL)
5378beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
5384b88c807SRodney W. Grimes 		for (i = nentries; --i >= 0; ++kp) {
539a4c8a745SGarance A Drosehn 			/*
540a4c8a745SGarance A Drosehn 			 * If the user specified multiple selection-criteria,
541a4c8a745SGarance A Drosehn 			 * then keep any process matched by the inclusive OR
542a4c8a745SGarance A Drosehn 			 * of all the selection-criteria given.
543a4c8a745SGarance A Drosehn 			 */
544a4c8a745SGarance A Drosehn 			if (pidlist.count > 0) {
545a4c8a745SGarance A Drosehn 				for (elem = 0; elem < pidlist.count; elem++)
546d822163fSGarance A Drosehn 					if (kp->ki_pid == pidlist.l.pids[elem])
547a4c8a745SGarance A Drosehn 						goto keepit;
548a4c8a745SGarance A Drosehn 			}
549a4c8a745SGarance A Drosehn 			/*
550a4c8a745SGarance A Drosehn 			 * Note that we had to process pidlist before
551a4c8a745SGarance A Drosehn 			 * filtering out processes which do not have
552a4c8a745SGarance A Drosehn 			 * a controlling terminal.
553a4c8a745SGarance A Drosehn 			 */
554a4c8a745SGarance A Drosehn 			if (xkeep == 0) {
555a4c8a745SGarance A Drosehn 				if ((kp->ki_tdev == NODEV ||
556a4c8a745SGarance A Drosehn 				    (kp->ki_flag & P_CONTROLT) == 0))
557a4c8a745SGarance A Drosehn 					continue;
558a4c8a745SGarance A Drosehn 			}
559a4c8a745SGarance A Drosehn 			if (nselectors == 0)
560a4c8a745SGarance A Drosehn 				goto keepit;
561a4c8a745SGarance A Drosehn 			if (gidlist.count > 0) {
562a4c8a745SGarance A Drosehn 				for (elem = 0; elem < gidlist.count; elem++)
563d822163fSGarance A Drosehn 					if (kp->ki_rgid == gidlist.l.gids[elem])
564a4c8a745SGarance A Drosehn 						goto keepit;
565a4c8a745SGarance A Drosehn 			}
56613767130SBryan Drewery 			if (jidlist.count > 0) {
56713767130SBryan Drewery 				for (elem = 0; elem < jidlist.count; elem++)
56813767130SBryan Drewery 					if (kp->ki_jid == jidlist.l.jids[elem])
56913767130SBryan Drewery 						goto keepit;
57013767130SBryan Drewery 			}
571a4c8a745SGarance A Drosehn 			if (pgrplist.count > 0) {
572a4c8a745SGarance A Drosehn 				for (elem = 0; elem < pgrplist.count; elem++)
573d822163fSGarance A Drosehn 					if (kp->ki_pgid ==
574d822163fSGarance A Drosehn 					    pgrplist.l.pids[elem])
575a4c8a745SGarance A Drosehn 						goto keepit;
576a4c8a745SGarance A Drosehn 			}
577a4c8a745SGarance A Drosehn 			if (ruidlist.count > 0) {
578a4c8a745SGarance A Drosehn 				for (elem = 0; elem < ruidlist.count; elem++)
579d822163fSGarance A Drosehn 					if (kp->ki_ruid ==
580d822163fSGarance A Drosehn 					    ruidlist.l.uids[elem])
581a4c8a745SGarance A Drosehn 						goto keepit;
582a4c8a745SGarance A Drosehn 			}
583a4c8a745SGarance A Drosehn 			if (sesslist.count > 0) {
584a4c8a745SGarance A Drosehn 				for (elem = 0; elem < sesslist.count; elem++)
585d822163fSGarance A Drosehn 					if (kp->ki_sid == sesslist.l.pids[elem])
586a4c8a745SGarance A Drosehn 						goto keepit;
587a4c8a745SGarance A Drosehn 			}
588a4c8a745SGarance A Drosehn 			if (ttylist.count > 0) {
589a4c8a745SGarance A Drosehn 				for (elem = 0; elem < ttylist.count; elem++)
590d822163fSGarance A Drosehn 					if (kp->ki_tdev == ttylist.l.ttys[elem])
591a4c8a745SGarance A Drosehn 						goto keepit;
592a4c8a745SGarance A Drosehn 			}
593a4c8a745SGarance A Drosehn 			if (uidlist.count > 0) {
594a4c8a745SGarance A Drosehn 				for (elem = 0; elem < uidlist.count; elem++)
595d822163fSGarance A Drosehn 					if (kp->ki_uid == uidlist.l.uids[elem])
596a4c8a745SGarance A Drosehn 						goto keepit;
597a4c8a745SGarance A Drosehn 			}
598a4c8a745SGarance A Drosehn 			/*
599a4c8a745SGarance A Drosehn 			 * This process did not match any of the user's
600a4c8a745SGarance A Drosehn 			 * selector-options, so skip the process.
601a4c8a745SGarance A Drosehn 			 */
602a4c8a745SGarance A Drosehn 			continue;
603a4c8a745SGarance A Drosehn 
604a4c8a745SGarance A Drosehn 		keepit:
6054bac4483SGarance A Drosehn 			next_KINFO = &kinfo[nkept];
6064bac4483SGarance A Drosehn 			next_KINFO->ki_p = kp;
607044fce53SBrian Somers 			next_KINFO->ki_d.level = 0;
608044fce53SBrian Somers 			next_KINFO->ki_d.prefix = NULL;
6094bac4483SGarance A Drosehn 			next_KINFO->ki_pcpu = getpcpu(next_KINFO);
6104bac4483SGarance A Drosehn 			if (sortby == SORTMEM)
6114bac4483SGarance A Drosehn 				next_KINFO->ki_memsize = kp->ki_tsize +
6124bac4483SGarance A Drosehn 				    kp->ki_dsize + kp->ki_ssize;
6134b88c807SRodney W. Grimes 			if (needuser)
6144bac4483SGarance A Drosehn 				saveuser(next_KINFO);
615a4c8a745SGarance A Drosehn 			nkept++;
6164b88c807SRodney W. Grimes 		}
6174e8b6a6fSGarance A Drosehn 	}
6186a2d726bSJordan K. Hubbard 
6196a2d726bSJordan K. Hubbard 	sizevars();
6206a2d726bSJordan K. Hubbard 
6211d1143ecSEdward Tomasz Napierala 	if (nkept == 0) {
6224b88c807SRodney W. Grimes 		printheader();
62381fc45fcSKonstantin Belousov 		xo_finish();
624f8c9c11cSWill Andrews 		exit(1);
6251d1143ecSEdward Tomasz Napierala 	}
626a4c8a745SGarance A Drosehn 
6274b88c807SRodney W. Grimes 	/*
6284b88c807SRodney W. Grimes 	 * sort proc list
6294b88c807SRodney W. Grimes 	 */
630a4c8a745SGarance A Drosehn 	qsort(kinfo, nkept, sizeof(KINFO), pscomp);
631044fce53SBrian Somers 
632044fce53SBrian Somers 	/*
633044fce53SBrian Somers 	 * We want things in descendant order
634044fce53SBrian Somers 	 */
635044fce53SBrian Somers 	if (descendancy)
636044fce53SBrian Somers 		descendant_sort(kinfo, nkept);
637044fce53SBrian Somers 
6381d1143ecSEdward Tomasz Napierala 
6394b88c807SRodney W. Grimes 	/*
6401d1143ecSEdward Tomasz Napierala 	 * Prepare formatted output.
6411d1143ecSEdward Tomasz Napierala 	 */
6421d1143ecSEdward Tomasz Napierala 	for (i = 0; i < nkept; i++)
6431d1143ecSEdward Tomasz Napierala 		format_output(&kinfo[i]);
6441d1143ecSEdward Tomasz Napierala 
6451d1143ecSEdward Tomasz Napierala 	/*
6461d1143ecSEdward Tomasz Napierala 	 * Print header.
6471d1143ecSEdward Tomasz Napierala 	 */
6488beb1a2fSMarcel Moolenaar 	xo_open_container("process-information");
6491d1143ecSEdward Tomasz Napierala 	printheader();
6508beb1a2fSMarcel Moolenaar 	if (xo_get_style(NULL) != XO_STYLE_TEXT)
6518beb1a2fSMarcel Moolenaar 		termwidth = UNLIMITED;
6521d1143ecSEdward Tomasz Napierala 
6531d1143ecSEdward Tomasz Napierala 	/*
6541d1143ecSEdward Tomasz Napierala 	 * Output formatted lines.
6554b88c807SRodney W. Grimes 	 */
6568beb1a2fSMarcel Moolenaar 	xo_open_list("process");
657a4c8a745SGarance A Drosehn 	for (i = lineno = 0; i < nkept; i++) {
6581d1143ecSEdward Tomasz Napierala 		linelen = 0;
6598beb1a2fSMarcel Moolenaar 		xo_open_instance("process");
660bdf8ab46SGarance A Drosehn 		STAILQ_FOREACH(vent, &varlist, next_ve) {
6611d1143ecSEdward Tomasz Napierala 			ks = STAILQ_FIRST(&kinfo[i].ki_ks);
6621d1143ecSEdward Tomasz Napierala 			STAILQ_REMOVE_HEAD(&kinfo[i].ki_ks, ks_next);
66338494effSUlrich Spörlein 			/* Truncate rightmost column if necessary.  */
6648beb1a2fSMarcel Moolenaar 			fwidthmax = _POSIX2_LINE_MAX;
6651d1143ecSEdward Tomasz Napierala 			if (STAILQ_NEXT(vent, next_ve) == NULL &&
6661d1143ecSEdward Tomasz Napierala 			   termwidth != UNLIMITED && ks->ks_str != NULL) {
6671d1143ecSEdward Tomasz Napierala 				left = termwidth - linelen;
6681d1143ecSEdward Tomasz Napierala 				if (left > 0 && left < (int)strlen(ks->ks_str))
6698beb1a2fSMarcel Moolenaar 					fwidthmax = left;
6701d1143ecSEdward Tomasz Napierala 			}
6718beb1a2fSMarcel Moolenaar 
6721d1143ecSEdward Tomasz Napierala 			str = ks->ks_str;
6731d1143ecSEdward Tomasz Napierala 			if (str == NULL)
6741d1143ecSEdward Tomasz Napierala 				str = "-";
6751d1143ecSEdward Tomasz Napierala 			/* No padding for the last column, if it's LJUST. */
6768beb1a2fSMarcel Moolenaar 			fwidthmin = (xo_get_style(NULL) != XO_STYLE_TEXT ||
6778beb1a2fSMarcel Moolenaar 			    (STAILQ_NEXT(vent, next_ve) == NULL &&
6788beb1a2fSMarcel Moolenaar 			    (vent->var->flag & LJUST))) ? 0 : vent->var->width;
6798beb1a2fSMarcel Moolenaar 			snprintf(fmtbuf, sizeof(fmtbuf), "{:%s/%%%s%d..%ds}",
6808beb1a2fSMarcel Moolenaar 			    vent->var->field ?: vent->var->name,
6818beb1a2fSMarcel Moolenaar 			    (vent->var->flag & LJUST) ? "-" : "",
6828beb1a2fSMarcel Moolenaar 			    fwidthmin, fwidthmax);
6838beb1a2fSMarcel Moolenaar 			xo_emit(fmtbuf, str);
6848beb1a2fSMarcel Moolenaar 			linelen += fwidthmin;
6851d1143ecSEdward Tomasz Napierala 
6861d1143ecSEdward Tomasz Napierala 			if (ks->ks_str != NULL) {
6871d1143ecSEdward Tomasz Napierala 				free(ks->ks_str);
6881d1143ecSEdward Tomasz Napierala 				ks->ks_str = NULL;
6891d1143ecSEdward Tomasz Napierala 			}
6901d1143ecSEdward Tomasz Napierala 			free(ks);
6911d1143ecSEdward Tomasz Napierala 			ks = NULL;
6921d1143ecSEdward Tomasz Napierala 
6931d1143ecSEdward Tomasz Napierala 			if (STAILQ_NEXT(vent, next_ve) != NULL) {
6948beb1a2fSMarcel Moolenaar 				xo_emit("{P: }");
6951d1143ecSEdward Tomasz Napierala 				linelen++;
6961d1143ecSEdward Tomasz Napierala 			}
6974b88c807SRodney W. Grimes 		}
6988beb1a2fSMarcel Moolenaar 	        xo_emit("\n");
6998beb1a2fSMarcel Moolenaar 		xo_close_instance("process");
7004b88c807SRodney W. Grimes 		if (prtheader && lineno++ == prtheader - 4) {
7018beb1a2fSMarcel Moolenaar 			xo_emit("\n");
7024b88c807SRodney W. Grimes 			printheader();
7034b88c807SRodney W. Grimes 			lineno = 0;
7044b88c807SRodney W. Grimes 		}
7054b88c807SRodney W. Grimes 	}
7068beb1a2fSMarcel Moolenaar 	xo_close_list("process");
7078beb1a2fSMarcel Moolenaar 	xo_close_container("process-information");
7088beb1a2fSMarcel Moolenaar 	xo_finish();
7098beb1a2fSMarcel Moolenaar 
710a4c8a745SGarance A Drosehn 	free_list(&gidlist);
71113767130SBryan Drewery 	free_list(&jidlist);
712a4c8a745SGarance A Drosehn 	free_list(&pidlist);
713a4c8a745SGarance A Drosehn 	free_list(&pgrplist);
714a4c8a745SGarance A Drosehn 	free_list(&ruidlist);
715a4c8a745SGarance A Drosehn 	free_list(&sesslist);
716a4c8a745SGarance A Drosehn 	free_list(&ttylist);
717a4c8a745SGarance A Drosehn 	free_list(&uidlist);
718044fce53SBrian Somers 	for (i = 0; i < nkept; i++)
719044fce53SBrian Somers 		free(kinfo[i].ki_d.prefix);
720044fce53SBrian Somers 	free(kinfo);
7212e6c6ac4SGarance A Drosehn 
7224b88c807SRodney W. Grimes 	exit(eval);
7234b88c807SRodney W. Grimes }
7244b88c807SRodney W. Grimes 
725a4c8a745SGarance A Drosehn static int
726a4c8a745SGarance A Drosehn addelem_gid(struct listinfo *inf, const char *elem)
7274e8b6a6fSGarance A Drosehn {
728a4c8a745SGarance A Drosehn 	struct group *grp;
729a4c8a745SGarance A Drosehn 	const char *nameorID;
730a4c8a745SGarance A Drosehn 	char *endp;
7310b42be7cSGarance A Drosehn 	u_long bigtemp;
7324e8b6a6fSGarance A Drosehn 
733a4c8a745SGarance A Drosehn 	if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
734a4c8a745SGarance A Drosehn 		if (*elem == '\0')
7358beb1a2fSMarcel Moolenaar 			xo_warnx("Invalid (zero-length) %s name", inf->lname);
736a4c8a745SGarance A Drosehn 		else
7378beb1a2fSMarcel Moolenaar 			xo_warnx("%s name too long: %s", inf->lname, elem);
738a4c8a745SGarance A Drosehn 		optfatal = 1;
739352b6523SGarance A Drosehn 		return (0);		/* Do not add this value. */
7404e8b6a6fSGarance A Drosehn 	}
741a4c8a745SGarance A Drosehn 
7424e8b6a6fSGarance A Drosehn 	/*
743a4c8a745SGarance A Drosehn 	 * SUSv3 states that `ps -G grouplist' should match "real-group
744a4c8a745SGarance A Drosehn 	 * ID numbers", and does not mention group-names.  I do want to
745a4c8a745SGarance A Drosehn 	 * also support group-names, so this tries for a group-id first,
746a4c8a745SGarance A Drosehn 	 * and only tries for a name if that doesn't work.  This is the
747a4c8a745SGarance A Drosehn 	 * opposite order of what is done in addelem_uid(), but in
748a4c8a745SGarance A Drosehn 	 * practice the order would only matter for group-names which
749a4c8a745SGarance A Drosehn 	 * are all-numeric.
7504e8b6a6fSGarance A Drosehn 	 */
751a4c8a745SGarance A Drosehn 	grp = NULL;
752a4c8a745SGarance A Drosehn 	nameorID = "named";
753a4c8a745SGarance A Drosehn 	errno = 0;
7540b42be7cSGarance A Drosehn 	bigtemp = strtoul(elem, &endp, 10);
7550b42be7cSGarance A Drosehn 	if (errno == 0 && *endp == '\0' && bigtemp <= GID_MAX) {
756a4c8a745SGarance A Drosehn 		nameorID = "name or ID matches";
7570b42be7cSGarance A Drosehn 		grp = getgrgid((gid_t)bigtemp);
758a4c8a745SGarance A Drosehn 	}
759a4c8a745SGarance A Drosehn 	if (grp == NULL)
760a4c8a745SGarance A Drosehn 		grp = getgrnam(elem);
761a4c8a745SGarance A Drosehn 	if (grp == NULL) {
7628beb1a2fSMarcel Moolenaar 		xo_warnx("No %s %s '%s'", inf->lname, nameorID, elem);
763a4c8a745SGarance A Drosehn 		optfatal = 1;
764c23b00b7SGarance A Drosehn 		return (0);
765a4c8a745SGarance A Drosehn 	}
766a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
767a4c8a745SGarance A Drosehn 		expand_list(inf);
768d822163fSGarance A Drosehn 	inf->l.gids[(inf->count)++] = grp->gr_gid;
769a4c8a745SGarance A Drosehn 	return (1);
770a4c8a745SGarance A Drosehn }
771a4c8a745SGarance A Drosehn 
772a4c8a745SGarance A Drosehn static int
77313767130SBryan Drewery addelem_jid(struct listinfo *inf, const char *elem)
77413767130SBryan Drewery {
77513767130SBryan Drewery 	int tempid;
77613767130SBryan Drewery 
77713767130SBryan Drewery 	if (*elem == '\0') {
77813767130SBryan Drewery 		warnx("Invalid (zero-length) jail id");
77913767130SBryan Drewery 		optfatal = 1;
78013767130SBryan Drewery 		return (0);		/* Do not add this value. */
78113767130SBryan Drewery 	}
78213767130SBryan Drewery 
78313767130SBryan Drewery 	tempid = jail_getid(elem);
78413767130SBryan Drewery 	if (tempid < 0) {
78513767130SBryan Drewery 		warnx("Invalid %s: %s", inf->lname, elem);
78613767130SBryan Drewery 		optfatal = 1;
78713767130SBryan Drewery 		return (0);
78813767130SBryan Drewery 	}
78913767130SBryan Drewery 
79013767130SBryan Drewery 	if (inf->count >= inf->maxcount)
79113767130SBryan Drewery 		expand_list(inf);
79213767130SBryan Drewery 	inf->l.jids[(inf->count)++] = tempid;
79313767130SBryan Drewery 	return (1);
79413767130SBryan Drewery }
79513767130SBryan Drewery 
79613767130SBryan Drewery static int
797a4c8a745SGarance A Drosehn addelem_pid(struct listinfo *inf, const char *elem)
798a4c8a745SGarance A Drosehn {
799a4c8a745SGarance A Drosehn 	char *endp;
800ca62e195SGarance A Drosehn 	long tempid;
801a4c8a745SGarance A Drosehn 
802de1702f4SGarance A Drosehn 	if (*elem == '\0') {
8038beb1a2fSMarcel Moolenaar 		xo_warnx("Invalid (zero-length) process id");
804de1702f4SGarance A Drosehn 		optfatal = 1;
805de1702f4SGarance A Drosehn 		return (0);		/* Do not add this value. */
80625113083SGarance A Drosehn 	}
80725113083SGarance A Drosehn 
808a4c8a745SGarance A Drosehn 	errno = 0;
809a4c8a745SGarance A Drosehn 	tempid = strtol(elem, &endp, 10);
810a4c8a745SGarance A Drosehn 	if (*endp != '\0' || tempid < 0 || elem == endp) {
8118beb1a2fSMarcel Moolenaar 		xo_warnx("Invalid %s: %s", inf->lname, elem);
8124e8b6a6fSGarance A Drosehn 		errno = ERANGE;
81364b0bf0bSPawel Jakub Dawidek 	} else if (errno != 0 || tempid > pid_max) {
8148beb1a2fSMarcel Moolenaar 		xo_warnx("%s too large: %s", inf->lname, elem);
8154e8b6a6fSGarance A Drosehn 		errno = ERANGE;
8164e8b6a6fSGarance A Drosehn 	}
8174e8b6a6fSGarance A Drosehn 	if (errno == ERANGE) {
818a4c8a745SGarance A Drosehn 		optfatal = 1;
819c23b00b7SGarance A Drosehn 		return (0);
8204e8b6a6fSGarance A Drosehn 	}
821a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
822a4c8a745SGarance A Drosehn 		expand_list(inf);
823d822163fSGarance A Drosehn 	inf->l.pids[(inf->count)++] = tempid;
824a4c8a745SGarance A Drosehn 	return (1);
8254e8b6a6fSGarance A Drosehn }
8264e8b6a6fSGarance A Drosehn 
82723b8efadSGarance A Drosehn /*-
82823b8efadSGarance A Drosehn  * The user can specify a device via one of three formats:
8298a529f59SJohn Baldwin  *     1) fully qualified, e.g.:     /dev/ttyp0 /dev/console	/dev/pts/0
8308a529f59SJohn Baldwin  *     2) missing "/dev", e.g.:      ttyp0      console		pts/0
8318a529f59SJohn Baldwin  *     3) two-letters, e.g.:         p0         co		0
83223b8efadSGarance A Drosehn  *        (matching letters that would be seen in the "TT" column)
83323b8efadSGarance A Drosehn  */
834a4c8a745SGarance A Drosehn static int
835a4c8a745SGarance A Drosehn addelem_tty(struct listinfo *inf, const char *elem)
836cf22dcfcSBrian Somers {
837a4c8a745SGarance A Drosehn 	const char *ttypath;
838ca62e195SGarance A Drosehn 	struct stat sb;
8398a529f59SJohn Baldwin 	char pathbuf[PATH_MAX], pathbuf2[PATH_MAX], pathbuf3[PATH_MAX];
840a4c8a745SGarance A Drosehn 
84123b8efadSGarance A Drosehn 	ttypath = NULL;
842f2fbfae6SGarance A Drosehn 	pathbuf2[0] = '\0';
8438a529f59SJohn Baldwin 	pathbuf3[0] = '\0';
84423b8efadSGarance A Drosehn 	switch (*elem) {
84523b8efadSGarance A Drosehn 	case '/':
846a4c8a745SGarance A Drosehn 		ttypath = elem;
84723b8efadSGarance A Drosehn 		break;
84823b8efadSGarance A Drosehn 	case 'c':
84923b8efadSGarance A Drosehn 		if (strcmp(elem, "co") == 0) {
85023b8efadSGarance A Drosehn 			ttypath = _PATH_CONSOLE;
85123b8efadSGarance A Drosehn 			break;
85223b8efadSGarance A Drosehn 		}
85323b8efadSGarance A Drosehn 		/* FALLTHROUGH */
85423b8efadSGarance A Drosehn 	default:
85523b8efadSGarance A Drosehn 		strlcpy(pathbuf, _PATH_DEV, sizeof(pathbuf));
856a4c8a745SGarance A Drosehn 		strlcat(pathbuf, elem, sizeof(pathbuf));
857a4c8a745SGarance A Drosehn 		ttypath = pathbuf;
858f2fbfae6SGarance A Drosehn 		if (strncmp(pathbuf, _PATH_TTY, strlen(_PATH_TTY)) == 0)
85923b8efadSGarance A Drosehn 			break;
8608a529f59SJohn Baldwin 		if (strncmp(pathbuf, _PATH_PTS, strlen(_PATH_PTS)) == 0)
8618a529f59SJohn Baldwin 			break;
86223b8efadSGarance A Drosehn 		if (strcmp(pathbuf, _PATH_CONSOLE) == 0)
86323b8efadSGarance A Drosehn 			break;
864f2fbfae6SGarance A Drosehn 		/* Check to see if /dev/tty${elem} exists */
865f2fbfae6SGarance A Drosehn 		strlcpy(pathbuf2, _PATH_TTY, sizeof(pathbuf2));
866f2fbfae6SGarance A Drosehn 		strlcat(pathbuf2, elem, sizeof(pathbuf2));
867f2fbfae6SGarance A Drosehn 		if (stat(pathbuf2, &sb) == 0 && S_ISCHR(sb.st_mode)) {
86823b8efadSGarance A Drosehn 			/* No need to repeat stat() && S_ISCHR() checks */
86923b8efadSGarance A Drosehn 			ttypath = NULL;
87023b8efadSGarance A Drosehn 			break;
871a4c8a745SGarance A Drosehn 		}
8728a529f59SJohn Baldwin 		/* Check to see if /dev/pts/${elem} exists */
8738a529f59SJohn Baldwin 		strlcpy(pathbuf3, _PATH_PTS, sizeof(pathbuf3));
8748a529f59SJohn Baldwin 		strlcat(pathbuf3, elem, sizeof(pathbuf3));
8758a529f59SJohn Baldwin 		if (stat(pathbuf3, &sb) == 0 && S_ISCHR(sb.st_mode)) {
8768a529f59SJohn Baldwin 			/* No need to repeat stat() && S_ISCHR() checks */
8778a529f59SJohn Baldwin 			ttypath = NULL;
8788a529f59SJohn Baldwin 			break;
8798a529f59SJohn Baldwin 		}
88023b8efadSGarance A Drosehn 		break;
88123b8efadSGarance A Drosehn 	}
88223b8efadSGarance A Drosehn 	if (ttypath) {
883a4c8a745SGarance A Drosehn 		if (stat(ttypath, &sb) == -1) {
8848a529f59SJohn Baldwin 			if (pathbuf3[0] != '\0')
8858beb1a2fSMarcel Moolenaar 				xo_warn("%s, %s, and %s", pathbuf3, pathbuf2,
8868a529f59SJohn Baldwin 				    ttypath);
887f2fbfae6SGarance A Drosehn 			else
8888beb1a2fSMarcel Moolenaar 				xo_warn("%s", ttypath);
889a4c8a745SGarance A Drosehn 			optfatal = 1;
890c23b00b7SGarance A Drosehn 			return (0);
891a4c8a745SGarance A Drosehn 		}
892a4c8a745SGarance A Drosehn 		if (!S_ISCHR(sb.st_mode)) {
8938a529f59SJohn Baldwin 			if (pathbuf3[0] != '\0')
8948beb1a2fSMarcel Moolenaar 				xo_warnx("%s, %s, and %s: Not a terminal",
8958a529f59SJohn Baldwin 				    pathbuf3, pathbuf2, ttypath);
896f2fbfae6SGarance A Drosehn 			else
8978beb1a2fSMarcel Moolenaar 				xo_warnx("%s: Not a terminal", ttypath);
898a4c8a745SGarance A Drosehn 			optfatal = 1;
899c23b00b7SGarance A Drosehn 			return (0);
900a4c8a745SGarance A Drosehn 		}
90123b8efadSGarance A Drosehn 	}
902a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
903a4c8a745SGarance A Drosehn 		expand_list(inf);
904d822163fSGarance A Drosehn 	inf->l.ttys[(inf->count)++] = sb.st_rdev;
905a4c8a745SGarance A Drosehn 	return (1);
906a4c8a745SGarance A Drosehn }
907a4c8a745SGarance A Drosehn 
908a4c8a745SGarance A Drosehn static int
909a4c8a745SGarance A Drosehn addelem_uid(struct listinfo *inf, const char *elem)
910a4c8a745SGarance A Drosehn {
911cf22dcfcSBrian Somers 	struct passwd *pwd;
912a4c8a745SGarance A Drosehn 	char *endp;
9130b42be7cSGarance A Drosehn 	u_long bigtemp;
914cf22dcfcSBrian Somers 
915a4c8a745SGarance A Drosehn 	if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
916a4c8a745SGarance A Drosehn 		if (*elem == '\0')
9178beb1a2fSMarcel Moolenaar 			xo_warnx("Invalid (zero-length) %s name", inf->lname);
918a4c8a745SGarance A Drosehn 		else
9198beb1a2fSMarcel Moolenaar 			xo_warnx("%s name too long: %s", inf->lname, elem);
920a4c8a745SGarance A Drosehn 		optfatal = 1;
921352b6523SGarance A Drosehn 		return (0);		/* Do not add this value. */
922a4c8a745SGarance A Drosehn 	}
923cf22dcfcSBrian Somers 
924a4c8a745SGarance A Drosehn 	pwd = getpwnam(elem);
925a4c8a745SGarance A Drosehn 	if (pwd == NULL) {
926a4c8a745SGarance A Drosehn 		errno = 0;
9270b42be7cSGarance A Drosehn 		bigtemp = strtoul(elem, &endp, 10);
9280b42be7cSGarance A Drosehn 		if (errno != 0 || *endp != '\0' || bigtemp > UID_MAX)
9298beb1a2fSMarcel Moolenaar 			xo_warnx("No %s named '%s'", inf->lname, elem);
930a4c8a745SGarance A Drosehn 		else {
931a4c8a745SGarance A Drosehn 			/* The string is all digits, so it might be a userID. */
9320b42be7cSGarance A Drosehn 			pwd = getpwuid((uid_t)bigtemp);
933a4c8a745SGarance A Drosehn 			if (pwd == NULL)
9348beb1a2fSMarcel Moolenaar 				xo_warnx("No %s name or ID matches '%s'",
935a4c8a745SGarance A Drosehn 				    inf->lname, elem);
936cf22dcfcSBrian Somers 		}
937cf22dcfcSBrian Somers 	}
938a4c8a745SGarance A Drosehn 	if (pwd == NULL) {
939e3c4e1ddSGarance A Drosehn 		/*
940e3c4e1ddSGarance A Drosehn 		 * These used to be treated as minor warnings (and the
941e3c4e1ddSGarance A Drosehn 		 * option was simply ignored), but now they are fatal
942e3c4e1ddSGarance A Drosehn 		 * errors (and the command will be aborted).
943e3c4e1ddSGarance A Drosehn 		 */
944e3c4e1ddSGarance A Drosehn 		optfatal = 1;
945c23b00b7SGarance A Drosehn 		return (0);
946cf22dcfcSBrian Somers 	}
947a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
948a4c8a745SGarance A Drosehn 		expand_list(inf);
949d822163fSGarance A Drosehn 	inf->l.uids[(inf->count)++] = pwd->pw_uid;
950a4c8a745SGarance A Drosehn 	return (1);
951a4c8a745SGarance A Drosehn }
952cf22dcfcSBrian Somers 
953a4c8a745SGarance A Drosehn static void
954a4c8a745SGarance A Drosehn add_list(struct listinfo *inf, const char *argp)
955a4c8a745SGarance A Drosehn {
956a4c8a745SGarance A Drosehn 	const char *savep;
957a4c8a745SGarance A Drosehn 	char *cp, *endp;
958a4c8a745SGarance A Drosehn 	int toolong;
959ca62e195SGarance A Drosehn 	char elemcopy[PATH_MAX];
960a4c8a745SGarance A Drosehn 
96142856668SEd Maste 	if (*argp == '\0')
96242856668SEd Maste 		inf->addelem(inf, argp);
963a4c8a745SGarance A Drosehn 	while (*argp != '\0') {
964a4c8a745SGarance A Drosehn 		while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
965a4c8a745SGarance A Drosehn 			argp++;
966a4c8a745SGarance A Drosehn 		savep = argp;
967a4c8a745SGarance A Drosehn 		toolong = 0;
968a4c8a745SGarance A Drosehn 		cp = elemcopy;
969a4c8a745SGarance A Drosehn 		if (strchr(T_SEP, *argp) == NULL) {
970a4c8a745SGarance A Drosehn 			endp = elemcopy + sizeof(elemcopy) - 1;
971a4c8a745SGarance A Drosehn 			while (*argp != '\0' && cp <= endp &&
972a4c8a745SGarance A Drosehn 			    strchr(W_SEP T_SEP, *argp) == NULL)
973a4c8a745SGarance A Drosehn 				*cp++ = *argp++;
974a4c8a745SGarance A Drosehn 			if (cp > endp)
975a4c8a745SGarance A Drosehn 				toolong = 1;
976a4c8a745SGarance A Drosehn 		}
977a4c8a745SGarance A Drosehn 		if (!toolong) {
978a4c8a745SGarance A Drosehn 			*cp = '\0';
979352b6523SGarance A Drosehn 			/*
980a9626fb3SGarance A Drosehn 			 * Add this single element to the given list.
981352b6523SGarance A Drosehn 			 */
982a4c8a745SGarance A Drosehn 			inf->addelem(inf, elemcopy);
983a4c8a745SGarance A Drosehn 		} else {
984a4c8a745SGarance A Drosehn 			/*
985a4c8a745SGarance A Drosehn 			 * The string is too long to copy.  Find the end
986a4c8a745SGarance A Drosehn 			 * of the string to print out the warning message.
987a4c8a745SGarance A Drosehn 			 */
988a4c8a745SGarance A Drosehn 			while (*argp != '\0' && strchr(W_SEP T_SEP,
989a4c8a745SGarance A Drosehn 			    *argp) == NULL)
990a4c8a745SGarance A Drosehn 				argp++;
9918beb1a2fSMarcel Moolenaar 			xo_warnx("Value too long: %.*s", (int)(argp - savep),
992a4c8a745SGarance A Drosehn 			    savep);
993a4c8a745SGarance A Drosehn 			optfatal = 1;
994a4c8a745SGarance A Drosehn 		}
995a4c8a745SGarance A Drosehn 		/*
996a4c8a745SGarance A Drosehn 		 * Skip over any number of trailing whitespace characters,
997a4c8a745SGarance A Drosehn 		 * but only one (at most) trailing element-terminating
998a4c8a745SGarance A Drosehn 		 * character.
999a4c8a745SGarance A Drosehn 		 */
1000a4c8a745SGarance A Drosehn 		while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
1001a4c8a745SGarance A Drosehn 			argp++;
1002a4c8a745SGarance A Drosehn 		if (*argp != '\0' && strchr(T_SEP, *argp) != NULL) {
1003a4c8a745SGarance A Drosehn 			argp++;
1004a4c8a745SGarance A Drosehn 			/* Catch case where string ended with a comma. */
1005a4c8a745SGarance A Drosehn 			if (*argp == '\0')
1006a4c8a745SGarance A Drosehn 				inf->addelem(inf, argp);
1007a4c8a745SGarance A Drosehn 		}
1008a4c8a745SGarance A Drosehn 	}
1009a4c8a745SGarance A Drosehn }
1010a4c8a745SGarance A Drosehn 
1011044fce53SBrian Somers static void
1012044fce53SBrian Somers descendant_sort(KINFO *ki, int items)
1013044fce53SBrian Somers {
1014044fce53SBrian Somers 	int dst, lvl, maxlvl, n, ndst, nsrc, siblings, src;
1015044fce53SBrian Somers 	unsigned char *path;
1016044fce53SBrian Somers 	KINFO kn;
1017044fce53SBrian Somers 
1018044fce53SBrian Somers 	/*
1019044fce53SBrian Somers 	 * First, sort the entries by descendancy, tracking the descendancy
1020044fce53SBrian Somers 	 * depth in the ki_d.level field.
1021044fce53SBrian Somers 	 */
1022044fce53SBrian Somers 	src = 0;
1023044fce53SBrian Somers 	maxlvl = 0;
1024044fce53SBrian Somers 	while (src < items) {
1025044fce53SBrian Somers 		if (ki[src].ki_d.level) {
1026044fce53SBrian Somers 			src++;
1027044fce53SBrian Somers 			continue;
1028044fce53SBrian Somers 		}
1029044fce53SBrian Somers 		for (nsrc = 1; src + nsrc < items; nsrc++)
1030044fce53SBrian Somers 			if (!ki[src + nsrc].ki_d.level)
1031044fce53SBrian Somers 				break;
1032044fce53SBrian Somers 
1033044fce53SBrian Somers 		for (dst = 0; dst < items; dst++) {
1034044fce53SBrian Somers 			if (ki[dst].ki_p->ki_pid == ki[src].ki_p->ki_pid)
1035044fce53SBrian Somers 				continue;
1036044fce53SBrian Somers 			if (ki[dst].ki_p->ki_pid == ki[src].ki_p->ki_ppid)
1037044fce53SBrian Somers 				break;
1038044fce53SBrian Somers 		}
1039044fce53SBrian Somers 
1040044fce53SBrian Somers 		if (dst == items) {
1041044fce53SBrian Somers 			src += nsrc;
1042044fce53SBrian Somers 			continue;
1043044fce53SBrian Somers 		}
1044044fce53SBrian Somers 
1045044fce53SBrian Somers 		for (ndst = 1; dst + ndst < items; ndst++)
1046044fce53SBrian Somers 			if (ki[dst + ndst].ki_d.level <= ki[dst].ki_d.level)
1047044fce53SBrian Somers 				break;
1048044fce53SBrian Somers 
1049044fce53SBrian Somers 		for (n = src; n < src + nsrc; n++) {
1050044fce53SBrian Somers 			ki[n].ki_d.level += ki[dst].ki_d.level + 1;
1051044fce53SBrian Somers 			if (maxlvl < ki[n].ki_d.level)
1052044fce53SBrian Somers 				maxlvl = ki[n].ki_d.level;
1053044fce53SBrian Somers 		}
1054044fce53SBrian Somers 
1055044fce53SBrian Somers 		while (nsrc) {
1056044fce53SBrian Somers 			if (src < dst) {
1057044fce53SBrian Somers 				kn = ki[src];
1058044fce53SBrian Somers 				memmove(ki + src, ki + src + 1,
1059044fce53SBrian Somers 				    (dst - src + ndst - 1) * sizeof *ki);
1060044fce53SBrian Somers 				ki[dst + ndst - 1] = kn;
1061044fce53SBrian Somers 				nsrc--;
1062044fce53SBrian Somers 				dst--;
1063044fce53SBrian Somers 				ndst++;
1064044fce53SBrian Somers 			} else if (src != dst + ndst) {
1065044fce53SBrian Somers 				kn = ki[src];
1066044fce53SBrian Somers 				memmove(ki + dst + ndst + 1, ki + dst + ndst,
1067044fce53SBrian Somers 				    (src - dst - ndst) * sizeof *ki);
1068044fce53SBrian Somers 				ki[dst + ndst] = kn;
1069044fce53SBrian Somers 				ndst++;
1070044fce53SBrian Somers 				nsrc--;
1071044fce53SBrian Somers 				src++;
1072044fce53SBrian Somers 			} else {
1073044fce53SBrian Somers 				ndst += nsrc;
1074044fce53SBrian Somers 				src += nsrc;
1075044fce53SBrian Somers 				nsrc = 0;
1076044fce53SBrian Somers 			}
1077044fce53SBrian Somers 		}
1078044fce53SBrian Somers 	}
1079044fce53SBrian Somers 
1080044fce53SBrian Somers 	/*
1081044fce53SBrian Somers 	 * Now populate ki_d.prefix (instead of ki_d.level) with the command
1082044fce53SBrian Somers 	 * prefix used to show descendancies.
1083044fce53SBrian Somers 	 */
1084044fce53SBrian Somers 	path = malloc((maxlvl + 7) / 8);
1085044fce53SBrian Somers 	memset(path, '\0', (maxlvl + 7) / 8);
1086044fce53SBrian Somers 	for (src = 0; src < items; src++) {
1087044fce53SBrian Somers 		if ((lvl = ki[src].ki_d.level) == 0) {
1088044fce53SBrian Somers 			ki[src].ki_d.prefix = NULL;
1089044fce53SBrian Somers 			continue;
1090044fce53SBrian Somers 		}
1091044fce53SBrian Somers 		if ((ki[src].ki_d.prefix = malloc(lvl * 2 + 1)) == NULL)
10928beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
1093044fce53SBrian Somers 		for (n = 0; n < lvl - 2; n++) {
1094044fce53SBrian Somers 			ki[src].ki_d.prefix[n * 2] =
1095044fce53SBrian Somers 			    path[n / 8] & 1 << (n % 8) ? '|' : ' ';
1096044fce53SBrian Somers 			ki[src].ki_d.prefix[n * 2 + 1] = ' ';
1097044fce53SBrian Somers 		}
1098044fce53SBrian Somers 		if (n == lvl - 2) {
1099044fce53SBrian Somers 			/* Have I any more siblings? */
1100044fce53SBrian Somers 			for (siblings = 0, dst = src + 1; dst < items; dst++) {
1101044fce53SBrian Somers 				if (ki[dst].ki_d.level > lvl)
1102044fce53SBrian Somers 					continue;
1103044fce53SBrian Somers 				if (ki[dst].ki_d.level == lvl)
1104044fce53SBrian Somers 					siblings = 1;
1105044fce53SBrian Somers 				break;
1106044fce53SBrian Somers 			}
1107044fce53SBrian Somers 			if (siblings)
1108044fce53SBrian Somers 				path[n / 8] |= 1 << (n % 8);
1109044fce53SBrian Somers 			else
1110044fce53SBrian Somers 				path[n / 8] &= ~(1 << (n % 8));
1111044fce53SBrian Somers 			ki[src].ki_d.prefix[n * 2] = siblings ? '|' : '`';
1112044fce53SBrian Somers 			ki[src].ki_d.prefix[n * 2 + 1] = '-';
1113044fce53SBrian Somers 			n++;
1114044fce53SBrian Somers 		}
1115044fce53SBrian Somers 		strcpy(ki[src].ki_d.prefix + n * 2, "- ");
1116044fce53SBrian Somers 	}
1117044fce53SBrian Somers 	free(path);
1118044fce53SBrian Somers }
1119044fce53SBrian Somers 
1120a4c8a745SGarance A Drosehn static void *
1121a4c8a745SGarance A Drosehn expand_list(struct listinfo *inf)
1122a4c8a745SGarance A Drosehn {
1123a4c8a745SGarance A Drosehn 	void *newlist;
1124ca62e195SGarance A Drosehn 	int newmax;
1125a4c8a745SGarance A Drosehn 
1126a4c8a745SGarance A Drosehn 	newmax = (inf->maxcount + 1) << 1;
1127d822163fSGarance A Drosehn 	newlist = realloc(inf->l.ptr, newmax * inf->elemsize);
1128a4c8a745SGarance A Drosehn 	if (newlist == NULL) {
1129d822163fSGarance A Drosehn 		free(inf->l.ptr);
11308beb1a2fSMarcel Moolenaar 		xo_errx(1, "realloc to %d %ss failed", newmax, inf->lname);
1131a4c8a745SGarance A Drosehn 	}
1132a4c8a745SGarance A Drosehn 	inf->maxcount = newmax;
1133d822163fSGarance A Drosehn 	inf->l.ptr = newlist;
1134a4c8a745SGarance A Drosehn 
1135a4c8a745SGarance A Drosehn 	return (newlist);
1136a4c8a745SGarance A Drosehn }
1137a4c8a745SGarance A Drosehn 
1138a4c8a745SGarance A Drosehn static void
1139a4c8a745SGarance A Drosehn free_list(struct listinfo *inf)
1140a4c8a745SGarance A Drosehn {
1141a4c8a745SGarance A Drosehn 
1142a4c8a745SGarance A Drosehn 	inf->count = inf->elemsize = inf->maxcount = 0;
1143d822163fSGarance A Drosehn 	if (inf->l.ptr != NULL)
1144d822163fSGarance A Drosehn 		free(inf->l.ptr);
1145a4c8a745SGarance A Drosehn 	inf->addelem = NULL;
1146a4c8a745SGarance A Drosehn 	inf->lname = NULL;
1147d822163fSGarance A Drosehn 	inf->l.ptr = NULL;
1148a4c8a745SGarance A Drosehn }
1149a4c8a745SGarance A Drosehn 
1150a4c8a745SGarance A Drosehn static void
1151a4c8a745SGarance A Drosehn init_list(struct listinfo *inf, addelem_rtn artn, int elemsize,
1152a4c8a745SGarance A Drosehn     const char *lname)
1153a4c8a745SGarance A Drosehn {
1154a4c8a745SGarance A Drosehn 
1155a4c8a745SGarance A Drosehn 	inf->count = inf->maxcount = 0;
1156a4c8a745SGarance A Drosehn 	inf->elemsize = elemsize;
1157a4c8a745SGarance A Drosehn 	inf->addelem = artn;
1158a4c8a745SGarance A Drosehn 	inf->lname = lname;
1159d822163fSGarance A Drosehn 	inf->l.ptr = NULL;
1160cf22dcfcSBrian Somers }
1161cf22dcfcSBrian Somers 
1162fde411d5SJuli Mallett VARENT *
1163fde411d5SJuli Mallett find_varentry(VAR *v)
1164fde411d5SJuli Mallett {
1165fde411d5SJuli Mallett 	struct varent *vent;
1166fde411d5SJuli Mallett 
1167bdf8ab46SGarance A Drosehn 	STAILQ_FOREACH(vent, &varlist, next_ve) {
1168fde411d5SJuli Mallett 		if (strcmp(vent->var->name, v->name) == 0)
1169fde411d5SJuli Mallett 			return vent;
1170fde411d5SJuli Mallett 	}
1171fde411d5SJuli Mallett 	return NULL;
1172fde411d5SJuli Mallett }
1173fde411d5SJuli Mallett 
11744b88c807SRodney W. Grimes static void
117546251ddeSWarner Losh scanvars(void)
11764b88c807SRodney W. Grimes {
11774b88c807SRodney W. Grimes 	struct varent *vent;
11784b88c807SRodney W. Grimes 	VAR *v;
11796a2d726bSJordan K. Hubbard 
1180bdf8ab46SGarance A Drosehn 	STAILQ_FOREACH(vent, &varlist, next_ve) {
11816a2d726bSJordan K. Hubbard 		v = vent->var;
11826a2d726bSJordan K. Hubbard 		if (v->flag & USER)
11836a2d726bSJordan K. Hubbard 			needuser = 1;
11846a2d726bSJordan K. Hubbard 		if (v->flag & COMM)
11856a2d726bSJordan K. Hubbard 			needcomm = 1;
11866a2d726bSJordan K. Hubbard 	}
11876a2d726bSJordan K. Hubbard }
11886a2d726bSJordan K. Hubbard 
11896a2d726bSJordan K. Hubbard static void
11901d1143ecSEdward Tomasz Napierala format_output(KINFO *ki)
11916a2d726bSJordan K. Hubbard {
11926a2d726bSJordan K. Hubbard 	struct varent *vent;
11936a2d726bSJordan K. Hubbard 	VAR *v;
11941d1143ecSEdward Tomasz Napierala 	KINFO_STR *ks;
11951d1143ecSEdward Tomasz Napierala 	char *str;
11961d1143ecSEdward Tomasz Napierala 	int len;
11976a2d726bSJordan K. Hubbard 
11981d1143ecSEdward Tomasz Napierala 	STAILQ_INIT(&ki->ki_ks);
1199bdf8ab46SGarance A Drosehn 	STAILQ_FOREACH(vent, &varlist, next_ve) {
12006a2d726bSJordan K. Hubbard 		v = vent->var;
12011d1143ecSEdward Tomasz Napierala 		str = (v->oproc)(ki, vent);
12021d1143ecSEdward Tomasz Napierala 		ks = malloc(sizeof(*ks));
12031d1143ecSEdward Tomasz Napierala 		if (ks == NULL)
12048beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
12051d1143ecSEdward Tomasz Napierala 		ks->ks_str = str;
12061d1143ecSEdward Tomasz Napierala 		STAILQ_INSERT_TAIL(&ki->ki_ks, ks, ks_next);
12071d1143ecSEdward Tomasz Napierala 		if (str != NULL) {
12081d1143ecSEdward Tomasz Napierala 			len = strlen(str);
12091d1143ecSEdward Tomasz Napierala 		} else
12101d1143ecSEdward Tomasz Napierala 			len = 1; /* "-" */
12111d1143ecSEdward Tomasz Napierala 		if (v->width < len)
12121d1143ecSEdward Tomasz Napierala 			v->width = len;
12136a2d726bSJordan K. Hubbard 	}
12146a2d726bSJordan K. Hubbard }
12156a2d726bSJordan K. Hubbard 
12166a2d726bSJordan K. Hubbard static void
121746251ddeSWarner Losh sizevars(void)
12186a2d726bSJordan K. Hubbard {
12196a2d726bSJordan K. Hubbard 	struct varent *vent;
12206a2d726bSJordan K. Hubbard 	VAR *v;
12214b88c807SRodney W. Grimes 	int i;
12224b88c807SRodney W. Grimes 
1223bdf8ab46SGarance A Drosehn 	STAILQ_FOREACH(vent, &varlist, next_ve) {
12244b88c807SRodney W. Grimes 		v = vent->var;
122578b1878aSJuli Mallett 		i = strlen(vent->header);
12264b88c807SRodney W. Grimes 		if (v->width < i)
12274b88c807SRodney W. Grimes 			v->width = i;
12284b88c807SRodney W. Grimes 	}
12294b88c807SRodney W. Grimes }
12304b88c807SRodney W. Grimes 
1231871e8d8cSMark Murray static const char *
123246251ddeSWarner Losh fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki,
12331c67ef12SJohn Baldwin     char *comm, char *thread, int maxlen)
12344b88c807SRodney W. Grimes {
1235871e8d8cSMark Murray 	const char *s;
12364b88c807SRodney W. Grimes 
12371c67ef12SJohn Baldwin 	s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm,
12389f0b6e5eSJohn Baldwin 	    showthreads && ki->ki_p->ki_numthreads > 1 ? thread : NULL, maxlen);
12394b88c807SRodney W. Grimes 	return (s);
12404b88c807SRodney W. Grimes }
12414b88c807SRodney W. Grimes 
1242b61ce5b0SJeff Roberson #define UREADOK(ki)	(forceuread || (ki->ki_p->ki_flag & P_INMEM))
12433ac5e955SJohn Dyson 
12444b88c807SRodney W. Grimes static void
124546251ddeSWarner Losh saveuser(KINFO *ki)
12464b88c807SRodney W. Grimes {
12475912ca59SDon Lewis 	char *argsp;
12484b88c807SRodney W. Grimes 
1249b61ce5b0SJeff Roberson 	if (ki->ki_p->ki_flag & P_INMEM) {
12504b88c807SRodney W. Grimes 		/*
12514b88c807SRodney W. Grimes 		 * The u-area might be swapped out, and we can't get
12524b88c807SRodney W. Grimes 		 * at it because we have a crashdump and no swap.
12534b88c807SRodney W. Grimes 		 * If it's here fill in these fields, otherwise, just
12544b88c807SRodney W. Grimes 		 * leave them 0.
12554b88c807SRodney W. Grimes 		 */
12561f7d2501SKirk McKusick 		ki->ki_valid = 1;
12574b88c807SRodney W. Grimes 	} else
12581f7d2501SKirk McKusick 		ki->ki_valid = 0;
12594b88c807SRodney W. Grimes 	/*
12604b88c807SRodney W. Grimes 	 * save arguments if needed
12614b88c807SRodney W. Grimes 	 */
1262dd693acfSGarance A Drosehn 	if (needcomm) {
1263dd693acfSGarance A Drosehn 		if (ki->ki_p->ki_stat == SZOMB)
1264dd693acfSGarance A Drosehn 			ki->ki_args = strdup("<defunct>");
1265dd693acfSGarance A Drosehn 		else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL))
12665912ca59SDon Lewis 			ki->ki_args = fmt(kvm_getargv, ki,
12675912ca59SDon Lewis 			    ki->ki_p->ki_comm, ki->ki_p->ki_tdname, MAXCOMLEN);
12685912ca59SDon Lewis 		else {
12695912ca59SDon Lewis 			asprintf(&argsp, "(%s)", ki->ki_p->ki_comm);
12705912ca59SDon Lewis 			ki->ki_args = argsp;
12715912ca59SDon Lewis 		}
1272bd6233fdSGarance A Drosehn 		if (ki->ki_args == NULL)
12738beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
12743ac5e955SJohn Dyson 	} else {
12754b88c807SRodney W. Grimes 		ki->ki_args = NULL;
12763ac5e955SJohn Dyson 	}
1277dd693acfSGarance A Drosehn 	if (needenv) {
1278dd693acfSGarance A Drosehn 		if (UREADOK(ki))
12795912ca59SDon Lewis 			ki->ki_env = fmt(kvm_getenvv, ki,
12805912ca59SDon Lewis 			    (char *)NULL, (char *)NULL, 0);
1281dd693acfSGarance A Drosehn 		else
1282dd693acfSGarance A Drosehn 			ki->ki_env = strdup("()");
1283dd693acfSGarance A Drosehn 		if (ki->ki_env == NULL)
12848beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
12853ac5e955SJohn Dyson 	} else {
12864b88c807SRodney W. Grimes 		ki->ki_env = NULL;
12874b88c807SRodney W. Grimes 	}
12883ac5e955SJohn Dyson }
12894b88c807SRodney W. Grimes 
12904bac4483SGarance A Drosehn /* A macro used to improve the readability of pscomp(). */
12914bac4483SGarance A Drosehn #define	DIFF_RETURN(a, b, field) do {	\
12924bac4483SGarance A Drosehn 	if ((a)->field != (b)->field)	\
12934bac4483SGarance A Drosehn 		return (((a)->field < (b)->field) ? -1 : 1); 	\
12944bac4483SGarance A Drosehn } while (0)
12954bac4483SGarance A Drosehn 
12964b88c807SRodney W. Grimes static int
129746251ddeSWarner Losh pscomp(const void *a, const void *b)
12984b88c807SRodney W. Grimes {
12995bd7b1f3SGarance A Drosehn 	const KINFO *ka, *kb;
13004b88c807SRodney W. Grimes 
13015bd7b1f3SGarance A Drosehn 	ka = a;
13025bd7b1f3SGarance A Drosehn 	kb = b;
13035bd7b1f3SGarance A Drosehn 	/* SORTCPU and SORTMEM are sorted in descending order. */
13044bac4483SGarance A Drosehn 	if (sortby == SORTCPU)
13054bac4483SGarance A Drosehn 		DIFF_RETURN(kb, ka, ki_pcpu);
13064bac4483SGarance A Drosehn 	if (sortby == SORTMEM)
13074bac4483SGarance A Drosehn 		DIFF_RETURN(kb, ka, ki_memsize);
13085bd7b1f3SGarance A Drosehn 	/*
13095bd7b1f3SGarance A Drosehn 	 * TTY's are sorted in ascending order, except that all NODEV
13105bd7b1f3SGarance A Drosehn 	 * processes come before all processes with a device.
13115bd7b1f3SGarance A Drosehn 	 */
13124bac4483SGarance A Drosehn 	if (ka->ki_p->ki_tdev != kb->ki_p->ki_tdev) {
13134bac4483SGarance A Drosehn 		if (ka->ki_p->ki_tdev == NODEV)
13145bd7b1f3SGarance A Drosehn 			return (-1);
13154bac4483SGarance A Drosehn 		if (kb->ki_p->ki_tdev == NODEV)
13165bd7b1f3SGarance A Drosehn 			return (1);
13174bac4483SGarance A Drosehn 		DIFF_RETURN(ka, kb, ki_p->ki_tdev);
13184bac4483SGarance A Drosehn 	}
13194bac4483SGarance A Drosehn 
1320b4b24324SGarance A Drosehn 	/* PID's and TID's (threads) are sorted in ascending order. */
13214bac4483SGarance A Drosehn 	DIFF_RETURN(ka, kb, ki_p->ki_pid);
1322b4b24324SGarance A Drosehn 	DIFF_RETURN(ka, kb, ki_p->ki_tid);
13235bd7b1f3SGarance A Drosehn 	return (0);
13244b88c807SRodney W. Grimes }
13254bac4483SGarance A Drosehn #undef DIFF_RETURN
13264b88c807SRodney W. Grimes 
13274b88c807SRodney W. Grimes /*
13284b88c807SRodney W. Grimes  * ICK (all for getopt), would rather hide the ugliness
13294b88c807SRodney W. Grimes  * here than taint the main code.
13304b88c807SRodney W. Grimes  *
13314b88c807SRodney W. Grimes  *  ps foo -> ps -foo
13324b88c807SRodney W. Grimes  *  ps 34 -> ps -p34
13334b88c807SRodney W. Grimes  *
13344b88c807SRodney W. Grimes  * The old convention that 't' with no trailing tty arg means the users
13354b88c807SRodney W. Grimes  * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
13364b88c807SRodney W. Grimes  * feature is available with the option 'T', which takes no argument.
13374b88c807SRodney W. Grimes  */
13384b88c807SRodney W. Grimes static char *
1339bf46a3bfSGarance A Drosehn kludge_oldps_options(const char *optlist, char *origval, const char *nextarg)
13404b88c807SRodney W. Grimes {
13414b88c807SRodney W. Grimes 	size_t len;
1342c675340aSGarance A Drosehn 	char *argp, *cp, *newopts, *ns, *optp, *pidp;
13434b88c807SRodney W. Grimes 
1344daed3ad6SJuli Mallett 	/*
1345c675340aSGarance A Drosehn 	 * See if the original value includes any option which takes an
1346c675340aSGarance A Drosehn 	 * argument (and will thus use up the remainder of the string).
1347daed3ad6SJuli Mallett 	 */
1348c675340aSGarance A Drosehn 	argp = NULL;
1349c675340aSGarance A Drosehn 	if (optlist != NULL) {
1350c675340aSGarance A Drosehn 		for (cp = origval; *cp != '\0'; cp++) {
1351c675340aSGarance A Drosehn 			optp = strchr(optlist, *cp);
1352c675340aSGarance A Drosehn 			if ((optp != NULL) && *(optp + 1) == ':') {
1353c675340aSGarance A Drosehn 				argp = cp;
1354c675340aSGarance A Drosehn 				break;
1355c675340aSGarance A Drosehn 			}
1356c675340aSGarance A Drosehn 		}
1357c675340aSGarance A Drosehn 	}
1358c675340aSGarance A Drosehn 	if (argp != NULL && *origval == '-')
1359c675340aSGarance A Drosehn 		return (origval);
1360daed3ad6SJuli Mallett 
13614b88c807SRodney W. Grimes 	/*
13624b88c807SRodney W. Grimes 	 * if last letter is a 't' flag with no argument (in the context
13634b88c807SRodney W. Grimes 	 * of the oldps options -- option string NOT starting with a '-' --
13644b88c807SRodney W. Grimes 	 * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
1365380434d4SBrian Somers 	 *
13662631c777SGarance A Drosehn 	 * However, if a flag accepting a string argument is found earlier
13672631c777SGarance A Drosehn 	 * in the option string (including a possible `t' flag), then the
13682631c777SGarance A Drosehn 	 * remainder of the string must be the argument to that flag; so
1369c675340aSGarance A Drosehn 	 * do not modify that argument.  Note that a trailing `t' would
1370c675340aSGarance A Drosehn 	 * cause argp to be set, if argp was not already set by some
1371c675340aSGarance A Drosehn 	 * earlier option.
13724b88c807SRodney W. Grimes 	 */
1373c675340aSGarance A Drosehn 	len = strlen(origval);
1374c675340aSGarance A Drosehn 	cp = origval + len - 1;
1375c675340aSGarance A Drosehn 	pidp = NULL;
1376bf46a3bfSGarance A Drosehn 	if (*cp == 't' && *origval != '-' && cp == argp) {
1377bf46a3bfSGarance A Drosehn 		if (nextarg == NULL || *nextarg == '-' || isdigitch(*nextarg))
13784b88c807SRodney W. Grimes 			*cp = 'T';
1379bf46a3bfSGarance A Drosehn 	} else if (argp == NULL) {
1380c675340aSGarance A Drosehn 		/*
1381c675340aSGarance A Drosehn 		 * The original value did not include any option which takes
1382c675340aSGarance A Drosehn 		 * an argument (and that would include `p' and `t'), so check
1383c675340aSGarance A Drosehn 		 * the value for trailing number, or comma-separated list of
1384c675340aSGarance A Drosehn 		 * numbers, which we will treat as a pid request.
1385c675340aSGarance A Drosehn 		 */
1386c675340aSGarance A Drosehn 		if (isdigitch(*cp)) {
1387c675340aSGarance A Drosehn 			while (cp >= origval && (*cp == ',' || isdigitch(*cp)))
1388c675340aSGarance A Drosehn 				--cp;
1389c675340aSGarance A Drosehn 			pidp = cp + 1;
1390c675340aSGarance A Drosehn 		}
1391c675340aSGarance A Drosehn 	}
1392c675340aSGarance A Drosehn 
1393c675340aSGarance A Drosehn 	/*
1394c675340aSGarance A Drosehn 	 * If nothing needs to be added to the string, then return
1395c675340aSGarance A Drosehn 	 * the "original" (although possibly modified) value.
1396c675340aSGarance A Drosehn 	 */
1397c675340aSGarance A Drosehn 	if (*origval == '-' && pidp == NULL)
1398c675340aSGarance A Drosehn 		return (origval);
1399c675340aSGarance A Drosehn 
1400c675340aSGarance A Drosehn 	/*
1401c675340aSGarance A Drosehn 	 * Create a copy of the string to add '-' and/or 'p' to the
1402c675340aSGarance A Drosehn 	 * original value.
1403c675340aSGarance A Drosehn 	 */
1404c675340aSGarance A Drosehn 	if ((newopts = ns = malloc(len + 3)) == NULL)
14058beb1a2fSMarcel Moolenaar 		xo_errx(1, "malloc failed");
1406c675340aSGarance A Drosehn 
1407c675340aSGarance A Drosehn 	if (*origval != '-')
1408c675340aSGarance A Drosehn 		*ns++ = '-';	/* add option flag */
1409c675340aSGarance A Drosehn 
1410c675340aSGarance A Drosehn 	if (pidp == NULL)
1411c675340aSGarance A Drosehn 		strcpy(ns, origval);
14124b88c807SRodney W. Grimes 	else {
14134b88c807SRodney W. Grimes 		/*
1414c675340aSGarance A Drosehn 		 * Copy everything before the pid string, add the `p',
1415c675340aSGarance A Drosehn 		 * and then copy the pid string.
14164b88c807SRodney W. Grimes 		 */
1417c675340aSGarance A Drosehn 		len = pidp - origval;
1418c675340aSGarance A Drosehn 		memcpy(ns, origval, len);
1419c675340aSGarance A Drosehn 		ns += len;
14204b88c807SRodney W. Grimes 		*ns++ = 'p';
1421c675340aSGarance A Drosehn 		strcpy(ns, pidp);
1422c675340aSGarance A Drosehn 	}
14234b88c807SRodney W. Grimes 
14244b88c807SRodney W. Grimes 	return (newopts);
14254b88c807SRodney W. Grimes }
14264b88c807SRodney W. Grimes 
14274b88c807SRodney W. Grimes static void
142864b0bf0bSPawel Jakub Dawidek pidmax_init(void)
142964b0bf0bSPawel Jakub Dawidek {
143064b0bf0bSPawel Jakub Dawidek 	size_t intsize;
143164b0bf0bSPawel Jakub Dawidek 
143264b0bf0bSPawel Jakub Dawidek 	intsize = sizeof(pid_max);
143364b0bf0bSPawel Jakub Dawidek 	if (sysctlbyname("kern.pid_max", &pid_max, &intsize, NULL, 0) < 0) {
14348beb1a2fSMarcel Moolenaar 		xo_warn("unable to read kern.pid_max");
143564b0bf0bSPawel Jakub Dawidek 		pid_max = 99999;
143664b0bf0bSPawel Jakub Dawidek 	}
143764b0bf0bSPawel Jakub Dawidek }
143864b0bf0bSPawel Jakub Dawidek 
143964b0bf0bSPawel Jakub Dawidek static void
144046251ddeSWarner Losh usage(void)
14414b88c807SRodney W. Grimes {
1442aa14b5abSBrian Somers #define	SINGLE_OPTS	"[-aCcde" OPT_LAZY_f "HhjlmrSTuvwXxZ]"
14434b88c807SRodney W. Grimes 
14448beb1a2fSMarcel Moolenaar 	(void)xo_error("%s\n%s\n%s\n%s\n",
1445a89237aeSRuslan Ermilov 	    "usage: ps " SINGLE_OPTS " [-O fmt | -o fmt] [-G gid[,gid...]]",
144613767130SBryan Drewery 	    "          [-J jid[,jid...]] [-M core] [-N system]",
1447a89237aeSRuslan Ermilov 	    "          [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]",
14484b88c807SRodney W. Grimes 	    "       ps [-L]");
14494b88c807SRodney W. Grimes 	exit(1);
14504b88c807SRodney W. Grimes }
1451