ps.c revision 127506
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1990, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 3. All advertising materials mentioning features or use of this software
141556Srgrimes *    must display the following acknowledgement:
151556Srgrimes *	This product includes software developed by the University of
161556Srgrimes *	California, Berkeley and its contributors.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
32127499Sgad * ------+---------+---------+-------- + --------+---------+---------+---------*
33127499Sgad * Copyright (c) 2004  - Garance Alistair Drosehn <gad@FreeBSD.org>.
34127499Sgad * All rights reserved.
35127499Sgad *
36127499Sgad * Significant modifications made to bring `ps' options somewhat closer
37127499Sgad * to the standard for `ps' as described in SingleUnixSpec-v3.
38127499Sgad * ------+---------+---------+-------- + --------+---------+---------+---------*
391556Srgrimes */
401556Srgrimes
411556Srgrimes#ifndef lint
4290143Smarkmstatic const char copyright[] =
431556Srgrimes"@(#) Copyright (c) 1990, 1993, 1994\n\
441556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
451556Srgrimes#endif /* not lint */
461556Srgrimes
4790143Smarkm#if 0
481556Srgrimes#ifndef lint
4936049Scharnierstatic char sccsid[] = "@(#)ps.c	8.4 (Berkeley) 4/2/94";
5090143Smarkm#endif /* not lint */
5136049Scharnier#endif
52110391Scharnier
5399110Sobrien#include <sys/cdefs.h>
5499110Sobrien__FBSDID("$FreeBSD: head/bin/ps/ps.c 127506 2004-03-27 21:40:04Z gad $");
551556Srgrimes
561556Srgrimes#include <sys/param.h>
573296Sdg#include <sys/user.h>
581556Srgrimes#include <sys/stat.h>
591556Srgrimes#include <sys/ioctl.h>
601556Srgrimes#include <sys/sysctl.h>
611556Srgrimes
621556Srgrimes#include <ctype.h>
631556Srgrimes#include <err.h>
64127149Sgad#include <errno.h>
651556Srgrimes#include <fcntl.h>
66127499Sgad#include <grp.h>
671556Srgrimes#include <kvm.h>
6813514Smpp#include <limits.h>
6973367Sache#include <locale.h>
701556Srgrimes#include <paths.h>
7190143Smarkm#include <pwd.h>
72127499Sgad#include <stdint.h>
731556Srgrimes#include <stdio.h>
741556Srgrimes#include <stdlib.h>
751556Srgrimes#include <string.h>
761556Srgrimes#include <unistd.h>
771556Srgrimes
781556Srgrimes#include "ps.h"
791556Srgrimes
80127499Sgad#define	W_SEP	" \t"		/* "Whitespace" list separators */
81127499Sgad#define	T_SEP	","		/* "Terminate-element" list separators */
8266377Sbrian
8390143Smarkmstatic KINFO *kinfo;
8490143Smarkmstruct varent *vhead;
851556Srgrimes
861556Srgrimesint	eval;			/* exit value */
8719068Speterint	cflag;			/* -c */
88127499Sgadint	optfatal;		/* Fatal error parsing some list-option */
891556Srgrimesint	rawcpu;			/* -C */
901556Srgrimesint	sumrusage;		/* -S */
911556Srgrimesint	termwidth;		/* width of screen (0 == infinity) */
921556Srgrimesint	totwidth;		/* calculated width of requested variables */
931556Srgrimes
9497966Sjmalletttime_t	now;			/* current time(3) value */
9597966Sjmallett
96127499Sgadstruct listinfo;
97127499Sgadtypedef	int	addelem_rtn(struct listinfo *_inf, const char *elem);
98127499Sgad
99127499Sgadstruct listinfo {
100127499Sgad	int		 count;
101127499Sgad	int		 maxcount;
102127499Sgad	int		 elemsize;
103127499Sgad	addelem_rtn	*addelem;
104127499Sgad	const char	*lname;
105127499Sgad	union {
106127499Sgad		gid_t	*gids;
107127499Sgad		pid_t	*pids;
108127499Sgad		dev_t	*ttys;
109127499Sgad		uid_t	*uids;
110127499Sgad		void	*ptr;
111127499Sgad	};
112127499Sgad};
113127499Sgad
1141556Srgrimesstatic int needuser, needcomm, needenv;
11531552Sdyson#if defined(LAZY_PS)
11631552Sdysonstatic int forceuread=0;
11731552Sdyson#else
11831552Sdysonstatic int forceuread=1;
11931552Sdyson#endif
1201556Srgrimes
12190143Smarkmstatic enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
1221556Srgrimes
12390143Smarkmstatic const	 char *fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int),
12490110Simp		    KINFO *, char *, int);
12590110Simpstatic char	*kludge_oldps_options(char *);
12690110Simpstatic int	 pscomp(const void *, const void *);
12790110Simpstatic void	 saveuser(KINFO *);
12890110Simpstatic void	 scanvars(void);
12990110Simpstatic void	 dynsizevars(KINFO *);
13090110Simpstatic void	 sizevars(void);
13190110Simpstatic void	 usage(void);
1321556Srgrimes
133127499Sgadstatic int	 addelem_gid(struct listinfo *, const char *);
134127499Sgadstatic int	 addelem_pid(struct listinfo *, const char *);
135127499Sgadstatic int	 addelem_tty(struct listinfo *, const char *);
136127499Sgadstatic int	 addelem_uid(struct listinfo *, const char *);
137127499Sgadstatic void	 add_list(struct listinfo *, const char *);
138127499Sgadstatic void	*expand_list(struct listinfo *);
139127499Sgadstatic void	 free_list(struct listinfo *);
140127499Sgadstatic void	 init_list(struct listinfo *, addelem_rtn, int, const char *);
141127499Sgad
14297875Sjmallettstatic char dfmt[] = "pid,tt,state,time,command";
14397875Sjmallettstatic char jfmt[] = "user,pid,ppid,pgid,jobc,state,tt,time,command";
14497875Sjmallettstatic char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state,tt,time,command";
14590143Smarkmstatic char   o1[] = "pid";
14697875Sjmallettstatic char   o2[] = "tt,state,time,command";
14797875Sjmallettstatic char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command";
14897875Sjmallettstatic char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz,%cpu,%mem,command";
149105831Srwatsonstatic char Zfmt[] = "label";
1501556Srgrimes
15190143Smarkmstatic kvm_t *kd;
1521556Srgrimes
15398494Ssobomax#if defined(LAZY_PS)
154127499Sgad#define	PS_ARGS	"AaCcefG:gHhjLlM:mN:O:o:p:rSTt:U:uvwXxZ"
15598494Ssobomax#else
156127499Sgad#define	PS_ARGS	"AaCceG:gHhjLlM:mN:O:o:p:rSTt:U:uvwXxZ"
15798494Ssobomax#endif
15898494Ssobomax
1591556Srgrimesint
16090110Simpmain(int argc, char *argv[])
1611556Srgrimes{
162127499Sgad	struct listinfo gidlist, pgrplist, pidlist;
163127499Sgad	struct listinfo ruidlist, sesslist, ttylist, uidlist;
1641556Srgrimes	struct kinfo_proc *kp;
1651556Srgrimes	struct varent *vent;
1661556Srgrimes	struct winsize ws;
167127499Sgad	int all, ch, dropgid, elem, flag, _fmt, i, lineno;
168127499Sgad	int nentries, nocludge, nkept, nselectors;
169127499Sgad	int prtheader, showthreads, wflag, what, xkeep, xkeep_implied;
17097804Stjr	char *cols;
17190143Smarkm	char errbuf[_POSIX2_LINE_MAX];
17298494Ssobomax	const char *cp, *nlistf, *memf;
1731556Srgrimes
17411809Sache	(void) setlocale(LC_ALL, "");
17597966Sjmallett	/* Set the time to what it is right now. */
17697966Sjmallett	time(&now);
17711809Sache
17897804Stjr	if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0')
17997804Stjr		termwidth = atoi(cols);
18097804Stjr	else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1811556Srgrimes	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1821556Srgrimes	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
1831556Srgrimes	     ws.ws_col == 0)
1841556Srgrimes		termwidth = 79;
1851556Srgrimes	else
1861556Srgrimes		termwidth = ws.ws_col - 1;
1871556Srgrimes
18898494Ssobomax	/*
18998494Ssobomax	 * Don't apply a kludge if the first argument is an option taking an
19098494Ssobomax	 * argument
19198494Ssobomax	 */
19298494Ssobomax	if (argc > 1) {
19398494Ssobomax		nocludge = 0;
19498494Ssobomax		if (argv[1][0] == '-') {
19598494Ssobomax			for (cp = PS_ARGS; *cp != '\0'; cp++) {
19698494Ssobomax				if (*cp != ':')
19798494Ssobomax					continue;
19898494Ssobomax				if (*(cp - 1) == argv[1][1]) {
19998494Ssobomax					nocludge = 1;
20098494Ssobomax					break;
20198494Ssobomax				}
20298494Ssobomax			}
20398494Ssobomax		}
20498494Ssobomax		if (nocludge == 0)
20598494Ssobomax			argv[1] = kludge_oldps_options(argv[1]);
20698494Ssobomax	}
2071556Srgrimes
208127499Sgad	xkeep = -1;				/* Neither -x nor -X */
209127499Sgad	all = _fmt = nselectors = prtheader = wflag = xkeep_implied = 0;
210127499Sgad	init_list(&gidlist, addelem_gid, sizeof(gid_t), "group");
211127499Sgad	init_list(&pgrplist, addelem_pid, sizeof(pid_t), "process group");
212127499Sgad	init_list(&pidlist, addelem_pid, sizeof(pid_t), "process id");
213127499Sgad	init_list(&ruidlist, addelem_uid, sizeof(uid_t), "ruser");
214127499Sgad	init_list(&sesslist, addelem_pid, sizeof(pid_t), "session id");
215127499Sgad	init_list(&ttylist, addelem_tty, sizeof(dev_t), "tty");
216127499Sgad	init_list(&uidlist, addelem_uid, sizeof(uid_t), "user");
21737317Sphk	dropgid = 0;
218127499Sgad	optfatal = 0;
21989909Sru	memf = nlistf = _PATH_DEVNULL;
220116265Sscottl	showthreads = 0;
22198494Ssobomax	while ((ch = getopt(argc, argv, PS_ARGS)) != -1)
2221556Srgrimes		switch((char)ch) {
223127499Sgad		case 'A':
224127499Sgad			/*
225127499Sgad			 * Exactly the same as `-ax'.   This has been
226127499Sgad			 * added for compatability with SUSv3, but for
227127499Sgad			 * now it will not be described in the man page.
228127499Sgad			 */
229127499Sgad			nselectors++;
230127499Sgad			all = xkeep = 1;
231127499Sgad			break;
2321556Srgrimes		case 'a':
233127499Sgad			nselectors++;
2341556Srgrimes			all = 1;
2351556Srgrimes			break;
23619068Speter		case 'C':
23719068Speter			rawcpu = 1;
23819068Speter			break;
23919068Speter		case 'c':
24019068Speter			cflag = 1;
24119068Speter			break;
2421556Srgrimes		case 'e':			/* XXX set ufmt */
2431556Srgrimes			needenv = 1;
2441556Srgrimes			break;
245127506Sgad#ifdef LAZY_PS
246127506Sgad		case 'f':
247127506Sgad			if (getuid() == 0 || getgid() == 0)
248127506Sgad			    forceuread = 1;
249127506Sgad			break;
250127506Sgad#endif
251127499Sgad		case 'G':
252127499Sgad			add_list(&gidlist, optarg);
253127499Sgad			xkeep_implied = 1;
254127499Sgad			nselectors++;
255127499Sgad			break;
256127499Sgad#if 0
257127499Sgad		/* XXX - This SUSv3 option is still under debate. */
258127499Sgad		/* (it conflicts with the undocumented `-g' option) */
2591556Srgrimes		case 'g':
260127499Sgad			add_list(&pgrplist, optarg);
261127499Sgad			xkeep_implied = 1;
262127499Sgad			nselectors++;
263127499Sgad			break;
264127499Sgad#else
265127499Sgad		case 'g':
266127499Sgad			/* Historical BSD-ish (from SunOS) option */
2671556Srgrimes			break;			/* no-op */
268127499Sgad#endif
269116265Sscottl		case 'H':
270126127Sdeischen			showthreads = KERN_PROC_INC_THREAD;
271116265Sscottl			break;
2721556Srgrimes		case 'h':
2731556Srgrimes			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
2741556Srgrimes			break;
2751556Srgrimes		case 'j':
276109502Sjmallett			parsefmt(jfmt, 0);
27790143Smarkm			_fmt = 1;
2781556Srgrimes			jfmt[0] = '\0';
2791556Srgrimes			break;
2801556Srgrimes		case 'L':
2811556Srgrimes			showkey();
2821556Srgrimes			exit(0);
2831556Srgrimes		case 'l':
284109502Sjmallett			parsefmt(lfmt, 0);
28590143Smarkm			_fmt = 1;
2861556Srgrimes			lfmt[0] = '\0';
2871556Srgrimes			break;
2881556Srgrimes		case 'M':
2891556Srgrimes			memf = optarg;
29037317Sphk			dropgid = 1;
2911556Srgrimes			break;
2921556Srgrimes		case 'm':
2931556Srgrimes			sortby = SORTMEM;
2941556Srgrimes			break;
2951556Srgrimes		case 'N':
2961556Srgrimes			nlistf = optarg;
29737317Sphk			dropgid = 1;
2981556Srgrimes			break;
2991556Srgrimes		case 'O':
300109502Sjmallett			parsefmt(o1, 1);
301109502Sjmallett			parsefmt(optarg, 1);
302109502Sjmallett			parsefmt(o2, 1);
3031556Srgrimes			o1[0] = o2[0] = '\0';
30490143Smarkm			_fmt = 1;
3051556Srgrimes			break;
3061556Srgrimes		case 'o':
307109502Sjmallett			parsefmt(optarg, 1);
30890143Smarkm			_fmt = 1;
3091556Srgrimes			break;
3101556Srgrimes		case 'p':
311127499Sgad			add_list(&pidlist, optarg);
312127499Sgad			/*
313127499Sgad			 * Note: `-p' does not *set* xkeep, but any values
314127499Sgad			 * from pidlist are checked before xkeep is.  That
315127499Sgad			 * way they are always matched, even if the user
316127499Sgad			 * specifies `-X'.
317127499Sgad			 */
318127499Sgad			nselectors++;
3191556Srgrimes			break;
320127499Sgad#if 0
321127499Sgad		/* XXX - This un-standard option is still under debate. */
322127499Sgad		case 'R':
323127499Sgad			/* This is what SUSv3 defines as the `-U' option. */
324127499Sgad			add_list(&ruidlist, optarg);
325127499Sgad			xkeep_implied = 1;
326127499Sgad			nselectors++;
327127499Sgad			break;
328127499Sgad#endif
3291556Srgrimes		case 'r':
3301556Srgrimes			sortby = SORTCPU;
3311556Srgrimes			break;
3321556Srgrimes		case 'S':
3331556Srgrimes			sumrusage = 1;
3341556Srgrimes			break;
335127499Sgad#if 0
336127499Sgad		/* XXX - This non-standard option is still under debate. */
337127499Sgad		/* (it conflicts with `-s' in NetBSD) */
338127499Sgad		case 's':
339127499Sgad			/* As seen on Solaris, Linux, IRIX. */
340127499Sgad			add_list(&sesslist, optarg);
341127499Sgad			xkeep_implied = 1;
342127499Sgad			nselectors++;
343127499Sgad			break;
344127499Sgad#endif
3451556Srgrimes		case 'T':
3461556Srgrimes			if ((optarg = ttyname(STDIN_FILENO)) == NULL)
3471556Srgrimes				errx(1, "stdin: not a terminal");
3481556Srgrimes			/* FALLTHROUGH */
349127499Sgad		case 't':
350127499Sgad			add_list(&ttylist, optarg);
351127499Sgad			xkeep_implied = 1;
352127499Sgad			nselectors++;
3531556Srgrimes			break;
35413020Speter		case 'U':
355127499Sgad			/* This is what SUSv3 defines as the `-u' option. */
356127499Sgad			add_list(&uidlist, optarg);
357127499Sgad			xkeep_implied = 1;
358127499Sgad			nselectors++;
35913020Speter			break;
3601556Srgrimes		case 'u':
361109502Sjmallett			parsefmt(ufmt, 0);
3621556Srgrimes			sortby = SORTCPU;
36390143Smarkm			_fmt = 1;
3641556Srgrimes			ufmt[0] = '\0';
3651556Srgrimes			break;
3661556Srgrimes		case 'v':
367109502Sjmallett			parsefmt(vfmt, 0);
3681556Srgrimes			sortby = SORTMEM;
36990143Smarkm			_fmt = 1;
3701556Srgrimes			vfmt[0] = '\0';
3711556Srgrimes			break;
3721556Srgrimes		case 'w':
3731556Srgrimes			if (wflag)
3741556Srgrimes				termwidth = UNLIMITED;
3751556Srgrimes			else if (termwidth < 131)
3761556Srgrimes				termwidth = 131;
3771556Srgrimes			wflag++;
3781556Srgrimes			break;
379127499Sgad		case 'X':
380127499Sgad			/*
381127499Sgad			 * Note that `-X' and `-x' are not standard "selector"
382127499Sgad			 * options. For most selector-options, we check *all*
383127499Sgad			 * processes to see if any are matched by the given
384127499Sgad			 * value(s).  After we have a set of all the matched
385127499Sgad			 * processes, then `-X' and `-x' govern whether we
386127499Sgad			 * modify that *matched* set for processes which do
387127499Sgad			 * not have a controlling terminal.  `-X' causes
388127499Sgad			 * those processes to be deleted from the matched
389127499Sgad			 * set, while `-x' causes them to be kept.
390127499Sgad			 */
391127499Sgad			xkeep = 0;
392127499Sgad			break;
3931556Srgrimes		case 'x':
394127499Sgad			xkeep = 1;
3951556Srgrimes			break;
39686922Sgreen		case 'Z':
397109502Sjmallett			parsefmt(Zfmt, 0);
39886922Sgreen			Zfmt[0] = '\0';
39986922Sgreen			break;
4001556Srgrimes		case '?':
4011556Srgrimes		default:
4021556Srgrimes			usage();
4031556Srgrimes		}
4041556Srgrimes	argc -= optind;
4051556Srgrimes	argv += optind;
4061556Srgrimes
407127499Sgad	if (optfatal)
408127499Sgad		exit(1);		/* Error messages already printed */
409127499Sgad
410127499Sgad	if (xkeep < 0)			/* Neither -X nor -x was specified */
411127499Sgad		xkeep = xkeep_implied;
412127499Sgad
4131556Srgrimes#define	BACKWARD_COMPATIBILITY
4141556Srgrimes#ifdef	BACKWARD_COMPATIBILITY
4151556Srgrimes	if (*argv) {
4161556Srgrimes		nlistf = *argv;
4171556Srgrimes		if (*++argv) {
4181556Srgrimes			memf = *argv;
4191556Srgrimes		}
4201556Srgrimes	}
4211556Srgrimes#endif
4221556Srgrimes	/*
4231556Srgrimes	 * Discard setgid privileges if not the running kernel so that bad
4241556Srgrimes	 * guys can't print interesting stuff from kernel memory.
4251556Srgrimes	 */
42637317Sphk	if (dropgid) {
4271556Srgrimes		setgid(getgid());
42837317Sphk		setuid(getuid());
42937317Sphk	}
4301556Srgrimes
43189909Sru	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
4321556Srgrimes	if (kd == 0)
4331556Srgrimes		errx(1, "%s", errbuf);
4341556Srgrimes
43590143Smarkm	if (!_fmt)
436109502Sjmallett		parsefmt(dfmt, 0);
4371556Srgrimes
438127499Sgad	if (nselectors == 0) {
439127499Sgad		uidlist.ptr = malloc(sizeof(uid_t));
440127499Sgad		if (uidlist.ptr == NULL)
44197877Sjmallett			errx(1, "malloc failed");
442127499Sgad		nselectors = 1;
443127499Sgad		uidlist.count = uidlist.maxcount = 1;
444127499Sgad		*uidlist.uids = getuid();
44566377Sbrian	}
4461556Srgrimes
4471556Srgrimes	/*
4481556Srgrimes	 * scan requested variables, noting what structures are needed,
44953170Skris	 * and adjusting header widths as appropriate.
4501556Srgrimes	 */
4511556Srgrimes	scanvars();
452127499Sgad
4531556Srgrimes	/*
454127499Sgad	 * Get process list.  If the user requested just one selector-
455127499Sgad	 * option, then kvm_getprocs can be asked to return just those
456127499Sgad	 * processes.  Otherwise, have it return all processes, and
457127499Sgad	 * then this routine will search that full list and select the
458127499Sgad	 * processes which match any of the user's selector-options.
4591556Srgrimes	 */
460127499Sgad	what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC;
461127499Sgad	flag = 0;
462127499Sgad	if (nselectors == 1) {
463127499Sgad		/* XXX - Apparently there's no KERN_PROC_GID flag. */
464127499Sgad		if (pgrplist.count == 1) {
465127499Sgad			what = KERN_PROC_PGRP | showthreads;
466127499Sgad			flag = *pgrplist.pids;
467127499Sgad			nselectors = 0;
468127499Sgad		} else if (pidlist.count == 1) {
469127499Sgad			what = KERN_PROC_PID | showthreads;
470127499Sgad			flag = *pidlist.pids;
471127499Sgad			nselectors = 0;
472127499Sgad		} else if (ruidlist.count == 1) {
473127499Sgad			what = KERN_PROC_RUID | showthreads;
474127499Sgad			flag = *ruidlist.uids;
475127499Sgad			nselectors = 0;
476127499Sgad#if 0		/* XXX - KERN_PROC_SESSION causes error in kvm_getprocs? */
477127499Sgad		} else if (sesslist.count == 1) {
478127499Sgad			what = KERN_PROC_SESSION | showthreads;
479127499Sgad			flag = *sesslist.pids;
480127499Sgad			nselectors = 0;
481127499Sgad#endif
482127499Sgad		} else if (ttylist.count == 1) {
483127499Sgad			what = KERN_PROC_TTY | showthreads;
484127499Sgad			flag = *ttylist.ttys;
485127499Sgad			nselectors = 0;
486127499Sgad		} else if (uidlist.count == 1) {
487127499Sgad			what = KERN_PROC_UID | showthreads;
488127499Sgad			flag = *uidlist.uids;
489127499Sgad			nselectors = 0;
490127499Sgad		} else if (all) {
491127499Sgad			/* No need for this routine to select processes. */
492127499Sgad			nselectors = 0;
493127499Sgad		}
4941556Srgrimes	}
495126127Sdeischen
4961556Srgrimes	/*
4971556Srgrimes	 * select procs
4981556Srgrimes	 */
499127499Sgad	nentries = -1;
500127149Sgad	kp = kvm_getprocs(kd, what, flag, &nentries);
501127149Sgad	if ((kp == 0 && nentries != 0) || nentries < 0)
5021556Srgrimes		errx(1, "%s", kvm_geterr(kd));
503127499Sgad	nkept = 0;
504127149Sgad	if (nentries > 0) {
505127149Sgad		if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL)
506127149Sgad			errx(1, "malloc failed");
507127149Sgad		for (i = nentries; --i >= 0; ++kp) {
508127499Sgad			/*
509127499Sgad			 * If the user specified multiple selection-criteria,
510127499Sgad			 * then keep any process matched by the inclusive OR
511127499Sgad			 * of all the selection-criteria given.
512127499Sgad			 */
513127499Sgad			if (pidlist.count > 0) {
514127499Sgad				for (elem = 0; elem < pidlist.count; elem++)
515127499Sgad					if (kp->ki_pid == pidlist.pids[elem])
516127499Sgad						goto keepit;
517127499Sgad			}
518127499Sgad			/*
519127499Sgad			 * Note that we had to process pidlist before
520127499Sgad			 * filtering out processes which do not have
521127499Sgad			 * a controlling terminal.
522127499Sgad			 */
523127499Sgad			if (xkeep == 0) {
524127499Sgad				if ((kp->ki_tdev == NODEV ||
525127499Sgad				    (kp->ki_flag & P_CONTROLT) == 0))
526127499Sgad					continue;
527127499Sgad			}
528127499Sgad			if (nselectors == 0)
529127499Sgad				goto keepit;
530127499Sgad			if (gidlist.count > 0) {
531127499Sgad				for (elem = 0; elem < gidlist.count; elem++)
532127499Sgad					if (kp->ki_rgid == gidlist.gids[elem])
533127499Sgad						goto keepit;
534127499Sgad			}
535127499Sgad			if (pgrplist.count > 0) {
536127499Sgad				for (elem = 0; elem < pgrplist.count; elem++)
537127499Sgad					if (kp->ki_pgid == pgrplist.pids[elem])
538127499Sgad						goto keepit;
539127499Sgad			}
540127499Sgad			if (ruidlist.count > 0) {
541127499Sgad				for (elem = 0; elem < ruidlist.count; elem++)
542127499Sgad					if (kp->ki_ruid == ruidlist.uids[elem])
543127499Sgad						goto keepit;
544127499Sgad			}
545127499Sgad			if (sesslist.count > 0) {
546127499Sgad				for (elem = 0; elem < sesslist.count; elem++)
547127499Sgad					if (kp->ki_sid == sesslist.pids[elem])
548127499Sgad						goto keepit;
549127499Sgad			}
550127499Sgad			if (ttylist.count > 0) {
551127499Sgad				for (elem = 0; elem < ttylist.count; elem++)
552127499Sgad					if (kp->ki_tdev == ttylist.ttys[elem])
553127499Sgad						goto keepit;
554127499Sgad			}
555127499Sgad			if (uidlist.count > 0) {
556127499Sgad				for (elem = 0; elem < uidlist.count; elem++)
557127499Sgad					if (kp->ki_uid == uidlist.uids[elem])
558127499Sgad						goto keepit;
559127499Sgad			}
560127499Sgad			/*
561127499Sgad			 * This process did not match any of the user's
562127499Sgad			 * selector-options, so skip the process.
563127499Sgad			 */
564127499Sgad			continue;
565127499Sgad
566127499Sgad		keepit:
567127499Sgad			kinfo[nkept].ki_p = kp;
568127149Sgad			if (needuser)
569127499Sgad				saveuser(&kinfo[nkept]);
570127499Sgad			dynsizevars(&kinfo[nkept]);
571127499Sgad			nkept++;
572127149Sgad		}
5731556Srgrimes	}
57425271Sjkh
57525271Sjkh	sizevars();
57625271Sjkh
5771556Srgrimes	/*
5781556Srgrimes	 * print header
5791556Srgrimes	 */
5801556Srgrimes	printheader();
581127499Sgad	if (nkept == 0)
58262803Swill		exit(1);
583127499Sgad
5841556Srgrimes	/*
5851556Srgrimes	 * sort proc list
5861556Srgrimes	 */
587127499Sgad	qsort(kinfo, nkept, sizeof(KINFO), pscomp);
5881556Srgrimes	/*
589127499Sgad	 * For each process, call each variable output function.
5901556Srgrimes	 */
591127499Sgad	for (i = lineno = 0; i < nkept; i++) {
5921556Srgrimes		for (vent = vhead; vent; vent = vent->next) {
5931556Srgrimes			(vent->var->oproc)(&kinfo[i], vent);
5941556Srgrimes			if (vent->next != NULL)
5951556Srgrimes				(void)putchar(' ');
5961556Srgrimes		}
5971556Srgrimes		(void)putchar('\n');
5981556Srgrimes		if (prtheader && lineno++ == prtheader - 4) {
5991556Srgrimes			(void)putchar('\n');
6001556Srgrimes			printheader();
6011556Srgrimes			lineno = 0;
6021556Srgrimes		}
6031556Srgrimes	}
604127499Sgad	free_list(&gidlist);
605127499Sgad	free_list(&pidlist);
606127499Sgad	free_list(&pgrplist);
607127499Sgad	free_list(&ruidlist);
608127499Sgad	free_list(&sesslist);
609127499Sgad	free_list(&ttylist);
610127499Sgad	free_list(&uidlist);
61166377Sbrian
6121556Srgrimes	exit(eval);
6131556Srgrimes}
6141556Srgrimes
615127499Sgadstatic int
616127499Sgadaddelem_gid(struct listinfo *inf, const char *elem)
617127499Sgad{
618127499Sgad	struct group *grp;
619127499Sgad	intmax_t ltemp;
620127499Sgad	const char *nameorID;
621127499Sgad	char *endp;
622127499Sgad
623127499Sgad	if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
624127499Sgad		if (*elem == '\0')
625127499Sgad			warnx("Invalid (zero-length) %s name", inf->lname);
626127499Sgad		else
627127499Sgad			warnx("%s name too long: %s", inf->lname, elem);
628127499Sgad		optfatal = 1;
629127499Sgad		return (0);			/* Do not add this value */
630127499Sgad	}
631127499Sgad
632127499Sgad	/*
633127499Sgad	 * SUSv3 states that `ps -G grouplist' should match "real-group
634127499Sgad	 * ID numbers", and does not mention group-names.  I do want to
635127499Sgad	 * also support group-names, so this tries for a group-id first,
636127499Sgad	 * and only tries for a name if that doesn't work.  This is the
637127499Sgad	 * opposite order of what is done in addelem_uid(), but in
638127499Sgad	 * practice the order would only matter for group-names which
639127499Sgad	 * are all-numeric.
640127499Sgad	 */
641127499Sgad	grp = NULL;
642127499Sgad	nameorID = "named";
643127499Sgad	errno = 0;
644127499Sgad	ltemp = strtol(elem, &endp, 10);
645127499Sgad	if (errno == 0 && *endp == '\0' && ltemp >= 0 && ltemp <= GID_MAX) {
646127499Sgad		nameorID = "name or ID matches";
647127499Sgad		grp = getgrgid((gid_t)ltemp);
648127499Sgad	}
649127499Sgad	if (grp == NULL)
650127499Sgad		grp = getgrnam(elem);
651127499Sgad	if (grp == NULL) {
652127499Sgad		warnx("No %s %s '%s'", inf->lname, nameorID, elem);
653127499Sgad		optfatal = 1;
654127499Sgad		return (0);			/* Do not add this value */
655127499Sgad	}
656127499Sgad
657127499Sgad	if (inf->count >= inf->maxcount)
658127499Sgad		expand_list(inf);
659127499Sgad	inf->gids[(inf->count)++] = grp->gr_gid;
660127499Sgad	return (1);
661127499Sgad}
662127499Sgad
663127149Sgad#define	BSD_PID_MAX	99999		/* Copy of PID_MAX from sys/proc.h */
664127499Sgadstatic int
665127499Sgadaddelem_pid(struct listinfo *inf, const char *elem)
666127149Sgad{
667127149Sgad	long tempid;
668127499Sgad	char *endp;
669127149Sgad
670127499Sgad	if (*elem == '\0')
671127499Sgad		tempid = 0L;
672127499Sgad	else {
673127499Sgad		errno = 0;
674127499Sgad		tempid = strtol(elem, &endp, 10);
675127499Sgad		if (*endp != '\0' || tempid < 0 || elem == endp) {
676127499Sgad			warnx("Invalid %s: %s", inf->lname, elem);
677127499Sgad			errno = ERANGE;
678127499Sgad		} else if (errno != 0 || tempid > BSD_PID_MAX) {
679127499Sgad			warnx("%s too large: %s", inf->lname, elem);
680127499Sgad			errno = ERANGE;
681127149Sgad		}
682127499Sgad		if (errno == ERANGE) {
683127499Sgad			optfatal = 1;
684127499Sgad			return (0);		/* Do not add this value */
685127149Sgad		}
686127149Sgad	}
687127149Sgad
688127499Sgad	if (inf->count >= inf->maxcount)
689127499Sgad		expand_list(inf);
690127499Sgad	inf->pids[(inf->count)++] = tempid;
691127499Sgad	return (1);
692127499Sgad}
693127499Sgad#undef	BSD_PID_MAX
694127149Sgad
695127499Sgadstatic int
696127499Sgadaddelem_tty(struct listinfo *inf, const char *elem)
697127499Sgad{
698127499Sgad	char pathbuf[PATH_MAX];
699127499Sgad	struct stat sb;
700127499Sgad	const char *ttypath;
701127499Sgad
702127499Sgad	if (strcmp(elem, "co") == 0)
703127499Sgad		ttypath = strdup(_PATH_CONSOLE);
704127499Sgad	else if (*elem == '/')
705127499Sgad		ttypath = elem;
706127499Sgad	else {
707127499Sgad		strlcpy(pathbuf, _PATH_TTY, sizeof(pathbuf));
708127499Sgad		strlcat(pathbuf, elem, sizeof(pathbuf));
709127499Sgad		ttypath = pathbuf;
710127499Sgad	}
711127499Sgad
712127499Sgad	if (stat(ttypath, &sb) == -1) {
713127499Sgad		warn("%s", ttypath);
714127499Sgad		optfatal = 1;
715127499Sgad		return (0);			/* Do not add this value */
716127499Sgad	}
717127499Sgad	if (!S_ISCHR(sb.st_mode)) {
718127499Sgad		warn("%s: Not a terminal", ttypath);
719127499Sgad		optfatal = 1;
720127499Sgad		return (0);			/* Do not add this value */
721127499Sgad	}
722127499Sgad
723127499Sgad	if (inf->count >= inf->maxcount)
724127499Sgad		expand_list(inf);
725127499Sgad	inf->ttys[(inf->count)++] = sb.st_rdev;
726127499Sgad	return (1);
727127149Sgad}
728127149Sgad
729127499Sgadstatic int
730127499Sgadaddelem_uid(struct listinfo *inf, const char *elem)
73166377Sbrian{
73266377Sbrian	struct passwd *pwd;
733127499Sgad	intmax_t ltemp;
734127499Sgad	char *endp;
73566377Sbrian
736127499Sgad	if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
737127499Sgad		if (*elem == '\0')
738127499Sgad			warnx("Invalid (zero-length) %s name", inf->lname);
739127499Sgad		else
740127499Sgad			warnx("%s name too long: %s", inf->lname, elem);
741127499Sgad		optfatal = 1;
742127499Sgad		return (0);			/* Do not add this value */
743127499Sgad	}
74466377Sbrian
745127499Sgad	/*
746127499Sgad	 * XXX - In the following, should the warnings be fatal errors?
747127499Sgad	 *	Historically they have been warnings, but I expect errors
748127499Sgad	 *	would be more appropriate.  A warning message might be
749127499Sgad	 *	missed in a long `ps' listing, and the user would not
750127499Sgad	 *	realize that they had mistyped a userid.  Also, Solaris
751127499Sgad	 *	and Linux treat these as errors.  On the other hand, I
752127499Sgad	 *	can imagine that some users *might* be used to the
753127499Sgad	 *	present behavior.
754127499Sgad	 */
755127499Sgad	pwd = getpwnam(elem);
756127499Sgad	if (pwd == NULL) {
757127499Sgad		errno = 0;
758127499Sgad		ltemp = strtol(elem, &endp, 10);
759127499Sgad		if (errno != 0 || *endp != '\0' || ltemp < 0 ||
760127499Sgad		    ltemp > UID_MAX)
761127499Sgad			warnx("No %s named '%s'", inf->lname, elem);
762127499Sgad		else {
763127499Sgad			/* The string is all digits, so it might be a userID. */
764127499Sgad			pwd = getpwuid((uid_t)ltemp);
765127499Sgad			if (pwd == NULL)
766127499Sgad				warnx("No %s name or ID matches '%s'",
767127499Sgad				    inf->lname, elem);
76866377Sbrian		}
769127499Sgad	}
770127499Sgad	if (pwd == NULL) {
771127499Sgad		/* XXX: optfatal = 1;		-- (see the above XXX) */
772127499Sgad		return (0);			/* Do not add this value */
773127499Sgad	}
774127499Sgad
775127499Sgad	if (inf->count >= inf->maxcount)
776127499Sgad		expand_list(inf);
777127499Sgad	inf->uids[(inf->count)++] = pwd->pw_uid;
778127499Sgad	return (1);
779127499Sgad}
780127499Sgad
781127499Sgadstatic void
782127499Sgadadd_list(struct listinfo *inf, const char *argp)
783127499Sgad{
784127499Sgad	char elemcopy[PATH_MAX];
785127499Sgad	const char *savep;
786127499Sgad	char *cp, *endp;
787127499Sgad	int toolong;
788127499Sgad
789127499Sgad	while (*argp != '\0') {
790127499Sgad		while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
791127499Sgad			argp++;
792127499Sgad		savep = argp;
793127499Sgad		toolong = 0;
794127499Sgad		cp = elemcopy;
795127499Sgad		if (strchr(T_SEP, *argp) == NULL) {
796127499Sgad			endp = elemcopy + sizeof(elemcopy) - 1;
797127499Sgad			while (*argp != '\0' && cp <= endp &&
798127499Sgad			    strchr(W_SEP T_SEP, *argp) == NULL)
799127499Sgad				*cp++ = *argp++;
800127499Sgad			if (cp > endp)
801127499Sgad				toolong = 1;
80266377Sbrian		}
803127499Sgad		if (!toolong) {
804127499Sgad			*cp = '\0';
805127499Sgad#ifndef ADD_PS_LISTRESET
806127499Sgad	/* This is how the standard expects lists to be handled. */
807127499Sgad			inf->addelem(inf, elemcopy);
808127499Sgad#else
809127499Sgad	/*
810127499Sgad	 * This would add a simple non-standard-but-convienent feature.
811127499Sgad	 *
812127499Sgad	 * XXX - Adding this check increased the total size of `ps' by
813127499Sgad	 *	3940 bytes on i386!  That's 12% of the entire program!
814127499Sgad	 *	The `ps.o' file grew by only about 40 bytes, but the
815127499Sgad	 *	final (stripped) executable in /bin/ps grew by 12%.
816127499Sgad	 */
817127499Sgad			/*
818127499Sgad			 * We now have a single element.  Add it to the
819127499Sgad			 * list, unless the element is ":".  In that case,
820127499Sgad			 * reset the list so previous entries are ignored.
821127499Sgad			 */
822127499Sgad			if (strcmp(elemcopy, ":") == 0)
823127499Sgad				inf->count = 0;
824127499Sgad			else
825127499Sgad				inf->addelem(inf, elemcopy);
826127499Sgad#endif
827127499Sgad		} else {
828127499Sgad			/*
829127499Sgad			 * The string is too long to copy.  Find the end
830127499Sgad			 * of the string to print out the warning message.
831127499Sgad			 */
832127499Sgad			while (*argp != '\0' && strchr(W_SEP T_SEP,
833127499Sgad			    *argp) == NULL)
834127499Sgad				argp++;
835127499Sgad			warnx("Value too long: %.*s", (int)(argp - savep),
836127499Sgad			    savep);
837127499Sgad			optfatal = 1;
83866377Sbrian		}
839127499Sgad		/*
840127499Sgad		 * Skip over any number of trailing whitespace characters,
841127499Sgad		 * but only one (at most) trailing element-terminating
842127499Sgad		 * character.
843127499Sgad		 */
844127499Sgad		while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
845127499Sgad			argp++;
846127499Sgad		if (*argp != '\0' && strchr(T_SEP, *argp) != NULL) {
847127499Sgad			argp++;
848127499Sgad			/* Catch case where string ended with a comma. */
849127499Sgad			if (*argp == '\0')
850127499Sgad				inf->addelem(inf, argp);
851127499Sgad		}
85266377Sbrian	}
853127499Sgad}
85466377Sbrian
855127499Sgadstatic void *
856127499Sgadexpand_list(struct listinfo *inf)
857127499Sgad{
858127499Sgad	int newmax;
859127499Sgad	void *newlist;
86066377Sbrian
861127499Sgad	newmax = (inf->maxcount + 1) << 1;
862127499Sgad	newlist = realloc(inf->ptr, newmax * inf->elemsize);
863127499Sgad	if (newlist == NULL) {
864127499Sgad		free(inf->ptr);
865127499Sgad		errx(1, "realloc to %d %ss failed", newmax,
866127499Sgad		    inf->lname);
867127499Sgad	}
868127499Sgad	inf->maxcount = newmax;
869127499Sgad	inf->ptr = newlist;
870127499Sgad
871127499Sgad	return (newlist);
87266377Sbrian}
87366377Sbrian
874127499Sgadstatic void
875127499Sgadfree_list(struct listinfo *inf)
876127499Sgad{
877127499Sgad
878127499Sgad	inf->count = inf->elemsize = inf->maxcount = 0;
879127499Sgad	if (inf->ptr != NULL)
880127499Sgad		free(inf->ptr);
881127499Sgad	inf->addelem = NULL;
882127499Sgad	inf->lname = NULL;
883127499Sgad	inf->ptr = NULL;
884127499Sgad}
885127499Sgad
886127499Sgadstatic void
887127499Sgadinit_list(struct listinfo *inf, addelem_rtn artn, int elemsize,
888127499Sgad    const char *lname)
889127499Sgad{
890127499Sgad
891127499Sgad	inf->count = inf->maxcount = 0;
892127499Sgad	inf->elemsize = elemsize;
893127499Sgad	inf->addelem = artn;
894127499Sgad	inf->lname = lname;
895127499Sgad	inf->ptr = NULL;
896127499Sgad}
897127499Sgad
898109502SjmallettVARENT *
899109502Sjmallettfind_varentry(VAR *v)
900109502Sjmallett{
901109502Sjmallett	struct varent *vent;
902109502Sjmallett
903109502Sjmallett	for (vent = vhead; vent; vent = vent->next) {
904109502Sjmallett		if (strcmp(vent->var->name, v->name) == 0)
905109502Sjmallett			return vent;
906109502Sjmallett	}
907109502Sjmallett	return NULL;
908109502Sjmallett}
909109502Sjmallett
9101556Srgrimesstatic void
91190110Simpscanvars(void)
9121556Srgrimes{
9131556Srgrimes	struct varent *vent;
9141556Srgrimes	VAR *v;
91525271Sjkh
91625271Sjkh	for (vent = vhead; vent; vent = vent->next) {
91725271Sjkh		v = vent->var;
91825271Sjkh		if (v->flag & DSIZ) {
91925271Sjkh			v->dwidth = v->width;
92025271Sjkh			v->width = 0;
92125271Sjkh		}
92225271Sjkh		if (v->flag & USER)
92325271Sjkh			needuser = 1;
92425271Sjkh		if (v->flag & COMM)
92525271Sjkh			needcomm = 1;
92625271Sjkh	}
92725271Sjkh}
92825271Sjkh
92925271Sjkhstatic void
93090110Simpdynsizevars(KINFO *ki)
93125271Sjkh{
93225271Sjkh	struct varent *vent;
93325271Sjkh	VAR *v;
9341556Srgrimes	int i;
9351556Srgrimes
9361556Srgrimes	for (vent = vhead; vent; vent = vent->next) {
9371556Srgrimes		v = vent->var;
93825271Sjkh		if (!(v->flag & DSIZ))
93925271Sjkh			continue;
94025271Sjkh		i = (v->sproc)( ki);
94125271Sjkh		if (v->width < i)
94225271Sjkh			v->width = i;
94325271Sjkh		if (v->width > v->dwidth)
94425271Sjkh			v->width = v->dwidth;
94525271Sjkh	}
94625271Sjkh}
94725271Sjkh
94825271Sjkhstatic void
94990110Simpsizevars(void)
95025271Sjkh{
95125271Sjkh	struct varent *vent;
95225271Sjkh	VAR *v;
95325271Sjkh	int i;
95425271Sjkh
95525271Sjkh	for (vent = vhead; vent; vent = vent->next) {
95625271Sjkh		v = vent->var;
957109504Sjmallett		i = strlen(vent->header);
9581556Srgrimes		if (v->width < i)
9591556Srgrimes			v->width = i;
9601556Srgrimes		totwidth += v->width + 1;	/* +1 for space */
9611556Srgrimes	}
9621556Srgrimes	totwidth--;
9631556Srgrimes}
9641556Srgrimes
96590143Smarkmstatic const char *
96690110Simpfmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki,
96790110Simp  char *comm, int maxlen)
9681556Srgrimes{
96990143Smarkm	const char *s;
9701556Srgrimes
97190143Smarkm	s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen);
9721556Srgrimes	return (s);
9731556Srgrimes}
9741556Srgrimes
97571578Sjhb#define UREADOK(ki)	(forceuread || (ki->ki_p->ki_sflag & PS_INMEM))
97631552Sdyson
9771556Srgrimesstatic void
97890110Simpsaveuser(KINFO *ki)
9791556Srgrimes{
9801556Srgrimes
98171578Sjhb	if (ki->ki_p->ki_sflag & PS_INMEM) {
9821556Srgrimes		/*
9831556Srgrimes		 * The u-area might be swapped out, and we can't get
9841556Srgrimes		 * at it because we have a crashdump and no swap.
9851556Srgrimes		 * If it's here fill in these fields, otherwise, just
9861556Srgrimes		 * leave them 0.
9871556Srgrimes		 */
98869896Smckusick		ki->ki_valid = 1;
9891556Srgrimes	} else
99069896Smckusick		ki->ki_valid = 0;
9911556Srgrimes	/*
9921556Srgrimes	 * save arguments if needed
9931556Srgrimes	 */
99469896Smckusick	if (needcomm && (UREADOK(ki) || (ki->ki_p->ki_args != NULL))) {
99590143Smarkm		ki->ki_args = strdup(fmt(kvm_getargv, ki, ki->ki_p->ki_comm,
99690143Smarkm		    MAXCOMLEN));
99731552Sdyson	} else if (needcomm) {
99890143Smarkm		asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm);
99953276Speter	} else {
100053276Speter		ki->ki_args = NULL;
100153276Speter	}
100253276Speter	if (needenv && UREADOK(ki)) {
100390143Smarkm		ki->ki_env = strdup(fmt(kvm_getenvv, ki, (char *)NULL, 0));
100453276Speter	} else if (needenv) {
100553276Speter		ki->ki_env = malloc(3);
100653276Speter		strcpy(ki->ki_env, "()");
100753276Speter	} else {
100853276Speter		ki->ki_env = NULL;
100953276Speter	}
10101556Srgrimes}
10111556Srgrimes
10121556Srgrimesstatic int
101390110Simppscomp(const void *a, const void *b)
10141556Srgrimes{
10151556Srgrimes	int i;
101669896Smckusick#define VSIZE(k) ((k)->ki_p->ki_dsize + (k)->ki_p->ki_ssize + \
101769896Smckusick		  (k)->ki_p->ki_tsize)
10181556Srgrimes
10191556Srgrimes	if (sortby == SORTCPU)
102090143Smarkm		return (getpcpu((const KINFO *)b) - getpcpu((const KINFO *)a));
10211556Srgrimes	if (sortby == SORTMEM)
102290143Smarkm		return (VSIZE((const KINFO *)b) - VSIZE((const KINFO *)a));
102390143Smarkm	i =  (int)((const KINFO *)a)->ki_p->ki_tdev - (int)((const KINFO *)b)->ki_p->ki_tdev;
10241556Srgrimes	if (i == 0)
102590143Smarkm		i = ((const KINFO *)a)->ki_p->ki_pid - ((const KINFO *)b)->ki_p->ki_pid;
10261556Srgrimes	return (i);
10271556Srgrimes}
10281556Srgrimes
10291556Srgrimes/*
10301556Srgrimes * ICK (all for getopt), would rather hide the ugliness
10311556Srgrimes * here than taint the main code.
10321556Srgrimes *
10331556Srgrimes *  ps foo -> ps -foo
10341556Srgrimes *  ps 34 -> ps -p34
10351556Srgrimes *
10361556Srgrimes * The old convention that 't' with no trailing tty arg means the users
10371556Srgrimes * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
10381556Srgrimes * feature is available with the option 'T', which takes no argument.
10391556Srgrimes */
10401556Srgrimesstatic char *
104190110Simpkludge_oldps_options(char *s)
10421556Srgrimes{
1043102886Sjmallett	int have_fmt;
10441556Srgrimes	size_t len;
10451556Srgrimes	char *newopts, *ns, *cp;
10461556Srgrimes
1047102886Sjmallett	/*
1048102886Sjmallett	 * If we have an 'o' option, then note it, since we don't want to do
1049102886Sjmallett	 * some types of munging.
1050102886Sjmallett	 */
1051102886Sjmallett	have_fmt = index(s, 'o') != NULL;
1052102886Sjmallett
10531556Srgrimes	len = strlen(s);
10541556Srgrimes	if ((newopts = ns = malloc(len + 2)) == NULL)
105597877Sjmallett		errx(1, "malloc failed");
10561556Srgrimes	/*
10571556Srgrimes	 * options begin with '-'
10581556Srgrimes	 */
10591556Srgrimes	if (*s != '-')
10601556Srgrimes		*ns++ = '-';	/* add option flag */
10611556Srgrimes	/*
10621556Srgrimes	 * gaze to end of argv[1]
10631556Srgrimes	 */
10641556Srgrimes	cp = s + len - 1;
10651556Srgrimes	/*
10661556Srgrimes	 * if last letter is a 't' flag with no argument (in the context
10671556Srgrimes	 * of the oldps options -- option string NOT starting with a '-' --
10681556Srgrimes	 * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
106981743Sbrian	 *
107081743Sbrian	 * However, if a flag accepting a string argument is found in the
107181743Sbrian	 * option string, the remainder of the string is the argument to
107281743Sbrian	 * that flag; do not modify that argument.
10731556Srgrimes	 */
107489909Sru	if (strcspn(s, "MNOoU") == len && *cp == 't' && *s != '-')
10751556Srgrimes		*cp = 'T';
10761556Srgrimes	else {
10771556Srgrimes		/*
10781556Srgrimes		 * otherwise check for trailing number, which *may* be a
10791556Srgrimes		 * pid.
10801556Srgrimes		 */
10811556Srgrimes		while (cp >= s && isdigit(*cp))
10821556Srgrimes			--cp;
10831556Srgrimes	}
10841556Srgrimes	cp++;
10851556Srgrimes	memmove(ns, s, (size_t)(cp - s));	/* copy up to trailing number */
10861556Srgrimes	ns += cp - s;
10871556Srgrimes	/*
10881556Srgrimes	 * if there's a trailing number, and not a preceding 'p' (pid) or
10891556Srgrimes	 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
10901556Srgrimes	 */
10918855Srgrimes	if (isdigit(*cp) &&
10927165Sjoerg	    (cp == s || (cp[-1] != 't' && cp[-1] != 'p')) &&
1093102886Sjmallett	    (cp - 1 == s || cp[-2] != 't') && !have_fmt)
10941556Srgrimes		*ns++ = 'p';
10951556Srgrimes	(void)strcpy(ns, cp);		/* and append the number */
10961556Srgrimes
10971556Srgrimes	return (newopts);
10981556Srgrimes}
10991556Srgrimes
11001556Srgrimesstatic void
110190110Simpusage(void)
11021556Srgrimes{
11031556Srgrimes
1104127499Sgad	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
1105127499Sgad	    "usage: ps [-aCHhjlmrSTuvwXxZ] [-G gid[,gid]] [-O|o fmt]",
1106127499Sgad	    "          [-p pid[,pid]] [-t tty[,tty]] [-U user[,user]]",
1107127499Sgad	    "          [-M core] [-N system]",
110826465Scharnier	    "       ps [-L]");
11091556Srgrimes	exit(1);
11101556Srgrimes}
1111