FreeBSD/contrib/ntp/ntpq/ntpq.c
/*
 * findcmd - find a command in a command description table
 */
find a command in a command description table
find a command in a command description table
static int
findcmd(
	const char *	str,
	struct xcmd *	clist1,
	struct xcmd *	clist2,
	struct xcmd **	cmd
	)
{
	struct xcmd *cl;
	size_t clen;
	int nmatch;
	struct xcmd *nearmatch = NULL;
	struct xcmd *clist;
	clen = strlen(str);
	nmatch = 0;
	if (clist1 != 0)
	    clist = clist1;
	else if (clist2 != 0)
	    clist = clist2;
	else
	    return 0;
    again:
	for (cl = clist; cl->keyword != 0; cl++) {
		if (*str != *(cl->keyword))
		    continue;
		if (strncmp(str, cl->keyword, (unsigned)clen) == 0) {
			if (*((cl->keyword) + clen) == '\0') {
				*cmd = cl;
				return 1;
			}
			nmatch++;
			nearmatch = cl;
		}
	}
	if (clist == clist1 && clist2 != 0) {
		clist = clist2;
		goto again;
	}
	if (nmatch == 1) {
		*cmd = nearmatch;
		return 1;
	}
	return nmatch;
}
static int findcmd ( const char * str , struct xcmd * clist1 , struct xcmd * clist2 , struct xcmd * * cmd ) { struct xcmd * cl ; size_t clen ; int nmatch ; struct xcmd * nearmatch = NULL ; struct xcmd * clist ; clen = strlen ( str ) ; nmatch = 0 ; if ( clist1 != 0 ) clist = clist1 ; else if ( clist2 != 0 ) clist = clist2 ; else return 0 ; again : for ( cl = clist ; cl -> keyword != 0 ; cl ++ ) { if ( * str != * ( cl -> keyword ) ) continue ; if ( strncmp ( str , cl -> keyword , ( unsigned ) clen ) == 0 ) { if ( * ( ( cl -> keyword ) + clen ) == ' \0 ' ) { * cmd = cl ; return 1 ; } nmatch ++ ; nearmatch = cl ; } } if ( clist == clist1 && clist2 != 0 ) { clist = clist2 ; goto again ; } if ( nmatch == 1 ) { * cmd = nearmatch ; return 1 ; } return nmatch ; }
findcmd - find a command in a command description table
findcmd - find a command in a command description table
/* do a first character check, for efficiency */ /*
			 * Could be extact match, could be approximate.
			 * Is exact if the length of the keyword is the
			 * same as the str.
			 */ /*
	 * See if there is more to do.  If so, go again.  Sorry about the
	 * goto, too much looking at BSD sources...
	 */ /*
	 * If we got extactly 1 near match, use it, else return number
	 * of matches.
	 */