xref: /freebsd/bin/ps/ps.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
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.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
144b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
154b88c807SRodney W. Grimes  *    without specific prior written permission.
164b88c807SRodney W. Grimes  *
174b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
184b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
194b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
214b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
224b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
254b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274b88c807SRodney W. Grimes  * SUCH DAMAGE.
28a4c8a745SGarance A Drosehn  * ------+---------+---------+-------- + --------+---------+---------+---------*
29a4c8a745SGarance A Drosehn  * Copyright (c) 2004  - Garance Alistair Drosehn <gad@FreeBSD.org>.
30a4c8a745SGarance A Drosehn  * All rights reserved.
31a4c8a745SGarance A Drosehn  *
32a4c8a745SGarance A Drosehn  * Significant modifications made to bring `ps' options somewhat closer
33a4c8a745SGarance A Drosehn  * to the standard for `ps' as described in SingleUnixSpec-v3.
34a4c8a745SGarance A Drosehn  * ------+---------+---------+-------- + --------+---------+---------+---------*
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
38871e8d8cSMark Murray static const char copyright[] =
394b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\
404b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
414b88c807SRodney W. Grimes #endif /* not lint */
424b88c807SRodney W. Grimes 
43c9a8d1f4SPhilippe Charnier #if 0
44871e8d8cSMark Murray #ifndef lint
45c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)ps.c	8.4 (Berkeley) 4/2/94";
464b88c807SRodney W. Grimes #endif /* not lint */
47871e8d8cSMark Murray #endif
48eaed5652SPhilippe Charnier 
492749b141SDavid E. O'Brien #include <sys/cdefs.h>
502749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
514b88c807SRodney W. Grimes 
524b88c807SRodney W. Grimes #include <sys/param.h>
5313767130SBryan Drewery #include <sys/jail.h>
54c7910c3aSGarance A Drosehn #include <sys/proc.h>
558b6f5f5fSDavid Greenman #include <sys/user.h>
564b88c807SRodney W. Grimes #include <sys/stat.h>
574b88c807SRodney W. Grimes #include <sys/ioctl.h>
584b88c807SRodney W. Grimes #include <sys/sysctl.h>
59a951d1f8SChristian S.J. Peron #include <sys/mount.h>
604b88c807SRodney W. Grimes 
614b88c807SRodney W. Grimes #include <ctype.h>
624b88c807SRodney W. Grimes #include <err.h>
634e8b6a6fSGarance A Drosehn #include <errno.h>
644b88c807SRodney W. Grimes #include <fcntl.h>
65a4c8a745SGarance A Drosehn #include <grp.h>
6613767130SBryan Drewery #include <jail.h>
674b88c807SRodney W. Grimes #include <kvm.h>
68b2496d93SMike Pritchard #include <limits.h>
6908017519SAndrey A. Chernov #include <locale.h>
704b88c807SRodney W. Grimes #include <paths.h>
71871e8d8cSMark Murray #include <pwd.h>
724b88c807SRodney W. Grimes #include <stdio.h>
734b88c807SRodney W. Grimes #include <stdlib.h>
744b88c807SRodney W. Grimes #include <string.h>
754b88c807SRodney W. Grimes #include <unistd.h>
768beb1a2fSMarcel Moolenaar #include <libxo/xo.h>
774b88c807SRodney W. Grimes 
784b88c807SRodney W. Grimes #include "ps.h"
794b88c807SRodney W. Grimes 
808a529f59SJohn Baldwin #define	_PATH_PTS	"/dev/pts/"
818a529f59SJohn Baldwin 
82a4c8a745SGarance A Drosehn #define	W_SEP	" \t"		/* "Whitespace" list separators */
83a4c8a745SGarance A Drosehn #define	T_SEP	","		/* "Terminate-element" list separators */
84cf22dcfcSBrian Somers 
85de800cd4SGarance A Drosehn #ifdef LAZY_PS
86c46bb4b3SGarance A Drosehn #define	DEF_UREAD	0
87de800cd4SGarance A Drosehn #define	OPT_LAZY_f	"f"
88de800cd4SGarance A Drosehn #else
89c46bb4b3SGarance A Drosehn #define	DEF_UREAD	1	/* Always do the more-expensive read. */
90de800cd4SGarance A Drosehn #define	OPT_LAZY_f		/* I.e., the `-f' option is not added. */
91de800cd4SGarance A Drosehn #endif
924b88c807SRodney W. Grimes 
93c675340aSGarance A Drosehn /*
94ecbb06f8SGarance A Drosehn  * isdigit takes an `int', but expects values in the range of unsigned char.
95ecbb06f8SGarance A Drosehn  * This wrapper ensures that values from a 'char' end up in the correct range.
96c675340aSGarance A Drosehn  */
97ecbb06f8SGarance A Drosehn #define	isdigitch(Anychar) isdigit((u_char)(Anychar))
98c675340aSGarance A Drosehn 
99db91faacSPeter Wemm int	 cflag;			/* -c */
100de800cd4SGarance A Drosehn int	 eval;			/* Exit value */
101de800cd4SGarance A Drosehn time_t	 now;			/* Current time(3) value */
1024b88c807SRodney W. Grimes int	 rawcpu;		/* -C */
1034b88c807SRodney W. Grimes int	 sumrusage;		/* -S */
104de800cd4SGarance A Drosehn int	 termwidth;		/* Width of the screen (0 == infinity). */
1057ab24ea3SJulian Elischer int	 showthreads;		/* will threads be shown? */
1064b88c807SRodney W. Grimes 
107bdf8ab46SGarance A Drosehn struct velisthead varlist = STAILQ_HEAD_INITIALIZER(varlist);
108de800cd4SGarance A Drosehn 
109de800cd4SGarance A Drosehn static int	 forceuread = DEF_UREAD; /* Do extra work to get u-area. */
110de800cd4SGarance A Drosehn static kvm_t	*kd;
111de800cd4SGarance A Drosehn static int	 needcomm;	/* -o "command" */
112de800cd4SGarance A Drosehn static int	 needenv;	/* -e */
113de800cd4SGarance A Drosehn static int	 needuser;	/* -o "user" */
114de800cd4SGarance A Drosehn static int	 optfatal;	/* Fatal error parsing some list-option. */
11564b0bf0bSPawel Jakub Dawidek static int	 pid_max;	/* kern.max_pid */
116de800cd4SGarance A Drosehn 
117de800cd4SGarance A Drosehn static enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
118ba2cd770SJuli Mallett 
119a4c8a745SGarance A Drosehn struct listinfo;
120de800cd4SGarance A Drosehn typedef	int	addelem_rtn(struct listinfo *_inf, const char *_elem);
121a4c8a745SGarance A Drosehn 
122a4c8a745SGarance A Drosehn struct listinfo {
123a4c8a745SGarance A Drosehn 	int		 count;
124a4c8a745SGarance A Drosehn 	int		 maxcount;
125a4c8a745SGarance A Drosehn 	int		 elemsize;
126a4c8a745SGarance A Drosehn 	addelem_rtn	*addelem;
127a4c8a745SGarance A Drosehn 	const char	*lname;
128a4c8a745SGarance A Drosehn 	union {
129a4c8a745SGarance A Drosehn 		gid_t	*gids;
13013767130SBryan Drewery 		int	*jids;
131a4c8a745SGarance A Drosehn 		pid_t	*pids;
132a4c8a745SGarance A Drosehn 		dev_t	*ttys;
133a4c8a745SGarance A Drosehn 		uid_t	*uids;
134a4c8a745SGarance A Drosehn 		void	*ptr;
135d822163fSGarance A Drosehn 	} l;
136a4c8a745SGarance A Drosehn };
137a4c8a745SGarance A Drosehn 
138a4c8a745SGarance A Drosehn static int	 addelem_gid(struct listinfo *, const char *);
13913767130SBryan Drewery static int	 addelem_jid(struct listinfo *, const char *);
140a4c8a745SGarance A Drosehn static int	 addelem_pid(struct listinfo *, const char *);
141a4c8a745SGarance A Drosehn static int	 addelem_tty(struct listinfo *, const char *);
142a4c8a745SGarance A Drosehn static int	 addelem_uid(struct listinfo *, const char *);
143a4c8a745SGarance A Drosehn static void	 add_list(struct listinfo *, const char *);
144044fce53SBrian Somers static void	 descendant_sort(KINFO *, int);
1451d1143ecSEdward Tomasz Napierala static void	 format_output(KINFO *);
146a4c8a745SGarance A Drosehn static void	*expand_list(struct listinfo *);
147f35e0715SGarance A Drosehn static const char *
148f35e0715SGarance A Drosehn 		 fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int),
1491c67ef12SJohn Baldwin 		    KINFO *, char *, char *, int);
150a4c8a745SGarance A Drosehn static void	 free_list(struct listinfo *);
151a4c8a745SGarance A Drosehn static void	 init_list(struct listinfo *, addelem_rtn, int, const char *);
15225113083SGarance A Drosehn static char	*kludge_oldps_options(const char *, char *, const char *);
1534857f240SGarance A Drosehn static int	 pscomp(const void *, const void *);
1544857f240SGarance A Drosehn static void	 saveuser(KINFO *);
1554857f240SGarance A Drosehn static void	 scanvars(void);
1564857f240SGarance A Drosehn static void	 sizevars(void);
15764b0bf0bSPawel Jakub Dawidek static void	 pidmax_init(void);
1584857f240SGarance A Drosehn static void	 usage(void);
1594b88c807SRodney W. Grimes 
160c0716492SJuli Mallett static char dfmt[] = "pid,tt,state,time,command";
161259fcfacSGarance A Drosehn static char jfmt[] = "user,pid,ppid,pgid,sid,jobc,state,tt,time,command";
1621d2324f4SGarance A Drosehn static char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state,"
1631d2324f4SGarance A Drosehn 			"tt,time,command";
164871e8d8cSMark Murray static char   o1[] = "pid";
165c0716492SJuli Mallett static char   o2[] = "tt,state,time,command";
166c0716492SJuli Mallett static char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command";
1671d2324f4SGarance A Drosehn static char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz,"
1681d2324f4SGarance A Drosehn 			"%cpu,%mem,command";
1692af538ebSRobert Watson static char Zfmt[] = "label";
1704b88c807SRodney W. Grimes 
17113767130SBryan Drewery #define	PS_ARGS	"AaCcde" OPT_LAZY_f "G:gHhjJ:LlM:mN:O:o:p:rSTt:U:uvwXxZ"
17241623b2dSMaxim Sobolev 
1734b88c807SRodney W. Grimes int
17446251ddeSWarner Losh main(int argc, char *argv[])
1754b88c807SRodney W. Grimes {
17613767130SBryan Drewery 	struct listinfo gidlist, jidlist, pgrplist, pidlist;
177a4c8a745SGarance A Drosehn 	struct listinfo ruidlist, sesslist, ttylist, uidlist;
1784b88c807SRodney W. Grimes 	struct kinfo_proc *kp;
1791d1143ecSEdward Tomasz Napierala 	KINFO *kinfo = NULL, *next_KINFO;
1801d1143ecSEdward Tomasz Napierala 	KINFO_STR *ks;
1814b88c807SRodney W. Grimes 	struct varent *vent;
182484f5781SPedro F. Giffuni 	struct winsize ws = { .ws_row = 0 };
18316eb3576SMarcelo Araujo 	const char *nlistf, *memf, *str;
184ca62e195SGarance A Drosehn 	char *cols;
1851d1143ecSEdward Tomasz Napierala 	int all, ch, elem, flag, _fmt, i, lineno, linelen, left;
186044fce53SBrian Somers 	int descendancy, nentries, nkept, nselectors;
1877ab24ea3SJulian Elischer 	int prtheader, wflag, what, xkeep, xkeep_implied;
1888beb1a2fSMarcel Moolenaar 	int fwidthmin, fwidthmax;
189871e8d8cSMark Murray 	char errbuf[_POSIX2_LINE_MAX];
1908beb1a2fSMarcel Moolenaar 	char fmtbuf[_POSIX2_LINE_MAX];
1914b88c807SRodney W. Grimes 
1922bf4b9cfSAndrey A. Chernov 	(void) setlocale(LC_ALL, "");
193352b6523SGarance A Drosehn 	time(&now);			/* Used by routines in print.c. */
1942bf4b9cfSAndrey A. Chernov 
1954f18100dSTim J. Robbins 	if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0')
1964f18100dSTim J. Robbins 		termwidth = atoi(cols);
1974f18100dSTim J. Robbins 	else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1984b88c807SRodney W. Grimes 	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1994b88c807SRodney W. Grimes 	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
2004b88c807SRodney W. Grimes 	     ws.ws_col == 0)
2014b88c807SRodney W. Grimes 		termwidth = 79;
2024b88c807SRodney W. Grimes 	else
2034b88c807SRodney W. Grimes 		termwidth = ws.ws_col - 1;
2044b88c807SRodney W. Grimes 
20541623b2dSMaxim Sobolev 	/*
206c675340aSGarance A Drosehn 	 * Hide a number of option-processing kludges in a separate routine,
207c675340aSGarance A Drosehn 	 * to support some historical BSD behaviors, such as `ps axu'.
20841623b2dSMaxim Sobolev 	 */
209c675340aSGarance A Drosehn 	if (argc > 1)
210bf46a3bfSGarance A Drosehn 		argv[1] = kludge_oldps_options(PS_ARGS, argv[1], argv[2]);
2114b88c807SRodney W. Grimes 
21264b0bf0bSPawel Jakub Dawidek 	pidmax_init();
21364b0bf0bSPawel Jakub Dawidek 
214044fce53SBrian Somers 	all = descendancy = _fmt = nselectors = optfatal = 0;
215352b6523SGarance A Drosehn 	prtheader = showthreads = wflag = xkeep_implied = 0;
216352b6523SGarance A Drosehn 	xkeep = -1;			/* Neither -x nor -X. */
217a4c8a745SGarance A Drosehn 	init_list(&gidlist, addelem_gid, sizeof(gid_t), "group");
21813767130SBryan Drewery 	init_list(&jidlist, addelem_jid, sizeof(int), "jail id");
219a4c8a745SGarance A Drosehn 	init_list(&pgrplist, addelem_pid, sizeof(pid_t), "process group");
220a4c8a745SGarance A Drosehn 	init_list(&pidlist, addelem_pid, sizeof(pid_t), "process id");
221a4c8a745SGarance A Drosehn 	init_list(&ruidlist, addelem_uid, sizeof(uid_t), "ruser");
222a4c8a745SGarance A Drosehn 	init_list(&sesslist, addelem_pid, sizeof(pid_t), "session id");
223a4c8a745SGarance A Drosehn 	init_list(&ttylist, addelem_tty, sizeof(dev_t), "tty");
224a4c8a745SGarance A Drosehn 	init_list(&uidlist, addelem_uid, sizeof(uid_t), "user");
225c08dcaf1SRebecca Cran 	memf = _PATH_DEVNULL;
226c08dcaf1SRebecca Cran 	nlistf = NULL;
2278beb1a2fSMarcel Moolenaar 
2288beb1a2fSMarcel Moolenaar 	argc = xo_parse_args(argc, argv);
2298beb1a2fSMarcel Moolenaar 	if (argc < 0)
2308beb1a2fSMarcel Moolenaar 		exit(1);
2318beb1a2fSMarcel Moolenaar 
23241623b2dSMaxim Sobolev 	while ((ch = getopt(argc, argv, PS_ARGS)) != -1)
2330c025af9SKevin Lo 		switch (ch) {
234a4c8a745SGarance A Drosehn 		case 'A':
235a4c8a745SGarance A Drosehn 			/*
236a4c8a745SGarance A Drosehn 			 * Exactly the same as `-ax'.   This has been
237bf2fe08eSUlrich Spörlein 			 * added for compatibility with SUSv3, but for
238a4c8a745SGarance A Drosehn 			 * now it will not be described in the man page.
239a4c8a745SGarance A Drosehn 			 */
240a4c8a745SGarance A Drosehn 			nselectors++;
241a4c8a745SGarance A Drosehn 			all = xkeep = 1;
242a4c8a745SGarance A Drosehn 			break;
2434b88c807SRodney W. Grimes 		case 'a':
244a4c8a745SGarance A Drosehn 			nselectors++;
2454b88c807SRodney W. Grimes 			all = 1;
2464b88c807SRodney W. Grimes 			break;
2474b88c807SRodney W. Grimes 		case 'C':
2484b88c807SRodney W. Grimes 			rawcpu = 1;
2494b88c807SRodney W. Grimes 			break;
250db91faacSPeter Wemm 		case 'c':
251db91faacSPeter Wemm 			cflag = 1;
252db91faacSPeter Wemm 			break;
253044fce53SBrian Somers 		case 'd':
254044fce53SBrian Somers 			descendancy = 1;
255044fce53SBrian Somers 			break;
256db91faacSPeter Wemm 		case 'e':			/* XXX set ufmt */
257db91faacSPeter Wemm 			needenv = 1;
258db91faacSPeter Wemm 			break;
2594a355d17SGarance A Drosehn #ifdef LAZY_PS
2604a355d17SGarance A Drosehn 		case 'f':
2614a355d17SGarance A Drosehn 			if (getuid() == 0 || getgid() == 0)
2624a355d17SGarance A Drosehn 				forceuread = 1;
2634a355d17SGarance A Drosehn 			break;
2644a355d17SGarance A Drosehn #endif
265a4c8a745SGarance A Drosehn 		case 'G':
266a4c8a745SGarance A Drosehn 			add_list(&gidlist, optarg);
267a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
268a4c8a745SGarance A Drosehn 			nselectors++;
269a4c8a745SGarance A Drosehn 			break;
2704b88c807SRodney W. Grimes 		case 'g':
271352b6523SGarance A Drosehn #if 0
272ba50b0e0SGarance A Drosehn 			/*-
273352b6523SGarance A Drosehn 			 * XXX - This SUSv3 behavior is still under debate
274352b6523SGarance A Drosehn 			 *	since it conflicts with the (undocumented)
275352b6523SGarance A Drosehn 			 *	`-g' option.  So we skip it for now.
276352b6523SGarance A Drosehn 			 */
277a4c8a745SGarance A Drosehn 			add_list(&pgrplist, optarg);
278a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
279a4c8a745SGarance A Drosehn 			nselectors++;
280a4c8a745SGarance A Drosehn 			break;
281a4c8a745SGarance A Drosehn #else
282352b6523SGarance A Drosehn 			/* The historical BSD-ish (from SunOS) behavior. */
2834b88c807SRodney W. Grimes 			break;			/* no-op */
284a4c8a745SGarance A Drosehn #endif
28548b8c0deSScott Long 		case 'H':
286d75c1d83SDaniel Eischen 			showthreads = KERN_PROC_INC_THREAD;
28748b8c0deSScott Long 			break;
2884b88c807SRodney W. Grimes 		case 'h':
2894b88c807SRodney W. Grimes 			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
2904b88c807SRodney W. Grimes 			break;
29113767130SBryan Drewery 		case 'J':
29213767130SBryan Drewery 			add_list(&jidlist, optarg);
29313767130SBryan Drewery 			xkeep_implied = 1;
29413767130SBryan Drewery 			nselectors++;
29513767130SBryan Drewery 			break;
2964b88c807SRodney W. Grimes 		case 'j':
297fde411d5SJuli Mallett 			parsefmt(jfmt, 0);
298871e8d8cSMark Murray 			_fmt = 1;
2994b88c807SRodney W. Grimes 			jfmt[0] = '\0';
3004b88c807SRodney W. Grimes 			break;
3014b88c807SRodney W. Grimes 		case 'L':
3024b88c807SRodney W. Grimes 			showkey();
3034b88c807SRodney W. Grimes 			exit(0);
3044b88c807SRodney W. Grimes 		case 'l':
305fde411d5SJuli Mallett 			parsefmt(lfmt, 0);
306871e8d8cSMark Murray 			_fmt = 1;
3074b88c807SRodney W. Grimes 			lfmt[0] = '\0';
3084b88c807SRodney W. Grimes 			break;
3094b88c807SRodney W. Grimes 		case 'M':
3104b88c807SRodney W. Grimes 			memf = optarg;
3114b88c807SRodney W. Grimes 			break;
3124b88c807SRodney W. Grimes 		case 'm':
3134b88c807SRodney W. Grimes 			sortby = SORTMEM;
3144b88c807SRodney W. Grimes 			break;
3154b88c807SRodney W. Grimes 		case 'N':
3164b88c807SRodney W. Grimes 			nlistf = optarg;
3174b88c807SRodney W. Grimes 			break;
3184b88c807SRodney W. Grimes 		case 'O':
319fde411d5SJuli Mallett 			parsefmt(o1, 1);
320fde411d5SJuli Mallett 			parsefmt(optarg, 1);
321fde411d5SJuli Mallett 			parsefmt(o2, 1);
3224b88c807SRodney W. Grimes 			o1[0] = o2[0] = '\0';
323871e8d8cSMark Murray 			_fmt = 1;
3244b88c807SRodney W. Grimes 			break;
3254b88c807SRodney W. Grimes 		case 'o':
326fde411d5SJuli Mallett 			parsefmt(optarg, 1);
327871e8d8cSMark Murray 			_fmt = 1;
3284b88c807SRodney W. Grimes 			break;
3294b88c807SRodney W. Grimes 		case 'p':
330a4c8a745SGarance A Drosehn 			add_list(&pidlist, optarg);
331a4c8a745SGarance A Drosehn 			/*
332a4c8a745SGarance A Drosehn 			 * Note: `-p' does not *set* xkeep, but any values
333a4c8a745SGarance A Drosehn 			 * from pidlist are checked before xkeep is.  That
334a4c8a745SGarance A Drosehn 			 * way they are always matched, even if the user
335a4c8a745SGarance A Drosehn 			 * specifies `-X'.
336a4c8a745SGarance A Drosehn 			 */
337a4c8a745SGarance A Drosehn 			nselectors++;
3384b88c807SRodney W. Grimes 			break;
339a4c8a745SGarance A Drosehn #if 0
340a4c8a745SGarance A Drosehn 		case 'R':
341ba50b0e0SGarance A Drosehn 			/*-
342352b6523SGarance A Drosehn 			 * XXX - This un-standard option is still under
343352b6523SGarance A Drosehn 			 *	debate.  This is what SUSv3 defines as
344352b6523SGarance A Drosehn 			 *	the `-U' option, and while it would be
345352b6523SGarance A Drosehn 			 *	nice to have, it could cause even more
346352b6523SGarance A Drosehn 			 *	confusion to implement it as `-R'.
347352b6523SGarance A Drosehn 			 */
348a4c8a745SGarance A Drosehn 			add_list(&ruidlist, optarg);
349a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
350a4c8a745SGarance A Drosehn 			nselectors++;
351a4c8a745SGarance A Drosehn 			break;
352a4c8a745SGarance A Drosehn #endif
3534b88c807SRodney W. Grimes 		case 'r':
3544b88c807SRodney W. Grimes 			sortby = SORTCPU;
3554b88c807SRodney W. Grimes 			break;
3564b88c807SRodney W. Grimes 		case 'S':
3574b88c807SRodney W. Grimes 			sumrusage = 1;
3584b88c807SRodney W. Grimes 			break;
359a4c8a745SGarance A Drosehn #if 0
360a4c8a745SGarance A Drosehn 		case 's':
361ba50b0e0SGarance A Drosehn 			/*-
362352b6523SGarance A Drosehn 			 * XXX - This non-standard option is still under
363352b6523SGarance A Drosehn 			 *	debate.  This *is* supported on Solaris,
364352b6523SGarance A Drosehn 			 *	Linux, and IRIX, but conflicts with `-s'
365352b6523SGarance A Drosehn 			 *	on NetBSD and maybe some older BSD's.
366352b6523SGarance A Drosehn 			 */
367a4c8a745SGarance A Drosehn 			add_list(&sesslist, optarg);
368a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
369a4c8a745SGarance A Drosehn 			nselectors++;
370a4c8a745SGarance A Drosehn 			break;
371a4c8a745SGarance A Drosehn #endif
3724b88c807SRodney W. Grimes 		case 'T':
3734b88c807SRodney W. Grimes 			if ((optarg = ttyname(STDIN_FILENO)) == NULL)
3748beb1a2fSMarcel Moolenaar 				xo_errx(1, "stdin: not a terminal");
3754b88c807SRodney W. Grimes 			/* FALLTHROUGH */
376a4c8a745SGarance A Drosehn 		case 't':
377a4c8a745SGarance A Drosehn 			add_list(&ttylist, optarg);
378a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
379a4c8a745SGarance A Drosehn 			nselectors++;
3804b88c807SRodney W. Grimes 			break;
38173eb8310SPeter Wemm 		case 'U':
382a4c8a745SGarance A Drosehn 			/* This is what SUSv3 defines as the `-u' option. */
383a4c8a745SGarance A Drosehn 			add_list(&uidlist, optarg);
384a4c8a745SGarance A Drosehn 			xkeep_implied = 1;
385a4c8a745SGarance A Drosehn 			nselectors++;
38673eb8310SPeter Wemm 			break;
3874b88c807SRodney W. Grimes 		case 'u':
388fde411d5SJuli Mallett 			parsefmt(ufmt, 0);
3894b88c807SRodney W. Grimes 			sortby = SORTCPU;
390871e8d8cSMark Murray 			_fmt = 1;
3914b88c807SRodney W. Grimes 			ufmt[0] = '\0';
3924b88c807SRodney W. Grimes 			break;
3934b88c807SRodney W. Grimes 		case 'v':
394fde411d5SJuli Mallett 			parsefmt(vfmt, 0);
3954b88c807SRodney W. Grimes 			sortby = SORTMEM;
396871e8d8cSMark Murray 			_fmt = 1;
3974b88c807SRodney W. Grimes 			vfmt[0] = '\0';
3984b88c807SRodney W. Grimes 			break;
3994b88c807SRodney W. Grimes 		case 'w':
4004b88c807SRodney W. Grimes 			if (wflag)
4014b88c807SRodney W. Grimes 				termwidth = UNLIMITED;
4024b88c807SRodney W. Grimes 			else if (termwidth < 131)
4034b88c807SRodney W. Grimes 				termwidth = 131;
4044b88c807SRodney W. Grimes 			wflag++;
4054b88c807SRodney W. Grimes 			break;
406a4c8a745SGarance A Drosehn 		case 'X':
407a4c8a745SGarance A Drosehn 			/*
408a4c8a745SGarance A Drosehn 			 * Note that `-X' and `-x' are not standard "selector"
409a4c8a745SGarance A Drosehn 			 * options. For most selector-options, we check *all*
410a4c8a745SGarance A Drosehn 			 * processes to see if any are matched by the given
411a4c8a745SGarance A Drosehn 			 * value(s).  After we have a set of all the matched
412a4c8a745SGarance A Drosehn 			 * processes, then `-X' and `-x' govern whether we
413a4c8a745SGarance A Drosehn 			 * modify that *matched* set for processes which do
414a4c8a745SGarance A Drosehn 			 * not have a controlling terminal.  `-X' causes
415a4c8a745SGarance A Drosehn 			 * those processes to be deleted from the matched
416a4c8a745SGarance A Drosehn 			 * set, while `-x' causes them to be kept.
417a4c8a745SGarance A Drosehn 			 */
418a4c8a745SGarance A Drosehn 			xkeep = 0;
419a4c8a745SGarance A Drosehn 			break;
4204b88c807SRodney W. Grimes 		case 'x':
421a4c8a745SGarance A Drosehn 			xkeep = 1;
4224b88c807SRodney W. Grimes 			break;
4237304f61fSBrian Feldman 		case 'Z':
424fde411d5SJuli Mallett 			parsefmt(Zfmt, 0);
4257304f61fSBrian Feldman 			Zfmt[0] = '\0';
4267304f61fSBrian Feldman 			break;
4274b88c807SRodney W. Grimes 		case '?':
4284b88c807SRodney W. Grimes 		default:
4294b88c807SRodney W. Grimes 			usage();
4304b88c807SRodney W. Grimes 		}
4314b88c807SRodney W. Grimes 	argc -= optind;
4324b88c807SRodney W. Grimes 	argv += optind;
433c675340aSGarance A Drosehn 
434c675340aSGarance A Drosehn 	/*
435c675340aSGarance A Drosehn 	 * If there arguments after processing all the options, attempt
436c675340aSGarance A Drosehn 	 * to treat them as a list of process ids.
437c675340aSGarance A Drosehn 	 */
438c675340aSGarance A Drosehn 	while (*argv) {
439c675340aSGarance A Drosehn 		if (!isdigitch(**argv))
440c675340aSGarance A Drosehn 			break;
441c675340aSGarance A Drosehn 		add_list(&pidlist, *argv);
442c675340aSGarance A Drosehn 		argv++;
443c675340aSGarance A Drosehn 	}
444c675340aSGarance A Drosehn 	if (*argv) {
4458beb1a2fSMarcel Moolenaar 		xo_warnx("illegal argument: %s\n", *argv);
446c675340aSGarance A Drosehn 		usage();
447c675340aSGarance A Drosehn 	}
448a4c8a745SGarance A Drosehn 	if (optfatal)
449352b6523SGarance A Drosehn 		exit(1);		/* Error messages already printed. */
450352b6523SGarance A Drosehn 	if (xkeep < 0)			/* Neither -X nor -x was specified. */
451a4c8a745SGarance A Drosehn 		xkeep = xkeep_implied;
452a4c8a745SGarance A Drosehn 
453831c910aSRuslan Ermilov 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
454ec23a763SMarcelo Araujo 	if (kd == NULL)
4558beb1a2fSMarcel Moolenaar 		xo_errx(1, "%s", errbuf);
4564b88c807SRodney W. Grimes 
457871e8d8cSMark Murray 	if (!_fmt)
458fde411d5SJuli Mallett 		parsefmt(dfmt, 0);
4594b88c807SRodney W. Grimes 
460a4c8a745SGarance A Drosehn 	if (nselectors == 0) {
461d822163fSGarance A Drosehn 		uidlist.l.ptr = malloc(sizeof(uid_t));
462d822163fSGarance A Drosehn 		if (uidlist.l.ptr == NULL)
4638beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
464a4c8a745SGarance A Drosehn 		nselectors = 1;
465a4c8a745SGarance A Drosehn 		uidlist.count = uidlist.maxcount = 1;
466d822163fSGarance A Drosehn 		*uidlist.l.uids = getuid();
467cf22dcfcSBrian Somers 	}
4684b88c807SRodney W. Grimes 
4694b88c807SRodney W. Grimes 	/*
4704b88c807SRodney W. Grimes 	 * scan requested variables, noting what structures are needed,
471bdfebd84SKris Kennaway 	 * and adjusting header widths as appropriate.
4724b88c807SRodney W. Grimes 	 */
4734b88c807SRodney W. Grimes 	scanvars();
474a4c8a745SGarance A Drosehn 
4754b88c807SRodney W. Grimes 	/*
476a4c8a745SGarance A Drosehn 	 * Get process list.  If the user requested just one selector-
477a4c8a745SGarance A Drosehn 	 * option, then kvm_getprocs can be asked to return just those
478a4c8a745SGarance A Drosehn 	 * processes.  Otherwise, have it return all processes, and
479a4c8a745SGarance A Drosehn 	 * then this routine will search that full list and select the
480a4c8a745SGarance A Drosehn 	 * processes which match any of the user's selector-options.
4814b88c807SRodney W. Grimes 	 */
482d75c1d83SDaniel Eischen 	what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC;
48348b8c0deSScott Long 	flag = 0;
484a4c8a745SGarance A Drosehn 	if (nselectors == 1) {
4857bd42165SGarance A Drosehn 		if (gidlist.count == 1) {
4867bd42165SGarance A Drosehn 			what = KERN_PROC_RGID | showthreads;
4877bd42165SGarance A Drosehn 			flag = *gidlist.l.gids;
4887bd42165SGarance A Drosehn 			nselectors = 0;
4897bd42165SGarance A Drosehn 		} else if (pgrplist.count == 1) {
490a4c8a745SGarance A Drosehn 			what = KERN_PROC_PGRP | showthreads;
491d822163fSGarance A Drosehn 			flag = *pgrplist.l.pids;
492a4c8a745SGarance A Drosehn 			nselectors = 0;
493a4c8a745SGarance A Drosehn 		} else if (pidlist.count == 1) {
494a4c8a745SGarance A Drosehn 			what = KERN_PROC_PID | showthreads;
495d822163fSGarance A Drosehn 			flag = *pidlist.l.pids;
496a4c8a745SGarance A Drosehn 			nselectors = 0;
497a4c8a745SGarance A Drosehn 		} else if (ruidlist.count == 1) {
498a4c8a745SGarance A Drosehn 			what = KERN_PROC_RUID | showthreads;
499d822163fSGarance A Drosehn 			flag = *ruidlist.l.uids;
500a4c8a745SGarance A Drosehn 			nselectors = 0;
501a4c8a745SGarance A Drosehn 		} else if (sesslist.count == 1) {
502a4c8a745SGarance A Drosehn 			what = KERN_PROC_SESSION | showthreads;
503d822163fSGarance A Drosehn 			flag = *sesslist.l.pids;
504a4c8a745SGarance A Drosehn 			nselectors = 0;
505a4c8a745SGarance A Drosehn 		} else if (ttylist.count == 1) {
506a4c8a745SGarance A Drosehn 			what = KERN_PROC_TTY | showthreads;
507d822163fSGarance A Drosehn 			flag = *ttylist.l.ttys;
508a4c8a745SGarance A Drosehn 			nselectors = 0;
509a4c8a745SGarance A Drosehn 		} else if (uidlist.count == 1) {
510a4c8a745SGarance A Drosehn 			what = KERN_PROC_UID | showthreads;
511d822163fSGarance A Drosehn 			flag = *uidlist.l.uids;
512a4c8a745SGarance A Drosehn 			nselectors = 0;
513a4c8a745SGarance A Drosehn 		} else if (all) {
514a4c8a745SGarance A Drosehn 			/* No need for this routine to select processes. */
515a4c8a745SGarance A Drosehn 			nselectors = 0;
516a4c8a745SGarance A Drosehn 		}
5174b88c807SRodney W. Grimes 	}
518d75c1d83SDaniel Eischen 
5194b88c807SRodney W. Grimes 	/*
5204b88c807SRodney W. Grimes 	 * select procs
5214b88c807SRodney W. Grimes 	 */
522a4c8a745SGarance A Drosehn 	nentries = -1;
5234e8b6a6fSGarance A Drosehn 	kp = kvm_getprocs(kd, what, flag, &nentries);
52487e47750SGarance A Drosehn 	if ((kp == NULL && nentries > 0) || (kp != NULL && nentries < 0))
5258beb1a2fSMarcel Moolenaar 		xo_errx(1, "%s", kvm_geterr(kd));
526a4c8a745SGarance A Drosehn 	nkept = 0;
5274e8b6a6fSGarance A Drosehn 	if (nentries > 0) {
5284b88c807SRodney W. Grimes 		if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL)
5298beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
5304b88c807SRodney W. Grimes 		for (i = nentries; --i >= 0; ++kp) {
531a4c8a745SGarance A Drosehn 			/*
532a4c8a745SGarance A Drosehn 			 * If the user specified multiple selection-criteria,
533a4c8a745SGarance A Drosehn 			 * then keep any process matched by the inclusive OR
534a4c8a745SGarance A Drosehn 			 * of all the selection-criteria given.
535a4c8a745SGarance A Drosehn 			 */
536a4c8a745SGarance A Drosehn 			if (pidlist.count > 0) {
537a4c8a745SGarance A Drosehn 				for (elem = 0; elem < pidlist.count; elem++)
538d822163fSGarance A Drosehn 					if (kp->ki_pid == pidlist.l.pids[elem])
539a4c8a745SGarance A Drosehn 						goto keepit;
540a4c8a745SGarance A Drosehn 			}
541a4c8a745SGarance A Drosehn 			/*
542a4c8a745SGarance A Drosehn 			 * Note that we had to process pidlist before
543a4c8a745SGarance A Drosehn 			 * filtering out processes which do not have
544a4c8a745SGarance A Drosehn 			 * a controlling terminal.
545a4c8a745SGarance A Drosehn 			 */
546a4c8a745SGarance A Drosehn 			if (xkeep == 0) {
547a4c8a745SGarance A Drosehn 				if ((kp->ki_tdev == NODEV ||
548a4c8a745SGarance A Drosehn 				    (kp->ki_flag & P_CONTROLT) == 0))
549a4c8a745SGarance A Drosehn 					continue;
550a4c8a745SGarance A Drosehn 			}
551a4c8a745SGarance A Drosehn 			if (nselectors == 0)
552a4c8a745SGarance A Drosehn 				goto keepit;
553a4c8a745SGarance A Drosehn 			if (gidlist.count > 0) {
554a4c8a745SGarance A Drosehn 				for (elem = 0; elem < gidlist.count; elem++)
555d822163fSGarance A Drosehn 					if (kp->ki_rgid == gidlist.l.gids[elem])
556a4c8a745SGarance A Drosehn 						goto keepit;
557a4c8a745SGarance A Drosehn 			}
55813767130SBryan Drewery 			if (jidlist.count > 0) {
55913767130SBryan Drewery 				for (elem = 0; elem < jidlist.count; elem++)
56013767130SBryan Drewery 					if (kp->ki_jid == jidlist.l.jids[elem])
56113767130SBryan Drewery 						goto keepit;
56213767130SBryan Drewery 			}
563a4c8a745SGarance A Drosehn 			if (pgrplist.count > 0) {
564a4c8a745SGarance A Drosehn 				for (elem = 0; elem < pgrplist.count; elem++)
565d822163fSGarance A Drosehn 					if (kp->ki_pgid ==
566d822163fSGarance A Drosehn 					    pgrplist.l.pids[elem])
567a4c8a745SGarance A Drosehn 						goto keepit;
568a4c8a745SGarance A Drosehn 			}
569a4c8a745SGarance A Drosehn 			if (ruidlist.count > 0) {
570a4c8a745SGarance A Drosehn 				for (elem = 0; elem < ruidlist.count; elem++)
571d822163fSGarance A Drosehn 					if (kp->ki_ruid ==
572d822163fSGarance A Drosehn 					    ruidlist.l.uids[elem])
573a4c8a745SGarance A Drosehn 						goto keepit;
574a4c8a745SGarance A Drosehn 			}
575a4c8a745SGarance A Drosehn 			if (sesslist.count > 0) {
576a4c8a745SGarance A Drosehn 				for (elem = 0; elem < sesslist.count; elem++)
577d822163fSGarance A Drosehn 					if (kp->ki_sid == sesslist.l.pids[elem])
578a4c8a745SGarance A Drosehn 						goto keepit;
579a4c8a745SGarance A Drosehn 			}
580a4c8a745SGarance A Drosehn 			if (ttylist.count > 0) {
581a4c8a745SGarance A Drosehn 				for (elem = 0; elem < ttylist.count; elem++)
582d822163fSGarance A Drosehn 					if (kp->ki_tdev == ttylist.l.ttys[elem])
583a4c8a745SGarance A Drosehn 						goto keepit;
584a4c8a745SGarance A Drosehn 			}
585a4c8a745SGarance A Drosehn 			if (uidlist.count > 0) {
586a4c8a745SGarance A Drosehn 				for (elem = 0; elem < uidlist.count; elem++)
587d822163fSGarance A Drosehn 					if (kp->ki_uid == uidlist.l.uids[elem])
588a4c8a745SGarance A Drosehn 						goto keepit;
589a4c8a745SGarance A Drosehn 			}
590a4c8a745SGarance A Drosehn 			/*
591a4c8a745SGarance A Drosehn 			 * This process did not match any of the user's
592a4c8a745SGarance A Drosehn 			 * selector-options, so skip the process.
593a4c8a745SGarance A Drosehn 			 */
594a4c8a745SGarance A Drosehn 			continue;
595a4c8a745SGarance A Drosehn 
596a4c8a745SGarance A Drosehn 		keepit:
5974bac4483SGarance A Drosehn 			next_KINFO = &kinfo[nkept];
5984bac4483SGarance A Drosehn 			next_KINFO->ki_p = kp;
599044fce53SBrian Somers 			next_KINFO->ki_d.level = 0;
600044fce53SBrian Somers 			next_KINFO->ki_d.prefix = NULL;
6014bac4483SGarance A Drosehn 			next_KINFO->ki_pcpu = getpcpu(next_KINFO);
6024bac4483SGarance A Drosehn 			if (sortby == SORTMEM)
6034bac4483SGarance A Drosehn 				next_KINFO->ki_memsize = kp->ki_tsize +
6044bac4483SGarance A Drosehn 				    kp->ki_dsize + kp->ki_ssize;
6054b88c807SRodney W. Grimes 			if (needuser)
6064bac4483SGarance A Drosehn 				saveuser(next_KINFO);
607a4c8a745SGarance A Drosehn 			nkept++;
6084b88c807SRodney W. Grimes 		}
6094e8b6a6fSGarance A Drosehn 	}
6106a2d726bSJordan K. Hubbard 
6116a2d726bSJordan K. Hubbard 	sizevars();
6126a2d726bSJordan K. Hubbard 
6131d1143ecSEdward Tomasz Napierala 	if (nkept == 0) {
6144b88c807SRodney W. Grimes 		printheader();
61581fc45fcSKonstantin Belousov 		xo_finish();
616f8c9c11cSWill Andrews 		exit(1);
6171d1143ecSEdward Tomasz Napierala 	}
618a4c8a745SGarance A Drosehn 
6194b88c807SRodney W. Grimes 	/*
6204b88c807SRodney W. Grimes 	 * sort proc list
6214b88c807SRodney W. Grimes 	 */
622a4c8a745SGarance A Drosehn 	qsort(kinfo, nkept, sizeof(KINFO), pscomp);
623044fce53SBrian Somers 
624044fce53SBrian Somers 	/*
625044fce53SBrian Somers 	 * We want things in descendant order
626044fce53SBrian Somers 	 */
627044fce53SBrian Somers 	if (descendancy)
628044fce53SBrian Somers 		descendant_sort(kinfo, nkept);
629044fce53SBrian Somers 
6301d1143ecSEdward Tomasz Napierala 
6314b88c807SRodney W. Grimes 	/*
6321d1143ecSEdward Tomasz Napierala 	 * Prepare formatted output.
6331d1143ecSEdward Tomasz Napierala 	 */
6341d1143ecSEdward Tomasz Napierala 	for (i = 0; i < nkept; i++)
6351d1143ecSEdward Tomasz Napierala 		format_output(&kinfo[i]);
6361d1143ecSEdward Tomasz Napierala 
6371d1143ecSEdward Tomasz Napierala 	/*
6381d1143ecSEdward Tomasz Napierala 	 * Print header.
6391d1143ecSEdward Tomasz Napierala 	 */
6408beb1a2fSMarcel Moolenaar 	xo_open_container("process-information");
6411d1143ecSEdward Tomasz Napierala 	printheader();
6428beb1a2fSMarcel Moolenaar 	if (xo_get_style(NULL) != XO_STYLE_TEXT)
6438beb1a2fSMarcel Moolenaar 		termwidth = UNLIMITED;
6441d1143ecSEdward Tomasz Napierala 
6451d1143ecSEdward Tomasz Napierala 	/*
6461d1143ecSEdward Tomasz Napierala 	 * Output formatted lines.
6474b88c807SRodney W. Grimes 	 */
6488beb1a2fSMarcel Moolenaar 	xo_open_list("process");
649a4c8a745SGarance A Drosehn 	for (i = lineno = 0; i < nkept; i++) {
6501d1143ecSEdward Tomasz Napierala 		linelen = 0;
6518beb1a2fSMarcel Moolenaar 		xo_open_instance("process");
652bdf8ab46SGarance A Drosehn 		STAILQ_FOREACH(vent, &varlist, next_ve) {
6531d1143ecSEdward Tomasz Napierala 			ks = STAILQ_FIRST(&kinfo[i].ki_ks);
6541d1143ecSEdward Tomasz Napierala 			STAILQ_REMOVE_HEAD(&kinfo[i].ki_ks, ks_next);
65538494effSUlrich Spörlein 			/* Truncate rightmost column if necessary.  */
6568beb1a2fSMarcel Moolenaar 			fwidthmax = _POSIX2_LINE_MAX;
6571d1143ecSEdward Tomasz Napierala 			if (STAILQ_NEXT(vent, next_ve) == NULL &&
6581d1143ecSEdward Tomasz Napierala 			   termwidth != UNLIMITED && ks->ks_str != NULL) {
6591d1143ecSEdward Tomasz Napierala 				left = termwidth - linelen;
6601d1143ecSEdward Tomasz Napierala 				if (left > 0 && left < (int)strlen(ks->ks_str))
6618beb1a2fSMarcel Moolenaar 					fwidthmax = left;
6621d1143ecSEdward Tomasz Napierala 			}
6638beb1a2fSMarcel Moolenaar 
6641d1143ecSEdward Tomasz Napierala 			str = ks->ks_str;
6651d1143ecSEdward Tomasz Napierala 			if (str == NULL)
6661d1143ecSEdward Tomasz Napierala 				str = "-";
6671d1143ecSEdward Tomasz Napierala 			/* No padding for the last column, if it's LJUST. */
6688beb1a2fSMarcel Moolenaar 			fwidthmin = (xo_get_style(NULL) != XO_STYLE_TEXT ||
6698beb1a2fSMarcel Moolenaar 			    (STAILQ_NEXT(vent, next_ve) == NULL &&
6708beb1a2fSMarcel Moolenaar 			    (vent->var->flag & LJUST))) ? 0 : vent->var->width;
6718beb1a2fSMarcel Moolenaar 			snprintf(fmtbuf, sizeof(fmtbuf), "{:%s/%%%s%d..%ds}",
6728beb1a2fSMarcel Moolenaar 			    vent->var->field ?: vent->var->name,
6738beb1a2fSMarcel Moolenaar 			    (vent->var->flag & LJUST) ? "-" : "",
6748beb1a2fSMarcel Moolenaar 			    fwidthmin, fwidthmax);
6758beb1a2fSMarcel Moolenaar 			xo_emit(fmtbuf, str);
6768beb1a2fSMarcel Moolenaar 			linelen += fwidthmin;
6771d1143ecSEdward Tomasz Napierala 
6781d1143ecSEdward Tomasz Napierala 			if (ks->ks_str != NULL) {
6791d1143ecSEdward Tomasz Napierala 				free(ks->ks_str);
6801d1143ecSEdward Tomasz Napierala 				ks->ks_str = NULL;
6811d1143ecSEdward Tomasz Napierala 			}
6821d1143ecSEdward Tomasz Napierala 			free(ks);
6831d1143ecSEdward Tomasz Napierala 			ks = NULL;
6841d1143ecSEdward Tomasz Napierala 
6851d1143ecSEdward Tomasz Napierala 			if (STAILQ_NEXT(vent, next_ve) != NULL) {
6868beb1a2fSMarcel Moolenaar 				xo_emit("{P: }");
6871d1143ecSEdward Tomasz Napierala 				linelen++;
6881d1143ecSEdward Tomasz Napierala 			}
6894b88c807SRodney W. Grimes 		}
6908beb1a2fSMarcel Moolenaar 	        xo_emit("\n");
6918beb1a2fSMarcel Moolenaar 		xo_close_instance("process");
6924b88c807SRodney W. Grimes 		if (prtheader && lineno++ == prtheader - 4) {
6938beb1a2fSMarcel Moolenaar 			xo_emit("\n");
6944b88c807SRodney W. Grimes 			printheader();
6954b88c807SRodney W. Grimes 			lineno = 0;
6964b88c807SRodney W. Grimes 		}
6974b88c807SRodney W. Grimes 	}
6988beb1a2fSMarcel Moolenaar 	xo_close_list("process");
6998beb1a2fSMarcel Moolenaar 	xo_close_container("process-information");
7008beb1a2fSMarcel Moolenaar 	xo_finish();
7018beb1a2fSMarcel Moolenaar 
702a4c8a745SGarance A Drosehn 	free_list(&gidlist);
70313767130SBryan Drewery 	free_list(&jidlist);
704a4c8a745SGarance A Drosehn 	free_list(&pidlist);
705a4c8a745SGarance A Drosehn 	free_list(&pgrplist);
706a4c8a745SGarance A Drosehn 	free_list(&ruidlist);
707a4c8a745SGarance A Drosehn 	free_list(&sesslist);
708a4c8a745SGarance A Drosehn 	free_list(&ttylist);
709a4c8a745SGarance A Drosehn 	free_list(&uidlist);
710044fce53SBrian Somers 	for (i = 0; i < nkept; i++)
711044fce53SBrian Somers 		free(kinfo[i].ki_d.prefix);
712044fce53SBrian Somers 	free(kinfo);
7132e6c6ac4SGarance A Drosehn 
7144b88c807SRodney W. Grimes 	exit(eval);
7154b88c807SRodney W. Grimes }
7164b88c807SRodney W. Grimes 
717a4c8a745SGarance A Drosehn static int
718a4c8a745SGarance A Drosehn addelem_gid(struct listinfo *inf, const char *elem)
7194e8b6a6fSGarance A Drosehn {
720a4c8a745SGarance A Drosehn 	struct group *grp;
721a4c8a745SGarance A Drosehn 	const char *nameorID;
722a4c8a745SGarance A Drosehn 	char *endp;
7230b42be7cSGarance A Drosehn 	u_long bigtemp;
7244e8b6a6fSGarance A Drosehn 
725a4c8a745SGarance A Drosehn 	if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
726a4c8a745SGarance A Drosehn 		if (*elem == '\0')
7278beb1a2fSMarcel Moolenaar 			xo_warnx("Invalid (zero-length) %s name", inf->lname);
728a4c8a745SGarance A Drosehn 		else
7298beb1a2fSMarcel Moolenaar 			xo_warnx("%s name too long: %s", inf->lname, elem);
730a4c8a745SGarance A Drosehn 		optfatal = 1;
731352b6523SGarance A Drosehn 		return (0);		/* Do not add this value. */
7324e8b6a6fSGarance A Drosehn 	}
733a4c8a745SGarance A Drosehn 
7344e8b6a6fSGarance A Drosehn 	/*
735a4c8a745SGarance A Drosehn 	 * SUSv3 states that `ps -G grouplist' should match "real-group
736a4c8a745SGarance A Drosehn 	 * ID numbers", and does not mention group-names.  I do want to
737a4c8a745SGarance A Drosehn 	 * also support group-names, so this tries for a group-id first,
738a4c8a745SGarance A Drosehn 	 * and only tries for a name if that doesn't work.  This is the
739a4c8a745SGarance A Drosehn 	 * opposite order of what is done in addelem_uid(), but in
740a4c8a745SGarance A Drosehn 	 * practice the order would only matter for group-names which
741a4c8a745SGarance A Drosehn 	 * are all-numeric.
7424e8b6a6fSGarance A Drosehn 	 */
743a4c8a745SGarance A Drosehn 	grp = NULL;
744a4c8a745SGarance A Drosehn 	nameorID = "named";
745a4c8a745SGarance A Drosehn 	errno = 0;
7460b42be7cSGarance A Drosehn 	bigtemp = strtoul(elem, &endp, 10);
7470b42be7cSGarance A Drosehn 	if (errno == 0 && *endp == '\0' && bigtemp <= GID_MAX) {
748a4c8a745SGarance A Drosehn 		nameorID = "name or ID matches";
7490b42be7cSGarance A Drosehn 		grp = getgrgid((gid_t)bigtemp);
750a4c8a745SGarance A Drosehn 	}
751a4c8a745SGarance A Drosehn 	if (grp == NULL)
752a4c8a745SGarance A Drosehn 		grp = getgrnam(elem);
753a4c8a745SGarance A Drosehn 	if (grp == NULL) {
7548beb1a2fSMarcel Moolenaar 		xo_warnx("No %s %s '%s'", inf->lname, nameorID, elem);
755a4c8a745SGarance A Drosehn 		optfatal = 1;
756c23b00b7SGarance A Drosehn 		return (0);
757a4c8a745SGarance A Drosehn 	}
758a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
759a4c8a745SGarance A Drosehn 		expand_list(inf);
760d822163fSGarance A Drosehn 	inf->l.gids[(inf->count)++] = grp->gr_gid;
761a4c8a745SGarance A Drosehn 	return (1);
762a4c8a745SGarance A Drosehn }
763a4c8a745SGarance A Drosehn 
764a4c8a745SGarance A Drosehn static int
76513767130SBryan Drewery addelem_jid(struct listinfo *inf, const char *elem)
76613767130SBryan Drewery {
76713767130SBryan Drewery 	int tempid;
76813767130SBryan Drewery 
76913767130SBryan Drewery 	if (*elem == '\0') {
77013767130SBryan Drewery 		warnx("Invalid (zero-length) jail id");
77113767130SBryan Drewery 		optfatal = 1;
77213767130SBryan Drewery 		return (0);		/* Do not add this value. */
77313767130SBryan Drewery 	}
77413767130SBryan Drewery 
77513767130SBryan Drewery 	tempid = jail_getid(elem);
77613767130SBryan Drewery 	if (tempid < 0) {
77713767130SBryan Drewery 		warnx("Invalid %s: %s", inf->lname, elem);
77813767130SBryan Drewery 		optfatal = 1;
77913767130SBryan Drewery 		return (0);
78013767130SBryan Drewery 	}
78113767130SBryan Drewery 
78213767130SBryan Drewery 	if (inf->count >= inf->maxcount)
78313767130SBryan Drewery 		expand_list(inf);
78413767130SBryan Drewery 	inf->l.jids[(inf->count)++] = tempid;
78513767130SBryan Drewery 	return (1);
78613767130SBryan Drewery }
78713767130SBryan Drewery 
78813767130SBryan Drewery static int
789a4c8a745SGarance A Drosehn addelem_pid(struct listinfo *inf, const char *elem)
790a4c8a745SGarance A Drosehn {
791a4c8a745SGarance A Drosehn 	char *endp;
792ca62e195SGarance A Drosehn 	long tempid;
793a4c8a745SGarance A Drosehn 
794de1702f4SGarance A Drosehn 	if (*elem == '\0') {
7958beb1a2fSMarcel Moolenaar 		xo_warnx("Invalid (zero-length) process id");
796de1702f4SGarance A Drosehn 		optfatal = 1;
797de1702f4SGarance A Drosehn 		return (0);		/* Do not add this value. */
79825113083SGarance A Drosehn 	}
79925113083SGarance A Drosehn 
800a4c8a745SGarance A Drosehn 	errno = 0;
801a4c8a745SGarance A Drosehn 	tempid = strtol(elem, &endp, 10);
802a4c8a745SGarance A Drosehn 	if (*endp != '\0' || tempid < 0 || elem == endp) {
8038beb1a2fSMarcel Moolenaar 		xo_warnx("Invalid %s: %s", inf->lname, elem);
8044e8b6a6fSGarance A Drosehn 		errno = ERANGE;
80564b0bf0bSPawel Jakub Dawidek 	} else if (errno != 0 || tempid > pid_max) {
8068beb1a2fSMarcel Moolenaar 		xo_warnx("%s too large: %s", inf->lname, elem);
8074e8b6a6fSGarance A Drosehn 		errno = ERANGE;
8084e8b6a6fSGarance A Drosehn 	}
8094e8b6a6fSGarance A Drosehn 	if (errno == ERANGE) {
810a4c8a745SGarance A Drosehn 		optfatal = 1;
811c23b00b7SGarance A Drosehn 		return (0);
8124e8b6a6fSGarance A Drosehn 	}
813a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
814a4c8a745SGarance A Drosehn 		expand_list(inf);
815d822163fSGarance A Drosehn 	inf->l.pids[(inf->count)++] = tempid;
816a4c8a745SGarance A Drosehn 	return (1);
8174e8b6a6fSGarance A Drosehn }
8184e8b6a6fSGarance A Drosehn 
81923b8efadSGarance A Drosehn /*-
82023b8efadSGarance A Drosehn  * The user can specify a device via one of three formats:
8218a529f59SJohn Baldwin  *     1) fully qualified, e.g.:     /dev/ttyp0 /dev/console	/dev/pts/0
8228a529f59SJohn Baldwin  *     2) missing "/dev", e.g.:      ttyp0      console		pts/0
8238a529f59SJohn Baldwin  *     3) two-letters, e.g.:         p0         co		0
82423b8efadSGarance A Drosehn  *        (matching letters that would be seen in the "TT" column)
82523b8efadSGarance A Drosehn  */
826a4c8a745SGarance A Drosehn static int
827a4c8a745SGarance A Drosehn addelem_tty(struct listinfo *inf, const char *elem)
828cf22dcfcSBrian Somers {
829a4c8a745SGarance A Drosehn 	const char *ttypath;
830ca62e195SGarance A Drosehn 	struct stat sb;
8318a529f59SJohn Baldwin 	char pathbuf[PATH_MAX], pathbuf2[PATH_MAX], pathbuf3[PATH_MAX];
832a4c8a745SGarance A Drosehn 
83323b8efadSGarance A Drosehn 	ttypath = NULL;
834f2fbfae6SGarance A Drosehn 	pathbuf2[0] = '\0';
8358a529f59SJohn Baldwin 	pathbuf3[0] = '\0';
83623b8efadSGarance A Drosehn 	switch (*elem) {
83723b8efadSGarance A Drosehn 	case '/':
838a4c8a745SGarance A Drosehn 		ttypath = elem;
83923b8efadSGarance A Drosehn 		break;
84023b8efadSGarance A Drosehn 	case 'c':
84123b8efadSGarance A Drosehn 		if (strcmp(elem, "co") == 0) {
84223b8efadSGarance A Drosehn 			ttypath = _PATH_CONSOLE;
84323b8efadSGarance A Drosehn 			break;
84423b8efadSGarance A Drosehn 		}
84523b8efadSGarance A Drosehn 		/* FALLTHROUGH */
84623b8efadSGarance A Drosehn 	default:
84723b8efadSGarance A Drosehn 		strlcpy(pathbuf, _PATH_DEV, sizeof(pathbuf));
848a4c8a745SGarance A Drosehn 		strlcat(pathbuf, elem, sizeof(pathbuf));
849a4c8a745SGarance A Drosehn 		ttypath = pathbuf;
850f2fbfae6SGarance A Drosehn 		if (strncmp(pathbuf, _PATH_TTY, strlen(_PATH_TTY)) == 0)
85123b8efadSGarance A Drosehn 			break;
8528a529f59SJohn Baldwin 		if (strncmp(pathbuf, _PATH_PTS, strlen(_PATH_PTS)) == 0)
8538a529f59SJohn Baldwin 			break;
85423b8efadSGarance A Drosehn 		if (strcmp(pathbuf, _PATH_CONSOLE) == 0)
85523b8efadSGarance A Drosehn 			break;
856f2fbfae6SGarance A Drosehn 		/* Check to see if /dev/tty${elem} exists */
857f2fbfae6SGarance A Drosehn 		strlcpy(pathbuf2, _PATH_TTY, sizeof(pathbuf2));
858f2fbfae6SGarance A Drosehn 		strlcat(pathbuf2, elem, sizeof(pathbuf2));
859f2fbfae6SGarance A Drosehn 		if (stat(pathbuf2, &sb) == 0 && S_ISCHR(sb.st_mode)) {
86023b8efadSGarance A Drosehn 			/* No need to repeat stat() && S_ISCHR() checks */
86123b8efadSGarance A Drosehn 			ttypath = NULL;
86223b8efadSGarance A Drosehn 			break;
863a4c8a745SGarance A Drosehn 		}
8648a529f59SJohn Baldwin 		/* Check to see if /dev/pts/${elem} exists */
8658a529f59SJohn Baldwin 		strlcpy(pathbuf3, _PATH_PTS, sizeof(pathbuf3));
8668a529f59SJohn Baldwin 		strlcat(pathbuf3, elem, sizeof(pathbuf3));
8678a529f59SJohn Baldwin 		if (stat(pathbuf3, &sb) == 0 && S_ISCHR(sb.st_mode)) {
8688a529f59SJohn Baldwin 			/* No need to repeat stat() && S_ISCHR() checks */
8698a529f59SJohn Baldwin 			ttypath = NULL;
8708a529f59SJohn Baldwin 			break;
8718a529f59SJohn Baldwin 		}
87223b8efadSGarance A Drosehn 		break;
87323b8efadSGarance A Drosehn 	}
87423b8efadSGarance A Drosehn 	if (ttypath) {
875a4c8a745SGarance A Drosehn 		if (stat(ttypath, &sb) == -1) {
8768a529f59SJohn Baldwin 			if (pathbuf3[0] != '\0')
8778beb1a2fSMarcel Moolenaar 				xo_warn("%s, %s, and %s", pathbuf3, pathbuf2,
8788a529f59SJohn Baldwin 				    ttypath);
879f2fbfae6SGarance A Drosehn 			else
8808beb1a2fSMarcel Moolenaar 				xo_warn("%s", ttypath);
881a4c8a745SGarance A Drosehn 			optfatal = 1;
882c23b00b7SGarance A Drosehn 			return (0);
883a4c8a745SGarance A Drosehn 		}
884a4c8a745SGarance A Drosehn 		if (!S_ISCHR(sb.st_mode)) {
8858a529f59SJohn Baldwin 			if (pathbuf3[0] != '\0')
8868beb1a2fSMarcel Moolenaar 				xo_warnx("%s, %s, and %s: Not a terminal",
8878a529f59SJohn Baldwin 				    pathbuf3, pathbuf2, ttypath);
888f2fbfae6SGarance A Drosehn 			else
8898beb1a2fSMarcel Moolenaar 				xo_warnx("%s: Not a terminal", ttypath);
890a4c8a745SGarance A Drosehn 			optfatal = 1;
891c23b00b7SGarance A Drosehn 			return (0);
892a4c8a745SGarance A Drosehn 		}
89323b8efadSGarance A Drosehn 	}
894a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
895a4c8a745SGarance A Drosehn 		expand_list(inf);
896d822163fSGarance A Drosehn 	inf->l.ttys[(inf->count)++] = sb.st_rdev;
897a4c8a745SGarance A Drosehn 	return (1);
898a4c8a745SGarance A Drosehn }
899a4c8a745SGarance A Drosehn 
900a4c8a745SGarance A Drosehn static int
901a4c8a745SGarance A Drosehn addelem_uid(struct listinfo *inf, const char *elem)
902a4c8a745SGarance A Drosehn {
903cf22dcfcSBrian Somers 	struct passwd *pwd;
904a4c8a745SGarance A Drosehn 	char *endp;
9050b42be7cSGarance A Drosehn 	u_long bigtemp;
906cf22dcfcSBrian Somers 
907a4c8a745SGarance A Drosehn 	if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
908a4c8a745SGarance A Drosehn 		if (*elem == '\0')
9098beb1a2fSMarcel Moolenaar 			xo_warnx("Invalid (zero-length) %s name", inf->lname);
910a4c8a745SGarance A Drosehn 		else
9118beb1a2fSMarcel Moolenaar 			xo_warnx("%s name too long: %s", inf->lname, elem);
912a4c8a745SGarance A Drosehn 		optfatal = 1;
913352b6523SGarance A Drosehn 		return (0);		/* Do not add this value. */
914a4c8a745SGarance A Drosehn 	}
915cf22dcfcSBrian Somers 
916a4c8a745SGarance A Drosehn 	pwd = getpwnam(elem);
917a4c8a745SGarance A Drosehn 	if (pwd == NULL) {
918a4c8a745SGarance A Drosehn 		errno = 0;
9190b42be7cSGarance A Drosehn 		bigtemp = strtoul(elem, &endp, 10);
9200b42be7cSGarance A Drosehn 		if (errno != 0 || *endp != '\0' || bigtemp > UID_MAX)
9218beb1a2fSMarcel Moolenaar 			xo_warnx("No %s named '%s'", inf->lname, elem);
922a4c8a745SGarance A Drosehn 		else {
923a4c8a745SGarance A Drosehn 			/* The string is all digits, so it might be a userID. */
9240b42be7cSGarance A Drosehn 			pwd = getpwuid((uid_t)bigtemp);
925a4c8a745SGarance A Drosehn 			if (pwd == NULL)
9268beb1a2fSMarcel Moolenaar 				xo_warnx("No %s name or ID matches '%s'",
927a4c8a745SGarance A Drosehn 				    inf->lname, elem);
928cf22dcfcSBrian Somers 		}
929cf22dcfcSBrian Somers 	}
930a4c8a745SGarance A Drosehn 	if (pwd == NULL) {
931e3c4e1ddSGarance A Drosehn 		/*
932e3c4e1ddSGarance A Drosehn 		 * These used to be treated as minor warnings (and the
933e3c4e1ddSGarance A Drosehn 		 * option was simply ignored), but now they are fatal
934e3c4e1ddSGarance A Drosehn 		 * errors (and the command will be aborted).
935e3c4e1ddSGarance A Drosehn 		 */
936e3c4e1ddSGarance A Drosehn 		optfatal = 1;
937c23b00b7SGarance A Drosehn 		return (0);
938cf22dcfcSBrian Somers 	}
939a4c8a745SGarance A Drosehn 	if (inf->count >= inf->maxcount)
940a4c8a745SGarance A Drosehn 		expand_list(inf);
941d822163fSGarance A Drosehn 	inf->l.uids[(inf->count)++] = pwd->pw_uid;
942a4c8a745SGarance A Drosehn 	return (1);
943a4c8a745SGarance A Drosehn }
944cf22dcfcSBrian Somers 
945a4c8a745SGarance A Drosehn static void
946a4c8a745SGarance A Drosehn add_list(struct listinfo *inf, const char *argp)
947a4c8a745SGarance A Drosehn {
948a4c8a745SGarance A Drosehn 	const char *savep;
949a4c8a745SGarance A Drosehn 	char *cp, *endp;
950a4c8a745SGarance A Drosehn 	int toolong;
951ca62e195SGarance A Drosehn 	char elemcopy[PATH_MAX];
952a4c8a745SGarance A Drosehn 
95342856668SEd Maste 	if (*argp == '\0')
95442856668SEd Maste 		inf->addelem(inf, argp);
955a4c8a745SGarance A Drosehn 	while (*argp != '\0') {
956a4c8a745SGarance A Drosehn 		while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
957a4c8a745SGarance A Drosehn 			argp++;
958a4c8a745SGarance A Drosehn 		savep = argp;
959a4c8a745SGarance A Drosehn 		toolong = 0;
960a4c8a745SGarance A Drosehn 		cp = elemcopy;
961a4c8a745SGarance A Drosehn 		if (strchr(T_SEP, *argp) == NULL) {
962a4c8a745SGarance A Drosehn 			endp = elemcopy + sizeof(elemcopy) - 1;
963a4c8a745SGarance A Drosehn 			while (*argp != '\0' && cp <= endp &&
964a4c8a745SGarance A Drosehn 			    strchr(W_SEP T_SEP, *argp) == NULL)
965a4c8a745SGarance A Drosehn 				*cp++ = *argp++;
966a4c8a745SGarance A Drosehn 			if (cp > endp)
967a4c8a745SGarance A Drosehn 				toolong = 1;
968a4c8a745SGarance A Drosehn 		}
969a4c8a745SGarance A Drosehn 		if (!toolong) {
970a4c8a745SGarance A Drosehn 			*cp = '\0';
971352b6523SGarance A Drosehn 			/*
972a9626fb3SGarance A Drosehn 			 * Add this single element to the given list.
973352b6523SGarance A Drosehn 			 */
974a4c8a745SGarance A Drosehn 			inf->addelem(inf, elemcopy);
975a4c8a745SGarance A Drosehn 		} else {
976a4c8a745SGarance A Drosehn 			/*
977a4c8a745SGarance A Drosehn 			 * The string is too long to copy.  Find the end
978a4c8a745SGarance A Drosehn 			 * of the string to print out the warning message.
979a4c8a745SGarance A Drosehn 			 */
980a4c8a745SGarance A Drosehn 			while (*argp != '\0' && strchr(W_SEP T_SEP,
981a4c8a745SGarance A Drosehn 			    *argp) == NULL)
982a4c8a745SGarance A Drosehn 				argp++;
9838beb1a2fSMarcel Moolenaar 			xo_warnx("Value too long: %.*s", (int)(argp - savep),
984a4c8a745SGarance A Drosehn 			    savep);
985a4c8a745SGarance A Drosehn 			optfatal = 1;
986a4c8a745SGarance A Drosehn 		}
987a4c8a745SGarance A Drosehn 		/*
988a4c8a745SGarance A Drosehn 		 * Skip over any number of trailing whitespace characters,
989a4c8a745SGarance A Drosehn 		 * but only one (at most) trailing element-terminating
990a4c8a745SGarance A Drosehn 		 * character.
991a4c8a745SGarance A Drosehn 		 */
992a4c8a745SGarance A Drosehn 		while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
993a4c8a745SGarance A Drosehn 			argp++;
994a4c8a745SGarance A Drosehn 		if (*argp != '\0' && strchr(T_SEP, *argp) != NULL) {
995a4c8a745SGarance A Drosehn 			argp++;
996a4c8a745SGarance A Drosehn 			/* Catch case where string ended with a comma. */
997a4c8a745SGarance A Drosehn 			if (*argp == '\0')
998a4c8a745SGarance A Drosehn 				inf->addelem(inf, argp);
999a4c8a745SGarance A Drosehn 		}
1000a4c8a745SGarance A Drosehn 	}
1001a4c8a745SGarance A Drosehn }
1002a4c8a745SGarance A Drosehn 
1003044fce53SBrian Somers static void
1004044fce53SBrian Somers descendant_sort(KINFO *ki, int items)
1005044fce53SBrian Somers {
1006044fce53SBrian Somers 	int dst, lvl, maxlvl, n, ndst, nsrc, siblings, src;
1007044fce53SBrian Somers 	unsigned char *path;
1008044fce53SBrian Somers 	KINFO kn;
1009044fce53SBrian Somers 
1010044fce53SBrian Somers 	/*
1011044fce53SBrian Somers 	 * First, sort the entries by descendancy, tracking the descendancy
1012044fce53SBrian Somers 	 * depth in the ki_d.level field.
1013044fce53SBrian Somers 	 */
1014044fce53SBrian Somers 	src = 0;
1015044fce53SBrian Somers 	maxlvl = 0;
1016044fce53SBrian Somers 	while (src < items) {
1017044fce53SBrian Somers 		if (ki[src].ki_d.level) {
1018044fce53SBrian Somers 			src++;
1019044fce53SBrian Somers 			continue;
1020044fce53SBrian Somers 		}
1021044fce53SBrian Somers 		for (nsrc = 1; src + nsrc < items; nsrc++)
1022044fce53SBrian Somers 			if (!ki[src + nsrc].ki_d.level)
1023044fce53SBrian Somers 				break;
1024044fce53SBrian Somers 
1025044fce53SBrian Somers 		for (dst = 0; dst < items; dst++) {
1026044fce53SBrian Somers 			if (ki[dst].ki_p->ki_pid == ki[src].ki_p->ki_pid)
1027044fce53SBrian Somers 				continue;
1028044fce53SBrian Somers 			if (ki[dst].ki_p->ki_pid == ki[src].ki_p->ki_ppid)
1029044fce53SBrian Somers 				break;
1030044fce53SBrian Somers 		}
1031044fce53SBrian Somers 
1032044fce53SBrian Somers 		if (dst == items) {
1033044fce53SBrian Somers 			src += nsrc;
1034044fce53SBrian Somers 			continue;
1035044fce53SBrian Somers 		}
1036044fce53SBrian Somers 
1037044fce53SBrian Somers 		for (ndst = 1; dst + ndst < items; ndst++)
1038044fce53SBrian Somers 			if (ki[dst + ndst].ki_d.level <= ki[dst].ki_d.level)
1039044fce53SBrian Somers 				break;
1040044fce53SBrian Somers 
1041044fce53SBrian Somers 		for (n = src; n < src + nsrc; n++) {
1042044fce53SBrian Somers 			ki[n].ki_d.level += ki[dst].ki_d.level + 1;
1043044fce53SBrian Somers 			if (maxlvl < ki[n].ki_d.level)
1044044fce53SBrian Somers 				maxlvl = ki[n].ki_d.level;
1045044fce53SBrian Somers 		}
1046044fce53SBrian Somers 
1047044fce53SBrian Somers 		while (nsrc) {
1048044fce53SBrian Somers 			if (src < dst) {
1049044fce53SBrian Somers 				kn = ki[src];
1050044fce53SBrian Somers 				memmove(ki + src, ki + src + 1,
1051044fce53SBrian Somers 				    (dst - src + ndst - 1) * sizeof *ki);
1052044fce53SBrian Somers 				ki[dst + ndst - 1] = kn;
1053044fce53SBrian Somers 				nsrc--;
1054044fce53SBrian Somers 				dst--;
1055044fce53SBrian Somers 				ndst++;
1056044fce53SBrian Somers 			} else if (src != dst + ndst) {
1057044fce53SBrian Somers 				kn = ki[src];
1058044fce53SBrian Somers 				memmove(ki + dst + ndst + 1, ki + dst + ndst,
1059044fce53SBrian Somers 				    (src - dst - ndst) * sizeof *ki);
1060044fce53SBrian Somers 				ki[dst + ndst] = kn;
1061044fce53SBrian Somers 				ndst++;
1062044fce53SBrian Somers 				nsrc--;
1063044fce53SBrian Somers 				src++;
1064044fce53SBrian Somers 			} else {
1065044fce53SBrian Somers 				ndst += nsrc;
1066044fce53SBrian Somers 				src += nsrc;
1067044fce53SBrian Somers 				nsrc = 0;
1068044fce53SBrian Somers 			}
1069044fce53SBrian Somers 		}
1070044fce53SBrian Somers 	}
1071044fce53SBrian Somers 
1072044fce53SBrian Somers 	/*
1073044fce53SBrian Somers 	 * Now populate ki_d.prefix (instead of ki_d.level) with the command
1074044fce53SBrian Somers 	 * prefix used to show descendancies.
1075044fce53SBrian Somers 	 */
1076044fce53SBrian Somers 	path = malloc((maxlvl + 7) / 8);
1077044fce53SBrian Somers 	memset(path, '\0', (maxlvl + 7) / 8);
1078044fce53SBrian Somers 	for (src = 0; src < items; src++) {
1079044fce53SBrian Somers 		if ((lvl = ki[src].ki_d.level) == 0) {
1080044fce53SBrian Somers 			ki[src].ki_d.prefix = NULL;
1081044fce53SBrian Somers 			continue;
1082044fce53SBrian Somers 		}
1083044fce53SBrian Somers 		if ((ki[src].ki_d.prefix = malloc(lvl * 2 + 1)) == NULL)
10848beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
1085044fce53SBrian Somers 		for (n = 0; n < lvl - 2; n++) {
1086044fce53SBrian Somers 			ki[src].ki_d.prefix[n * 2] =
1087044fce53SBrian Somers 			    path[n / 8] & 1 << (n % 8) ? '|' : ' ';
1088044fce53SBrian Somers 			ki[src].ki_d.prefix[n * 2 + 1] = ' ';
1089044fce53SBrian Somers 		}
1090044fce53SBrian Somers 		if (n == lvl - 2) {
1091044fce53SBrian Somers 			/* Have I any more siblings? */
1092044fce53SBrian Somers 			for (siblings = 0, dst = src + 1; dst < items; dst++) {
1093044fce53SBrian Somers 				if (ki[dst].ki_d.level > lvl)
1094044fce53SBrian Somers 					continue;
1095044fce53SBrian Somers 				if (ki[dst].ki_d.level == lvl)
1096044fce53SBrian Somers 					siblings = 1;
1097044fce53SBrian Somers 				break;
1098044fce53SBrian Somers 			}
1099044fce53SBrian Somers 			if (siblings)
1100044fce53SBrian Somers 				path[n / 8] |= 1 << (n % 8);
1101044fce53SBrian Somers 			else
1102044fce53SBrian Somers 				path[n / 8] &= ~(1 << (n % 8));
1103044fce53SBrian Somers 			ki[src].ki_d.prefix[n * 2] = siblings ? '|' : '`';
1104044fce53SBrian Somers 			ki[src].ki_d.prefix[n * 2 + 1] = '-';
1105044fce53SBrian Somers 			n++;
1106044fce53SBrian Somers 		}
1107044fce53SBrian Somers 		strcpy(ki[src].ki_d.prefix + n * 2, "- ");
1108044fce53SBrian Somers 	}
1109044fce53SBrian Somers 	free(path);
1110044fce53SBrian Somers }
1111044fce53SBrian Somers 
1112a4c8a745SGarance A Drosehn static void *
1113a4c8a745SGarance A Drosehn expand_list(struct listinfo *inf)
1114a4c8a745SGarance A Drosehn {
1115a4c8a745SGarance A Drosehn 	void *newlist;
1116ca62e195SGarance A Drosehn 	int newmax;
1117a4c8a745SGarance A Drosehn 
1118a4c8a745SGarance A Drosehn 	newmax = (inf->maxcount + 1) << 1;
1119d822163fSGarance A Drosehn 	newlist = realloc(inf->l.ptr, newmax * inf->elemsize);
1120a4c8a745SGarance A Drosehn 	if (newlist == NULL) {
1121d822163fSGarance A Drosehn 		free(inf->l.ptr);
11228beb1a2fSMarcel Moolenaar 		xo_errx(1, "realloc to %d %ss failed", newmax, inf->lname);
1123a4c8a745SGarance A Drosehn 	}
1124a4c8a745SGarance A Drosehn 	inf->maxcount = newmax;
1125d822163fSGarance A Drosehn 	inf->l.ptr = newlist;
1126a4c8a745SGarance A Drosehn 
1127a4c8a745SGarance A Drosehn 	return (newlist);
1128a4c8a745SGarance A Drosehn }
1129a4c8a745SGarance A Drosehn 
1130a4c8a745SGarance A Drosehn static void
1131a4c8a745SGarance A Drosehn free_list(struct listinfo *inf)
1132a4c8a745SGarance A Drosehn {
1133a4c8a745SGarance A Drosehn 
1134a4c8a745SGarance A Drosehn 	inf->count = inf->elemsize = inf->maxcount = 0;
1135d822163fSGarance A Drosehn 	if (inf->l.ptr != NULL)
1136d822163fSGarance A Drosehn 		free(inf->l.ptr);
1137a4c8a745SGarance A Drosehn 	inf->addelem = NULL;
1138a4c8a745SGarance A Drosehn 	inf->lname = NULL;
1139d822163fSGarance A Drosehn 	inf->l.ptr = NULL;
1140a4c8a745SGarance A Drosehn }
1141a4c8a745SGarance A Drosehn 
1142a4c8a745SGarance A Drosehn static void
1143a4c8a745SGarance A Drosehn init_list(struct listinfo *inf, addelem_rtn artn, int elemsize,
1144a4c8a745SGarance A Drosehn     const char *lname)
1145a4c8a745SGarance A Drosehn {
1146a4c8a745SGarance A Drosehn 
1147a4c8a745SGarance A Drosehn 	inf->count = inf->maxcount = 0;
1148a4c8a745SGarance A Drosehn 	inf->elemsize = elemsize;
1149a4c8a745SGarance A Drosehn 	inf->addelem = artn;
1150a4c8a745SGarance A Drosehn 	inf->lname = lname;
1151d822163fSGarance A Drosehn 	inf->l.ptr = NULL;
1152cf22dcfcSBrian Somers }
1153cf22dcfcSBrian Somers 
1154fde411d5SJuli Mallett VARENT *
1155fde411d5SJuli Mallett find_varentry(VAR *v)
1156fde411d5SJuli Mallett {
1157fde411d5SJuli Mallett 	struct varent *vent;
1158fde411d5SJuli Mallett 
1159bdf8ab46SGarance A Drosehn 	STAILQ_FOREACH(vent, &varlist, next_ve) {
1160fde411d5SJuli Mallett 		if (strcmp(vent->var->name, v->name) == 0)
1161fde411d5SJuli Mallett 			return vent;
1162fde411d5SJuli Mallett 	}
1163fde411d5SJuli Mallett 	return NULL;
1164fde411d5SJuli Mallett }
1165fde411d5SJuli Mallett 
11664b88c807SRodney W. Grimes static void
116746251ddeSWarner Losh scanvars(void)
11684b88c807SRodney W. Grimes {
11694b88c807SRodney W. Grimes 	struct varent *vent;
11704b88c807SRodney W. Grimes 	VAR *v;
11716a2d726bSJordan K. Hubbard 
1172bdf8ab46SGarance A Drosehn 	STAILQ_FOREACH(vent, &varlist, next_ve) {
11736a2d726bSJordan K. Hubbard 		v = vent->var;
11746a2d726bSJordan K. Hubbard 		if (v->flag & USER)
11756a2d726bSJordan K. Hubbard 			needuser = 1;
11766a2d726bSJordan K. Hubbard 		if (v->flag & COMM)
11776a2d726bSJordan K. Hubbard 			needcomm = 1;
11786a2d726bSJordan K. Hubbard 	}
11796a2d726bSJordan K. Hubbard }
11806a2d726bSJordan K. Hubbard 
11816a2d726bSJordan K. Hubbard static void
11821d1143ecSEdward Tomasz Napierala format_output(KINFO *ki)
11836a2d726bSJordan K. Hubbard {
11846a2d726bSJordan K. Hubbard 	struct varent *vent;
11856a2d726bSJordan K. Hubbard 	VAR *v;
11861d1143ecSEdward Tomasz Napierala 	KINFO_STR *ks;
11871d1143ecSEdward Tomasz Napierala 	char *str;
11881d1143ecSEdward Tomasz Napierala 	int len;
11896a2d726bSJordan K. Hubbard 
11901d1143ecSEdward Tomasz Napierala 	STAILQ_INIT(&ki->ki_ks);
1191bdf8ab46SGarance A Drosehn 	STAILQ_FOREACH(vent, &varlist, next_ve) {
11926a2d726bSJordan K. Hubbard 		v = vent->var;
11931d1143ecSEdward Tomasz Napierala 		str = (v->oproc)(ki, vent);
11941d1143ecSEdward Tomasz Napierala 		ks = malloc(sizeof(*ks));
11951d1143ecSEdward Tomasz Napierala 		if (ks == NULL)
11968beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
11971d1143ecSEdward Tomasz Napierala 		ks->ks_str = str;
11981d1143ecSEdward Tomasz Napierala 		STAILQ_INSERT_TAIL(&ki->ki_ks, ks, ks_next);
11991d1143ecSEdward Tomasz Napierala 		if (str != NULL) {
12001d1143ecSEdward Tomasz Napierala 			len = strlen(str);
12011d1143ecSEdward Tomasz Napierala 		} else
12021d1143ecSEdward Tomasz Napierala 			len = 1; /* "-" */
12031d1143ecSEdward Tomasz Napierala 		if (v->width < len)
12041d1143ecSEdward Tomasz Napierala 			v->width = len;
12056a2d726bSJordan K. Hubbard 	}
12066a2d726bSJordan K. Hubbard }
12076a2d726bSJordan K. Hubbard 
12086a2d726bSJordan K. Hubbard static void
120946251ddeSWarner Losh sizevars(void)
12106a2d726bSJordan K. Hubbard {
12116a2d726bSJordan K. Hubbard 	struct varent *vent;
12126a2d726bSJordan K. Hubbard 	VAR *v;
12134b88c807SRodney W. Grimes 	int i;
12144b88c807SRodney W. Grimes 
1215bdf8ab46SGarance A Drosehn 	STAILQ_FOREACH(vent, &varlist, next_ve) {
12164b88c807SRodney W. Grimes 		v = vent->var;
121778b1878aSJuli Mallett 		i = strlen(vent->header);
12184b88c807SRodney W. Grimes 		if (v->width < i)
12194b88c807SRodney W. Grimes 			v->width = i;
12204b88c807SRodney W. Grimes 	}
12214b88c807SRodney W. Grimes }
12224b88c807SRodney W. Grimes 
1223871e8d8cSMark Murray static const char *
122446251ddeSWarner Losh fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki,
12251c67ef12SJohn Baldwin     char *comm, char *thread, int maxlen)
12264b88c807SRodney W. Grimes {
1227871e8d8cSMark Murray 	const char *s;
12284b88c807SRodney W. Grimes 
12291c67ef12SJohn Baldwin 	s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm,
12309f0b6e5eSJohn Baldwin 	    showthreads && ki->ki_p->ki_numthreads > 1 ? thread : NULL, maxlen);
12314b88c807SRodney W. Grimes 	return (s);
12324b88c807SRodney W. Grimes }
12334b88c807SRodney W. Grimes 
1234b61ce5b0SJeff Roberson #define UREADOK(ki)	(forceuread || (ki->ki_p->ki_flag & P_INMEM))
12353ac5e955SJohn Dyson 
12364b88c807SRodney W. Grimes static void
123746251ddeSWarner Losh saveuser(KINFO *ki)
12384b88c807SRodney W. Grimes {
12395912ca59SDon Lewis 	char *argsp;
12404b88c807SRodney W. Grimes 
1241b61ce5b0SJeff Roberson 	if (ki->ki_p->ki_flag & P_INMEM) {
12424b88c807SRodney W. Grimes 		/*
12434b88c807SRodney W. Grimes 		 * The u-area might be swapped out, and we can't get
12444b88c807SRodney W. Grimes 		 * at it because we have a crashdump and no swap.
12454b88c807SRodney W. Grimes 		 * If it's here fill in these fields, otherwise, just
12464b88c807SRodney W. Grimes 		 * leave them 0.
12474b88c807SRodney W. Grimes 		 */
12481f7d2501SKirk McKusick 		ki->ki_valid = 1;
12494b88c807SRodney W. Grimes 	} else
12501f7d2501SKirk McKusick 		ki->ki_valid = 0;
12514b88c807SRodney W. Grimes 	/*
12524b88c807SRodney W. Grimes 	 * save arguments if needed
12534b88c807SRodney W. Grimes 	 */
1254dd693acfSGarance A Drosehn 	if (needcomm) {
1255dd693acfSGarance A Drosehn 		if (ki->ki_p->ki_stat == SZOMB)
1256dd693acfSGarance A Drosehn 			ki->ki_args = strdup("<defunct>");
1257dd693acfSGarance A Drosehn 		else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL))
12585912ca59SDon Lewis 			ki->ki_args = fmt(kvm_getargv, ki,
12595912ca59SDon Lewis 			    ki->ki_p->ki_comm, ki->ki_p->ki_tdname, MAXCOMLEN);
12605912ca59SDon Lewis 		else {
12615912ca59SDon Lewis 			asprintf(&argsp, "(%s)", ki->ki_p->ki_comm);
12625912ca59SDon Lewis 			ki->ki_args = argsp;
12635912ca59SDon Lewis 		}
1264bd6233fdSGarance A Drosehn 		if (ki->ki_args == NULL)
12658beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
12663ac5e955SJohn Dyson 	} else {
12674b88c807SRodney W. Grimes 		ki->ki_args = NULL;
12683ac5e955SJohn Dyson 	}
1269dd693acfSGarance A Drosehn 	if (needenv) {
1270dd693acfSGarance A Drosehn 		if (UREADOK(ki))
12715912ca59SDon Lewis 			ki->ki_env = fmt(kvm_getenvv, ki,
12725912ca59SDon Lewis 			    (char *)NULL, (char *)NULL, 0);
1273dd693acfSGarance A Drosehn 		else
1274dd693acfSGarance A Drosehn 			ki->ki_env = strdup("()");
1275dd693acfSGarance A Drosehn 		if (ki->ki_env == NULL)
12768beb1a2fSMarcel Moolenaar 			xo_errx(1, "malloc failed");
12773ac5e955SJohn Dyson 	} else {
12784b88c807SRodney W. Grimes 		ki->ki_env = NULL;
12794b88c807SRodney W. Grimes 	}
12803ac5e955SJohn Dyson }
12814b88c807SRodney W. Grimes 
12824bac4483SGarance A Drosehn /* A macro used to improve the readability of pscomp(). */
12834bac4483SGarance A Drosehn #define	DIFF_RETURN(a, b, field) do {	\
12844bac4483SGarance A Drosehn 	if ((a)->field != (b)->field)	\
12854bac4483SGarance A Drosehn 		return (((a)->field < (b)->field) ? -1 : 1); 	\
12864bac4483SGarance A Drosehn } while (0)
12874bac4483SGarance A Drosehn 
12884b88c807SRodney W. Grimes static int
128946251ddeSWarner Losh pscomp(const void *a, const void *b)
12904b88c807SRodney W. Grimes {
12915bd7b1f3SGarance A Drosehn 	const KINFO *ka, *kb;
12924b88c807SRodney W. Grimes 
12935bd7b1f3SGarance A Drosehn 	ka = a;
12945bd7b1f3SGarance A Drosehn 	kb = b;
12955bd7b1f3SGarance A Drosehn 	/* SORTCPU and SORTMEM are sorted in descending order. */
12964bac4483SGarance A Drosehn 	if (sortby == SORTCPU)
12974bac4483SGarance A Drosehn 		DIFF_RETURN(kb, ka, ki_pcpu);
12984bac4483SGarance A Drosehn 	if (sortby == SORTMEM)
12994bac4483SGarance A Drosehn 		DIFF_RETURN(kb, ka, ki_memsize);
13005bd7b1f3SGarance A Drosehn 	/*
13015bd7b1f3SGarance A Drosehn 	 * TTY's are sorted in ascending order, except that all NODEV
13025bd7b1f3SGarance A Drosehn 	 * processes come before all processes with a device.
13035bd7b1f3SGarance A Drosehn 	 */
13044bac4483SGarance A Drosehn 	if (ka->ki_p->ki_tdev != kb->ki_p->ki_tdev) {
13054bac4483SGarance A Drosehn 		if (ka->ki_p->ki_tdev == NODEV)
13065bd7b1f3SGarance A Drosehn 			return (-1);
13074bac4483SGarance A Drosehn 		if (kb->ki_p->ki_tdev == NODEV)
13085bd7b1f3SGarance A Drosehn 			return (1);
13094bac4483SGarance A Drosehn 		DIFF_RETURN(ka, kb, ki_p->ki_tdev);
13104bac4483SGarance A Drosehn 	}
13114bac4483SGarance A Drosehn 
1312b4b24324SGarance A Drosehn 	/* PID's and TID's (threads) are sorted in ascending order. */
13134bac4483SGarance A Drosehn 	DIFF_RETURN(ka, kb, ki_p->ki_pid);
1314b4b24324SGarance A Drosehn 	DIFF_RETURN(ka, kb, ki_p->ki_tid);
13155bd7b1f3SGarance A Drosehn 	return (0);
13164b88c807SRodney W. Grimes }
13174bac4483SGarance A Drosehn #undef DIFF_RETURN
13184b88c807SRodney W. Grimes 
13194b88c807SRodney W. Grimes /*
13204b88c807SRodney W. Grimes  * ICK (all for getopt), would rather hide the ugliness
13214b88c807SRodney W. Grimes  * here than taint the main code.
13224b88c807SRodney W. Grimes  *
13234b88c807SRodney W. Grimes  *  ps foo -> ps -foo
13244b88c807SRodney W. Grimes  *  ps 34 -> ps -p34
13254b88c807SRodney W. Grimes  *
13264b88c807SRodney W. Grimes  * The old convention that 't' with no trailing tty arg means the users
13274b88c807SRodney W. Grimes  * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
13284b88c807SRodney W. Grimes  * feature is available with the option 'T', which takes no argument.
13294b88c807SRodney W. Grimes  */
13304b88c807SRodney W. Grimes static char *
1331bf46a3bfSGarance A Drosehn kludge_oldps_options(const char *optlist, char *origval, const char *nextarg)
13324b88c807SRodney W. Grimes {
13334b88c807SRodney W. Grimes 	size_t len;
1334c675340aSGarance A Drosehn 	char *argp, *cp, *newopts, *ns, *optp, *pidp;
13354b88c807SRodney W. Grimes 
1336daed3ad6SJuli Mallett 	/*
1337c675340aSGarance A Drosehn 	 * See if the original value includes any option which takes an
1338c675340aSGarance A Drosehn 	 * argument (and will thus use up the remainder of the string).
1339daed3ad6SJuli Mallett 	 */
1340c675340aSGarance A Drosehn 	argp = NULL;
1341c675340aSGarance A Drosehn 	if (optlist != NULL) {
1342c675340aSGarance A Drosehn 		for (cp = origval; *cp != '\0'; cp++) {
1343c675340aSGarance A Drosehn 			optp = strchr(optlist, *cp);
1344c675340aSGarance A Drosehn 			if ((optp != NULL) && *(optp + 1) == ':') {
1345c675340aSGarance A Drosehn 				argp = cp;
1346c675340aSGarance A Drosehn 				break;
1347c675340aSGarance A Drosehn 			}
1348c675340aSGarance A Drosehn 		}
1349c675340aSGarance A Drosehn 	}
1350c675340aSGarance A Drosehn 	if (argp != NULL && *origval == '-')
1351c675340aSGarance A Drosehn 		return (origval);
1352daed3ad6SJuli Mallett 
13534b88c807SRodney W. Grimes 	/*
13544b88c807SRodney W. Grimes 	 * if last letter is a 't' flag with no argument (in the context
13554b88c807SRodney W. Grimes 	 * of the oldps options -- option string NOT starting with a '-' --
13564b88c807SRodney W. Grimes 	 * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
1357380434d4SBrian Somers 	 *
13582631c777SGarance A Drosehn 	 * However, if a flag accepting a string argument is found earlier
13592631c777SGarance A Drosehn 	 * in the option string (including a possible `t' flag), then the
13602631c777SGarance A Drosehn 	 * remainder of the string must be the argument to that flag; so
1361c675340aSGarance A Drosehn 	 * do not modify that argument.  Note that a trailing `t' would
1362c675340aSGarance A Drosehn 	 * cause argp to be set, if argp was not already set by some
1363c675340aSGarance A Drosehn 	 * earlier option.
13644b88c807SRodney W. Grimes 	 */
1365c675340aSGarance A Drosehn 	len = strlen(origval);
1366c675340aSGarance A Drosehn 	cp = origval + len - 1;
1367c675340aSGarance A Drosehn 	pidp = NULL;
1368bf46a3bfSGarance A Drosehn 	if (*cp == 't' && *origval != '-' && cp == argp) {
1369bf46a3bfSGarance A Drosehn 		if (nextarg == NULL || *nextarg == '-' || isdigitch(*nextarg))
13704b88c807SRodney W. Grimes 			*cp = 'T';
1371bf46a3bfSGarance A Drosehn 	} else if (argp == NULL) {
1372c675340aSGarance A Drosehn 		/*
1373c675340aSGarance A Drosehn 		 * The original value did not include any option which takes
1374c675340aSGarance A Drosehn 		 * an argument (and that would include `p' and `t'), so check
1375c675340aSGarance A Drosehn 		 * the value for trailing number, or comma-separated list of
1376c675340aSGarance A Drosehn 		 * numbers, which we will treat as a pid request.
1377c675340aSGarance A Drosehn 		 */
1378c675340aSGarance A Drosehn 		if (isdigitch(*cp)) {
1379c675340aSGarance A Drosehn 			while (cp >= origval && (*cp == ',' || isdigitch(*cp)))
1380c675340aSGarance A Drosehn 				--cp;
1381c675340aSGarance A Drosehn 			pidp = cp + 1;
1382c675340aSGarance A Drosehn 		}
1383c675340aSGarance A Drosehn 	}
1384c675340aSGarance A Drosehn 
1385c675340aSGarance A Drosehn 	/*
1386c675340aSGarance A Drosehn 	 * If nothing needs to be added to the string, then return
1387c675340aSGarance A Drosehn 	 * the "original" (although possibly modified) value.
1388c675340aSGarance A Drosehn 	 */
1389c675340aSGarance A Drosehn 	if (*origval == '-' && pidp == NULL)
1390c675340aSGarance A Drosehn 		return (origval);
1391c675340aSGarance A Drosehn 
1392c675340aSGarance A Drosehn 	/*
1393c675340aSGarance A Drosehn 	 * Create a copy of the string to add '-' and/or 'p' to the
1394c675340aSGarance A Drosehn 	 * original value.
1395c675340aSGarance A Drosehn 	 */
1396c675340aSGarance A Drosehn 	if ((newopts = ns = malloc(len + 3)) == NULL)
13978beb1a2fSMarcel Moolenaar 		xo_errx(1, "malloc failed");
1398c675340aSGarance A Drosehn 
1399c675340aSGarance A Drosehn 	if (*origval != '-')
1400c675340aSGarance A Drosehn 		*ns++ = '-';	/* add option flag */
1401c675340aSGarance A Drosehn 
1402c675340aSGarance A Drosehn 	if (pidp == NULL)
1403c675340aSGarance A Drosehn 		strcpy(ns, origval);
14044b88c807SRodney W. Grimes 	else {
14054b88c807SRodney W. Grimes 		/*
1406c675340aSGarance A Drosehn 		 * Copy everything before the pid string, add the `p',
1407c675340aSGarance A Drosehn 		 * and then copy the pid string.
14084b88c807SRodney W. Grimes 		 */
1409c675340aSGarance A Drosehn 		len = pidp - origval;
1410c675340aSGarance A Drosehn 		memcpy(ns, origval, len);
1411c675340aSGarance A Drosehn 		ns += len;
14124b88c807SRodney W. Grimes 		*ns++ = 'p';
1413c675340aSGarance A Drosehn 		strcpy(ns, pidp);
1414c675340aSGarance A Drosehn 	}
14154b88c807SRodney W. Grimes 
14164b88c807SRodney W. Grimes 	return (newopts);
14174b88c807SRodney W. Grimes }
14184b88c807SRodney W. Grimes 
14194b88c807SRodney W. Grimes static void
142064b0bf0bSPawel Jakub Dawidek pidmax_init(void)
142164b0bf0bSPawel Jakub Dawidek {
142264b0bf0bSPawel Jakub Dawidek 	size_t intsize;
142364b0bf0bSPawel Jakub Dawidek 
142464b0bf0bSPawel Jakub Dawidek 	intsize = sizeof(pid_max);
142564b0bf0bSPawel Jakub Dawidek 	if (sysctlbyname("kern.pid_max", &pid_max, &intsize, NULL, 0) < 0) {
14268beb1a2fSMarcel Moolenaar 		xo_warn("unable to read kern.pid_max");
142764b0bf0bSPawel Jakub Dawidek 		pid_max = 99999;
142864b0bf0bSPawel Jakub Dawidek 	}
142964b0bf0bSPawel Jakub Dawidek }
143064b0bf0bSPawel Jakub Dawidek 
143164b0bf0bSPawel Jakub Dawidek static void
143246251ddeSWarner Losh usage(void)
14334b88c807SRodney W. Grimes {
1434aa14b5abSBrian Somers #define	SINGLE_OPTS	"[-aCcde" OPT_LAZY_f "HhjlmrSTuvwXxZ]"
14354b88c807SRodney W. Grimes 
14368beb1a2fSMarcel Moolenaar 	(void)xo_error("%s\n%s\n%s\n%s\n",
1437a89237aeSRuslan Ermilov 	    "usage: ps " SINGLE_OPTS " [-O fmt | -o fmt] [-G gid[,gid...]]",
143813767130SBryan Drewery 	    "          [-J jid[,jid...]] [-M core] [-N system]",
1439a89237aeSRuslan Ermilov 	    "          [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]",
14404b88c807SRodney W. Grimes 	    "       ps [-L]");
14414b88c807SRodney W. Grimes 	exit(1);
14424b88c807SRodney W. Grimes }
1443