Path: blob/main/crypto/heimdal/appl/telnet/libtelnet/genget.c
34889 views
/*-1* Copyright (c) 1991, 19932* The Regents of the University of California. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. All advertising materials mentioning features or use of this software13* must display the following acknowledgement:14* This product includes software developed by the University of15* California, Berkeley and its contributors.16* 4. Neither the name of the University nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <config.h>34#ifdef HAVE_SYS_TYPES_H35#include <sys/types.h>36#endif37#include <ctype.h>38#include "misc-proto.h"3940RCSID("$Id$");414243#define LOWER(x) (isupper(x) ? tolower(x) : (x))44/*45* The prefix function returns 0 if *s1 is not a prefix46* of *s2. If *s1 exactly matches *s2, the negative of47* the length is returned. If *s1 is a prefix of *s2,48* the length of *s1 is returned.49*/5051int52isprefix(char *s1, char *s2)53{54char *os1;55char c1, c2;5657if (*s1 == '\0')58return(-1);59os1 = s1;60c1 = *s1;61c2 = *s2;62while (tolower((unsigned char)c1) == tolower((unsigned char)c2)) {63if (c1 == '\0')64break;65c1 = *++s1;66c2 = *++s2;67}68return(*s1 ? 0 : (*s2 ? (s1 - os1) : (os1 - s1)));69}7071static char *ambiguous; /* special return value for command routines */7273char **74genget(char *name, char **table, int stlen)75/* name to match */76/* name entry in table */7778{79char **c, **found;80int n;8182if (name == 0)83return 0;8485found = 0;86for (c = table; *c != 0; c = (char **)((char *)c + stlen)) {87if ((n = isprefix(name, *c)) == 0)88continue;89if (n < 0) /* exact match */90return(c);91if (found)92return(&ambiguous);93found = c;94}95return(found);96}9798/*99* Function call version of Ambiguous()100*/101int102Ambiguous(void *s)103{104return((char **)s == &ambiguous);105}106107108