Home | History | Annotate | Line # | Download | only in ps
ps.c revision 1.11
      1   1.1  cgd /*-
      2  1.11  cgd  * Copyright (c) 1990, 1993, 1994
      3  1.11  cgd  *	The Regents of the University of California.  All rights reserved.
      4   1.1  cgd  *
      5   1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6   1.1  cgd  * modification, are permitted provided that the following conditions
      7   1.1  cgd  * are met:
      8   1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9   1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10   1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11   1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12   1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13   1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14   1.1  cgd  *    must display the following acknowledgement:
     15   1.1  cgd  *	This product includes software developed by the University of
     16   1.1  cgd  *	California, Berkeley and its contributors.
     17   1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18   1.1  cgd  *    may be used to endorse or promote products derived from this software
     19   1.1  cgd  *    without specific prior written permission.
     20   1.1  cgd  *
     21   1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22   1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24   1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25   1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26   1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27   1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28   1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29   1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30   1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31   1.1  cgd  * SUCH DAMAGE.
     32   1.1  cgd  */
     33   1.1  cgd 
     34   1.1  cgd #ifndef lint
     35  1.11  cgd static char copyright[] =
     36  1.11  cgd "@(#) Copyright (c) 1990, 1993, 1994\n\
     37  1.11  cgd 	The Regents of the University of California.  All rights reserved.\n";
     38   1.1  cgd #endif /* not lint */
     39   1.1  cgd 
     40   1.1  cgd #ifndef lint
     41  1.11  cgd static char sccsid[] = "@(#)ps.c	8.4 (Berkeley) 4/2/94";
     42   1.1  cgd #endif /* not lint */
     43   1.1  cgd 
     44   1.1  cgd #include <sys/param.h>
     45   1.1  cgd #include <sys/user.h>
     46   1.1  cgd #include <sys/time.h>
     47   1.1  cgd #include <sys/resource.h>
     48   1.1  cgd #include <sys/proc.h>
     49   1.1  cgd #include <sys/stat.h>
     50   1.1  cgd #include <sys/ioctl.h>
     51  1.11  cgd #include <sys/sysctl.h>
     52  1.11  cgd 
     53  1.11  cgd #include <ctype.h>
     54  1.11  cgd #include <err.h>
     55  1.11  cgd #include <errno.h>
     56  1.11  cgd #include <fcntl.h>
     57  1.11  cgd #include <kvm.h>
     58   1.1  cgd #include <nlist.h>
     59  1.11  cgd #include <paths.h>
     60   1.1  cgd #include <stdio.h>
     61   1.1  cgd #include <stdlib.h>
     62   1.1  cgd #include <string.h>
     63  1.11  cgd #include <unistd.h>
     64  1.11  cgd 
     65   1.1  cgd #include "ps.h"
     66   1.1  cgd 
     67  1.10  cgd #ifdef P_PPWAIT
     68   1.1  cgd #define NEWVM
     69   1.1  cgd #endif
     70   1.1  cgd 
     71   1.1  cgd KINFO *kinfo;
     72   1.1  cgd struct varent *vhead, *vtail;
     73   1.1  cgd 
     74   1.1  cgd int	eval;			/* exit value */
     75   1.1  cgd int	rawcpu;			/* -C */
     76   1.1  cgd int	sumrusage;		/* -S */
     77   1.1  cgd int	termwidth;		/* width of screen (0 == infinity) */
     78   1.1  cgd int	totwidth;		/* calculated width of requested variables */
     79   1.1  cgd 
     80   1.5  cgd static int needuser, needcomm, needenv;
     81   1.1  cgd 
     82   1.1  cgd enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
     83   1.1  cgd 
     84  1.11  cgd static char	*fmt __P((char **(*)(kvm_t *, const struct kinfo_proc *, int),
     85  1.11  cgd 		    KINFO *, char *, int));
     86  1.11  cgd static char	*kludge_oldps_options __P((char *));
     87  1.11  cgd static int	 pscomp __P((const void *, const void *));
     88  1.11  cgd static void	 saveuser __P((KINFO *));
     89  1.11  cgd static void	 scanvars __P((void));
     90  1.11  cgd static void	 usage __P((void));
     91   1.1  cgd 
     92   1.1  cgd char dfmt[] = "pid tt state time command";
     93   1.1  cgd char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
     94   1.1  cgd char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
     95   1.1  cgd char   o1[] = "pid";
     96   1.1  cgd char   o2[] = "tt state time command";
     97   1.1  cgd char ufmt[] = "user pid %cpu %mem vsz rss tt state start time command";
     98  1.11  cgd char vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command";
     99  1.11  cgd 
    100  1.11  cgd kvm_t *kd;
    101   1.1  cgd 
    102  1.11  cgd int
    103   1.1  cgd main(argc, argv)
    104   1.1  cgd 	int argc;
    105  1.11  cgd 	char *argv[];
    106   1.1  cgd {
    107  1.11  cgd 	struct kinfo_proc *kp;
    108  1.11  cgd 	struct varent *vent;
    109   1.1  cgd 	struct winsize ws;
    110   1.1  cgd 	dev_t ttydev;
    111  1.11  cgd 	pid_t pid;
    112  1.11  cgd 	uid_t uid;
    113  1.11  cgd 	int all, ch, flag, i, fmt, lineno, nentries;
    114  1.11  cgd 	int prtheader, wflag, what, xflg;
    115  1.11  cgd 	char *nlistf, *memf, *swapf, errbuf[256];
    116   1.1  cgd 
    117   1.1  cgd 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
    118   1.1  cgd 	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
    119   1.1  cgd 	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
    120   1.1  cgd 	     ws.ws_col == 0)
    121   1.1  cgd 		termwidth = 79;
    122   1.1  cgd 	else
    123   1.1  cgd 		termwidth = ws.ws_col - 1;
    124   1.1  cgd 
    125   1.1  cgd 	if (argc > 1)
    126   1.1  cgd 		argv[1] = kludge_oldps_options(argv[1]);
    127   1.1  cgd 
    128  1.11  cgd 	all = fmt = prtheader = wflag = xflg = 0;
    129  1.11  cgd 	pid = -1;
    130  1.11  cgd 	uid = (uid_t) -1;
    131   1.1  cgd 	ttydev = NODEV;
    132   1.1  cgd 	memf = nlistf = swapf = NULL;
    133   1.1  cgd 	while ((ch = getopt(argc, argv,
    134   1.5  cgd 	    "aCeghjLlM:mN:O:o:p:rSTt:uvW:wx")) != EOF)
    135   1.1  cgd 		switch((char)ch) {
    136   1.1  cgd 		case 'a':
    137   1.1  cgd 			all = 1;
    138   1.1  cgd 			break;
    139  1.11  cgd 		case 'e':			/* XXX set ufmt */
    140  1.11  cgd 			needenv = 1;
    141  1.11  cgd 			break;
    142   1.1  cgd 		case 'C':
    143   1.1  cgd 			rawcpu = 1;
    144   1.1  cgd 			break;
    145   1.1  cgd 		case 'g':
    146  1.11  cgd 			break;			/* no-op */
    147   1.1  cgd 		case 'h':
    148   1.1  cgd 			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
    149   1.1  cgd 			break;
    150   1.1  cgd 		case 'j':
    151   1.1  cgd 			parsefmt(jfmt);
    152   1.1  cgd 			fmt = 1;
    153   1.1  cgd 			jfmt[0] = '\0';
    154   1.1  cgd 			break;
    155  1.11  cgd 		case 'L':
    156   1.1  cgd 			showkey();
    157   1.1  cgd 			exit(0);
    158   1.1  cgd 		case 'l':
    159   1.1  cgd 			parsefmt(lfmt);
    160   1.1  cgd 			fmt = 1;
    161   1.1  cgd 			lfmt[0] = '\0';
    162   1.1  cgd 			break;
    163   1.1  cgd 		case 'M':
    164   1.1  cgd 			memf = optarg;
    165   1.1  cgd 			break;
    166   1.1  cgd 		case 'm':
    167   1.1  cgd 			sortby = SORTMEM;
    168   1.1  cgd 			break;
    169   1.1  cgd 		case 'N':
    170   1.1  cgd 			nlistf = optarg;
    171   1.1  cgd 			break;
    172   1.1  cgd 		case 'O':
    173   1.1  cgd 			parsefmt(o1);
    174   1.1  cgd 			parsefmt(optarg);
    175   1.1  cgd 			parsefmt(o2);
    176   1.1  cgd 			o1[0] = o2[0] = '\0';
    177   1.1  cgd 			fmt = 1;
    178   1.1  cgd 			break;
    179   1.1  cgd 		case 'o':
    180   1.1  cgd 			parsefmt(optarg);
    181   1.1  cgd 			fmt = 1;
    182   1.1  cgd 			break;
    183   1.1  cgd 		case 'p':
    184  1.11  cgd 			pid = atol(optarg);
    185   1.1  cgd 			xflg = 1;
    186   1.1  cgd 			break;
    187   1.1  cgd 		case 'r':
    188   1.1  cgd 			sortby = SORTCPU;
    189   1.1  cgd 			break;
    190   1.1  cgd 		case 'S':
    191   1.1  cgd 			sumrusage = 1;
    192   1.1  cgd 			break;
    193   1.1  cgd 		case 'T':
    194   1.1  cgd 			if ((optarg = ttyname(STDIN_FILENO)) == NULL)
    195  1.11  cgd 				errx(1, "stdin: not a terminal");
    196   1.1  cgd 			/* FALLTHROUGH */
    197   1.1  cgd 		case 't': {
    198  1.11  cgd 			struct stat sb;
    199  1.11  cgd 			char *ttypath, pathbuf[MAXPATHLEN];
    200   1.1  cgd 
    201   1.1  cgd 			if (strcmp(optarg, "co") == 0)
    202   1.1  cgd 				ttypath = _PATH_CONSOLE;
    203   1.1  cgd 			else if (*optarg != '/')
    204  1.11  cgd 				(void)snprintf(ttypath = pathbuf,
    205  1.11  cgd 				    sizeof(pathbuf), "%s%s", _PATH_TTY, optarg);
    206   1.1  cgd 			else
    207   1.1  cgd 				ttypath = optarg;
    208  1.11  cgd 			if (stat(ttypath, &sb) == -1)
    209  1.11  cgd 				err(1, "%s", ttypath);
    210  1.11  cgd 			if (!S_ISCHR(sb.st_mode))
    211  1.11  cgd 				errx(1, "%s: not a terminal", ttypath);
    212  1.11  cgd 			ttydev = sb.st_rdev;
    213   1.1  cgd 			break;
    214   1.1  cgd 		}
    215   1.1  cgd 		case 'u':
    216   1.1  cgd 			parsefmt(ufmt);
    217   1.1  cgd 			sortby = SORTCPU;
    218   1.1  cgd 			fmt = 1;
    219   1.1  cgd 			ufmt[0] = '\0';
    220   1.1  cgd 			break;
    221   1.1  cgd 		case 'v':
    222   1.1  cgd 			parsefmt(vfmt);
    223   1.1  cgd 			sortby = SORTMEM;
    224   1.1  cgd 			fmt = 1;
    225   1.1  cgd 			vfmt[0] = '\0';
    226   1.1  cgd 			break;
    227   1.1  cgd 		case 'W':
    228   1.1  cgd 			swapf = optarg;
    229   1.1  cgd 			break;
    230   1.1  cgd 		case 'w':
    231   1.6  cgd 			if (wflag)
    232   1.6  cgd 				termwidth = UNLIMITED;
    233   1.6  cgd 			else if (termwidth < 131)
    234   1.1  cgd 				termwidth = 131;
    235  1.11  cgd 			wflag++;
    236   1.1  cgd 			break;
    237   1.1  cgd 		case 'x':
    238   1.1  cgd 			xflg = 1;
    239   1.1  cgd 			break;
    240   1.1  cgd 		case '?':
    241   1.1  cgd 		default:
    242   1.1  cgd 			usage();
    243   1.1  cgd 		}
    244   1.1  cgd 	argc -= optind;
    245   1.1  cgd 	argv += optind;
    246   1.1  cgd 
    247   1.1  cgd #define	BACKWARD_COMPATIBILITY
    248   1.1  cgd #ifdef	BACKWARD_COMPATIBILITY
    249   1.1  cgd 	if (*argv) {
    250   1.1  cgd 		nlistf = *argv;
    251   1.1  cgd 		if (*++argv) {
    252   1.1  cgd 			memf = *argv;
    253   1.1  cgd 			if (*++argv)
    254   1.1  cgd 				swapf = *argv;
    255   1.1  cgd 		}
    256   1.1  cgd 	}
    257   1.1  cgd #endif
    258  1.11  cgd 	/*
    259  1.11  cgd 	 * Discard setgid privileges if not the running kernel so that bad
    260  1.11  cgd 	 * guys can't print interesting stuff from kernel memory.
    261  1.11  cgd 	 */
    262  1.11  cgd 	if (nlistf != NULL || memf != NULL || swapf != NULL)
    263  1.11  cgd 		setgid(getgid());
    264  1.11  cgd 
    265  1.11  cgd 	kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf);
    266  1.11  cgd 	if (kd == 0)
    267  1.11  cgd 		errx(1, "%s", errbuf);
    268   1.1  cgd 
    269   1.1  cgd 	if (!fmt)
    270   1.1  cgd 		parsefmt(dfmt);
    271   1.1  cgd 
    272   1.1  cgd 	if (!all && ttydev == NODEV && pid == -1)  /* XXX - should be cleaner */
    273   1.1  cgd 		uid = getuid();
    274   1.1  cgd 
    275   1.1  cgd 	/*
    276   1.1  cgd 	 * scan requested variables, noting what structures are needed,
    277   1.1  cgd 	 * and adjusting header widths as appropiate.
    278   1.1  cgd 	 */
    279   1.1  cgd 	scanvars();
    280   1.1  cgd 	/*
    281   1.1  cgd 	 * get proc list
    282   1.1  cgd 	 */
    283  1.11  cgd 	if (uid != (uid_t) -1) {
    284  1.11  cgd 		what = KERN_PROC_UID;
    285   1.1  cgd 		flag = uid;
    286   1.1  cgd 	} else if (ttydev != NODEV) {
    287  1.11  cgd 		what = KERN_PROC_TTY;
    288   1.1  cgd 		flag = ttydev;
    289   1.1  cgd 	} else if (pid != -1) {
    290  1.11  cgd 		what = KERN_PROC_PID;
    291   1.1  cgd 		flag = pid;
    292  1.11  cgd 	} else {
    293  1.11  cgd 		what = KERN_PROC_ALL;
    294  1.11  cgd 		flag = 0;
    295  1.11  cgd 	}
    296   1.1  cgd 	/*
    297   1.1  cgd 	 * select procs
    298   1.1  cgd 	 */
    299  1.11  cgd 	if ((kp = kvm_getprocs(kd, what, flag, &nentries)) == 0)
    300  1.11  cgd 		errx(1, "%s", kvm_geterr(kd));
    301  1.11  cgd 	if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL)
    302  1.11  cgd 		err(1, NULL);
    303  1.11  cgd 	for (i = nentries; --i >= 0; ++kp) {
    304  1.11  cgd 		kinfo[i].ki_p = kp;
    305  1.11  cgd 		if (needuser)
    306  1.11  cgd 			saveuser(&kinfo[i]);
    307   1.1  cgd 	}
    308   1.1  cgd 	/*
    309   1.1  cgd 	 * print header
    310   1.1  cgd 	 */
    311   1.1  cgd 	printheader();
    312   1.1  cgd 	if (nentries == 0)
    313   1.1  cgd 		exit(0);
    314   1.1  cgd 	/*
    315   1.1  cgd 	 * sort proc list
    316   1.1  cgd 	 */
    317  1.11  cgd 	qsort(kinfo, nentries, sizeof(KINFO), pscomp);
    318   1.1  cgd 	/*
    319   1.1  cgd 	 * for each proc, call each variable output function.
    320   1.1  cgd 	 */
    321   1.1  cgd 	for (i = lineno = 0; i < nentries; i++) {
    322  1.11  cgd 		if (xflg == 0 && (KI_EPROC(&kinfo[i])->e_tdev == NODEV ||
    323  1.11  cgd 		    (KI_PROC(&kinfo[i])->p_flag & P_CONTROLT ) == 0))
    324   1.1  cgd 			continue;
    325   1.1  cgd 		for (vent = vhead; vent; vent = vent->next) {
    326  1.11  cgd 			(vent->var->oproc)(&kinfo[i], vent);
    327   1.1  cgd 			if (vent->next != NULL)
    328  1.11  cgd 				(void)putchar(' ');
    329   1.1  cgd 		}
    330  1.11  cgd 		(void)putchar('\n');
    331  1.11  cgd 		if (prtheader && lineno++ == prtheader - 4) {
    332  1.11  cgd 			(void)putchar('\n');
    333   1.1  cgd 			printheader();
    334   1.1  cgd 			lineno = 0;
    335   1.1  cgd 		}
    336   1.1  cgd 	}
    337   1.1  cgd 	exit(eval);
    338   1.1  cgd }
    339   1.1  cgd 
    340  1.11  cgd static void
    341   1.1  cgd scanvars()
    342   1.1  cgd {
    343  1.11  cgd 	struct varent *vent;
    344  1.11  cgd 	VAR *v;
    345  1.11  cgd 	int i;
    346   1.1  cgd 
    347   1.1  cgd 	for (vent = vhead; vent; vent = vent->next) {
    348   1.1  cgd 		v = vent->var;
    349   1.1  cgd 		i = strlen(v->header);
    350   1.1  cgd 		if (v->width < i)
    351   1.1  cgd 			v->width = i;
    352   1.1  cgd 		totwidth += v->width + 1;	/* +1 for space */
    353   1.1  cgd 		if (v->flag & USER)
    354   1.1  cgd 			needuser = 1;
    355   1.1  cgd 		if (v->flag & COMM)
    356   1.1  cgd 			needcomm = 1;
    357   1.1  cgd 	}
    358   1.1  cgd 	totwidth--;
    359   1.1  cgd }
    360   1.1  cgd 
    361  1.11  cgd static char *
    362  1.11  cgd fmt(fn, ki, comm, maxlen)
    363  1.11  cgd 	char **(*fn) __P((kvm_t *, const struct kinfo_proc *, int));
    364  1.11  cgd 	KINFO *ki;
    365  1.11  cgd 	char *comm;
    366  1.11  cgd 	int maxlen;
    367  1.11  cgd {
    368  1.11  cgd 	char *s;
    369  1.11  cgd 
    370  1.11  cgd 	if ((s =
    371  1.11  cgd 	    fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen)) == NULL)
    372  1.11  cgd 		err(1, NULL);
    373  1.11  cgd 	return (s);
    374  1.11  cgd }
    375   1.1  cgd 
    376  1.11  cgd static void
    377   1.1  cgd saveuser(ki)
    378   1.1  cgd 	KINFO *ki;
    379   1.1  cgd {
    380  1.11  cgd 	struct pstats pstats;
    381  1.11  cgd 	struct usave *usp;
    382   1.5  cgd 
    383  1.11  cgd 	usp = &ki->ki_u;
    384  1.11  cgd 	if (kvm_read(kd, (u_long)&KI_PROC(ki)->p_addr->u_stats,
    385  1.11  cgd 	    (char *)&pstats, sizeof(pstats)) == sizeof(pstats)) {
    386   1.1  cgd 		/*
    387  1.11  cgd 		 * The u-area might be swapped out, and we can't get
    388  1.11  cgd 		 * at it because we have a crashdump and no swap.
    389  1.11  cgd 		 * If it's here fill in these fields, otherwise, just
    390  1.11  cgd 		 * leave them 0.
    391   1.1  cgd 		 */
    392  1.11  cgd 		usp->u_start = pstats.p_start;
    393  1.11  cgd 		usp->u_ru = pstats.p_ru;
    394  1.11  cgd 		usp->u_cru = pstats.p_cru;
    395  1.11  cgd 		usp->u_valid = 1;
    396   1.1  cgd 	} else
    397  1.11  cgd 		usp->u_valid = 0;
    398  1.11  cgd 	/*
    399  1.11  cgd 	 * save arguments if needed
    400  1.11  cgd 	 */
    401  1.11  cgd 	if (needcomm)
    402  1.11  cgd 		ki->ki_args = fmt(kvm_getargv, ki, KI_PROC(ki)->p_comm,
    403  1.11  cgd 		    MAXCOMLEN);
    404  1.11  cgd 	else
    405  1.11  cgd 		ki->ki_args = NULL;
    406  1.11  cgd 	if (needenv)
    407  1.11  cgd 		ki->ki_env = fmt(kvm_getenvv, ki, (char *)NULL, 0);
    408  1.11  cgd 	else
    409  1.11  cgd 		ki->ki_env = NULL;
    410   1.1  cgd }
    411   1.1  cgd 
    412  1.11  cgd static int
    413  1.11  cgd pscomp(a, b)
    414  1.11  cgd 	const void *a, *b;
    415   1.1  cgd {
    416   1.1  cgd 	int i;
    417   1.1  cgd #ifdef NEWVM
    418  1.11  cgd #define VSIZE(k) (KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize + \
    419  1.11  cgd 		  KI_EPROC(k)->e_vm.vm_tsize)
    420   1.1  cgd #else
    421   1.1  cgd #define VSIZE(k) ((k)->ki_p->p_dsize + (k)->ki_p->p_ssize + (k)->ki_e->e_xsize)
    422   1.1  cgd #endif
    423   1.1  cgd 
    424   1.1  cgd 	if (sortby == SORTCPU)
    425  1.11  cgd 		return (getpcpu((KINFO *)b) - getpcpu((KINFO *)a));
    426   1.1  cgd 	if (sortby == SORTMEM)
    427  1.11  cgd 		return (VSIZE((KINFO *)b) - VSIZE((KINFO *)a));
    428  1.11  cgd 	i =  KI_EPROC((KINFO *)a)->e_tdev - KI_EPROC((KINFO *)b)->e_tdev;
    429   1.1  cgd 	if (i == 0)
    430  1.11  cgd 		i = KI_PROC((KINFO *)a)->p_pid - KI_PROC((KINFO *)b)->p_pid;
    431   1.1  cgd 	return (i);
    432   1.1  cgd }
    433   1.1  cgd 
    434   1.1  cgd /*
    435   1.1  cgd  * ICK (all for getopt), would rather hide the ugliness
    436   1.1  cgd  * here than taint the main code.
    437   1.1  cgd  *
    438   1.1  cgd  *  ps foo -> ps -foo
    439   1.1  cgd  *  ps 34 -> ps -p34
    440   1.1  cgd  *
    441   1.1  cgd  * The old convention that 't' with no trailing tty arg means the users
    442   1.1  cgd  * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
    443   1.1  cgd  * feature is available with the option 'T', which takes no argument.
    444   1.1  cgd  */
    445  1.11  cgd static char *
    446   1.1  cgd kludge_oldps_options(s)
    447   1.1  cgd 	char *s;
    448   1.1  cgd {
    449   1.1  cgd 	size_t len;
    450   1.1  cgd 	char *newopts, *ns, *cp;
    451   1.1  cgd 
    452   1.1  cgd 	len = strlen(s);
    453   1.1  cgd 	if ((newopts = ns = malloc(len + 2)) == NULL)
    454  1.11  cgd 		err(1, NULL);
    455   1.1  cgd 	/*
    456   1.1  cgd 	 * options begin with '-'
    457   1.1  cgd 	 */
    458   1.1  cgd 	if (*s != '-')
    459   1.1  cgd 		*ns++ = '-';	/* add option flag */
    460   1.1  cgd 	/*
    461   1.1  cgd 	 * gaze to end of argv[1]
    462   1.1  cgd 	 */
    463   1.1  cgd 	cp = s + len - 1;
    464   1.1  cgd 	/*
    465   1.1  cgd 	 * if last letter is a 't' flag with no argument (in the context
    466   1.1  cgd 	 * of the oldps options -- option string NOT starting with a '-' --
    467   1.1  cgd 	 * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
    468   1.1  cgd 	 */
    469   1.1  cgd 	if (*cp == 't' && *s != '-')
    470   1.1  cgd 		*cp = 'T';
    471   1.1  cgd 	else {
    472   1.1  cgd 		/*
    473   1.1  cgd 		 * otherwise check for trailing number, which *may* be a
    474   1.1  cgd 		 * pid.
    475   1.1  cgd 		 */
    476   1.1  cgd 		while (cp >= s && isdigit(*cp))
    477   1.1  cgd 			--cp;
    478   1.1  cgd 	}
    479   1.1  cgd 	cp++;
    480  1.11  cgd 	memmove(ns, s, (size_t)(cp - s));	/* copy up to trailing number */
    481   1.1  cgd 	ns += cp - s;
    482   1.1  cgd 	/*
    483   1.1  cgd 	 * if there's a trailing number, and not a preceding 'p' (pid) or
    484   1.1  cgd 	 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
    485   1.1  cgd 	 */
    486   1.1  cgd 	if (isdigit(*cp) && (cp == s || cp[-1] != 't' && cp[-1] != 'p' &&
    487   1.1  cgd 	    (cp - 1 == s || cp[-2] != 't')))
    488   1.1  cgd 		*ns++ = 'p';
    489  1.11  cgd 	(void)strcpy(ns, cp);		/* and append the number */
    490   1.1  cgd 
    491   1.1  cgd 	return (newopts);
    492   1.1  cgd }
    493   1.1  cgd 
    494  1.11  cgd static void
    495  1.11  cgd usage()
    496   1.1  cgd {
    497   1.1  cgd 
    498  1.11  cgd 	(void)fprintf(stderr,
    499  1.11  cgd 	    "usage:\t%s\n\t   %s\n\t%s\n",
    500  1.11  cgd 	    "ps [-aChjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty]",
    501  1.11  cgd 	    "[-M core] [-N system] [-W swap]",
    502  1.11  cgd 	    "ps [-L]");
    503   1.1  cgd 	exit(1);
    504   1.1  cgd }
    505