home *** CD-ROM | disk | FTP | other *** search
- /* This source file is part of the LynxLib miscellaneous library by
- Robert Fischer, and is Copyright 1990 by Robert Fischer. It costs no
- money, and you may not make money off of it, but you may redistribute
- it. It comes with ABSOLUTELY NO WARRANTY. See the file LYNXLIB.DOC
- for more details.
- To contact the author:
- Robert Fischer \\80 Killdeer Rd \\Hamden, CT 06517 USA
- (203) 288-9599 fischer-robert@cs.yale.edu */
-
- #include <stddef.h>
-
- /* ----------------------------------------------------------- */
- BOOLEAN match_wild(wild, stringg) /* Sees if stringg matches with wild */
- char *stringg;
- char *wild;
- {
- register char *s, *w;
- char *last_star; /* Position just after last star */
- last_star = wild;
- for (s = stringg, w = wild; *s != '\0'; ) {
- /* Try to match the * to zero characters */
- if (*w == '*') {
- w++;
- last_star = w;
- } else if (*w == '?' || *w == *s) {
- w++;
- s++;
- } else if (last_star == wild) return FALSE; /* Fail */
- else { /* Backup */
- s = s + 1 - (w - last_star);
- w = last_star;
- }
- }
-
- while (*w == '*') w++;
- return (*w == NIL); /* Check that wildcard doesn't have too many chars */
- }
- /* ----------------------------------------------------------- */
- BOOLEAN match_fwild(wild, fname)
- /* Matches a file wildcard to a file -- takes care of '*.*' */
- char *wild;
- char *fname;
- {
- file_root sroot, wroot;
- file_extender sext, wext;
- pleaf(wild, wroot, wext);
- pleaf(fname, sroot, sext);
- if (wext[0] == NIL) {
- wext[0] = '*';
- wext[1] = NIL;
- }
- return match_wild(wroot, sroot) && match_wild(wext, sext);
- }
- /* ----------------------------------------------------------- */
- BOOLEAN match_fwilds(wild, fname)
- /* Matches a whole sequence of wildcards to a file */
- char *wild; /* The string of wildcards */
- char *fname; /* Leaf_name to match to the wildcards */
- {
- int i;
- char s[80];
- int start;
- BOOLEAN ret;
- file_root froot, wroot;
- file_extender fext, wext;
- BOOLEAN bang; /* Does current wildcard have a '!' before it? */
- BOOLEAN match; /* Return from match_fname() */
- char ch;
- /* Take care of *.* matching wunk */
- pleaf(fname, froot, fext);
-
- ret = FALSE;
- i = 0;
- while (wild[i] != NIL) {
- /* Get a wildcard string */
- for(start = i; wild[start] == ' '; start++) ;
- if (wild[start] == NIL) return ret;
-
- if (wild[start] == '!') {
- bang = TRUE;
- start++;
- while (wild[start] == ' ') start++;
- if (wild[start] == NIL) return ret; /* Ignore extraneous '!' */
- } else bang = FALSE;
-
- /* Copy wildcard to s */
- for (i = start; wild[i] != ' ' && wild[i] != NIL; i++);
- ch = wild[i];
- wild[i] = NIL;
- strcpy(s, wild + start);
- wild[i] = ch;
-
- /* Try to match the wildcard */
- pleaf(s, wroot, wext);
- match = match_wild(wroot, froot) && match_wild(wext, fext);
- if (bang) ret &= !match;
- else ret |= match;
- }
- return ret;
- }
- /* ----------------------------------------------------------- */
- #if 0
- main()
- {
- char s[100];
- char w[100];
- do {
- printf("Wild: ");
- gets(w);
- printf("String: ");
- gets(s);
- printf("Returned: %d\n", match_fwilds(w, s));
- } while (w[0] != NIL);
- }
- #endif
-