|
|
1.1 ! root 1: /* $Source: /src386/usr/bin/pax/wildmat.c,v $ ! 2: * ! 3: * $Revision: 1.1 $ ! 4: * ! 5: * wildmat.c - simple regular expression pattern matching routines ! 6: * ! 7: * DESCRIPTION ! 8: * ! 9: * These routines provide simple UNIX style regular expression matching. ! 10: * They were originally written by Rich Salz, the comp.sources.unix ! 11: * moderator for inclusion in some of his software. These routines ! 12: * were released into the public domain and used by John Gilmore in ! 13: * USTAR. ! 14: * ! 15: * AUTHORS ! 16: * ! 17: * Mark H. Colburn, NAPS International ([email protected]) ! 18: * John Gilmore (gnu@hoptoad) ! 19: * Rich Salz ([email protected]) ! 20: * ! 21: * ! 22: * Sponsored by The USENIX Association for public distribution. ! 23: * ! 24: * Copyright (c) 1989 Mark H. Colburn. ! 25: * All rights reserved. ! 26: * ! 27: * Redistribution and use in source and binary forms are permitted ! 28: * provided that the above copyright notice is duplicated in all such ! 29: * forms and that any documentation, advertising materials, and other ! 30: * materials related to such distribution and use acknowledge that the ! 31: * software was developed * by Mark H. Colburn and sponsored by The ! 32: * USENIX Association. ! 33: * ! 34: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 35: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 36: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 37: * ! 38: * $Log: wildmat.c,v $ ! 39: * Revision 1.1 92/08/28 08:03:16 bin ! 40: * Initial revision ! 41: * ! 42: * Revision 1.1 89/02/14 16:48:52 jep ! 43: * Initial revision ! 44: * ! 45: * Revision 1.1 88/12/23 18:02:41 mark ! 46: * Initial revision ! 47: * ! 48: */ ! 49: ! 50: #ifndef lint ! 51: static char *ident = "$Id: wildmat.c,v 1.1 92/08/28 08:03:16 bin Exp Locker: bin $"; ! 52: static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n"; ! 53: #endif /* ! lint */ ! 54: ! 55: ! 56: /* Headers */ ! 57: ! 58: #include "pax.h" ! 59: ! 60: ! 61: /* Function Prototypes */ ! 62: ! 63: #if __STDC__ ! 64: static int star(char *, char *); ! 65: #else /* !__STDC__ */ ! 66: static int star(); ! 67: #endif /* __STDC__ */ ! 68: ! 69: ! 70: /* ! 71: * star - handle trailing * in a regular expression ! 72: * ! 73: * DESCRIPTION ! 74: * ! 75: * Star is used to match filename expansions containing a trailing ! 76: * asterisk ('*'). Star call wildmat() to determine if the substring ! 77: * passed to it is matches the regular expression. ! 78: * ! 79: * PARAMETERS ! 80: * ! 81: * char *pattern - The regular expression which we are supposed to ! 82: * match to. ! 83: * char *source - The source string which is to be compared to the ! 84: * regular expression pattern. ! 85: * ! 86: * RETURNS ! 87: * ! 88: * Returns non-zero if the entire source string is completely matched by ! 89: * the regular expression pattern, returns 0 otherwise. This is used to ! 90: * see if *'s in a pattern matched the entire source string. ! 91: * ! 92: */ ! 93: ! 94: #if __STDC__ ! 95: ! 96: static int star(char *source, char *pattern) ! 97: ! 98: #else ! 99: ! 100: static int star(source, pattern) ! 101: char *source; /* source operand */ ! 102: char *pattern; /* regular expression to match */ ! 103: ! 104: #endif ! 105: { ! 106: while (!wildmat(pattern, source)) { ! 107: if (*++source == '\0') { ! 108: return (0); ! 109: } ! 110: } ! 111: return (1); ! 112: } ! 113: ! 114: ! 115: /* ! 116: * wildmat - match a regular expression ! 117: * ! 118: * DESCRIPTION ! 119: * ! 120: * Wildmat attempts to match the string pointed to by source to the ! 121: * regular expression pointed to by pattern. The subset of regular ! 122: * expression syntax which is supported is defined by POSIX P1003.2 ! 123: * FILENAME EXPANSION rules. ! 124: * ! 125: * PARAMETERS ! 126: * ! 127: * char *pattern - The regular expression which we are supposed to ! 128: * match to. ! 129: * char *source - The source string which is to be compared to the ! 130: * regular expression pattern. ! 131: * ! 132: * RETURNS ! 133: * ! 134: * Returns non-zero if the source string matches the regular expression ! 135: * pattern specified, returns 0 otherwise. ! 136: * ! 137: */ ! 138: ! 139: #if __STDC__ ! 140: ! 141: int wildmat(char *pattern, char *source) ! 142: ! 143: #else ! 144: ! 145: int wildmat(pattern, source) ! 146: char *pattern; /* regular expression to match */ ! 147: char *source; /* source operand */ ! 148: ! 149: #endif ! 150: { ! 151: int last; /* last character matched */ ! 152: int matched; /* !0 if a match occurred */ ! 153: int reverse; /* !0 if sense of match is reversed */ ! 154: ! 155: for (; *pattern; source++, pattern++) { ! 156: switch (*pattern) { ! 157: case '\\': ! 158: /* Literal match with following character */ ! 159: pattern++; ! 160: /* FALLTHRU */ ! 161: default: ! 162: if (*source != *pattern) { ! 163: return (0); ! 164: } ! 165: continue; ! 166: case '?': ! 167: /* Match anything. */ ! 168: if (*source == '\0') { ! 169: return (0); ! 170: } ! 171: continue; ! 172: case '*': ! 173: /* Trailing star matches everything. */ ! 174: return (*++pattern ? star(source, pattern) : 1); ! 175: case '[': ! 176: /* [^....] means inverse character class. */ ! 177: if (reverse = pattern[1] == '^') { ! 178: pattern++; ! 179: } ! 180: for (last = 0400, matched = 0; ! 181: *++pattern && *pattern != ']'; last = *pattern) { ! 182: /* This next line requires a good C compiler. */ ! 183: if (*pattern == '-' ! 184: ? *source <= *++pattern && *source >= last ! 185: : *source == *pattern) { ! 186: matched = 1; ! 187: } ! 188: } ! 189: if (matched == reverse) { ! 190: return (0); ! 191: } ! 192: continue; ! 193: } ! 194: } ! 195: ! 196: /* ! 197: * For "tar" use, matches that end at a slash also work. --hoptoad!gnu ! 198: */ ! 199: return (*source == '\0' || *source == '/'); ! 200: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.