~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/3rdparty/fnmatch/fnmatch.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * This file is included as part of the Grid Engine source
 
4
 * to provide the fnmatch C library function for the NEC SX
 
5
 * platform which does not have this function.
 
6
 */
 
7
 
 
8
/*      $OpenBSD: fnmatch.c,v 1.7 2000/03/23 19:13:51 millert Exp $     */
 
9
 
 
10
/*
 
11
 * Copyright (c) 1989, 1993, 1994
 
12
 *      The Regents of the University of California.  All rights reserved.
 
13
 *
 
14
 * This code is derived from software contributed to Berkeley by
 
15
 * Guido van Rossum.
 
16
 *
 
17
 * Redistribution and use in source and binary forms, with or without
 
18
 * modification, are permitted provided that the following conditions
 
19
 * are met:
 
20
 * 1. Redistributions of source code must retain the above copyright
 
21
 *    notice, this list of conditions and the following disclaimer.
 
22
 * 2. Redistributions in binary form must reproduce the above copyright
 
23
 *    notice, this list of conditions and the following disclaimer in the
 
24
 *    documentation and/or other materials provided with the distribution.
 
25
 * 3. All advertising materials mentioning features or use of this software
 
26
 *    must display the following acknowledgement:
 
27
 *      This product includes software developed by the University of
 
28
 *      California, Berkeley and its contributors.
 
29
 * 4. Neither the name of the University nor the names of its contributors
 
30
 *    may be used to endorse or promote products derived from this software
 
31
 *    without specific prior written permission.
 
32
 *
 
33
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 
34
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
35
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
36
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
37
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
38
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
39
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
40
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
41
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
42
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
43
 * SUCH DAMAGE.
 
44
 */
 
45
 
 
46
#if defined(LIBC_SCCS) && !defined(lint)
 
47
#if 0
 
48
(#)fnmatch.c   8.2 (Berkeley) 4/16/94";
 
49
#else
 
50
static char rcsid[] = "$OpenBSD: fnmatch.c,v 1.7 2000/03/23 19:13:51 millert Exp $";
 
51
#endif
 
52
#endif /* LIBC_SCCS and not lint */
 
53
 
 
54
/*
 
55
 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
 
56
 * Compares a filename or pathname to a pattern.
 
57
 */
 
58
 
 
59
#include <ctype.h>
 
60
#include <stdio.h>
 
61
#include <string.h>
 
62
#include "fnmatch.h"
 
63
 
 
64
#define EOS     '\0'
 
65
 
 
66
#define RANGE_MATCH     1
 
67
#define RANGE_NOMATCH   0
 
68
#define RANGE_ERROR     (-1)
 
69
 
 
70
static int rangematch (const char *, char, int, char **);
 
71
 
 
72
int
 
73
fnmatch(pattern, string, flags)
 
74
        const char *pattern, *string;
 
75
        int flags;
 
76
{
 
77
        const char *stringstart;
 
78
        char *newp;
 
79
        char c, test;
 
80
 
 
81
        for (stringstart = string;;)
 
82
                switch (c = *pattern++) {
 
83
                case EOS:
 
84
                        if ((flags & FNM_LEADING_DIR) && *string == '/')
 
85
                                return (0);
 
86
                        return (*string == EOS ? 0 : FNM_NOMATCH);
 
87
                case '?':
 
88
                        if (*string == EOS)
 
89
                                return (FNM_NOMATCH);
 
90
                        if (*string == '/' && (flags & FNM_PATHNAME))
 
91
                                return (FNM_NOMATCH);
 
92
                        if (*string == '.' && (flags & FNM_PERIOD) &&
 
93
                            (string == stringstart ||
 
94
                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
 
95
                                return (FNM_NOMATCH);
 
96
                        ++string;
 
97
                        break;
 
98
                case '*':
 
99
                        c = *pattern;
 
100
                        /* Collapse multiple stars. */
 
101
                        while (c == '*')
 
102
                                c = *++pattern;
 
103
 
 
104
                        if (*string == '.' && (flags & FNM_PERIOD) &&
 
105
                            (string == stringstart ||
 
106
                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
 
107
                                return (FNM_NOMATCH);
 
108
 
 
109
                        /* Optimize for pattern with * at end or before /. */
 
110
                        if (c == EOS) {
 
111
                                if (flags & FNM_PATHNAME)
 
112
                                        return ((flags & FNM_LEADING_DIR) ||
 
113
                                            strchr(string, '/') == NULL ?
 
114
                                            0 : FNM_NOMATCH);
 
115
                                else
 
116
                                        return (0);
 
117
                        } else if (c == '/' && (flags & FNM_PATHNAME)) {
 
118
                                if ((string = strchr(string, '/')) == NULL)
 
119
                                        return (FNM_NOMATCH);
 
120
                                break;
 
121
                        }
 
122
 
 
123
                        /* General case, use recursion. */
 
124
                        while ((test = *string) != EOS) {
 
125
                                if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
 
126
                                        return (0);
 
127
                                if (test == '/' && (flags & FNM_PATHNAME))
 
128
                                        break;
 
129
                                ++string;
 
130
                        }
 
131
                        return (FNM_NOMATCH);
 
132
                case '[':
 
133
                        if (*string == EOS)
 
134
                                return (FNM_NOMATCH);
 
135
                        if (*string == '/' && (flags & FNM_PATHNAME))
 
136
                                return (FNM_NOMATCH);
 
137
                        if (*string == '.' && (flags & FNM_PERIOD) &&
 
138
                            (string == stringstart ||
 
139
                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
 
140
                                return (FNM_NOMATCH);
 
141
 
 
142
                        switch (rangematch(pattern, *string, flags, &newp)) {
 
143
                        case RANGE_ERROR:
 
144
                                /* not a good range, treat as normal text */
 
145
                                goto normal;
 
146
                        case RANGE_MATCH:
 
147
                                pattern = newp;
 
148
                                break;
 
149
                        case RANGE_NOMATCH:
 
150
                                return (FNM_NOMATCH);
 
151
                        }
 
152
                        ++string;
 
153
                        break;
 
154
                case '\\':
 
155
                        if (!(flags & FNM_NOESCAPE)) {
 
156
                                if ((c = *pattern++) == EOS) {
 
157
                                        c = '\\';
 
158
                                        --pattern;
 
159
                                }
 
160
                        }
 
161
                        /* FALLTHROUGH */
 
162
                default:
 
163
                normal:
 
164
                        if (c != *string && !((flags & FNM_CASEFOLD) &&
 
165
                                 (tolower((unsigned char)c) ==
 
166
                                 tolower((unsigned char)*string))))
 
167
                                return (FNM_NOMATCH);
 
168
                        ++string;
 
169
                        break;
 
170
                }
 
171
        /* NOTREACHED */
 
172
}
 
173
 
 
174
static int
 
175
#ifdef __STDC__
 
176
rangematch(const char *pattern, char test, int flags, char **newp)
 
177
#else
 
178
rangematch(pattern, test, flags, newp)
 
179
        char *pattern;
 
180
        char test;
 
181
        int flags;
 
182
        char **newp;
 
183
#endif
 
184
{
 
185
        int negate, ok;
 
186
        char c, c2;
 
187
 
 
188
        /*
 
189
         * A bracket expression starting with an unquoted circumflex
 
190
         * character produces unspecified results (IEEE 1003.2-1992,
 
191
         * 3.13.2).  This implementation treats it like '!', for
 
192
         * consistency with the regular expression syntax.
 
193
ngai.kaleida.com)
 
194
         */
 
195
        if ((negate = (*pattern == '!' || *pattern == '^')))
 
196
                ++pattern;
 
197
 
 
198
        if (flags & FNM_CASEFOLD)
 
199
                test = tolower((unsigned char)test);
 
200
 
 
201
        /*
 
202
         * A right bracket shall lose its special meaning and represent
 
203
         * itself in a bracket expression if it occurs first in the list.
 
204
         * -- POSIX.2 2.8.3.2
 
205
         */
 
206
        ok = 0;
 
207
        c = *pattern++;
 
208
        do {
 
209
                if (c == '\\' && !(flags & FNM_NOESCAPE))
 
210
                        c = *pattern++;
 
211
                if (c == EOS)
 
212
                        return (RANGE_ERROR);
 
213
                if (c == '/' && (flags & FNM_PATHNAME))
 
214
                        return (RANGE_NOMATCH);
 
215
                if ((flags & FNM_CASEFOLD))
 
216
                        c = tolower((unsigned char)c);
 
217
                if (*pattern == '-'
 
218
                    && (c2 = *(pattern+1)) != EOS && c2 != ']') {
 
219
                        pattern += 2;
 
220
                        if (c2 == '\\' && !(flags & FNM_NOESCAPE))
 
221
                                c2 = *pattern++;
 
222
                        if (c2 == EOS)
 
223
                                return (RANGE_ERROR);
 
224
                        if (flags & FNM_CASEFOLD)
 
225
                                c2 = tolower((unsigned char)c2);
 
226
                        if (c <= test && test <= c2)
 
227
                                ok = 1;
 
228
                } else if (c == test)
 
229
                        ok = 1;
 
230
        } while ((c = *pattern++) != ']');
 
231
 
 
232
        *newp = (char *)pattern;
 
233
        return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
 
234
}