if (kd == 0)
if (kd == NULL && (nlistf != NULL || memf != NULL || swapf != NULL))
main(argc, argv)	int argc;	char *argv[];{	struct kinfo_proc *kp;	struct varent *vent;	struct winsize ws;	struct passwd *pwd;	dev_t ttydev;	pid_t pid;	uid_t uid;	int all, ch, flag, i, fmt, lineno, nentries, mib[4], mibcnt, nproc;	int prtheader, wflag, kflag, what, xflg;	char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX];	size_t size;	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||	     ws.ws_col == 0)		termwidth = 79;	else		termwidth = ws.ws_col - 1;	if (argc > 1)		argv[1] = kludge_oldps_options(argv[1]);	all = fmt = prtheader = wflag = kflag = xflg = 0;	pid = -1;	uid = (uid_t) -1;	ttydev = NODEV;	memf = nlistf = swapf = NULL;	while ((ch = getopt(argc, argv,	    "acCeghjkLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1)		switch((char)ch) {		case 'a':			all = 1;			break;		case 'c':			commandonly = 1;			break;		case 'e':			/* XXX set ufmt */			needenv = 1;			break;		case 'C':			rawcpu = 1;			break;		case 'g':			break;			/* no-op */		case 'h':			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;			break;		case 'j':			parsefmt(jfmt);			fmt = 1;			jfmt[0] = '\0';			break;		case 'k':			kflag++;			break;		case 'L':			showkey();			exit(0);		case 'l':			parsefmt(lfmt);			fmt = 1;			lfmt[0] = '\0';			break;		case 'M':			memf = optarg;			break;		case 'm':			sortby = SORTMEM;			break;		case 'N':			nlistf = optarg;			break;		case 'O':			parsefmt(o1);			parsefmt(optarg);			parsefmt(o2);			o1[0] = o2[0] = '\0';			fmt = 1;			break;		case 'o':			parsefmt(optarg);			fmt = 1;			break;		case 'p':			pid = atol(optarg);			xflg = 1;			break;		case 'r':			sortby = SORTCPU;			break;		case 'S':			sumrusage = 1;			break;		case 'T':			if ((optarg = ttyname(STDIN_FILENO)) == NULL)				errx(1, "stdin: not a terminal");			/* FALLTHROUGH */		case 't': {			struct stat sb;			char *ttypath, pathbuf[MAXPATHLEN];			if (strcmp(optarg, "co") == 0)				ttypath = _PATH_CONSOLE;			else if (*optarg != '/')				(void)snprintf(ttypath = pathbuf,				    sizeof(pathbuf), "%s%s", _PATH_TTY, optarg);			else				ttypath = optarg;			if (stat(ttypath, &sb) == -1)				err(1, "%s", ttypath);			if (!S_ISCHR(sb.st_mode))				errx(1, "%s: not a terminal", ttypath);			ttydev = sb.st_rdev;			break;		}		case 'U':			pwd = getpwnam(optarg);			if (pwd == NULL)				errx(1, "%s: no such user", optarg);			uid = pwd->pw_uid;			endpwent();			xflg = 1;			break;		case 'u':			parsefmt(ufmt);			sortby = SORTCPU;			fmt = 1;			ufmt[0] = '\0';			break;		case 'v':			parsefmt(vfmt);			sortby = SORTMEM;			fmt = 1;			vfmt[0] = '\0';			break;		case 'W':			swapf = optarg;			break;		case 'w':			if (wflag)				termwidth = UNLIMITED;			else if (termwidth < 131)				termwidth = 131;			wflag++;			break;		case 'x':			xflg = 1;			break;		default:			usage();		}	argc -= optind;	argv += optind;#define	BACKWARD_COMPATIBILITY#ifdef	BACKWARD_COMPATIBILITY	if (*argv) {		nlistf = *argv;		if (*++argv) {			memf = *argv;			if (*++argv)				swapf = *argv;		}	}#endif	/*	 * Discard setgid privileges if not the running kernel so that bad	 * guys can't print interesting stuff from kernel memory.	 */	if (nlistf != NULL || memf != NULL || swapf != NULL) {		setegid(getgid());		setgid(getgid());	}	kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf);	if (kd == 0)		errx(1, "%s", errbuf);	setegid(getgid());	setgid(getgid());	if (!fmt)		parsefmt(dfmt);	/* XXX - should be cleaner */	if (!all && ttydev == NODEV && pid == -1 && uid == (uid_t)-1)		uid = getuid();	/*	 * scan requested variables, noting what structures are needed,	 * and adjusting header widths as appropiate.	 */	scanvars();	/*	 * get proc list	 */	if (uid != (uid_t) -1) {		what = mib[2] = KERN_PROC_UID;		flag = mib[3] = uid;		mibcnt = 4;	} else if (ttydev != NODEV) {		what = mib[2] = KERN_PROC_TTY;		flag = mib[3] = ttydev;		mibcnt = 4;	} else if (pid != -1) {		what = mib[2] = KERN_PROC_PID;		flag = mib[3] = pid;		mibcnt = 4;	} else if (kflag) {		what = mib[2] = KERN_PROC_KTHREAD;		flag = 0;		mibcnt = 3;	} else {		what = mib[2] = KERN_PROC_ALL;		flag = 0;		mibcnt = 3;	}	/*	 * select procs	 */	if (kd != NULL) {		if ((kp = kvm_getprocs(kd, what, flag, &nentries)) == 0)			errx(1, "%s", kvm_geterr(kd));	}	else {		mib[0] = CTL_KERN;		mib[1] = KERN_NPROCS;		size = sizeof (nproc);		if (sysctl(mib, 2, &nproc, &size, NULL, 0) < 0)			err(1, "could not get kern.nproc");		/* Allocate more memory than is needed, just in case */		size = (5 * nproc * sizeof(struct kinfo_proc)) / 4;		kp = calloc(size, sizeof(char));		if (kp == NULL)			err(1,			    "failed to allocated memory for proc structures");		mib[1] = KERN_PROC;		if (sysctl(mib, mibcnt, kp, &size, NULL, 0) < 0)			err(1, "could not read kern.proc");		nentries = size / sizeof(struct kinfo_proc);	}	if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL)		err(1, NULL);	for (i = nentries; --i >= 0; ++kp) {		kinfo[i].ki_p = kp;		saveuser(&kinfo[i]);	}	/*	 * print header	 */	printheader();	if (nentries == 0)		exit(1);	/*	 * sort proc list	 */	qsort(kinfo, nentries, sizeof(KINFO), pscomp);	/*	 * for each proc, call each variable output function.	 */	for (i = lineno = 0; i < nentries; i++) {		KINFO *ki = &kinfo[i];		if (xflg == 0 && (KI_EPROC(ki)->e_tdev == NODEV ||		    (KI_PROC(ki)->p_flag & P_CONTROLT ) == 0))			continue;		for (vent = vhead; vent; vent = vent->next) {			(vent->var->oproc)(ki, vent);			if (vent->next != NULL)				(void)putchar(' ');		}		(void)putchar('\n');		if (prtheader && lineno++ == prtheader - 4) {			(void)putchar('\n');			printheader();			lineno = 0;		}	}	exit(eval);}