1 /*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)genget.c 5.1 (Berkeley) 02/28/91"; 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 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 ** 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 75 Ambiguous(s) 76 char *s; 77 { 78 return((char **)s == &ambiguous); 79 } 80