xref: /freebsd/bin/ps/ps.c (revision 4e8b6a6fb0aac145419706d2fd726058e7b92e25)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
64b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
74b88c807SRodney W. Grimes  * are met:
84b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
94b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
104b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
124b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
134b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
144b88c807SRodney W. Grimes  *    must display the following acknowledgement:
154b88c807SRodney W. Grimes  *	This product includes software developed by the University of
164b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
174b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
184b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
194b88c807SRodney W. Grimes  *    without specific prior written permission.
204b88c807SRodney W. Grimes  *
214b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314b88c807SRodney W. Grimes  * SUCH DAMAGE.
324b88c807SRodney W. Grimes  */
334b88c807SRodney W. Grimes 
344b88c807SRodney W. Grimes #ifndef lint
35871e8d8cSMark Murray static const char copyright[] =
364b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\
374b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
384b88c807SRodney W. Grimes #endif /* not lint */
394b88c807SRodney W. Grimes 
40c9a8d1f4SPhilippe Charnier #if 0
41871e8d8cSMark Murray #ifndef lint
42c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)ps.c	8.4 (Berkeley) 4/2/94";
434b88c807SRodney W. Grimes #endif /* not lint */
44871e8d8cSMark Murray #endif
45eaed5652SPhilippe Charnier 
462749b141SDavid E. O'Brien #include <sys/cdefs.h>
472749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
484b88c807SRodney W. Grimes 
494b88c807SRodney W. Grimes #include <sys/param.h>
508b6f5f5fSDavid Greenman #include <sys/user.h>
514b88c807SRodney W. Grimes #include <sys/stat.h>
524b88c807SRodney W. Grimes #include <sys/ioctl.h>
534b88c807SRodney W. Grimes #include <sys/sysctl.h>
544b88c807SRodney W. Grimes 
554b88c807SRodney W. Grimes #include <ctype.h>
564b88c807SRodney W. Grimes #include <err.h>
574e8b6a6fSGarance A Drosehn #include <errno.h>
584b88c807SRodney W. Grimes #include <fcntl.h>
594b88c807SRodney W. Grimes #include <kvm.h>
60b2496d93SMike Pritchard #include <limits.h>
6108017519SAndrey A. Chernov #include <locale.h>
624b88c807SRodney W. Grimes #include <paths.h>
63871e8d8cSMark Murray #include <pwd.h>
644b88c807SRodney W. Grimes #include <stdio.h>
654b88c807SRodney W. Grimes #include <stdlib.h>
664b88c807SRodney W. Grimes #include <string.h>
674b88c807SRodney W. Grimes #include <unistd.h>
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes #include "ps.h"
704b88c807SRodney W. Grimes 
71cf22dcfcSBrian Somers #define SEP ", \t"		/* username separators */
72cf22dcfcSBrian Somers 
73871e8d8cSMark Murray static KINFO *kinfo;
74871e8d8cSMark Murray struct varent *vhead;
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes int	eval;			/* exit value */
77db91faacSPeter Wemm int	cflag;			/* -c */
784b88c807SRodney W. Grimes int	rawcpu;			/* -C */
794b88c807SRodney W. Grimes int	sumrusage;		/* -S */
804b88c807SRodney W. Grimes int	termwidth;		/* width of screen (0 == infinity) */
814b88c807SRodney W. Grimes int	totwidth;		/* calculated width of requested variables */
824b88c807SRodney W. Grimes 
83ba2cd770SJuli Mallett time_t	now;			/* current time(3) value */
84ba2cd770SJuli Mallett 
854b88c807SRodney W. Grimes static int needuser, needcomm, needenv;
863ac5e955SJohn Dyson #if defined(LAZY_PS)
873ac5e955SJohn Dyson static int forceuread=0;
883ac5e955SJohn Dyson #else
893ac5e955SJohn Dyson static int forceuread=1;
903ac5e955SJohn Dyson #endif
914b88c807SRodney W. Grimes 
92871e8d8cSMark Murray static enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
934b88c807SRodney W. Grimes 
94871e8d8cSMark Murray static const	 char *fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int),
9546251ddeSWarner Losh 		    KINFO *, char *, int);
9646251ddeSWarner Losh static char	*kludge_oldps_options(char *);
9746251ddeSWarner Losh static int	 pscomp(const void *, const void *);
9846251ddeSWarner Losh static void	 saveuser(KINFO *);
9946251ddeSWarner Losh static void	 scanvars(void);
10046251ddeSWarner Losh static void	 dynsizevars(KINFO *);
10146251ddeSWarner Losh static void	 sizevars(void);
10246251ddeSWarner Losh static void	 usage(void);
1034e8b6a6fSGarance A Drosehn static pid_t	*getpids(const char *, int *);
104cf22dcfcSBrian Somers static uid_t	*getuids(const char *, int *);
1054b88c807SRodney W. Grimes 
106c0716492SJuli Mallett static char dfmt[] = "pid,tt,state,time,command";
107c0716492SJuli Mallett static char jfmt[] = "user,pid,ppid,pgid,jobc,state,tt,time,command";
108c0716492SJuli Mallett static char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state,tt,time,command";
109871e8d8cSMark Murray static char   o1[] = "pid";
110c0716492SJuli Mallett static char   o2[] = "tt,state,time,command";
111c0716492SJuli Mallett static char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command";
112c0716492SJuli Mallett static char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz,%cpu,%mem,command";
1132af538ebSRobert Watson static char Zfmt[] = "label";
1144b88c807SRodney W. Grimes 
115871e8d8cSMark Murray static kvm_t *kd;
1164b88c807SRodney W. Grimes 
11741623b2dSMaxim Sobolev #if defined(LAZY_PS)
11848b8c0deSScott Long #define PS_ARGS	"aCcefgHhjLlM:mN:O:o:p:rSTt:U:uvwxZ"
11941623b2dSMaxim Sobolev #else
12048b8c0deSScott Long #define PS_ARGS	"aCcegHhjLlM:mN:O:o:p:rSTt:U:uvwxZ"
12141623b2dSMaxim Sobolev #endif
12241623b2dSMaxim Sobolev 
1234b88c807SRodney W. Grimes int
12446251ddeSWarner Losh main(int argc, char *argv[])
1254b88c807SRodney W. Grimes {
1264b88c807SRodney W. Grimes 	struct kinfo_proc *kp;
1274b88c807SRodney W. Grimes 	struct varent *vent;
1284b88c807SRodney W. Grimes 	struct winsize ws;
1294b88c807SRodney W. Grimes 	dev_t ttydev;
1304e8b6a6fSGarance A Drosehn 	pid_t *pids;
131cf22dcfcSBrian Somers 	uid_t *uids;
13241623b2dSMaxim Sobolev 	int all, ch, flag, i, _fmt, lineno, nentries, nocludge, dropgid;
1334e8b6a6fSGarance A Drosehn 	int prtheader, wflag, what, xflg, pid, uid, npids, nuids, showthreads;
1344f18100dSTim J. Robbins 	char *cols;
135871e8d8cSMark Murray 	char errbuf[_POSIX2_LINE_MAX];
13641623b2dSMaxim Sobolev 	const char *cp, *nlistf, *memf;
1374b88c807SRodney W. Grimes 
1382bf4b9cfSAndrey A. Chernov 	(void) setlocale(LC_ALL, "");
139ba2cd770SJuli Mallett 	/* Set the time to what it is right now. */
140ba2cd770SJuli Mallett 	time(&now);
1412bf4b9cfSAndrey A. Chernov 
1424f18100dSTim J. Robbins 	if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0')
1434f18100dSTim J. Robbins 		termwidth = atoi(cols);
1444f18100dSTim J. Robbins 	else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1454b88c807SRodney W. Grimes 	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1464b88c807SRodney W. Grimes 	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
1474b88c807SRodney W. Grimes 	     ws.ws_col == 0)
1484b88c807SRodney W. Grimes 		termwidth = 79;
1494b88c807SRodney W. Grimes 	else
1504b88c807SRodney W. Grimes 		termwidth = ws.ws_col - 1;
1514b88c807SRodney W. Grimes 
15241623b2dSMaxim Sobolev 	/*
15341623b2dSMaxim Sobolev 	 * Don't apply a kludge if the first argument is an option taking an
15441623b2dSMaxim Sobolev 	 * argument
15541623b2dSMaxim Sobolev 	 */
15641623b2dSMaxim Sobolev 	if (argc > 1) {
15741623b2dSMaxim Sobolev 		nocludge = 0;
15841623b2dSMaxim Sobolev 		if (argv[1][0] == '-') {
15941623b2dSMaxim Sobolev 			for (cp = PS_ARGS; *cp != '\0'; cp++) {
16041623b2dSMaxim Sobolev 				if (*cp != ':')
16141623b2dSMaxim Sobolev 					continue;
16241623b2dSMaxim Sobolev 				if (*(cp - 1) == argv[1][1]) {
16341623b2dSMaxim Sobolev 					nocludge = 1;
16441623b2dSMaxim Sobolev 					break;
16541623b2dSMaxim Sobolev 				}
16641623b2dSMaxim Sobolev 			}
16741623b2dSMaxim Sobolev 		}
16841623b2dSMaxim Sobolev 		if (nocludge == 0)
1694b88c807SRodney W. Grimes 			argv[1] = kludge_oldps_options(argv[1]);
17041623b2dSMaxim Sobolev 	}
1714b88c807SRodney W. Grimes 
172871e8d8cSMark Murray 	all = _fmt = prtheader = wflag = xflg = 0;
1734e8b6a6fSGarance A Drosehn 	npids = nuids = 0;
1744e8b6a6fSGarance A Drosehn 	pids = uids = NULL;
1754b88c807SRodney W. Grimes 	ttydev = NODEV;
17685082fc3SPoul-Henning Kamp 	dropgid = 0;
177831c910aSRuslan Ermilov 	memf = nlistf = _PATH_DEVNULL;
17848b8c0deSScott Long 	showthreads = 0;
17941623b2dSMaxim Sobolev 	while ((ch = getopt(argc, argv, PS_ARGS)) != -1)
1804b88c807SRodney W. Grimes 		switch((char)ch) {
1814b88c807SRodney W. Grimes 		case 'a':
1824b88c807SRodney W. Grimes 			all = 1;
1834b88c807SRodney W. Grimes 			break;
1844b88c807SRodney W. Grimes 		case 'C':
1854b88c807SRodney W. Grimes 			rawcpu = 1;
1864b88c807SRodney W. Grimes 			break;
187db91faacSPeter Wemm 		case 'c':
188db91faacSPeter Wemm 			cflag = 1;
189db91faacSPeter Wemm 			break;
190db91faacSPeter Wemm 		case 'e':			/* XXX set ufmt */
191db91faacSPeter Wemm 			needenv = 1;
192db91faacSPeter Wemm 			break;
1934b88c807SRodney W. Grimes 		case 'g':
1944b88c807SRodney W. Grimes 			break;			/* no-op */
19548b8c0deSScott Long 		case 'H':
196d75c1d83SDaniel Eischen 			showthreads = KERN_PROC_INC_THREAD;
19748b8c0deSScott Long 			break;
1984b88c807SRodney W. Grimes 		case 'h':
1994b88c807SRodney W. Grimes 			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
2004b88c807SRodney W. Grimes 			break;
2014b88c807SRodney W. Grimes 		case 'j':
202fde411d5SJuli Mallett 			parsefmt(jfmt, 0);
203871e8d8cSMark Murray 			_fmt = 1;
2044b88c807SRodney W. Grimes 			jfmt[0] = '\0';
2054b88c807SRodney W. Grimes 			break;
2064b88c807SRodney W. Grimes 		case 'L':
2074b88c807SRodney W. Grimes 			showkey();
2084b88c807SRodney W. Grimes 			exit(0);
2094b88c807SRodney W. Grimes 		case 'l':
210fde411d5SJuli Mallett 			parsefmt(lfmt, 0);
211871e8d8cSMark Murray 			_fmt = 1;
2124b88c807SRodney W. Grimes 			lfmt[0] = '\0';
2134b88c807SRodney W. Grimes 			break;
2144b88c807SRodney W. Grimes 		case 'M':
2154b88c807SRodney W. Grimes 			memf = optarg;
21685082fc3SPoul-Henning Kamp 			dropgid = 1;
2174b88c807SRodney W. Grimes 			break;
2184b88c807SRodney W. Grimes 		case 'm':
2194b88c807SRodney W. Grimes 			sortby = SORTMEM;
2204b88c807SRodney W. Grimes 			break;
2214b88c807SRodney W. Grimes 		case 'N':
2224b88c807SRodney W. Grimes 			nlistf = optarg;
22385082fc3SPoul-Henning Kamp 			dropgid = 1;
2244b88c807SRodney W. Grimes 			break;
2254b88c807SRodney W. Grimes 		case 'O':
226fde411d5SJuli Mallett 			parsefmt(o1, 1);
227fde411d5SJuli Mallett 			parsefmt(optarg, 1);
228fde411d5SJuli Mallett 			parsefmt(o2, 1);
2294b88c807SRodney W. Grimes 			o1[0] = o2[0] = '\0';
230871e8d8cSMark Murray 			_fmt = 1;
2314b88c807SRodney W. Grimes 			break;
2324b88c807SRodney W. Grimes 		case 'o':
233fde411d5SJuli Mallett 			parsefmt(optarg, 1);
234871e8d8cSMark Murray 			_fmt = 1;
2354b88c807SRodney W. Grimes 			break;
2363ac5e955SJohn Dyson #if defined(LAZY_PS)
2373ac5e955SJohn Dyson 		case 'f':
2383ac5e955SJohn Dyson 			if (getuid() == 0 || getgid() == 0)
2393ac5e955SJohn Dyson 			    forceuread = 1;
2403ac5e955SJohn Dyson 			break;
2413ac5e955SJohn Dyson #endif
2424b88c807SRodney W. Grimes 		case 'p':
2434e8b6a6fSGarance A Drosehn 			pids = getpids(optarg, &npids);
2444b88c807SRodney W. Grimes 			xflg = 1;
2454b88c807SRodney W. Grimes 			break;
2464b88c807SRodney W. Grimes 		case 'r':
2474b88c807SRodney W. Grimes 			sortby = SORTCPU;
2484b88c807SRodney W. Grimes 			break;
2494b88c807SRodney W. Grimes 		case 'S':
2504b88c807SRodney W. Grimes 			sumrusage = 1;
2514b88c807SRodney W. Grimes 			break;
2524b88c807SRodney W. Grimes 		case 'T':
2534b88c807SRodney W. Grimes 			if ((optarg = ttyname(STDIN_FILENO)) == NULL)
2544b88c807SRodney W. Grimes 				errx(1, "stdin: not a terminal");
2554b88c807SRodney W. Grimes 			/* FALLTHROUGH */
2564b88c807SRodney W. Grimes 		case 't': {
2574b88c807SRodney W. Grimes 			struct stat sb;
2585782e272SWarner Losh 			char *ttypath, pathbuf[PATH_MAX];
2594b88c807SRodney W. Grimes 
2604b88c807SRodney W. Grimes 			if (strcmp(optarg, "co") == 0)
261871e8d8cSMark Murray 				ttypath = strdup(_PATH_CONSOLE);
2624b88c807SRodney W. Grimes 			else if (*optarg != '/')
2634b88c807SRodney W. Grimes 				(void)snprintf(ttypath = pathbuf,
2644b88c807SRodney W. Grimes 				    sizeof(pathbuf), "%s%s", _PATH_TTY, optarg);
2654b88c807SRodney W. Grimes 			else
2664b88c807SRodney W. Grimes 				ttypath = optarg;
2674b88c807SRodney W. Grimes 			if (stat(ttypath, &sb) == -1)
2684b88c807SRodney W. Grimes 				err(1, "%s", ttypath);
2694b88c807SRodney W. Grimes 			if (!S_ISCHR(sb.st_mode))
2704b88c807SRodney W. Grimes 				errx(1, "%s: not a terminal", ttypath);
2714b88c807SRodney W. Grimes 			ttydev = sb.st_rdev;
2724b88c807SRodney W. Grimes 			break;
2734b88c807SRodney W. Grimes 		}
27473eb8310SPeter Wemm 		case 'U':
275cf22dcfcSBrian Somers 			uids = getuids(optarg, &nuids);
27673eb8310SPeter Wemm 			xflg++;		/* XXX: intuitive? */
27773eb8310SPeter Wemm 			break;
2784b88c807SRodney W. Grimes 		case 'u':
279fde411d5SJuli Mallett 			parsefmt(ufmt, 0);
2804b88c807SRodney W. Grimes 			sortby = SORTCPU;
281871e8d8cSMark Murray 			_fmt = 1;
2824b88c807SRodney W. Grimes 			ufmt[0] = '\0';
2834b88c807SRodney W. Grimes 			break;
2844b88c807SRodney W. Grimes 		case 'v':
285fde411d5SJuli Mallett 			parsefmt(vfmt, 0);
2864b88c807SRodney W. Grimes 			sortby = SORTMEM;
287871e8d8cSMark Murray 			_fmt = 1;
2884b88c807SRodney W. Grimes 			vfmt[0] = '\0';
2894b88c807SRodney W. Grimes 			break;
2904b88c807SRodney W. Grimes 		case 'w':
2914b88c807SRodney W. Grimes 			if (wflag)
2924b88c807SRodney W. Grimes 				termwidth = UNLIMITED;
2934b88c807SRodney W. Grimes 			else if (termwidth < 131)
2944b88c807SRodney W. Grimes 				termwidth = 131;
2954b88c807SRodney W. Grimes 			wflag++;
2964b88c807SRodney W. Grimes 			break;
2974b88c807SRodney W. Grimes 		case 'x':
2984b88c807SRodney W. Grimes 			xflg = 1;
2994b88c807SRodney W. Grimes 			break;
3007304f61fSBrian Feldman 		case 'Z':
301fde411d5SJuli Mallett 			parsefmt(Zfmt, 0);
3027304f61fSBrian Feldman 			Zfmt[0] = '\0';
3037304f61fSBrian Feldman 			break;
3044b88c807SRodney W. Grimes 		case '?':
3054b88c807SRodney W. Grimes 		default:
3064b88c807SRodney W. Grimes 			usage();
3074b88c807SRodney W. Grimes 		}
3084b88c807SRodney W. Grimes 	argc -= optind;
3094b88c807SRodney W. Grimes 	argv += optind;
3104b88c807SRodney W. Grimes 
3114b88c807SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
3124b88c807SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
3134b88c807SRodney W. Grimes 	if (*argv) {
3144b88c807SRodney W. Grimes 		nlistf = *argv;
3154b88c807SRodney W. Grimes 		if (*++argv) {
3164b88c807SRodney W. Grimes 			memf = *argv;
3174b88c807SRodney W. Grimes 		}
3184b88c807SRodney W. Grimes 	}
3194b88c807SRodney W. Grimes #endif
3204b88c807SRodney W. Grimes 	/*
3214b88c807SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
3224b88c807SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
3234b88c807SRodney W. Grimes 	 */
32485082fc3SPoul-Henning Kamp 	if (dropgid) {
3254b88c807SRodney W. Grimes 		setgid(getgid());
32685082fc3SPoul-Henning Kamp 		setuid(getuid());
32785082fc3SPoul-Henning Kamp 	}
3284b88c807SRodney W. Grimes 
329831c910aSRuslan Ermilov 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
3304b88c807SRodney W. Grimes 	if (kd == 0)
3314b88c807SRodney W. Grimes 		errx(1, "%s", errbuf);
3324b88c807SRodney W. Grimes 
333871e8d8cSMark Murray 	if (!_fmt)
334fde411d5SJuli Mallett 		parsefmt(dfmt, 0);
3354b88c807SRodney W. Grimes 
33673eb8310SPeter Wemm 	/* XXX - should be cleaner */
3374e8b6a6fSGarance A Drosehn 	if (!all && ttydev == NODEV && !npids && !nuids) {
338cf22dcfcSBrian Somers 		if ((uids = malloc(sizeof (*uids))) == NULL)
3394fa7d788SJuli Mallett 			errx(1, "malloc failed");
340cf22dcfcSBrian Somers 		nuids = 1;
341cf22dcfcSBrian Somers 		*uids = getuid();
342cf22dcfcSBrian Somers 	}
3434b88c807SRodney W. Grimes 
3444b88c807SRodney W. Grimes 	/*
3454b88c807SRodney W. Grimes 	 * scan requested variables, noting what structures are needed,
346bdfebd84SKris Kennaway 	 * and adjusting header widths as appropriate.
3474b88c807SRodney W. Grimes 	 */
3484b88c807SRodney W. Grimes 	scanvars();
3494b88c807SRodney W. Grimes 	/*
3504b88c807SRodney W. Grimes 	 * get proc list
3514b88c807SRodney W. Grimes 	 */
352cf22dcfcSBrian Somers 	if (nuids == 1) {
353d75c1d83SDaniel Eischen 		what = KERN_PROC_UID | showthreads;
354cf22dcfcSBrian Somers 		flag = *uids;
3554b88c807SRodney W. Grimes 	} else if (ttydev != NODEV) {
356d75c1d83SDaniel Eischen 		what = KERN_PROC_TTY | showthreads;
3574b88c807SRodney W. Grimes 		flag = ttydev;
3584e8b6a6fSGarance A Drosehn 	} else if (npids == 1) {
359d75c1d83SDaniel Eischen 		what = KERN_PROC_PID | showthreads;
3604e8b6a6fSGarance A Drosehn 		flag = *pids;
36148b8c0deSScott Long 	} else {
362d75c1d83SDaniel Eischen 		what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC;
36348b8c0deSScott Long 		flag = 0;
3644b88c807SRodney W. Grimes 	}
365d75c1d83SDaniel Eischen 
3664b88c807SRodney W. Grimes 	/*
3674b88c807SRodney W. Grimes 	 * select procs
3684b88c807SRodney W. Grimes 	 */
3694e8b6a6fSGarance A Drosehn 	kp = kvm_getprocs(kd, what, flag, &nentries);
3704e8b6a6fSGarance A Drosehn 	if ((kp == 0 && nentries != 0) || nentries < 0)
3714b88c807SRodney W. Grimes 		errx(1, "%s", kvm_geterr(kd));
3724e8b6a6fSGarance A Drosehn 	if (nentries > 0) {
3734b88c807SRodney W. Grimes 		if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL)
3744fa7d788SJuli Mallett 			errx(1, "malloc failed");
3754b88c807SRodney W. Grimes 		for (i = nentries; --i >= 0; ++kp) {
3764b88c807SRodney W. Grimes 			kinfo[i].ki_p = kp;
3774b88c807SRodney W. Grimes 			if (needuser)
3784b88c807SRodney W. Grimes 				saveuser(&kinfo[i]);
3796a2d726bSJordan K. Hubbard 			dynsizevars(&kinfo[i]);
3804b88c807SRodney W. Grimes 		}
3814e8b6a6fSGarance A Drosehn 	}
3826a2d726bSJordan K. Hubbard 
3836a2d726bSJordan K. Hubbard 	sizevars();
3846a2d726bSJordan K. Hubbard 
3854b88c807SRodney W. Grimes 	/*
3864b88c807SRodney W. Grimes 	 * print header
3874b88c807SRodney W. Grimes 	 */
3884b88c807SRodney W. Grimes 	printheader();
3894b88c807SRodney W. Grimes 	if (nentries == 0)
390f8c9c11cSWill Andrews 		exit(1);
3914b88c807SRodney W. Grimes 	/*
3924b88c807SRodney W. Grimes 	 * sort proc list
3934b88c807SRodney W. Grimes 	 */
3944b88c807SRodney W. Grimes 	qsort(kinfo, nentries, sizeof(KINFO), pscomp);
3954b88c807SRodney W. Grimes 	/*
3964b88c807SRodney W. Grimes 	 * for each proc, call each variable output function.
3974b88c807SRodney W. Grimes 	 */
3984b88c807SRodney W. Grimes 	for (i = lineno = 0; i < nentries; i++) {
3991f7d2501SKirk McKusick 		if (xflg == 0 && ((&kinfo[i])->ki_p->ki_tdev == NODEV ||
4001f7d2501SKirk McKusick 		    ((&kinfo[i])->ki_p->ki_flag & P_CONTROLT ) == 0))
4014b88c807SRodney W. Grimes 			continue;
4024e8b6a6fSGarance A Drosehn 		if (npids > 1) {
4034e8b6a6fSGarance A Drosehn 			for (pid = 0; pid < npids; pid++)
4044e8b6a6fSGarance A Drosehn 				if ((&kinfo[i])->ki_p->ki_pid == pids[pid])
4054e8b6a6fSGarance A Drosehn 					break;
4064e8b6a6fSGarance A Drosehn 			if (pid == npids)
4074e8b6a6fSGarance A Drosehn 				continue;
4084e8b6a6fSGarance A Drosehn 		}
409cf22dcfcSBrian Somers 		if (nuids > 1) {
410cf22dcfcSBrian Somers 			for (uid = 0; uid < nuids; uid++)
4111f7d2501SKirk McKusick 				if ((&kinfo[i])->ki_p->ki_uid == uids[uid])
412cf22dcfcSBrian Somers 					break;
413cf22dcfcSBrian Somers 			if (uid == nuids)
414cf22dcfcSBrian Somers 				continue;
415cf22dcfcSBrian Somers 		}
4164b88c807SRodney W. Grimes 		for (vent = vhead; vent; vent = vent->next) {
4174b88c807SRodney W. Grimes 			(vent->var->oproc)(&kinfo[i], vent);
4184b88c807SRodney W. Grimes 			if (vent->next != NULL)
4194b88c807SRodney W. Grimes 				(void)putchar(' ');
4204b88c807SRodney W. Grimes 		}
4214b88c807SRodney W. Grimes 		(void)putchar('\n');
4224b88c807SRodney W. Grimes 		if (prtheader && lineno++ == prtheader - 4) {
4234b88c807SRodney W. Grimes 			(void)putchar('\n');
4244b88c807SRodney W. Grimes 			printheader();
4254b88c807SRodney W. Grimes 			lineno = 0;
4264b88c807SRodney W. Grimes 		}
4274b88c807SRodney W. Grimes 	}
428cf22dcfcSBrian Somers 	free(uids);
429cf22dcfcSBrian Somers 
4304b88c807SRodney W. Grimes 	exit(eval);
4314b88c807SRodney W. Grimes }
4324b88c807SRodney W. Grimes 
4334e8b6a6fSGarance A Drosehn #define	BSD_PID_MAX	99999		/* Copy of PID_MAX from sys/proc.h */
4344e8b6a6fSGarance A Drosehn pid_t *
4354e8b6a6fSGarance A Drosehn getpids(const char *arg, int *npids)
4364e8b6a6fSGarance A Drosehn {
4374e8b6a6fSGarance A Drosehn 	char copyarg[32];
4384e8b6a6fSGarance A Drosehn 	char *copyp, *endp;
4394e8b6a6fSGarance A Drosehn 	pid_t *pids, *morepids;
4404e8b6a6fSGarance A Drosehn 	int alloc;
4414e8b6a6fSGarance A Drosehn 	long tempid;
4424e8b6a6fSGarance A Drosehn 
4434e8b6a6fSGarance A Drosehn 	alloc = 0;
4444e8b6a6fSGarance A Drosehn 	*npids = 0;
4454e8b6a6fSGarance A Drosehn 	pids = NULL;
4464e8b6a6fSGarance A Drosehn 	while (*arg != '\0') {
4474e8b6a6fSGarance A Drosehn 		while (*arg != '\0' && strchr(SEP, *arg) != NULL)
4484e8b6a6fSGarance A Drosehn 			arg++;
4494e8b6a6fSGarance A Drosehn 		if (*arg == '\0' || strchr(SEP, *arg) != NULL)
4504e8b6a6fSGarance A Drosehn 			tempid = 0;
4514e8b6a6fSGarance A Drosehn 		else {
4524e8b6a6fSGarance A Drosehn 			copyp = copyarg;
4534e8b6a6fSGarance A Drosehn 			endp = copyarg + sizeof(copyarg) - 1;
4544e8b6a6fSGarance A Drosehn 			while (*arg != '\0' && strchr(SEP, *arg) == NULL &&
4554e8b6a6fSGarance A Drosehn 			    copyp <= endp)
4564e8b6a6fSGarance A Drosehn 				*copyp++ = *arg++;
4574e8b6a6fSGarance A Drosehn 			if (copyp > endp) {
4584e8b6a6fSGarance A Drosehn 				*endp = '\0';
4594e8b6a6fSGarance A Drosehn 				tempid = -1;
4604e8b6a6fSGarance A Drosehn 				while (*arg != '\0' &&
4614e8b6a6fSGarance A Drosehn 				    strchr(SEP, *arg) == NULL)
4624e8b6a6fSGarance A Drosehn 					arg++;
4634e8b6a6fSGarance A Drosehn 			} else {
4644e8b6a6fSGarance A Drosehn 				*copyp = '\0';
4654e8b6a6fSGarance A Drosehn 				errno = 0;
4664e8b6a6fSGarance A Drosehn 				tempid = strtol(copyarg, &endp, 10);
4674e8b6a6fSGarance A Drosehn 			}
4684e8b6a6fSGarance A Drosehn 			/*
4694e8b6a6fSGarance A Drosehn 			 * Write warning messages for any values which
4704e8b6a6fSGarance A Drosehn 			 * would never be a valid process number.
4714e8b6a6fSGarance A Drosehn 			 */
4724e8b6a6fSGarance A Drosehn 			if (*endp != '\0' || tempid < 0 || copyarg == endp) {
4734e8b6a6fSGarance A Drosehn 				warnx("invalid process number: %s", copyarg);
4744e8b6a6fSGarance A Drosehn 				errno = ERANGE;
4754e8b6a6fSGarance A Drosehn 			} else if (errno != 0 || tempid > BSD_PID_MAX) {
4764e8b6a6fSGarance A Drosehn 				warnx("process number too large: %s", copyarg);
4774e8b6a6fSGarance A Drosehn 				errno = ERANGE;
4784e8b6a6fSGarance A Drosehn 			}
4794e8b6a6fSGarance A Drosehn 			if (errno == ERANGE) {
4804e8b6a6fSGarance A Drosehn 				/* Ignore this value from the given list. */
4814e8b6a6fSGarance A Drosehn 				continue;
4824e8b6a6fSGarance A Drosehn 			}
4834e8b6a6fSGarance A Drosehn 		}
4844e8b6a6fSGarance A Drosehn 		if (*npids >= alloc) {
4854e8b6a6fSGarance A Drosehn 			alloc = (alloc + 1) << 1;
4864e8b6a6fSGarance A Drosehn 			morepids = realloc(pids, alloc * sizeof (*pids));
4874e8b6a6fSGarance A Drosehn 			if (morepids == NULL) {
4884e8b6a6fSGarance A Drosehn 				free(pids);
4894e8b6a6fSGarance A Drosehn 				errx(1, "realloc failed");
4904e8b6a6fSGarance A Drosehn 			}
4914e8b6a6fSGarance A Drosehn 			pids = morepids;
4924e8b6a6fSGarance A Drosehn 		}
4934e8b6a6fSGarance A Drosehn 		pids[(*npids)++] = tempid;
4944e8b6a6fSGarance A Drosehn 	}
4954e8b6a6fSGarance A Drosehn 
4964e8b6a6fSGarance A Drosehn 	if (!*npids)
4974e8b6a6fSGarance A Drosehn 		errx(1, "No valid process numbers specified");
4984e8b6a6fSGarance A Drosehn 
4994e8b6a6fSGarance A Drosehn 	return (pids);
5004e8b6a6fSGarance A Drosehn }
5014e8b6a6fSGarance A Drosehn 
502cf22dcfcSBrian Somers uid_t *
503cf22dcfcSBrian Somers getuids(const char *arg, int *nuids)
504cf22dcfcSBrian Somers {
505a755f1c9SRobert Drehmel 	char name[MAXLOGNAME];
506cf22dcfcSBrian Somers 	struct passwd *pwd;
507cf22dcfcSBrian Somers 	uid_t *uids, *moreuids;
508871e8d8cSMark Murray 	int alloc;
509871e8d8cSMark Murray 	size_t l;
510cf22dcfcSBrian Somers 
511cf22dcfcSBrian Somers 
512cf22dcfcSBrian Somers 	alloc = 0;
513cf22dcfcSBrian Somers 	*nuids = 0;
514cf22dcfcSBrian Somers 	uids = NULL;
515cf22dcfcSBrian Somers 	for (; (l = strcspn(arg, SEP)) > 0; arg += l + strspn(arg + l, SEP)) {
516cf22dcfcSBrian Somers 		if (l >= sizeof name) {
517871e8d8cSMark Murray 			warnx("%.*s: name too long", (int)l, arg);
518cf22dcfcSBrian Somers 			continue;
519cf22dcfcSBrian Somers 		}
520cf22dcfcSBrian Somers 		strncpy(name, arg, l);
521cf22dcfcSBrian Somers 		name[l] = '\0';
522cf22dcfcSBrian Somers 		if ((pwd = getpwnam(name)) == NULL) {
523cf22dcfcSBrian Somers 			warnx("%s: no such user", name);
524cf22dcfcSBrian Somers 			continue;
525cf22dcfcSBrian Somers 		}
526cf22dcfcSBrian Somers 		if (*nuids >= alloc) {
527cf22dcfcSBrian Somers 			alloc = (alloc + 1) << 1;
528cf22dcfcSBrian Somers 			moreuids = realloc(uids, alloc * sizeof (*uids));
529cf22dcfcSBrian Somers 			if (moreuids == NULL) {
530cf22dcfcSBrian Somers 				free(uids);
5314fa7d788SJuli Mallett 				errx(1, "realloc failed");
532cf22dcfcSBrian Somers 			}
533cf22dcfcSBrian Somers 			uids = moreuids;
534cf22dcfcSBrian Somers 		}
535cf22dcfcSBrian Somers 		uids[(*nuids)++] = pwd->pw_uid;
536cf22dcfcSBrian Somers 	}
537cf22dcfcSBrian Somers 	endpwent();
538cf22dcfcSBrian Somers 
539cf22dcfcSBrian Somers 	if (!*nuids)
540cf22dcfcSBrian Somers 		errx(1, "No users specified");
541cf22dcfcSBrian Somers 
542cf22dcfcSBrian Somers 	return uids;
543cf22dcfcSBrian Somers }
544cf22dcfcSBrian Somers 
545fde411d5SJuli Mallett VARENT *
546fde411d5SJuli Mallett find_varentry(VAR *v)
547fde411d5SJuli Mallett {
548fde411d5SJuli Mallett 	struct varent *vent;
549fde411d5SJuli Mallett 
550fde411d5SJuli Mallett 	for (vent = vhead; vent; vent = vent->next) {
551fde411d5SJuli Mallett 		if (strcmp(vent->var->name, v->name) == 0)
552fde411d5SJuli Mallett 			return vent;
553fde411d5SJuli Mallett 	}
554fde411d5SJuli Mallett 	return NULL;
555fde411d5SJuli Mallett }
556fde411d5SJuli Mallett 
5574b88c807SRodney W. Grimes static void
55846251ddeSWarner Losh scanvars(void)
5594b88c807SRodney W. Grimes {
5604b88c807SRodney W. Grimes 	struct varent *vent;
5614b88c807SRodney W. Grimes 	VAR *v;
5626a2d726bSJordan K. Hubbard 
5636a2d726bSJordan K. Hubbard 	for (vent = vhead; vent; vent = vent->next) {
5646a2d726bSJordan K. Hubbard 		v = vent->var;
5656a2d726bSJordan K. Hubbard 		if (v->flag & DSIZ) {
5666a2d726bSJordan K. Hubbard 			v->dwidth = v->width;
5676a2d726bSJordan K. Hubbard 			v->width = 0;
5686a2d726bSJordan K. Hubbard 		}
5696a2d726bSJordan K. Hubbard 		if (v->flag & USER)
5706a2d726bSJordan K. Hubbard 			needuser = 1;
5716a2d726bSJordan K. Hubbard 		if (v->flag & COMM)
5726a2d726bSJordan K. Hubbard 			needcomm = 1;
5736a2d726bSJordan K. Hubbard 	}
5746a2d726bSJordan K. Hubbard }
5756a2d726bSJordan K. Hubbard 
5766a2d726bSJordan K. Hubbard static void
57746251ddeSWarner Losh dynsizevars(KINFO *ki)
5786a2d726bSJordan K. Hubbard {
5796a2d726bSJordan K. Hubbard 	struct varent *vent;
5806a2d726bSJordan K. Hubbard 	VAR *v;
5816a2d726bSJordan K. Hubbard 	int i;
5826a2d726bSJordan K. Hubbard 
5836a2d726bSJordan K. Hubbard 	for (vent = vhead; vent; vent = vent->next) {
5846a2d726bSJordan K. Hubbard 		v = vent->var;
5856a2d726bSJordan K. Hubbard 		if (!(v->flag & DSIZ))
5866a2d726bSJordan K. Hubbard 			continue;
5876a2d726bSJordan K. Hubbard 		i = (v->sproc)( ki);
5886a2d726bSJordan K. Hubbard 		if (v->width < i)
5896a2d726bSJordan K. Hubbard 			v->width = i;
5906a2d726bSJordan K. Hubbard 		if (v->width > v->dwidth)
5916a2d726bSJordan K. Hubbard 			v->width = v->dwidth;
5926a2d726bSJordan K. Hubbard 	}
5936a2d726bSJordan K. Hubbard }
5946a2d726bSJordan K. Hubbard 
5956a2d726bSJordan K. Hubbard static void
59646251ddeSWarner Losh sizevars(void)
5976a2d726bSJordan K. Hubbard {
5986a2d726bSJordan K. Hubbard 	struct varent *vent;
5996a2d726bSJordan K. Hubbard 	VAR *v;
6004b88c807SRodney W. Grimes 	int i;
6014b88c807SRodney W. Grimes 
6024b88c807SRodney W. Grimes 	for (vent = vhead; vent; vent = vent->next) {
6034b88c807SRodney W. Grimes 		v = vent->var;
60478b1878aSJuli Mallett 		i = strlen(vent->header);
6054b88c807SRodney W. Grimes 		if (v->width < i)
6064b88c807SRodney W. Grimes 			v->width = i;
6074b88c807SRodney W. Grimes 		totwidth += v->width + 1;	/* +1 for space */
6084b88c807SRodney W. Grimes 	}
6094b88c807SRodney W. Grimes 	totwidth--;
6104b88c807SRodney W. Grimes }
6114b88c807SRodney W. Grimes 
612871e8d8cSMark Murray static const char *
61346251ddeSWarner Losh fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki,
61446251ddeSWarner Losh   char *comm, int maxlen)
6154b88c807SRodney W. Grimes {
616871e8d8cSMark Murray 	const char *s;
6174b88c807SRodney W. Grimes 
618871e8d8cSMark Murray 	s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen);
6194b88c807SRodney W. Grimes 	return (s);
6204b88c807SRodney W. Grimes }
6214b88c807SRodney W. Grimes 
622e0aa5ab7SJohn Baldwin #define UREADOK(ki)	(forceuread || (ki->ki_p->ki_sflag & PS_INMEM))
6233ac5e955SJohn Dyson 
6244b88c807SRodney W. Grimes static void
62546251ddeSWarner Losh saveuser(KINFO *ki)
6264b88c807SRodney W. Grimes {
6274b88c807SRodney W. Grimes 
628e0aa5ab7SJohn Baldwin 	if (ki->ki_p->ki_sflag & PS_INMEM) {
6294b88c807SRodney W. Grimes 		/*
6304b88c807SRodney W. Grimes 		 * The u-area might be swapped out, and we can't get
6314b88c807SRodney W. Grimes 		 * at it because we have a crashdump and no swap.
6324b88c807SRodney W. Grimes 		 * If it's here fill in these fields, otherwise, just
6334b88c807SRodney W. Grimes 		 * leave them 0.
6344b88c807SRodney W. Grimes 		 */
6351f7d2501SKirk McKusick 		ki->ki_valid = 1;
6364b88c807SRodney W. Grimes 	} else
6371f7d2501SKirk McKusick 		ki->ki_valid = 0;
6384b88c807SRodney W. Grimes 	/*
6394b88c807SRodney W. Grimes 	 * save arguments if needed
6404b88c807SRodney W. Grimes 	 */
6411f7d2501SKirk McKusick 	if (needcomm && (UREADOK(ki) || (ki->ki_p->ki_args != NULL))) {
642871e8d8cSMark Murray 		ki->ki_args = strdup(fmt(kvm_getargv, ki, ki->ki_p->ki_comm,
643871e8d8cSMark Murray 		    MAXCOMLEN));
6443ac5e955SJohn Dyson 	} else if (needcomm) {
645871e8d8cSMark Murray 		asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm);
6463ac5e955SJohn Dyson 	} else {
6474b88c807SRodney W. Grimes 		ki->ki_args = NULL;
6483ac5e955SJohn Dyson 	}
6493ac5e955SJohn Dyson 	if (needenv && UREADOK(ki)) {
650871e8d8cSMark Murray 		ki->ki_env = strdup(fmt(kvm_getenvv, ki, (char *)NULL, 0));
6513ac5e955SJohn Dyson 	} else if (needenv) {
6523ac5e955SJohn Dyson 		ki->ki_env = malloc(3);
6533ac5e955SJohn Dyson 		strcpy(ki->ki_env, "()");
6543ac5e955SJohn Dyson 	} else {
6554b88c807SRodney W. Grimes 		ki->ki_env = NULL;
6564b88c807SRodney W. Grimes 	}
6573ac5e955SJohn Dyson }
6584b88c807SRodney W. Grimes 
6594b88c807SRodney W. Grimes static int
66046251ddeSWarner Losh pscomp(const void *a, const void *b)
6614b88c807SRodney W. Grimes {
6624b88c807SRodney W. Grimes 	int i;
6631f7d2501SKirk McKusick #define VSIZE(k) ((k)->ki_p->ki_dsize + (k)->ki_p->ki_ssize + \
6641f7d2501SKirk McKusick 		  (k)->ki_p->ki_tsize)
6654b88c807SRodney W. Grimes 
6664b88c807SRodney W. Grimes 	if (sortby == SORTCPU)
667871e8d8cSMark Murray 		return (getpcpu((const KINFO *)b) - getpcpu((const KINFO *)a));
6684b88c807SRodney W. Grimes 	if (sortby == SORTMEM)
669871e8d8cSMark Murray 		return (VSIZE((const KINFO *)b) - VSIZE((const KINFO *)a));
670871e8d8cSMark Murray 	i =  (int)((const KINFO *)a)->ki_p->ki_tdev - (int)((const KINFO *)b)->ki_p->ki_tdev;
6714b88c807SRodney W. Grimes 	if (i == 0)
672871e8d8cSMark Murray 		i = ((const KINFO *)a)->ki_p->ki_pid - ((const KINFO *)b)->ki_p->ki_pid;
6734b88c807SRodney W. Grimes 	return (i);
6744b88c807SRodney W. Grimes }
6754b88c807SRodney W. Grimes 
6764b88c807SRodney W. Grimes /*
6774b88c807SRodney W. Grimes  * ICK (all for getopt), would rather hide the ugliness
6784b88c807SRodney W. Grimes  * here than taint the main code.
6794b88c807SRodney W. Grimes  *
6804b88c807SRodney W. Grimes  *  ps foo -> ps -foo
6814b88c807SRodney W. Grimes  *  ps 34 -> ps -p34
6824b88c807SRodney W. Grimes  *
6834b88c807SRodney W. Grimes  * The old convention that 't' with no trailing tty arg means the users
6844b88c807SRodney W. Grimes  * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
6854b88c807SRodney W. Grimes  * feature is available with the option 'T', which takes no argument.
6864b88c807SRodney W. Grimes  */
6874b88c807SRodney W. Grimes static char *
68846251ddeSWarner Losh kludge_oldps_options(char *s)
6894b88c807SRodney W. Grimes {
690daed3ad6SJuli Mallett 	int have_fmt;
6914b88c807SRodney W. Grimes 	size_t len;
6924b88c807SRodney W. Grimes 	char *newopts, *ns, *cp;
6934b88c807SRodney W. Grimes 
694daed3ad6SJuli Mallett 	/*
695daed3ad6SJuli Mallett 	 * If we have an 'o' option, then note it, since we don't want to do
696daed3ad6SJuli Mallett 	 * some types of munging.
697daed3ad6SJuli Mallett 	 */
698daed3ad6SJuli Mallett 	have_fmt = index(s, 'o') != NULL;
699daed3ad6SJuli Mallett 
7004b88c807SRodney W. Grimes 	len = strlen(s);
7014b88c807SRodney W. Grimes 	if ((newopts = ns = malloc(len + 2)) == NULL)
7024fa7d788SJuli Mallett 		errx(1, "malloc failed");
7034b88c807SRodney W. Grimes 	/*
7044b88c807SRodney W. Grimes 	 * options begin with '-'
7054b88c807SRodney W. Grimes 	 */
7064b88c807SRodney W. Grimes 	if (*s != '-')
7074b88c807SRodney W. Grimes 		*ns++ = '-';	/* add option flag */
7084b88c807SRodney W. Grimes 	/*
7094b88c807SRodney W. Grimes 	 * gaze to end of argv[1]
7104b88c807SRodney W. Grimes 	 */
7114b88c807SRodney W. Grimes 	cp = s + len - 1;
7124b88c807SRodney W. Grimes 	/*
7134b88c807SRodney W. Grimes 	 * if last letter is a 't' flag with no argument (in the context
7144b88c807SRodney W. Grimes 	 * of the oldps options -- option string NOT starting with a '-' --
7154b88c807SRodney W. Grimes 	 * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
716380434d4SBrian Somers 	 *
717380434d4SBrian Somers 	 * However, if a flag accepting a string argument is found in the
718380434d4SBrian Somers 	 * option string, the remainder of the string is the argument to
719380434d4SBrian Somers 	 * that flag; do not modify that argument.
7204b88c807SRodney W. Grimes 	 */
721831c910aSRuslan Ermilov 	if (strcspn(s, "MNOoU") == len && *cp == 't' && *s != '-')
7224b88c807SRodney W. Grimes 		*cp = 'T';
7234b88c807SRodney W. Grimes 	else {
7244b88c807SRodney W. Grimes 		/*
7254b88c807SRodney W. Grimes 		 * otherwise check for trailing number, which *may* be a
7264b88c807SRodney W. Grimes 		 * pid.
7274b88c807SRodney W. Grimes 		 */
7284b88c807SRodney W. Grimes 		while (cp >= s && isdigit(*cp))
7294b88c807SRodney W. Grimes 			--cp;
7304b88c807SRodney W. Grimes 	}
7314b88c807SRodney W. Grimes 	cp++;
7324b88c807SRodney W. Grimes 	memmove(ns, s, (size_t)(cp - s));	/* copy up to trailing number */
7334b88c807SRodney W. Grimes 	ns += cp - s;
7344b88c807SRodney W. Grimes 	/*
7354b88c807SRodney W. Grimes 	 * if there's a trailing number, and not a preceding 'p' (pid) or
7364b88c807SRodney W. Grimes 	 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
7374b88c807SRodney W. Grimes 	 */
7380fd510b7SJoerg Wunsch 	if (isdigit(*cp) &&
7390fd510b7SJoerg Wunsch 	    (cp == s || (cp[-1] != 't' && cp[-1] != 'p')) &&
740daed3ad6SJuli Mallett 	    (cp - 1 == s || cp[-2] != 't') && !have_fmt)
7414b88c807SRodney W. Grimes 		*ns++ = 'p';
7424b88c807SRodney W. Grimes 	(void)strcpy(ns, cp);		/* and append the number */
7434b88c807SRodney W. Grimes 
7444b88c807SRodney W. Grimes 	return (newopts);
7454b88c807SRodney W. Grimes }
7464b88c807SRodney W. Grimes 
7474b88c807SRodney W. Grimes static void
74846251ddeSWarner Losh usage(void)
7494b88c807SRodney W. Grimes {
7504b88c807SRodney W. Grimes 
751749d4bb6SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n%s\n",
7524e8b6a6fSGarance A Drosehn 	    "usage: ps [-aCHhjlmrSTuvwxZ] [-O|o fmt] [-p pid[,pid]] [-t tty]",
7534e8b6a6fSGarance A Drosehn 	    "          [-U user[,user]] [-M core] [-N system]",
7544b88c807SRodney W. Grimes 	    "       ps [-L]");
7554b88c807SRodney W. Grimes 	exit(1);
7564b88c807SRodney W. Grimes }
757