Home | History | Annotate | Line # | Download | only in ps
ps.c revision 1.47
      1  1.47  jdolecek /*	$NetBSD: ps.c,v 1.47 2002/06/19 08:11:56 jdolecek Exp $	*/
      2  1.13       cgd 
      3  1.40    simonb /*
      4  1.40    simonb  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.40    simonb  * All rights reserved.
      6  1.40    simonb  *
      7  1.40    simonb  * This code is derived from software contributed to The NetBSD Foundation
      8  1.40    simonb  * by Simon Burge.
      9  1.40    simonb  *
     10  1.40    simonb  * Redistribution and use in source and binary forms, with or without
     11  1.40    simonb  * modification, are permitted provided that the following conditions
     12  1.40    simonb  * are met:
     13  1.40    simonb  * 1. Redistributions of source code must retain the above copyright
     14  1.40    simonb  *    notice, this list of conditions and the following disclaimer.
     15  1.40    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.40    simonb  *    notice, this list of conditions and the following disclaimer in the
     17  1.40    simonb  *    documentation and/or other materials provided with the distribution.
     18  1.40    simonb  * 3. All advertising materials mentioning features or use of this software
     19  1.40    simonb  *    must display the following acknowledgement:
     20  1.40    simonb  *        This product includes software developed by the NetBSD
     21  1.40    simonb  *        Foundation, Inc. and its contributors.
     22  1.40    simonb  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.40    simonb  *    contributors may be used to endorse or promote products derived
     24  1.40    simonb  *    from this software without specific prior written permission.
     25  1.40    simonb  *
     26  1.40    simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.40    simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.40    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.40    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.40    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.40    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.40    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.40    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.40    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.40    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.40    simonb  * POSSIBILITY OF SUCH DAMAGE.
     37  1.40    simonb  */
     38  1.40    simonb 
     39  1.40    simonb /*
     40  1.11       cgd  * Copyright (c) 1990, 1993, 1994
     41  1.11       cgd  *	The Regents of the University of California.  All rights reserved.
     42   1.1       cgd  *
     43   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     44   1.1       cgd  * modification, are permitted provided that the following conditions
     45   1.1       cgd  * are met:
     46   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     47   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     48   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     50   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     51   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     52   1.1       cgd  *    must display the following acknowledgement:
     53   1.1       cgd  *	This product includes software developed by the University of
     54   1.1       cgd  *	California, Berkeley and its contributors.
     55   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     56   1.1       cgd  *    may be used to endorse or promote products derived from this software
     57   1.1       cgd  *    without specific prior written permission.
     58   1.1       cgd  *
     59   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.1       cgd  * SUCH DAMAGE.
     70   1.1       cgd  */
     71   1.1       cgd 
     72  1.19  christos #include <sys/cdefs.h>
     73   1.1       cgd #ifndef lint
     74  1.19  christos __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\n\
     75  1.19  christos 	The Regents of the University of California.  All rights reserved.\n");
     76   1.1       cgd #endif /* not lint */
     77   1.1       cgd 
     78   1.1       cgd #ifndef lint
     79  1.13       cgd #if 0
     80  1.11       cgd static char sccsid[] = "@(#)ps.c	8.4 (Berkeley) 4/2/94";
     81  1.13       cgd #else
     82  1.47  jdolecek __RCSID("$NetBSD: ps.c,v 1.47 2002/06/19 08:11:56 jdolecek Exp $");
     83  1.13       cgd #endif
     84   1.1       cgd #endif /* not lint */
     85   1.1       cgd 
     86   1.1       cgd #include <sys/param.h>
     87   1.1       cgd #include <sys/user.h>
     88   1.1       cgd #include <sys/time.h>
     89   1.1       cgd #include <sys/resource.h>
     90   1.1       cgd #include <sys/proc.h>
     91   1.1       cgd #include <sys/stat.h>
     92   1.1       cgd #include <sys/ioctl.h>
     93  1.11       cgd #include <sys/sysctl.h>
     94  1.11       cgd 
     95  1.11       cgd #include <ctype.h>
     96  1.11       cgd #include <err.h>
     97  1.11       cgd #include <errno.h>
     98  1.11       cgd #include <fcntl.h>
     99  1.11       cgd #include <kvm.h>
    100  1.17        pk #include <limits.h>
    101   1.1       cgd #include <nlist.h>
    102  1.11       cgd #include <paths.h>
    103  1.25   mycroft #include <pwd.h>
    104   1.1       cgd #include <stdio.h>
    105   1.1       cgd #include <stdlib.h>
    106   1.1       cgd #include <string.h>
    107  1.11       cgd #include <unistd.h>
    108  1.11       cgd 
    109   1.1       cgd #include "ps.h"
    110   1.1       cgd 
    111  1.40    simonb /*
    112  1.40    simonb  * ARGOPTS must contain all option characters that take arguments
    113  1.40    simonb  * (except for 't'!) - it is used in kludge_oldps_options()
    114  1.40    simonb  */
    115  1.40    simonb #define	GETOPTSTR	"acCeghjKLlM:mN:O:o:p:rSTt:U:uvW:wx"
    116  1.40    simonb #define	ARGOPTS		"MNOopUW"
    117  1.40    simonb 
    118  1.38    simonb struct kinfo_proc2 *kinfo;
    119   1.1       cgd struct varent *vhead, *vtail;
    120   1.1       cgd 
    121   1.1       cgd int	eval;			/* exit value */
    122   1.1       cgd int	rawcpu;			/* -C */
    123   1.1       cgd int	sumrusage;		/* -S */
    124   1.1       cgd int	termwidth;		/* width of screen (0 == infinity) */
    125   1.1       cgd int	totwidth;		/* calculated width of requested variables */
    126   1.1       cgd 
    127  1.38    simonb int	needcomm, needenv, commandonly, use_procfs;
    128  1.33    simonb uid_t	myuid;
    129   1.1       cgd 
    130   1.1       cgd enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
    131   1.1       cgd 
    132  1.38    simonb static struct kinfo_proc2
    133  1.38    simonb 		*getkinfo_kvm __P((kvm_t *, int, int, int *));
    134  1.11       cgd static char	*kludge_oldps_options __P((char *));
    135  1.11       cgd static int	 pscomp __P((const void *, const void *));
    136  1.11       cgd static void	 scanvars __P((void));
    137  1.11       cgd static void	 usage __P((void));
    138  1.19  christos int		 main __P((int, char *[]));
    139   1.1       cgd 
    140   1.1       cgd char dfmt[] = "pid tt state time command";
    141   1.1       cgd char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
    142   1.1       cgd char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
    143   1.1       cgd char   o1[] = "pid";
    144   1.1       cgd char   o2[] = "tt state time command";
    145   1.1       cgd char ufmt[] = "user pid %cpu %mem vsz rss tt state start time command";
    146  1.11       cgd char vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command";
    147  1.11       cgd 
    148  1.11       cgd kvm_t *kd;
    149   1.1       cgd 
    150  1.11       cgd int
    151   1.1       cgd main(argc, argv)
    152   1.1       cgd 	int argc;
    153  1.11       cgd 	char *argv[];
    154   1.1       cgd {
    155  1.11       cgd 	struct varent *vent;
    156   1.1       cgd 	struct winsize ws;
    157  1.25   mycroft 	int ch, flag, i, fmt, lineno, nentries;
    158  1.39    simonb 	int prtheader, wflag, what, xflg, mode;
    159  1.17        pk 	char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX];
    160  1.26       kim 	char *ttname;
    161   1.1       cgd 
    162  1.38    simonb 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
    163  1.38    simonb 	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
    164  1.38    simonb 	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
    165   1.1       cgd 	     ws.ws_col == 0)
    166   1.1       cgd 		termwidth = 79;
    167   1.1       cgd 	else
    168   1.1       cgd 		termwidth = ws.ws_col - 1;
    169   1.1       cgd 
    170   1.1       cgd 	if (argc > 1)
    171   1.1       cgd 		argv[1] = kludge_oldps_options(argv[1]);
    172   1.1       cgd 
    173  1.25   mycroft 	fmt = prtheader = wflag = xflg = 0;
    174  1.25   mycroft 	what = KERN_PROC_UID;
    175  1.33    simonb 	flag = myuid = getuid();
    176   1.1       cgd 	memf = nlistf = swapf = NULL;
    177  1.39    simonb 	mode = PRINTMODE;
    178  1.40    simonb 	while ((ch = getopt(argc, argv, GETOPTSTR)) != -1)
    179   1.1       cgd 		switch((char)ch) {
    180   1.1       cgd 		case 'a':
    181  1.25   mycroft 			what = KERN_PROC_ALL;
    182  1.25   mycroft 			flag = 0;
    183   1.1       cgd 			break;
    184  1.12   mycroft 		case 'c':
    185  1.12   mycroft 			commandonly = 1;
    186  1.12   mycroft 			break;
    187  1.11       cgd 		case 'e':			/* XXX set ufmt */
    188  1.11       cgd 			needenv = 1;
    189  1.11       cgd 			break;
    190   1.1       cgd 		case 'C':
    191   1.1       cgd 			rawcpu = 1;
    192   1.1       cgd 			break;
    193   1.1       cgd 		case 'g':
    194  1.11       cgd 			break;			/* no-op */
    195   1.1       cgd 		case 'h':
    196   1.1       cgd 			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
    197   1.1       cgd 			break;
    198   1.1       cgd 		case 'j':
    199   1.1       cgd 			parsefmt(jfmt);
    200  1.15   mycroft 			fmt = 1;
    201   1.1       cgd 			jfmt[0] = '\0';
    202   1.1       cgd 			break;
    203  1.28  bgrayson 		case 'K':
    204  1.47  jdolecek 			break;			/* no-op - was dontuseprocfs */
    205  1.11       cgd 		case 'L':
    206   1.1       cgd 			showkey();
    207   1.1       cgd 			exit(0);
    208  1.23   mycroft 			/* NOTREACHED */
    209   1.1       cgd 		case 'l':
    210   1.1       cgd 			parsefmt(lfmt);
    211  1.15   mycroft 			fmt = 1;
    212   1.1       cgd 			lfmt[0] = '\0';
    213   1.1       cgd 			break;
    214   1.1       cgd 		case 'M':
    215   1.1       cgd 			memf = optarg;
    216   1.1       cgd 			break;
    217   1.1       cgd 		case 'm':
    218   1.1       cgd 			sortby = SORTMEM;
    219   1.1       cgd 			break;
    220   1.1       cgd 		case 'N':
    221   1.1       cgd 			nlistf = optarg;
    222   1.1       cgd 			break;
    223   1.1       cgd 		case 'O':
    224   1.1       cgd 			parsefmt(o1);
    225   1.1       cgd 			parsefmt(optarg);
    226   1.1       cgd 			parsefmt(o2);
    227   1.1       cgd 			o1[0] = o2[0] = '\0';
    228  1.15   mycroft 			fmt = 1;
    229   1.1       cgd 			break;
    230   1.1       cgd 		case 'o':
    231   1.1       cgd 			parsefmt(optarg);
    232  1.15   mycroft 			fmt = 1;
    233   1.1       cgd 			break;
    234   1.1       cgd 		case 'p':
    235  1.25   mycroft 			what = KERN_PROC_PID;
    236  1.25   mycroft 			flag = atol(optarg);
    237   1.1       cgd 			xflg = 1;
    238   1.1       cgd 			break;
    239   1.1       cgd 		case 'r':
    240   1.1       cgd 			sortby = SORTCPU;
    241   1.1       cgd 			break;
    242   1.1       cgd 		case 'S':
    243   1.1       cgd 			sumrusage = 1;
    244   1.1       cgd 			break;
    245   1.1       cgd 		case 'T':
    246  1.22   mycroft 			if ((ttname = ttyname(STDIN_FILENO)) == NULL)
    247  1.11       cgd 				errx(1, "stdin: not a terminal");
    248  1.22   mycroft 			goto tty;
    249  1.22   mycroft 		case 't':
    250  1.22   mycroft 			ttname = optarg;
    251  1.22   mycroft 		tty: {
    252  1.11       cgd 			struct stat sb;
    253  1.11       cgd 			char *ttypath, pathbuf[MAXPATHLEN];
    254   1.1       cgd 
    255  1.36    simonb 			flag = 0;
    256  1.36    simonb 			if (strcmp(ttname, "?") == 0)
    257  1.36    simonb 				flag = KERN_PROC_TTY_NODEV;
    258  1.36    simonb 			else if (strcmp(ttname, "-") == 0)
    259  1.36    simonb 				flag = KERN_PROC_TTY_REVOKE;
    260  1.36    simonb 			else if (strcmp(ttname, "co") == 0)
    261   1.1       cgd 				ttypath = _PATH_CONSOLE;
    262  1.26       kim 			else if (*ttname != '/')
    263  1.11       cgd 				(void)snprintf(ttypath = pathbuf,
    264  1.26       kim 				    sizeof(pathbuf), "%s%s", _PATH_TTY, ttname);
    265   1.1       cgd 			else
    266  1.26       kim 				ttypath = ttname;
    267  1.25   mycroft 			what = KERN_PROC_TTY;
    268  1.36    simonb 			if (flag == 0) {
    269  1.36    simonb 				if (stat(ttypath, &sb) == -1)
    270  1.36    simonb 					err(1, "%s", ttypath);
    271  1.36    simonb 				if (!S_ISCHR(sb.st_mode))
    272  1.36    simonb 					errx(1, "%s: not a terminal", ttypath);
    273  1.36    simonb 				flag = sb.st_rdev;
    274  1.36    simonb 			}
    275   1.1       cgd 			break;
    276   1.1       cgd 		}
    277  1.25   mycroft 		case 'U':
    278  1.25   mycroft 			if (*optarg != '\0') {
    279  1.25   mycroft 				struct passwd *pw;
    280  1.25   mycroft 				char *ep;
    281  1.25   mycroft 
    282  1.25   mycroft 				what = KERN_PROC_UID;
    283  1.25   mycroft 				pw = getpwnam(optarg);
    284  1.25   mycroft 				if (pw == NULL) {
    285  1.25   mycroft 					errno = 0;
    286  1.25   mycroft 					flag = strtoul(optarg, &ep, 10);
    287  1.25   mycroft 					if (errno)
    288  1.25   mycroft 						err(1, "%s", optarg);
    289  1.25   mycroft 					if (*ep != '\0')
    290  1.25   mycroft 						errx(1, "%s: illegal user name",
    291  1.25   mycroft 						    optarg);
    292  1.25   mycroft 				} else
    293  1.25   mycroft 					flag = pw->pw_uid;
    294  1.25   mycroft 			}
    295  1.25   mycroft 			break;
    296   1.1       cgd 		case 'u':
    297   1.1       cgd 			parsefmt(ufmt);
    298   1.1       cgd 			sortby = SORTCPU;
    299  1.15   mycroft 			fmt = 1;
    300   1.1       cgd 			ufmt[0] = '\0';
    301   1.1       cgd 			break;
    302   1.1       cgd 		case 'v':
    303   1.1       cgd 			parsefmt(vfmt);
    304   1.1       cgd 			sortby = SORTMEM;
    305  1.15   mycroft 			fmt = 1;
    306   1.1       cgd 			vfmt[0] = '\0';
    307   1.1       cgd 			break;
    308   1.1       cgd 		case 'W':
    309   1.1       cgd 			swapf = optarg;
    310   1.1       cgd 			break;
    311   1.1       cgd 		case 'w':
    312   1.6       cgd 			if (wflag)
    313   1.6       cgd 				termwidth = UNLIMITED;
    314   1.6       cgd 			else if (termwidth < 131)
    315   1.1       cgd 				termwidth = 131;
    316  1.11       cgd 			wflag++;
    317   1.1       cgd 			break;
    318   1.1       cgd 		case 'x':
    319   1.1       cgd 			xflg = 1;
    320   1.1       cgd 			break;
    321   1.1       cgd 		case '?':
    322   1.1       cgd 		default:
    323   1.1       cgd 			usage();
    324   1.1       cgd 		}
    325   1.1       cgd 	argc -= optind;
    326   1.1       cgd 	argv += optind;
    327   1.1       cgd 
    328   1.1       cgd #define	BACKWARD_COMPATIBILITY
    329   1.1       cgd #ifdef	BACKWARD_COMPATIBILITY
    330   1.1       cgd 	if (*argv) {
    331   1.1       cgd 		nlistf = *argv;
    332   1.1       cgd 		if (*++argv) {
    333   1.1       cgd 			memf = *argv;
    334   1.1       cgd 			if (*++argv)
    335   1.1       cgd 				swapf = *argv;
    336   1.1       cgd 		}
    337   1.1       cgd 	}
    338   1.1       cgd #endif
    339  1.11       cgd 
    340  1.41    simonb 	if (memf == NULL && swapf == NULL) {
    341  1.38    simonb 		kd = kvm_openfiles(nlistf, memf, swapf, KVM_NO_FILES, errbuf);
    342  1.38    simonb 		donlist_sysctl();
    343  1.38    simonb 	} else
    344  1.38    simonb 		kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf);
    345  1.38    simonb 
    346  1.47  jdolecek 	if (kd == 0)
    347  1.47  jdolecek 		errx(1, "%s", errbuf);
    348  1.21       mrg 
    349  1.15   mycroft 	if (!fmt)
    350   1.1       cgd 		parsefmt(dfmt);
    351   1.1       cgd 
    352   1.1       cgd 	/*
    353  1.39    simonb 	 * scan requested variables, noting what structures are needed.
    354   1.1       cgd 	 */
    355   1.1       cgd 	scanvars();
    356  1.29  jdolecek 
    357   1.1       cgd 	/*
    358   1.1       cgd 	 * select procs
    359   1.1       cgd 	 */
    360  1.38    simonb 	if (!kd || !(kinfo = getkinfo_kvm(kd, what, flag, &nentries)))
    361  1.27  bgrayson 	{
    362  1.29  jdolecek 		if (kd)
    363  1.29  jdolecek 			warnx("%s.", kvm_geterr(kd));
    364  1.47  jdolecek 		exit(1);
    365  1.27  bgrayson 	}
    366  1.39    simonb 	if (nentries == 0) {
    367  1.39    simonb 		printheader();
    368  1.42     cyber 		exit(1);
    369  1.39    simonb 	}
    370   1.1       cgd 	/*
    371   1.1       cgd 	 * sort proc list
    372   1.1       cgd 	 */
    373  1.38    simonb 	qsort(kinfo, nentries, sizeof(struct kinfo_proc2), pscomp);
    374   1.1       cgd 	/*
    375  1.39    simonb 	 * For each proc, call each variable output function in
    376  1.39    simonb 	 * "setwidth" mode to determine the widest element of
    377  1.39    simonb 	 * the column.
    378  1.39    simonb 	 */
    379  1.39    simonb 	if (mode == PRINTMODE)
    380  1.39    simonb 		for (i = 0; i < nentries; i++) {
    381  1.39    simonb 			struct kinfo_proc2 *ki = &kinfo[i];
    382  1.39    simonb 
    383  1.39    simonb 			if (xflg == 0 && (ki->p_tdev == NODEV ||
    384  1.39    simonb 			    (ki->p_flag & P_CONTROLT) == 0))
    385  1.39    simonb 				continue;
    386  1.39    simonb 			for (vent = vhead; vent; vent = vent->next)
    387  1.39    simonb 				(vent->var->oproc)(ki, vent, WIDTHMODE);
    388  1.39    simonb 		}
    389  1.39    simonb 	/*
    390  1.39    simonb 	 * Print header - AFTER determining process field widths.
    391  1.39    simonb 	 * printheader() also adds up the total width of all
    392  1.39    simonb 	 * fields the first time it's called.
    393  1.39    simonb 	 */
    394  1.39    simonb 	printheader();
    395  1.39    simonb 	/*
    396  1.39    simonb 	 * For each proc, call each variable output function in
    397  1.39    simonb 	 * print mode.
    398   1.1       cgd 	 */
    399   1.1       cgd 	for (i = lineno = 0; i < nentries; i++) {
    400  1.38    simonb 		struct kinfo_proc2 *ki = &kinfo[i];
    401  1.14   mycroft 
    402  1.38    simonb 		if (xflg == 0 && (ki->p_tdev == NODEV ||
    403  1.38    simonb 		    (ki->p_flag & P_CONTROLT ) == 0))
    404   1.1       cgd 			continue;
    405   1.1       cgd 		for (vent = vhead; vent; vent = vent->next) {
    406  1.39    simonb 			(vent->var->oproc)(ki, vent, mode);
    407   1.1       cgd 			if (vent->next != NULL)
    408  1.11       cgd 				(void)putchar(' ');
    409   1.1       cgd 		}
    410  1.11       cgd 		(void)putchar('\n');
    411  1.11       cgd 		if (prtheader && lineno++ == prtheader - 4) {
    412  1.11       cgd 			(void)putchar('\n');
    413   1.1       cgd 			printheader();
    414   1.1       cgd 			lineno = 0;
    415   1.1       cgd 		}
    416   1.1       cgd 	}
    417   1.1       cgd 	exit(eval);
    418  1.23   mycroft 	/* NOTREACHED */
    419   1.1       cgd }
    420   1.1       cgd 
    421  1.38    simonb static struct kinfo_proc2 *
    422  1.45     lukem getkinfo_kvm(kdp, what, flag, nentriesp)
    423  1.45     lukem 	kvm_t *kdp;
    424  1.38    simonb 	int what, flag, *nentriesp;
    425  1.29  jdolecek {
    426  1.45     lukem 	return (kvm_getproc2(kdp, what, flag, sizeof(struct kinfo_proc2),
    427  1.38    simonb 	    nentriesp));
    428  1.29  jdolecek }
    429  1.29  jdolecek 
    430  1.11       cgd static void
    431   1.1       cgd scanvars()
    432   1.1       cgd {
    433  1.11       cgd 	struct varent *vent;
    434  1.11       cgd 	VAR *v;
    435   1.1       cgd 
    436   1.1       cgd 	for (vent = vhead; vent; vent = vent->next) {
    437   1.1       cgd 		v = vent->var;
    438  1.43      matt 		if (v->flag & COMM) {
    439   1.1       cgd 			needcomm = 1;
    440  1.43      matt 			break;
    441  1.43      matt 		}
    442   1.1       cgd 	}
    443  1.11       cgd }
    444   1.1       cgd 
    445  1.11       cgd static int
    446  1.11       cgd pscomp(a, b)
    447  1.11       cgd 	const void *a, *b;
    448   1.1       cgd {
    449  1.38    simonb 	struct kinfo_proc2 *ka = (struct kinfo_proc2 *)a;
    450  1.38    simonb 	struct kinfo_proc2 *kb = (struct kinfo_proc2 *)b;
    451  1.38    simonb 
    452   1.1       cgd 	int i;
    453  1.38    simonb #define VSIZE(k) (k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize)
    454   1.1       cgd 
    455   1.1       cgd 	if (sortby == SORTCPU)
    456  1.38    simonb 		return (getpcpu(kb) - getpcpu(ka));
    457   1.1       cgd 	if (sortby == SORTMEM)
    458  1.38    simonb 		return (VSIZE(kb) - VSIZE(ka));
    459  1.38    simonb 	i =  ka->p_tdev - kb->p_tdev;
    460  1.38    simonb 
    461   1.1       cgd 	if (i == 0)
    462  1.38    simonb 		i = ka->p_pid - kb->p_pid;
    463   1.1       cgd 	return (i);
    464   1.1       cgd }
    465   1.1       cgd 
    466   1.1       cgd /*
    467   1.1       cgd  * ICK (all for getopt), would rather hide the ugliness
    468   1.1       cgd  * here than taint the main code.
    469   1.1       cgd  *
    470   1.1       cgd  *  ps foo -> ps -foo
    471   1.1       cgd  *  ps 34 -> ps -p34
    472   1.1       cgd  *
    473  1.25   mycroft  * The old convention that 't' with no trailing tty arg means the user's
    474   1.1       cgd  * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
    475   1.1       cgd  * feature is available with the option 'T', which takes no argument.
    476   1.1       cgd  */
    477  1.11       cgd static char *
    478   1.1       cgd kludge_oldps_options(s)
    479   1.1       cgd 	char *s;
    480   1.1       cgd {
    481   1.1       cgd 	size_t len;
    482   1.1       cgd 	char *newopts, *ns, *cp;
    483   1.1       cgd 
    484   1.1       cgd 	len = strlen(s);
    485  1.16   thorpej 	if ((newopts = ns = malloc(len + 3)) == NULL)
    486  1.32  drochner 		err(1, NULL);
    487   1.1       cgd 	/*
    488   1.1       cgd 	 * options begin with '-'
    489   1.1       cgd 	 */
    490   1.1       cgd 	if (*s != '-')
    491   1.1       cgd 		*ns++ = '-';	/* add option flag */
    492   1.1       cgd 	/*
    493   1.1       cgd 	 * gaze to end of argv[1]
    494   1.1       cgd 	 */
    495   1.1       cgd 	cp = s + len - 1;
    496   1.1       cgd 	/*
    497  1.40    simonb 	 * if the last letter is a 't' flag and there are no other option
    498  1.40    simonb 	 * characters that take arguments (eg U, p, o) in the option
    499  1.40    simonb 	 * string and the option string doesn't start with a '-' then
    500  1.40    simonb 	 * convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
    501   1.1       cgd 	 */
    502  1.40    simonb 	if (*cp == 't' && *s != '-' && strpbrk(s, ARGOPTS) == NULL)
    503   1.1       cgd 		*cp = 'T';
    504   1.1       cgd 	else {
    505   1.1       cgd 		/*
    506   1.1       cgd 		 * otherwise check for trailing number, which *may* be a
    507   1.1       cgd 		 * pid.
    508   1.1       cgd 		 */
    509   1.1       cgd 		while (cp >= s && isdigit(*cp))
    510   1.1       cgd 			--cp;
    511   1.1       cgd 	}
    512   1.1       cgd 	cp++;
    513  1.11       cgd 	memmove(ns, s, (size_t)(cp - s));	/* copy up to trailing number */
    514   1.1       cgd 	ns += cp - s;
    515   1.1       cgd 	/*
    516   1.1       cgd 	 * if there's a trailing number, and not a preceding 'p' (pid) or
    517   1.1       cgd 	 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
    518   1.1       cgd 	 */
    519  1.25   mycroft 	if (isdigit(*cp) &&
    520  1.25   mycroft 	    (cp == s || (cp[-1] != 'U' && cp[-1] != 't' && cp[-1] != 'p' &&
    521  1.25   mycroft 	     (cp - 1 == s || cp[-2] != 't'))))
    522   1.1       cgd 		*ns++ = 'p';
    523  1.18       mrg 	/* and append the number */
    524  1.40    simonb 	(void)strcpy(ns, cp);		/* XXX strcpy is safe here */
    525   1.1       cgd 
    526   1.1       cgd 	return (newopts);
    527   1.1       cgd }
    528   1.1       cgd 
    529  1.11       cgd static void
    530  1.11       cgd usage()
    531   1.1       cgd {
    532   1.1       cgd 
    533  1.11       cgd 	(void)fprintf(stderr,
    534  1.11       cgd 	    "usage:\t%s\n\t   %s\n\t%s\n",
    535  1.47  jdolecek 	    "ps [-acCehjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty]",
    536  1.34   hubertf 	    "[-M core] [-N system] [-W swap] [-U username]",
    537  1.11       cgd 	    "ps [-L]");
    538   1.1       cgd 	exit(1);
    539  1.23   mycroft 	/* NOTREACHED */
    540   1.1       cgd }
    541