Path: utzoo!utgpu!watmath!iuvax!mailrus!bbn!bbn.com!rsalz From: rsalz@bbn.com (Rich Salz) Newsgroups: alt.sources Subject: Re: amatch Message-ID: <1936@prune.bbn.com> Date: 17 Aug 89 14:40:18 GMT References: <3@sixhub.UUCP> Followup-To: alt.sources.d Organization: BBN Systems and Technologies Corporation Lines: 132 I wrote this a long time ago. It's short, fast, and clean. It was posted some time back and appears, e.g., in John Gilmore's PDtar. #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh'wildmat.c' <<'END_OF_FILE' X/* X** Do shell-style pattern matching for ?, \, [], and * characters. X** Might not be robust in face of malformed patterns; e.g., "foo[a-" X** could cause a segmentation violation. I think it's 8bit clean. X** X** Written by Rich $alz, bbn!rsalz, Wed Nov 26 19:03:17 EST 1986. X*/ X X#define TRUE 1 X#define FALSE 0 X X Xstatic int XStar(s, p) X register char *s; X register char *p; X{ X while (wildmat(s, p) == FALSE) X if (*++s == '\0') X return FALSE; X return TRUE; X} X X Xint Xwildmat(s, p) X register char *s; X register char *p; X{ X register int last; X register int matched; X register int reverse; X X for ( ; *p; s++, p++) X switch (*p) { X case '\\': X /* Literal match with following character. */ X p++; X /* FALLTHROUGH */ X default: X if (*s != *p) X return FALSE; X continue; X case '?': X /* Match anything. */ X if (*s == '\0') X return FALSE; X continue; X case '*': X /* Trailing star matches everything. */ X return *++p ? Star(s, p) : TRUE; X case '[': X /* [^....] means inverse character class. */ X if (reverse = p[1] == '^') X p++; X for (last = 0400, matched = FALSE; *++p && *p != ']'; last = *p) X /* This next line requires a good C compiler. */ X if (*p == '-' ? *s <= *++p && *s >= last : *s == *p) X matched = TRUE; X if (matched == reverse) X return FALSE; X continue; X } X X return *s == '\0'; X} X X X#ifdef TEST X#include X X/* Yes, we use gets not fgets. Sue me. */ Xextern char *gets(); X X Xmain() X{ X char pattern[80]; X char text[80]; X X printf("Wildmat tester. Enter pattern, then strings to test.\n"); X printf("A blank line gets prompts for a new pattern; a blank pattern\n"); X printf("exits the program.\n\n"); X X for ( ; ; ) { X printf("Enter pattern: "); X if (gets(pattern) == NULL) X break; X for ( ; ; ) { X printf("Enter text: "); X if (gets(text) == NULL) X exit(0); X if (text[0] == '\0') X /* Blank line; go back and get a new pattern. */ X break; X printf(" %s\n", wildmat(text, pattern) ? "YES" : "NO"); X } X } X X exit(0); X /* NOTREACHED */ X} X#endif /* TEST */ END_OF_FILE if test 2123 -ne `wc -c <'wildmat.c'`; then echo shar: \"'wildmat.c'\" unpacked with wrong size! fi # end of 'wildmat.c' fi echo shar: End of shell archive. exit 0 -- Please send comp.sources.unix-related mail to rsalz@uunet.uu.net. Use a domain-based address or give alternate paths, or you may lose out.