if ((*action)(kp) == -1)
if ((*action)(kp, j++) == -1)
main(int argc, char **argv){	extern char *optarg;	extern int optind;	char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;	int i, j, ch, bestidx, rv, criteria;	int (*action)(struct kinfo_proc2 *);	struct kinfo_proc2 *kp;	struct list *li;	u_int32_t bestsec, bestusec;	regex_t reg;	regmatch_t regmatch;	if (strcmp(__progname, "pgrep") == 0) {		action = grepact;		pgrep = 1;	} else {		action = killact;		p = argv[1];		if (argc > 1 && p[0] == '-') {			p++;			i = (int)strtol(p, &q, 10);			if (*q == '\0') {				signum = i;				argv++;				argc--;			} else {				if (strncasecmp(p, "sig", 3) == 0)					p += 3;				for (i = 1; i < NSIG; i++)					if (strcasecmp(sys_signame[i], p) == 0)						break;				if (i != NSIG) {					signum = i;					argv++;					argc--;				}			}		}	}	criteria = 0;	while ((ch = getopt(argc, argv, "G:P:U:d:fg:lns:t:u:vx")) != -1)		switch (ch) {		case 'G':			makelist(&rgidlist, LT_GROUP, optarg);			criteria = 1;			break;		case 'P':			makelist(&ppidlist, LT_GENERIC, optarg);			criteria = 1;			break;		case 'U':			makelist(&ruidlist, LT_USER, optarg);			criteria = 1;			break;		case 'd':			if (!pgrep)				usage();			delim = optarg;			break;		case 'f':			matchargs = 1;			break;		case 'g':			makelist(&pgrplist, LT_PGRP, optarg);			criteria = 1;			break;		case 'l':			if (!pgrep)				usage();			longfmt = 1;			break;		case 'n':			newest = 1;			criteria = 1;			break;		case 's':			makelist(&sidlist, LT_SID, optarg);			criteria = 1;			break;		case 't':			makelist(&tdevlist, LT_TTY, optarg);			criteria = 1;			break;		case 'u':			makelist(&euidlist, LT_USER, optarg);			criteria = 1;			break;		case 'v':			inverse = 1;			break;		case 'x':			fullmatch = 1;			break;		default:			usage();			/* NOTREACHED */		}	argc -= optind;	argv += optind;	if (argc != 0)		criteria = 1;	if (!criteria)		usage();	mypid = getpid();	/*	 * Retrieve the list of running processes from the kernel.	 */	kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf);	if (kd == NULL)		errx(STATUS_ERROR, "kvm_openfiles(): %s", buf);	plist = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc);	if (plist == NULL)		errx(STATUS_ERROR, "kvm_getproc2() failed");	/*	 * Allocate memory which will be used to keep track of the	 * selection.	 */	if ((selected = malloc(nproc)) == NULL)		errx(STATUS_ERROR, "memory allocation failure");	memset(selected, 0, nproc);	/*	 * Refine the selection.	 */	for (; *argv != NULL; argv++) {		if ((rv = regcomp(&reg, *argv, REG_EXTENDED)) != 0) {			regerror(rv, &reg, buf, sizeof(buf));			errx(STATUS_BADUSAGE, "bad expression: %s", buf);		}		for (i = 0, kp = plist; i < nproc; i++, kp++) {			if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)				continue;			if (matchargs) {				if ((pargv = kvm_getargv2(kd, kp, 0)) == NULL)					continue;				j = 0;				while (j < sizeof(buf) && *pargv != NULL) {					int ret;					ret = snprintf(buf + j, sizeof(buf) - j,					    pargv[1] != NULL ? "%s " : "%s",					    pargv[0]);					if (ret > 0)						j += ret;					pargv++;				}				mstr = buf;			} else				mstr = kp->p_comm;			rv = regexec(&reg, mstr, 1, &regmatch, 0);			if (rv == 0) {				if (fullmatch) {					if (regmatch.rm_so == 0 &&					    regmatch.rm_eo == strlen(mstr))						selected[i] = 1;				} else					selected[i] = 1;			} else if (rv != REG_NOMATCH) {				regerror(rv, &reg, buf, sizeof(buf));				errx(STATUS_ERROR, "regexec(): %s", buf);			}		}		regfree(&reg);	}	for (i = 0, kp = plist; i < nproc; i++, kp++) {		if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)			continue;		SLIST_FOREACH(li, &ruidlist, li_chain)			if (kp->p_ruid == (uid_t)li->li_number)				break;		if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {			selected[i] = 0;			continue;		}		SLIST_FOREACH(li, &rgidlist, li_chain)			if (kp->p_rgid == (gid_t)li->li_number)				break;		if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {			selected[i] = 0;			continue;		}		SLIST_FOREACH(li, &euidlist, li_chain)			if (kp->p_uid == (uid_t)li->li_number)				break;		if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {			selected[i] = 0;			continue;		}		SLIST_FOREACH(li, &ppidlist, li_chain)			if (kp->p_ppid == (uid_t)li->li_number)				break;		if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {			selected[i] = 0;			continue;		}		SLIST_FOREACH(li, &pgrplist, li_chain)			if (kp->p__pgid == (uid_t)li->li_number)				break;		if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {			selected[i] = 0;			continue;		}		SLIST_FOREACH(li, &tdevlist, li_chain) {			if (li->li_number == -1 &&			    (kp->p_flag & P_CONTROLT) == 0)				break;			if (kp->p_tdev == (uid_t)li->li_number)				break;		}		if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {			selected[i] = 0;			continue;		}		SLIST_FOREACH(li, &sidlist, li_chain)			if (kp->p_sid == (uid_t)li->li_number)				break;		if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {			selected[i] = 0;			continue;		}		if (argc == 0)			selected[i] = 1;	}	if (newest) {		bestsec = 0;		bestusec = 0;		bestidx = -1;		for (i = 0, kp = plist; i < nproc; i++, kp++) {			if (!selected[i])				continue;			if (kp->p_ustart_sec > bestsec ||			    (kp->p_ustart_sec == bestsec			    && kp->p_ustart_usec > bestusec)) {				bestsec = kp->p_ustart_sec;				bestusec = kp->p_ustart_usec;				bestidx = i;			}		}		memset(selected, 0, nproc);		if (bestidx != -1)			selected[bestidx] = 1;	}	/*	 * Take the appropriate action for each matched process, if any.	 */	for (i = 0, rv = STATUS_NOMATCH, kp = plist; i < nproc; i++, kp++) {		if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)			continue;		if (selected[i]) {			if (inverse)				continue;		} else if (!inverse)			continue;		if ((*action)(kp) == -1)			rv = STATUS_ERROR;		else if (rv != STATUS_ERROR)			rv = STATUS_MATCH;	}	exit(rv);}