Category : File Managers
Archive   : UNIXFIND.ZIP
Filename : AMATCH.C

 
Output of file : AMATCH.C contained in archive : UNIXFIND.ZIP

#include

#define TRUE 1
#define FALSE 0

extern char pathchar;
extern void error(char *format, ...);
extern int match_dot_flag;
extern int match_slash_flag;

/*
* amatch -- Return true if 'str' matches 'pat'. Based on the
* recursive routine amatch() (for "anchored match") in
* Kernighan & Plauger's "Software Tools".
*/
int amatch(char *str, char *pat)
{
int sc, pc; /* character at current position */

while ((pc = *pat++) != '\0')
{
if (pc == '[') {
sc = *str++;
for (;;) {
if (*pat == ']') return FALSE;
if (sc == *pat++) break;
}
while (*pat != ']' && *pat != '\0') pat++;
if (*pat != ']') error("pattern missing closing \"]\"");
}
else if (pc == '*') {
int nchars;

/* Try to match as many characters as possible. */

nchars = 0;
while ((sc = *str) != '\0') {
if (sc == '.' && !match_dot_flag) break;
if (sc == pathchar && !match_slash_flag) break;
str++;
nchars++;
}

/* We are now at the character that made us
* fail. Try to match the rest of the pattern.
* Shrink the closure by one for each failure.
* Since closure matches *zero* or more occurences
* of a pattern, a match may start even if the
* previous loop matched no characters.
*/
for (;;)
{
if (amatch(str--, pat)) return (TRUE);
if (nchars-- == 0) return (FALSE);
}
}
/* ? matches anything, but we only advance if not null. */
else if (pc == '?' && *str == '.') {if (match_dot_flag) str++;}
else if (pc == '?' && *str == pathchar) {if (match_slash_flag) str++;}
else if (pc == '?' && *str == '\0') ;
else if (pc == '?') str++;
/* exit if they don't match. */
else if (pc != *str++) return (FALSE);
} /* End of *str++ loop.*/
return (*str == '\0');
}

#ifdef DEBUG
#include
#include

char pathchar = '\\';
int match_dot_flag = 0;
int match_slash_flag = 0;

void error(char *format, ...)
{
va_list argptr;

printf("find: ");
va_start(argptr, format);
vprintf(format, argptr);
va_end(argptr);
printf("\n");
exit(1);
}

main(int argc, char *argv[])
{
if (argc < 3) {
fprintf(stderr, "usage: %s pat str");
exit(2);
}
if (amatch(argv[1], argv[2])) {
printf("Yes\n");
exit(0);
} else {
printf("No\n");
exit(1);
}
}
#endif