xref: /original-bsd/lib/libtelnet/genget.c (revision c4f3b704)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)genget.c	8.2 (Berkeley) 05/30/95";
10 #endif /* not lint */
11 
12 
13 #include <ctype.h>
14 
15 #define	LOWER(x) (isupper(x) ? tolower(x) : (x))
16 /*
17  * The prefix function returns 0 if *s1 is not a prefix
18  * of *s2.  If *s1 exactly matches *s2, the negative of
19  * the length is returned.  If *s1 is a prefix of *s2,
20  * the length of *s1 is returned.
21  */
22 	int
isprefix(s1,s2)23 isprefix(s1, s2)
24 	register char *s1, *s2;
25 {
26 	register int n = 0;
27 	char *os1;
28 	register char c1, c2;
29 
30 	if (*s1 == '\0')
31 		return(-1);
32 	os1 = s1;
33 	c1 = *s1;
34 	c2 = *s2;
35 	while (LOWER(c1) == LOWER(c2)) {
36 		if (c1 == '\0')
37 			break;
38 		c1 = *++s1;
39 		c2 = *++s2;
40 	}
41 	return(*s1 ? 0 : (*s2 ? (s1 - os1) : (os1 - s1)));
42 }
43 
44 static char *ambiguous;		/* special return value for command routines */
45 
46 	char **
genget(name,table,stlen)47 genget(name, table, stlen)
48 	char	*name;		/* name to match */
49 	char	**table;	/* name entry in table */
50 	int	stlen;
51 {
52 	register char **c, **found;
53 	register int n;
54 
55 	if (name == 0)
56 	    return 0;
57 
58 	found = 0;
59 	for (c = table; *c != 0; c = (char **)((char *)c + stlen)) {
60 		if ((n = isprefix(name, *c)) == 0)
61 			continue;
62 		if (n < 0)		/* exact match */
63 			return(c);
64 		if (found)
65 			return(&ambiguous);
66 		found = c;
67 	}
68 	return(found);
69 }
70 
71 /*
72  * Function call version of Ambiguous()
73  */
74 	int
Ambiguous(s)75 Ambiguous(s)
76 	char *s;
77 {
78 	return((char **)s == &ambiguous);
79 }
80