Xref: utzoo comp.sources.wanted:7461 comp.unix.questions:13711 alt.sources:604 Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!shelby!apple!well!pokey From: pokey@well.UUCP (Jef Poskanzer) Newsgroups: comp.sources.wanted,comp.unix.questions,alt.sources Subject: Re: pattern matching Message-ID: <11733@well.UUCP> Date: 18 May 89 18:55:25 GMT References: <2414@cveg.uucp> Reply-To: Jef Poskanzer Organization: Paratheo-Anametamystikhood Of Eris Esoteric, Ada Lovelace Cabal Lines: 71 In the referenced message, mhb@hcx.uucp (MARK H BOTNER) wrote: }Can anybody help me with pattern matching? }P.S. if you have any answer or a subroutine for me, please mail it to me. }My address is: } mhb@cseg.uucp That address is useless, so I'm posting this. This is free software, no copyright or trade secret restrictions whatsoever. --- Jef Jef Poskanzer jef@helios.ee.lbl.gov ...well!pokey /* ** Simple pattern matcher. Does ?, *, and [], much like various shells. ** ** by Jef Poskanzer */ amatch( s, p ) register char *s, *p; { for ( ; *p != '\0'; p++, s++ ) { if ( *p == '*' ) { if ( *++p == '\0' ) return 1; for ( ; *s != '\0'; s++ ) if ( amatch( s, p ) ) return 1; return 0; } if ( *s == '\0' ) return 0; if ( *p == '?' ) continue; if ( *p == '[' ) { int negcc = 0, ccmatch = 0; char prevc; if ( *++p == '!' ) { negcc = 1; p++; } for ( ; *p != ']'; p++ ) { if ( *p == '\0' ) { fprintf( stderr, "amatch: missing ']'\n" ); return 0; } if ( *p == '-' ) { if ( prevc <= *s && *++p >= *s ) ccmatch = 1; } else if ( *p == *s ) ccmatch = 1; prevc = *p; } if ( ( ccmatch && ! negcc ) || ( negcc && ! ccmatch ) ) continue; return 0; } if ( *p != *s ) return 0; } return *s == '\0'; }