ps.c revision 3296
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.
323044Sdg *
333296Sdg *	$Id: ps.c,v 1.3 1994/10/02 08:19:13 davidg Exp $
341556Srgrimes */
351556Srgrimes
361556Srgrimes#ifndef lint
371556Srgrimesstatic char copyright[] =
381556Srgrimes"@(#) Copyright (c) 1990, 1993, 1994\n\
391556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
401556Srgrimes#endif /* not lint */
411556Srgrimes
421556Srgrimes#ifndef lint
431556Srgrimesstatic char sccsid[] = "@(#)ps.c	8.4 (Berkeley) 4/2/94";
441556Srgrimes#endif /* not lint */
451556Srgrimes
461556Srgrimes#include <sys/param.h>
473296Sdg#include <sys/user.h>
481556Srgrimes#include <sys/time.h>
491556Srgrimes#include <sys/resource.h>
501556Srgrimes#include <sys/proc.h>
511556Srgrimes#include <sys/stat.h>
521556Srgrimes#include <sys/ioctl.h>
531556Srgrimes#include <sys/sysctl.h>
541556Srgrimes
551556Srgrimes#include <ctype.h>
561556Srgrimes#include <err.h>
571556Srgrimes#include <errno.h>
581556Srgrimes#include <fcntl.h>
591556Srgrimes#include <kvm.h>
601556Srgrimes#include <nlist.h>
611556Srgrimes#include <paths.h>
621556Srgrimes#include <stdio.h>
631556Srgrimes#include <stdlib.h>
641556Srgrimes#include <string.h>
651556Srgrimes#include <unistd.h>
661556Srgrimes
671556Srgrimes#include "ps.h"
681556Srgrimes
691556Srgrimes#ifdef P_PPWAIT
701556Srgrimes#define NEWVM
711556Srgrimes#endif
721556Srgrimes
731556SrgrimesKINFO *kinfo;
741556Srgrimesstruct varent *vhead, *vtail;
751556Srgrimes
761556Srgrimesint	eval;			/* exit value */
771556Srgrimesint	rawcpu;			/* -C */
781556Srgrimesint	sumrusage;		/* -S */
791556Srgrimesint	termwidth;		/* width of screen (0 == infinity) */
801556Srgrimesint	totwidth;		/* calculated width of requested variables */
811556Srgrimes
821556Srgrimesstatic int needuser, needcomm, needenv;
831556Srgrimes
841556Srgrimesenum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
851556Srgrimes
861556Srgrimesstatic char	*fmt __P((char **(*)(kvm_t *, const struct kinfo_proc *, int),
871556Srgrimes		    KINFO *, char *, int));
881556Srgrimesstatic char	*kludge_oldps_options __P((char *));
891556Srgrimesstatic int	 pscomp __P((const void *, const void *));
901556Srgrimesstatic void	 saveuser __P((KINFO *));
911556Srgrimesstatic void	 scanvars __P((void));
921556Srgrimesstatic void	 usage __P((void));
931556Srgrimes
941556Srgrimeschar dfmt[] = "pid tt state time command";
951556Srgrimeschar jfmt[] = "user pid ppid pgid sess jobc state tt time command";
961556Srgrimeschar lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
971556Srgrimeschar   o1[] = "pid";
981556Srgrimeschar   o2[] = "tt state time command";
991556Srgrimeschar ufmt[] = "user pid %cpu %mem vsz rss tt state start time command";
1001556Srgrimeschar vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command";
1011556Srgrimes
1021556Srgrimeskvm_t *kd;
1031556Srgrimes
1041556Srgrimesint
1051556Srgrimesmain(argc, argv)
1061556Srgrimes	int argc;
1071556Srgrimes	char *argv[];
1081556Srgrimes{
1091556Srgrimes	struct kinfo_proc *kp;
1101556Srgrimes	struct varent *vent;
1111556Srgrimes	struct winsize ws;
1121556Srgrimes	dev_t ttydev;
1131556Srgrimes	pid_t pid;
1141556Srgrimes	uid_t uid;
1151556Srgrimes	int all, ch, flag, i, fmt, lineno, nentries;
1161556Srgrimes	int prtheader, wflag, what, xflg;
1171556Srgrimes	char *nlistf, *memf, *swapf, errbuf[256];
1181556Srgrimes
1191556Srgrimes	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1201556Srgrimes	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
1211556Srgrimes	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
1221556Srgrimes	     ws.ws_col == 0)
1231556Srgrimes		termwidth = 79;
1241556Srgrimes	else
1251556Srgrimes		termwidth = ws.ws_col - 1;
1261556Srgrimes
1271556Srgrimes	if (argc > 1)
1281556Srgrimes		argv[1] = kludge_oldps_options(argv[1]);
1291556Srgrimes
1301556Srgrimes	all = fmt = prtheader = wflag = xflg = 0;
1311556Srgrimes	pid = -1;
1321556Srgrimes	uid = (uid_t) -1;
1331556Srgrimes	ttydev = NODEV;
1341556Srgrimes	memf = nlistf = swapf = NULL;
1351556Srgrimes	while ((ch = getopt(argc, argv,
1361556Srgrimes	    "aCeghjLlM:mN:O:o:p:rSTt:uvW:wx")) != EOF)
1371556Srgrimes		switch((char)ch) {
1381556Srgrimes		case 'a':
1391556Srgrimes			all = 1;
1401556Srgrimes			break;
1411556Srgrimes		case 'e':			/* XXX set ufmt */
1421556Srgrimes			needenv = 1;
1431556Srgrimes			break;
1441556Srgrimes		case 'C':
1451556Srgrimes			rawcpu = 1;
1461556Srgrimes			break;
1471556Srgrimes		case 'g':
1481556Srgrimes			break;			/* no-op */
1491556Srgrimes		case 'h':
1501556Srgrimes			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
1511556Srgrimes			break;
1521556Srgrimes		case 'j':
1531556Srgrimes			parsefmt(jfmt);
1541556Srgrimes			fmt = 1;
1551556Srgrimes			jfmt[0] = '\0';
1561556Srgrimes			break;
1571556Srgrimes		case 'L':
1581556Srgrimes			showkey();
1591556Srgrimes			exit(0);
1601556Srgrimes		case 'l':
1611556Srgrimes			parsefmt(lfmt);
1621556Srgrimes			fmt = 1;
1631556Srgrimes			lfmt[0] = '\0';
1641556Srgrimes			break;
1651556Srgrimes		case 'M':
1661556Srgrimes			memf = optarg;
1671556Srgrimes			break;
1681556Srgrimes		case 'm':
1691556Srgrimes			sortby = SORTMEM;
1701556Srgrimes			break;
1711556Srgrimes		case 'N':
1721556Srgrimes			nlistf = optarg;
1731556Srgrimes			break;
1741556Srgrimes		case 'O':
1751556Srgrimes			parsefmt(o1);
1761556Srgrimes			parsefmt(optarg);
1771556Srgrimes			parsefmt(o2);
1781556Srgrimes			o1[0] = o2[0] = '\0';
1791556Srgrimes			fmt = 1;
1801556Srgrimes			break;
1811556Srgrimes		case 'o':
1821556Srgrimes			parsefmt(optarg);
1831556Srgrimes			fmt = 1;
1841556Srgrimes			break;
1851556Srgrimes		case 'p':
1861556Srgrimes			pid = atol(optarg);
1871556Srgrimes			xflg = 1;
1881556Srgrimes			break;
1891556Srgrimes		case 'r':
1901556Srgrimes			sortby = SORTCPU;
1911556Srgrimes			break;
1921556Srgrimes		case 'S':
1931556Srgrimes			sumrusage = 1;
1941556Srgrimes			break;
1951556Srgrimes		case 'T':
1961556Srgrimes			if ((optarg = ttyname(STDIN_FILENO)) == NULL)
1971556Srgrimes				errx(1, "stdin: not a terminal");
1981556Srgrimes			/* FALLTHROUGH */
1991556Srgrimes		case 't': {
2001556Srgrimes			struct stat sb;
2011556Srgrimes			char *ttypath, pathbuf[MAXPATHLEN];
2021556Srgrimes
2031556Srgrimes			if (strcmp(optarg, "co") == 0)
2041556Srgrimes				ttypath = _PATH_CONSOLE;
2051556Srgrimes			else if (*optarg != '/')
2061556Srgrimes				(void)snprintf(ttypath = pathbuf,
2071556Srgrimes				    sizeof(pathbuf), "%s%s", _PATH_TTY, optarg);
2081556Srgrimes			else
2091556Srgrimes				ttypath = optarg;
2101556Srgrimes			if (stat(ttypath, &sb) == -1)
2111556Srgrimes				err(1, "%s", ttypath);
2121556Srgrimes			if (!S_ISCHR(sb.st_mode))
2131556Srgrimes				errx(1, "%s: not a terminal", ttypath);
2141556Srgrimes			ttydev = sb.st_rdev;
2151556Srgrimes			break;
2161556Srgrimes		}
2171556Srgrimes		case 'u':
2181556Srgrimes			parsefmt(ufmt);
2191556Srgrimes			sortby = SORTCPU;
2201556Srgrimes			fmt = 1;
2211556Srgrimes			ufmt[0] = '\0';
2221556Srgrimes			break;
2231556Srgrimes		case 'v':
2241556Srgrimes			parsefmt(vfmt);
2251556Srgrimes			sortby = SORTMEM;
2261556Srgrimes			fmt = 1;
2271556Srgrimes			vfmt[0] = '\0';
2281556Srgrimes			break;
2291556Srgrimes		case 'W':
2301556Srgrimes			swapf = optarg;
2311556Srgrimes			break;
2321556Srgrimes		case 'w':
2331556Srgrimes			if (wflag)
2341556Srgrimes				termwidth = UNLIMITED;
2351556Srgrimes			else if (termwidth < 131)
2361556Srgrimes				termwidth = 131;
2371556Srgrimes			wflag++;
2381556Srgrimes			break;
2391556Srgrimes		case 'x':
2401556Srgrimes			xflg = 1;
2411556Srgrimes			break;
2421556Srgrimes		case '?':
2431556Srgrimes		default:
2441556Srgrimes			usage();
2451556Srgrimes		}
2461556Srgrimes	argc -= optind;
2471556Srgrimes	argv += optind;
2481556Srgrimes
2491556Srgrimes#define	BACKWARD_COMPATIBILITY
2501556Srgrimes#ifdef	BACKWARD_COMPATIBILITY
2511556Srgrimes	if (*argv) {
2521556Srgrimes		nlistf = *argv;
2531556Srgrimes		if (*++argv) {
2541556Srgrimes			memf = *argv;
2551556Srgrimes			if (*++argv)
2561556Srgrimes				swapf = *argv;
2571556Srgrimes		}
2581556Srgrimes	}
2591556Srgrimes#endif
2601556Srgrimes	/*
2611556Srgrimes	 * Discard setgid privileges if not the running kernel so that bad
2621556Srgrimes	 * guys can't print interesting stuff from kernel memory.
2631556Srgrimes	 */
2641556Srgrimes	if (nlistf != NULL || memf != NULL || swapf != NULL)
2651556Srgrimes		setgid(getgid());
2661556Srgrimes
2671556Srgrimes	kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf);
2681556Srgrimes	if (kd == 0)
2691556Srgrimes		errx(1, "%s", errbuf);
2701556Srgrimes
2711556Srgrimes	if (!fmt)
2721556Srgrimes		parsefmt(dfmt);
2731556Srgrimes
2741556Srgrimes	if (!all && ttydev == NODEV && pid == -1)  /* XXX - should be cleaner */
2751556Srgrimes		uid = getuid();
2761556Srgrimes
2771556Srgrimes	/*
2781556Srgrimes	 * scan requested variables, noting what structures are needed,
2791556Srgrimes	 * and adjusting header widths as appropiate.
2801556Srgrimes	 */
2811556Srgrimes	scanvars();
2821556Srgrimes	/*
2831556Srgrimes	 * get proc list
2841556Srgrimes	 */
2851556Srgrimes	if (uid != (uid_t) -1) {
2861556Srgrimes		what = KERN_PROC_UID;
2871556Srgrimes		flag = uid;
2881556Srgrimes	} else if (ttydev != NODEV) {
2891556Srgrimes		what = KERN_PROC_TTY;
2901556Srgrimes		flag = ttydev;
2911556Srgrimes	} else if (pid != -1) {
2921556Srgrimes		what = KERN_PROC_PID;
2931556Srgrimes		flag = pid;
2941556Srgrimes	} else {
2951556Srgrimes		what = KERN_PROC_ALL;
2961556Srgrimes		flag = 0;
2971556Srgrimes	}
2981556Srgrimes	/*
2991556Srgrimes	 * select procs
3001556Srgrimes	 */
3011556Srgrimes	if ((kp = kvm_getprocs(kd, what, flag, &nentries)) == 0)
3021556Srgrimes		errx(1, "%s", kvm_geterr(kd));
3031556Srgrimes	if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL)
3041556Srgrimes		err(1, NULL);
3051556Srgrimes	for (i = nentries; --i >= 0; ++kp) {
3061556Srgrimes		kinfo[i].ki_p = kp;
3071556Srgrimes		if (needuser)
3081556Srgrimes			saveuser(&kinfo[i]);
3091556Srgrimes	}
3101556Srgrimes	/*
3111556Srgrimes	 * print header
3121556Srgrimes	 */
3131556Srgrimes	printheader();
3141556Srgrimes	if (nentries == 0)
3151556Srgrimes		exit(0);
3161556Srgrimes	/*
3171556Srgrimes	 * sort proc list
3181556Srgrimes	 */
3191556Srgrimes	qsort(kinfo, nentries, sizeof(KINFO), pscomp);
3201556Srgrimes	/*
3211556Srgrimes	 * for each proc, call each variable output function.
3221556Srgrimes	 */
3231556Srgrimes	for (i = lineno = 0; i < nentries; i++) {
3241556Srgrimes		if (xflg == 0 && (KI_EPROC(&kinfo[i])->e_tdev == NODEV ||
3251556Srgrimes		    (KI_PROC(&kinfo[i])->p_flag & P_CONTROLT ) == 0))
3261556Srgrimes			continue;
3271556Srgrimes		for (vent = vhead; vent; vent = vent->next) {
3281556Srgrimes			(vent->var->oproc)(&kinfo[i], vent);
3291556Srgrimes			if (vent->next != NULL)
3301556Srgrimes				(void)putchar(' ');
3311556Srgrimes		}
3321556Srgrimes		(void)putchar('\n');
3331556Srgrimes		if (prtheader && lineno++ == prtheader - 4) {
3341556Srgrimes			(void)putchar('\n');
3351556Srgrimes			printheader();
3361556Srgrimes			lineno = 0;
3371556Srgrimes		}
3381556Srgrimes	}
3391556Srgrimes	exit(eval);
3401556Srgrimes}
3411556Srgrimes
3421556Srgrimesstatic void
3431556Srgrimesscanvars()
3441556Srgrimes{
3451556Srgrimes	struct varent *vent;
3461556Srgrimes	VAR *v;
3471556Srgrimes	int i;
3481556Srgrimes
3491556Srgrimes	for (vent = vhead; vent; vent = vent->next) {
3501556Srgrimes		v = vent->var;
3511556Srgrimes		i = strlen(v->header);
3521556Srgrimes		if (v->width < i)
3531556Srgrimes			v->width = i;
3541556Srgrimes		totwidth += v->width + 1;	/* +1 for space */
3551556Srgrimes		if (v->flag & USER)
3561556Srgrimes			needuser = 1;
3571556Srgrimes		if (v->flag & COMM)
3581556Srgrimes			needcomm = 1;
3591556Srgrimes	}
3601556Srgrimes	totwidth--;
3611556Srgrimes}
3621556Srgrimes
3631556Srgrimesstatic char *
3641556Srgrimesfmt(fn, ki, comm, maxlen)
3651556Srgrimes	char **(*fn) __P((kvm_t *, const struct kinfo_proc *, int));
3661556Srgrimes	KINFO *ki;
3671556Srgrimes	char *comm;
3681556Srgrimes	int maxlen;
3691556Srgrimes{
3701556Srgrimes	char *s;
3711556Srgrimes
3721556Srgrimes	if ((s =
3731556Srgrimes	    fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen)) == NULL)
3741556Srgrimes		err(1, NULL);
3751556Srgrimes	return (s);
3761556Srgrimes}
3771556Srgrimes
3781556Srgrimesstatic void
3791556Srgrimessaveuser(ki)
3801556Srgrimes	KINFO *ki;
3811556Srgrimes{
3821556Srgrimes	struct pstats pstats;
3831556Srgrimes	struct usave *usp;
3841556Srgrimes
3851556Srgrimes	usp = &ki->ki_u;
3861556Srgrimes	if (kvm_read(kd, (u_long)&KI_PROC(ki)->p_addr->u_stats,
3871556Srgrimes	    (char *)&pstats, sizeof(pstats)) == sizeof(pstats)) {
3881556Srgrimes		/*
3891556Srgrimes		 * The u-area might be swapped out, and we can't get
3901556Srgrimes		 * at it because we have a crashdump and no swap.
3911556Srgrimes		 * If it's here fill in these fields, otherwise, just
3921556Srgrimes		 * leave them 0.
3931556Srgrimes		 */
3941556Srgrimes		usp->u_start = pstats.p_start;
3951556Srgrimes		usp->u_ru = pstats.p_ru;
3961556Srgrimes		usp->u_cru = pstats.p_cru;
3971556Srgrimes		usp->u_valid = 1;
3981556Srgrimes	} else
3991556Srgrimes		usp->u_valid = 0;
4001556Srgrimes	/*
4011556Srgrimes	 * save arguments if needed
4021556Srgrimes	 */
4031556Srgrimes	if (needcomm)
4041556Srgrimes		ki->ki_args = fmt(kvm_getargv, ki, KI_PROC(ki)->p_comm,
4051556Srgrimes		    MAXCOMLEN);
4061556Srgrimes	else
4071556Srgrimes		ki->ki_args = NULL;
4081556Srgrimes	if (needenv)
4091556Srgrimes		ki->ki_env = fmt(kvm_getenvv, ki, (char *)NULL, 0);
4101556Srgrimes	else
4111556Srgrimes		ki->ki_env = NULL;
4121556Srgrimes}
4131556Srgrimes
4141556Srgrimesstatic int
4151556Srgrimespscomp(a, b)
4161556Srgrimes	const void *a, *b;
4171556Srgrimes{
4181556Srgrimes	int i;
4191556Srgrimes#ifdef NEWVM
4201556Srgrimes#define VSIZE(k) (KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize + \
4211556Srgrimes		  KI_EPROC(k)->e_vm.vm_tsize)
4221556Srgrimes#else
4231556Srgrimes#define VSIZE(k) ((k)->ki_p->p_dsize + (k)->ki_p->p_ssize + (k)->ki_e->e_xsize)
4241556Srgrimes#endif
4251556Srgrimes
4261556Srgrimes	if (sortby == SORTCPU)
4271556Srgrimes		return (getpcpu((KINFO *)b) - getpcpu((KINFO *)a));
4281556Srgrimes	if (sortby == SORTMEM)
4291556Srgrimes		return (VSIZE((KINFO *)b) - VSIZE((KINFO *)a));
4301556Srgrimes	i =  KI_EPROC((KINFO *)a)->e_tdev - KI_EPROC((KINFO *)b)->e_tdev;
4311556Srgrimes	if (i == 0)
4321556Srgrimes		i = KI_PROC((KINFO *)a)->p_pid - KI_PROC((KINFO *)b)->p_pid;
4331556Srgrimes	return (i);
4341556Srgrimes}
4351556Srgrimes
4361556Srgrimes/*
4371556Srgrimes * ICK (all for getopt), would rather hide the ugliness
4381556Srgrimes * here than taint the main code.
4391556Srgrimes *
4401556Srgrimes *  ps foo -> ps -foo
4411556Srgrimes *  ps 34 -> ps -p34
4421556Srgrimes *
4431556Srgrimes * The old convention that 't' with no trailing tty arg means the users
4441556Srgrimes * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
4451556Srgrimes * feature is available with the option 'T', which takes no argument.
4461556Srgrimes */
4471556Srgrimesstatic char *
4481556Srgrimeskludge_oldps_options(s)
4491556Srgrimes	char *s;
4501556Srgrimes{
4511556Srgrimes	size_t len;
4521556Srgrimes	char *newopts, *ns, *cp;
4531556Srgrimes
4541556Srgrimes	len = strlen(s);
4551556Srgrimes	if ((newopts = ns = malloc(len + 2)) == NULL)
4561556Srgrimes		err(1, NULL);
4571556Srgrimes	/*
4581556Srgrimes	 * options begin with '-'
4591556Srgrimes	 */
4601556Srgrimes	if (*s != '-')
4611556Srgrimes		*ns++ = '-';	/* add option flag */
4621556Srgrimes	/*
4631556Srgrimes	 * gaze to end of argv[1]
4641556Srgrimes	 */
4651556Srgrimes	cp = s + len - 1;
4661556Srgrimes	/*
4671556Srgrimes	 * if last letter is a 't' flag with no argument (in the context
4681556Srgrimes	 * of the oldps options -- option string NOT starting with a '-' --
4691556Srgrimes	 * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
4701556Srgrimes	 */
4711556Srgrimes	if (*cp == 't' && *s != '-')
4721556Srgrimes		*cp = 'T';
4731556Srgrimes	else {
4741556Srgrimes		/*
4751556Srgrimes		 * otherwise check for trailing number, which *may* be a
4761556Srgrimes		 * pid.
4771556Srgrimes		 */
4781556Srgrimes		while (cp >= s && isdigit(*cp))
4791556Srgrimes			--cp;
4801556Srgrimes	}
4811556Srgrimes	cp++;
4821556Srgrimes	memmove(ns, s, (size_t)(cp - s));	/* copy up to trailing number */
4831556Srgrimes	ns += cp - s;
4841556Srgrimes	/*
4851556Srgrimes	 * if there's a trailing number, and not a preceding 'p' (pid) or
4861556Srgrimes	 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
4871556Srgrimes	 */
4881556Srgrimes	if (isdigit(*cp) && (cp == s || cp[-1] != 't' && cp[-1] != 'p' &&
4891556Srgrimes	    (cp - 1 == s || cp[-2] != 't')))
4901556Srgrimes		*ns++ = 'p';
4911556Srgrimes	(void)strcpy(ns, cp);		/* and append the number */
4921556Srgrimes
4931556Srgrimes	return (newopts);
4941556Srgrimes}
4951556Srgrimes
4961556Srgrimesstatic void
4971556Srgrimesusage()
4981556Srgrimes{
4991556Srgrimes
5001556Srgrimes	(void)fprintf(stderr,
5011556Srgrimes	    "usage:\t%s\n\t   %s\n\t%s\n",
5021556Srgrimes	    "ps [-aChjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty]",
5031556Srgrimes	    "[-M core] [-N system] [-W swap]",
5041556Srgrimes	    "ps [-L]");
5051556Srgrimes	exit(1);
5061556Srgrimes}
507