]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/Extras/libh/swmtch.c
Replaced use of snprintf() with memcpy()/memmove().
[nmh] / docs / historical / mh-jun-1982 / Extras / libh / swmtch.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 /* switch match, or any unambiguous abbreviation */
9 /* exact match always wins, even if shares same root */
10 /* returns subscript in zero-terminated tbl[] of strings */
11 /* returns -1 if no match, -2 if ambiguous */
12
13 swmtch(string,tbl)
14 char *tbl[],*string;
15 {
16 register char *sp, *tcp, c;
17 char **tp;
18 int firstone;
19
20 firstone = -1;
21
22 for (tp=tbl; tcp = *tp; tp++) {
23 for (sp = string; *sp == *tcp++; ) {
24 if (*sp++ == 0) return(tp-tbl); /* exact match */
25 }
26 if (*sp != 0) {
27 if (*sp != ' ') continue; /* no match */
28 if (*--tcp == 0) return(tp-tbl); /* exact match */
29 }
30 if (firstone == -1) firstone = tp-tbl; /* possible match */
31 else firstone = -2; /* ambiguous */
32 }
33
34 return (firstone);
35 }