Annotation of quake2/linux/glob.c, revision 1.1.1.2

1.1.1.2 ! root        1: /*
        !             2: Copyright (C) 1997-2001 Id Software, Inc.
        !             3: 
        !             4: This program is free software; you can redistribute it and/or
        !             5: modify it under the terms of the GNU General Public License
        !             6: as published by the Free Software Foundation; either version 2
        !             7: of the License, or (at your option) any later version.
        !             8: 
        !             9: This program is distributed in the hope that it will be useful,
        !            10: but WITHOUT ANY WARRANTY; without even the implied warranty of
        !            11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
        !            12: 
        !            13: See the GNU General Public License for more details.
        !            14: 
        !            15: You should have received a copy of the GNU General Public License
        !            16: along with this program; if not, write to the Free Software
        !            17: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
        !            18: 
        !            19: */
1.1       root       20: 
                     21: #include <stdio.h>
                     22: #include "../linux/glob.h"
                     23: 
                     24: /* Like glob_match, but match PATTERN against any final segment of TEXT.  */
                     25: static int glob_match_after_star(char *pattern, char *text)
                     26: {
                     27:        register char *p = pattern, *t = text;
                     28:        register char c, c1;
                     29: 
                     30:        while ((c = *p++) == '?' || c == '*')
                     31:                if (c == '?' && *t++ == '\0')
                     32:                        return 0;
                     33: 
                     34:        if (c == '\0')
                     35:                return 1;
                     36: 
                     37:        if (c == '\\')
                     38:                c1 = *p;
                     39:        else
                     40:                c1 = c;
                     41: 
                     42:        while (1) {
                     43:                if ((c == '[' || *t == c1) && glob_match(p - 1, t))
                     44:                        return 1;
                     45:                if (*t++ == '\0')
                     46:                        return 0;
                     47:        }
                     48: }
                     49: 
                     50: /* Return nonzero if PATTERN has any special globbing chars in it.  */
                     51: static int glob_pattern_p(char *pattern)
                     52: {
                     53:        register char *p = pattern;
                     54:        register char c;
                     55:        int open = 0;
                     56: 
                     57:        while ((c = *p++) != '\0')
                     58:                switch (c) {
                     59:                case '?':
                     60:                case '*':
                     61:                        return 1;
                     62: 
                     63:                case '[':               /* Only accept an open brace if there is a close */
                     64:                        open++;         /* brace to match it.  Bracket expressions must be */
                     65:                        continue;       /* complete, according to Posix.2 */
                     66:                case ']':
                     67:                        if (open)
                     68:                                return 1;
                     69:                        continue;
                     70: 
                     71:                case '\\':
                     72:                        if (*p++ == '\0')
                     73:                                return 0;
                     74:                }
                     75: 
                     76:        return 0;
                     77: }
                     78: 
                     79: /* Match the pattern PATTERN against the string TEXT;
                     80:    return 1 if it matches, 0 otherwise.
                     81: 
                     82:    A match means the entire string TEXT is used up in matching.
                     83: 
                     84:    In the pattern string, `*' matches any sequence of characters,
                     85:    `?' matches any character, [SET] matches any character in the specified set,
                     86:    [!SET] matches any character not in the specified set.
                     87: 
                     88:    A set is composed of characters or ranges; a range looks like
                     89:    character hyphen character (as in 0-9 or A-Z).
                     90:    [0-9a-zA-Z_] is the set of characters allowed in C identifiers.
                     91:    Any other character in the pattern must be matched exactly.
                     92: 
                     93:    To suppress the special syntactic significance of any of `[]*?!-\',
                     94:    and match the character exactly, precede it with a `\'.
                     95: */
                     96: 
                     97: int glob_match(char *pattern, char *text)
                     98: {
                     99:        register char *p = pattern, *t = text;
                    100:        register char c;
                    101: 
                    102:        while ((c = *p++) != '\0')
                    103:                switch (c) {
                    104:                case '?':
                    105:                        if (*t == '\0')
                    106:                                return 0;
                    107:                        else
                    108:                                ++t;
                    109:                        break;
                    110: 
                    111:                case '\\':
                    112:                        if (*p++ != *t++)
                    113:                                return 0;
                    114:                        break;
                    115: 
                    116:                case '*':
                    117:                        return glob_match_after_star(p, t);
                    118: 
                    119:                case '[':
                    120:                        {
                    121:                                register char c1 = *t++;
                    122:                                int invert;
                    123: 
                    124:                                if (!c1)
                    125:                                        return (0);
                    126: 
                    127:                                invert = ((*p == '!') || (*p == '^'));
                    128:                                if (invert)
                    129:                                        p++;
                    130: 
                    131:                                c = *p++;
                    132:                                while (1) {
                    133:                                        register char cstart = c, cend = c;
                    134: 
                    135:                                        if (c == '\\') {
                    136:                                                cstart = *p++;
                    137:                                                cend = cstart;
                    138:                                        }
                    139:                                        if (c == '\0')
                    140:                                                return 0;
                    141: 
                    142:                                        c = *p++;
                    143:                                        if (c == '-' && *p != ']') {
                    144:                                                cend = *p++;
                    145:                                                if (cend == '\\')
                    146:                                                        cend = *p++;
                    147:                                                if (cend == '\0')
                    148:                                                        return 0;
                    149:                                                c = *p++;
                    150:                                        }
                    151:                                        if (c1 >= cstart && c1 <= cend)
                    152:                                                goto match;
                    153:                                        if (c == ']')
                    154:                                                break;
                    155:                                }
                    156:                                if (!invert)
                    157:                                        return 0;
                    158:                                break;
                    159: 
                    160:                          match:
                    161:                                /* Skip the rest of the [...] construct that already matched.  */
                    162:                                while (c != ']') {
                    163:                                        if (c == '\0')
                    164:                                                return 0;
                    165:                                        c = *p++;
                    166:                                        if (c == '\0')
                    167:                                                return 0;
                    168:                                        else if (c == '\\')
                    169:                                                ++p;
                    170:                                }
                    171:                                if (invert)
                    172:                                        return 0;
                    173:                                break;
                    174:                        }
                    175: 
                    176:                default:
                    177:                        if (c != *t++)
                    178:                                return 0;
                    179:                }
                    180: 
                    181:        return *t == '\0';
                    182: }
                    183: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.