]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/Extras/libh/glbmtc.c
Replaced use of snprintf() with memcpy()/memmove().
[nmh] / docs / historical / mh-jun-1982 / Extras / libh / glbmtc.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 #
9 /* glob match subroutine --
10
11 "*" in params matches r.e ".*"
12 "?" in params matches r.e. "."
13 "[...]" in params matches character class
14 "[...a-z...]" in params matches a through z.
15 */
16
17 glbmtch(as, ap)
18 char *as, *ap;
19 {
20 register char *s, *p;
21 register scc;
22 int c, cc, ok, lc;
23
24 s = as;
25 p = ap;
26 if ((scc = *s++) && (scc =& 0177) == 0) scc = 0200;
27 switch (c = *p++) {
28 case '[':
29 ok = 0;
30 lc = 077777;
31 while (cc = *p++) {
32 if (cc==']') {
33 if (ok) return(glbmtch(s, p));
34 else return(0);
35 } else if (cc=='-') {
36 if (lc<=scc && scc<=(c = *p++)) ok++;
37 } else if (scc == (lc=cc)) ok++;
38 }
39 return(0);
40 default:
41 if (c!=scc) return(0);
42 case '?':
43 if (scc) return(glbmtch(s, p));
44 return(0);
45 case '*':
46 return(umatch(--s, p));
47 case '\0':
48 return(!scc);
49 }
50 }
51
52 umatch(s, p)
53 char *s, *p;
54 {
55 if(*p==0) return(1);
56 while(*s) if (glbmtch(s++,p)) return(1);
57 return(0);
58 }