/* * MSWILD.C Wild-card matching * * This subroutine package provides the ability to use the '*' and '?' * wild-card characters in string matching (and particularly matching * file names). * * Copyright (c) 1991 Shal Farley * Cheshire Engineering Corporation * 650 Sierra Madre Villa Avenue, Suite 201 * Pasadena, California 91107 * (818) 351-5493 * (818) 351-8645 FAX * shal@alumni.caltech.edu * * This software may be used and distributed for any purpose without license or * royalty payments so long as the above copyright notice is preserved. If you * have any comments, bug fixes, improvements, or new programs based upon this * software I'd like to hear from you. */ #include #ifdef decus # include "msasse.h" #else # include # include #endif #include "msport.h" #include "msdefs.h" #if 0 /* Make this a zero when not debugging */ # define SONAR(s) fprintf(stderr, s) #else # define SONAR(s) #endif int WildMatch(sP, sM) /* Return True if sP matches sM */ char *sP; /* Pattern string, with wild-cards */ char *sM; /* Match string (no wild-cards) */ { assert(sP != NULL); assert(sM != NULL); /* SONAR(("WildMatch(\"%s\", \"%s\")\n", sP, sM)); */ if ((*sP == EOS) && (*sM == EOS)) return TRUE; if ((*sP == '*') && WildMatch(sP+1, sM)) return TRUE; if (*sM == EOS) return FALSE; if (*sP == '?') return WildMatch(sP+1, sM+1); if (*sP == '*') return WildMatch(sP, sM+1); if (tolower(*sP) == tolower(*sM)) return WildMatch(sP+1, sM+1); return FALSE; } int WfnMatch(sP, sM) /* Return True if sP matches sM */ char *sP; /* Pattern Filename, with wild-cards */ char *sM; /* Match filename, (no wild-cards) */ { char *sPT; /* Type field of pattern filename */ char *sMT; /* Type field of match filename */ int fPT; /* True if there was a type field */ int fMT; /* " */ int fMatch; assert(sP != NULL); assert(sM != NULL); fPT = FALSE; fMT = FALSE; if (*sP == EOS) sP = "*"; /* Wild-card Implicit */ if ((sPT = strchr(sP, '.')) != NULL) { fPT = TRUE; *sPT++ = EOS; } if ((sMT = strchr(sM, '.')) != NULL) { fMT = TRUE; *sMT++ = EOS; } else sMT = ""; fMatch = WildMatch(sP,sM) && (fPT? WildMatch(sPT,sMT) : TRUE); if (fPT == TRUE) *--sPT = '.'; if (fMT == TRUE) *--sMT = '.'; SONAR(("WfnMatch(\"%s\", \"%s\") == %d\n", sP, sM, fMatch)); return fMatch; } #if 0 /* Make this non-zero to compile a test */ main(argc,argv) int argc; char *argv[]; { if (argc != 3) { printf("Usage: MSWILD pattern string"); exit(1); } printf("WildMatch(\"%s\", \"%s\") == ", argv[1], argv[2]); printf("%s\n", WildMatch(argv[1], argv[2])? "True" : "False"); printf("WfnMatch(\"%s\", \"%s\") == ", argv[1], argv[2]); printf("%s\n", WfnMatch(argv[1], argv[2])? "True" : "False"); exit(0); } #endif /* test main */