xref: /openbsd-src/usr.bin/telnet/genget.c (revision 29625f71f8480740478b31221eded302116f1615)
1*29625f71Sderaadt /*     $OpenBSD: genget.c,v 1.6 2019/07/11 05:11:16 deraadt Exp $  */
2d5337edcSderaadt 
3d5337edcSderaadt /*-
4d5337edcSderaadt  * Copyright (c) 1991, 1993
5d5337edcSderaadt  *	The Regents of the University of California.  All rights reserved.
6d5337edcSderaadt  *
7d5337edcSderaadt  * Redistribution and use in source and binary forms, with or without
8d5337edcSderaadt  * modification, are permitted provided that the following conditions
9d5337edcSderaadt  * are met:
10d5337edcSderaadt  * 1. Redistributions of source code must retain the above copyright
11d5337edcSderaadt  *    notice, this list of conditions and the following disclaimer.
12d5337edcSderaadt  * 2. Redistributions in binary form must reproduce the above copyright
13d5337edcSderaadt  *    notice, this list of conditions and the following disclaimer in the
14d5337edcSderaadt  *    documentation and/or other materials provided with the distribution.
15d5337edcSderaadt  * 3. Neither the name of the University nor the names of its contributors
16d5337edcSderaadt  *    may be used to endorse or promote products derived from this software
17d5337edcSderaadt  *    without specific prior written permission.
18d5337edcSderaadt  *
19d5337edcSderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20d5337edcSderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21d5337edcSderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22d5337edcSderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23d5337edcSderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24d5337edcSderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25d5337edcSderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26d5337edcSderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27d5337edcSderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28d5337edcSderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29d5337edcSderaadt  * SUCH DAMAGE.
30d5337edcSderaadt  */
31d5337edcSderaadt 
32d5337edcSderaadt /* $KTH: genget.c,v 1.6 1997/05/04 09:01:34 assar Exp $ */
33d5337edcSderaadt 
34d5337edcSderaadt #include <ctype.h>
355b25ad01Sguenther #include "telnet_locl.h"
36d5337edcSderaadt 
37d5337edcSderaadt /*
38d5337edcSderaadt  * The prefix function returns 0 if *s1 is not a prefix
39d5337edcSderaadt  * of *s2.  If *s1 exactly matches *s2, the negative of
40d5337edcSderaadt  * the length is returned.  If *s1 is a prefix of *s2,
41d5337edcSderaadt  * the length of *s1 is returned.
42d5337edcSderaadt  */
43d5337edcSderaadt int
isprefix(char * s1,char * s2)44d5337edcSderaadt isprefix(char *s1, char *s2)
45d5337edcSderaadt {
46d5337edcSderaadt     char *os1;
47d5337edcSderaadt     char c1, c2;
48d5337edcSderaadt 
49d5337edcSderaadt     if (*s1 == '\0')
50d5337edcSderaadt 	return(-1);
51d5337edcSderaadt     os1 = s1;
52d5337edcSderaadt     c1 = *s1;
53d5337edcSderaadt     c2 = *s2;
541186fcf9Sguenther     while (tolower((unsigned char)c1) == tolower((unsigned char)c2)) {
55d5337edcSderaadt 	if (c1 == '\0')
56d5337edcSderaadt 	    break;
57d5337edcSderaadt 	c1 = *++s1;
58d5337edcSderaadt 	c2 = *++s2;
59d5337edcSderaadt     }
60d5337edcSderaadt     return(*s1 ? 0 : (*s2 ? (s1 - os1) : (os1 - s1)));
61d5337edcSderaadt }
62d5337edcSderaadt 
63d5337edcSderaadt static char *ambiguous;		/* special return value for command routines */
64d5337edcSderaadt 
65d5337edcSderaadt char **
genget(char * name,char ** table,int stlen)66d5337edcSderaadt genget(char *name, char **table, int stlen)
67d5337edcSderaadt      /* name to match */
68d5337edcSderaadt      /* name entry in table */
69d5337edcSderaadt {
70d5337edcSderaadt     char **c, **found;
71d5337edcSderaadt     int n;
72d5337edcSderaadt 
73908f84c2Sjsg     if (name == NULL)
74908f84c2Sjsg 	return (NULL);
75d5337edcSderaadt 
76908f84c2Sjsg     found = NULL;
77908f84c2Sjsg     for (c = table; *c != NULL; c = (char **)((char *)c + stlen)) {
78d5337edcSderaadt 	if ((n = isprefix(name, *c)) == 0)
79d5337edcSderaadt 	    continue;
80d5337edcSderaadt 	if (n < 0)		/* exact match */
81d5337edcSderaadt 	    return(c);
82d5337edcSderaadt 	if (found)
83d5337edcSderaadt 	    return(&ambiguous);
84d5337edcSderaadt 	found = c;
85d5337edcSderaadt     }
86d5337edcSderaadt     return(found);
87d5337edcSderaadt }
88d5337edcSderaadt 
89d5337edcSderaadt /*
90d5337edcSderaadt  * Function call version of Ambiguous()
91d5337edcSderaadt  */
92d5337edcSderaadt int
Ambiguous(void * s)93d5337edcSderaadt Ambiguous(void *s)
94d5337edcSderaadt {
95d5337edcSderaadt     return((char **)s == &ambiguous);
96d5337edcSderaadt }
97