[runtime] Fix "make distcheck"
[mono-project.git] / mono / io-layer / wapi_glob.c
blob31c7f6d94111971a913552abda8b601c234446e3
1 /* $OpenBSD: glob.c,v 1.26 2005/11/28 17:50:12 deraadt Exp $ */
2 /*
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Guido van Rossum.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * _wapi_glob(3) -- a subset of the one defined in POSIX 1003.2.
37 * Optional extra services, controlled by flags not defined by POSIX:
39 * GLOB_MAGCHAR:
40 * Set in gl_flags if pattern contained a globbing character.
42 #include <sys/types.h>
43 #include <sys/stat.h>
45 #include <glib.h>
46 #include <ctype.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
53 #include "wapi_glob.h"
55 #define EOS '\0'
56 #define NOT '!'
57 #define QUESTION '?'
58 #define QUOTE '\\'
59 #define STAR '*'
61 #ifndef DEBUG
63 #define M_QUOTE 0x8000
64 #define M_PROTECT 0x4000
65 #define M_MASK 0xffff
66 #define M_ASCII 0x00ff
68 typedef unsigned short Char;
70 #else
72 #define M_QUOTE 0x80
73 #define M_PROTECT 0x40
74 #define M_MASK 0xff
75 #define M_ASCII 0x7f
77 typedef char Char;
79 #endif
82 #define CHAR(c) ((gchar)((c)&M_ASCII))
83 #define META(c) ((gchar)((c)|M_QUOTE))
84 #define M_ALL META('*')
85 #define M_ONE META('?')
86 #define ismeta(c) (((c)&M_QUOTE) != 0)
89 static int g_Ctoc(const gchar *, char *, unsigned int);
90 static int glob0(GDir *dir, const gchar *, wapi_glob_t *, gboolean,
91 gboolean);
92 static int glob1(GDir *dir, gchar *, gchar *, wapi_glob_t *, size_t *,
93 gboolean, gboolean);
94 static int glob3(GDir *dir, gchar *, gchar *, wapi_glob_t *, size_t *,
95 gboolean, gboolean);
96 static int globextend(const gchar *, wapi_glob_t *, size_t *);
97 static int match(const gchar *, gchar *, gchar *, gboolean);
98 #ifdef DEBUG_ENABLED
99 static void qprintf(const char *, Char *);
100 #endif
103 _wapi_glob(GDir *dir, const char *pattern, int flags, wapi_glob_t *pglob)
105 const unsigned char *patnext;
106 int c;
107 gchar *bufnext, *bufend, patbuf[PATH_MAX];
109 patnext = (unsigned char *) pattern;
110 if (!(flags & WAPI_GLOB_APPEND)) {
111 pglob->gl_pathc = 0;
112 pglob->gl_pathv = NULL;
113 pglob->gl_offs = 0;
115 pglob->gl_flags = flags & ~WAPI_GLOB_MAGCHAR;
117 bufnext = patbuf;
118 bufend = bufnext + PATH_MAX - 1;
120 /* Protect the quoted characters. */
121 while (bufnext < bufend && (c = *patnext++) != EOS)
122 if (c == QUOTE) {
123 if ((c = *patnext++) == EOS) {
124 c = QUOTE;
125 --patnext;
127 *bufnext++ = c | M_PROTECT;
128 } else
129 *bufnext++ = c;
131 *bufnext = EOS;
133 return glob0(dir, patbuf, pglob, flags & WAPI_GLOB_IGNORECASE,
134 flags & WAPI_GLOB_UNIQUE);
138 * The main glob() routine: compiles the pattern (optionally processing
139 * quotes), calls glob1() to do the real pattern matching, and finally
140 * sorts the list (unless unsorted operation is requested). Returns 0
141 * if things went well, nonzero if errors occurred. It is not an error
142 * to find no matches.
144 static int
145 glob0(GDir *dir, const gchar *pattern, wapi_glob_t *pglob, gboolean ignorecase,
146 gboolean unique)
148 const gchar *qpatnext;
149 int c, err, oldpathc;
150 gchar *bufnext, patbuf[PATH_MAX];
151 size_t limit = 0;
153 qpatnext = pattern;
154 oldpathc = pglob->gl_pathc;
155 bufnext = patbuf;
157 /* We don't need to check for buffer overflow any more. */
158 while ((c = *qpatnext++) != EOS) {
159 switch (c) {
160 case QUESTION:
161 pglob->gl_flags |= WAPI_GLOB_MAGCHAR;
162 *bufnext++ = M_ONE;
163 break;
164 case STAR:
165 pglob->gl_flags |= WAPI_GLOB_MAGCHAR;
166 /* collapse adjacent stars to one,
167 * to avoid exponential behavior
169 if (bufnext == patbuf || bufnext[-1] != M_ALL)
170 *bufnext++ = M_ALL;
171 break;
172 default:
173 *bufnext++ = CHAR(c);
174 break;
177 *bufnext = EOS;
178 #ifdef DEBUG_ENABLED
179 qprintf("glob0:", patbuf);
180 #endif
182 if ((err = glob1(dir, patbuf, patbuf+PATH_MAX-1, pglob, &limit,
183 ignorecase, unique)) != 0)
184 return(err);
186 if (pglob->gl_pathc == oldpathc) {
187 return(WAPI_GLOB_NOMATCH);
190 return(0);
193 static int
194 glob1(GDir *dir, gchar *pattern, gchar *pattern_last, wapi_glob_t *pglob,
195 size_t *limitp, gboolean ignorecase, gboolean unique)
197 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
198 if (*pattern == EOS)
199 return(0);
200 return(glob3(dir, pattern, pattern_last, pglob, limitp, ignorecase,
201 unique));
204 static gboolean contains (wapi_glob_t *pglob, const gchar *name)
206 int i;
207 char **pp;
209 if (pglob->gl_pathv != NULL) {
210 pp = pglob->gl_pathv + pglob->gl_offs;
211 for (i = pglob->gl_pathc; i--; ++pp) {
212 if (*pp) {
213 if (!strcmp (*pp, name)) {
214 return(TRUE);
220 return(FALSE);
223 static int
224 glob3(GDir *dir, gchar *pattern, gchar *pattern_last, wapi_glob_t *pglob,
225 size_t *limitp, gboolean ignorecase, gboolean unique)
227 const gchar *name;
229 /* Search directory for matching names. */
230 while ((name = g_dir_read_name(dir))) {
231 if (!match(name, pattern, pattern + strlen (pattern),
232 ignorecase)) {
233 continue;
235 if (!unique ||
236 !contains (pglob, name)) {
237 globextend (name, pglob, limitp);
241 return(0);
246 * Extend the gl_pathv member of a wapi_glob_t structure to accommodate a new item,
247 * add the new item, and update gl_pathc.
249 * This assumes the BSD realloc, which only copies the block when its size
250 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
251 * behavior.
253 * Return 0 if new item added, error code if memory couldn't be allocated.
255 * Invariant of the wapi_glob_t structure:
256 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
257 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
259 static int
260 globextend(const gchar *path, wapi_glob_t *pglob, size_t *limitp)
262 char **pathv;
263 int i;
264 unsigned int newsize, len;
265 char *copy;
266 const gchar *p;
268 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
269 pathv = pglob->gl_pathv ? realloc((char *)pglob->gl_pathv, newsize) :
270 malloc(newsize);
271 if (pathv == NULL) {
272 if (pglob->gl_pathv) {
273 free(pglob->gl_pathv);
274 pglob->gl_pathv = NULL;
276 return(WAPI_GLOB_NOSPACE);
279 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
280 /* first time around -- clear initial gl_offs items */
281 pathv += pglob->gl_offs;
282 for (i = pglob->gl_offs; --i >= 0; )
283 *--pathv = NULL;
285 pglob->gl_pathv = pathv;
287 for (p = path; *p++;)
289 len = (size_t)(p - path);
290 *limitp += len;
291 if ((copy = malloc(len)) != NULL) {
292 if (g_Ctoc(path, copy, len)) {
293 free(copy);
294 return(WAPI_GLOB_NOSPACE);
296 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
298 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
300 #if 0
301 /* Broken on opensuse 11 */
302 if ((pglob->gl_flags & WAPI_GLOB_LIMIT) &&
303 newsize + *limitp >= ARG_MAX) {
304 errno = 0;
305 return(WAPI_GLOB_NOSPACE);
307 #endif
309 return(copy == NULL ? WAPI_GLOB_NOSPACE : 0);
314 * pattern matching function for filenames. Each occurrence of the *
315 * pattern causes a recursion level.
317 static int
318 match(const gchar *name, gchar *pat, gchar *patend, gboolean ignorecase)
320 gchar c;
322 while (pat < patend) {
323 c = *pat++;
324 switch (c & M_MASK) {
325 case M_ALL:
326 if (pat == patend)
327 return(1);
328 do {
329 if (match(name, pat, patend, ignorecase))
330 return(1);
331 } while (*name++ != EOS);
332 return(0);
333 case M_ONE:
334 if (*name++ == EOS)
335 return(0);
336 break;
337 default:
338 if (ignorecase) {
339 if (g_ascii_tolower (*name++) != g_ascii_tolower (c))
340 return(0);
341 } else {
342 if (*name++ != c)
343 return(0);
346 break;
349 return(*name == EOS);
352 /* Free allocated data belonging to a wapi_glob_t structure. */
353 void
354 _wapi_globfree(wapi_glob_t *pglob)
356 int i;
357 char **pp;
359 if (pglob->gl_pathv != NULL) {
360 pp = pglob->gl_pathv + pglob->gl_offs;
361 for (i = pglob->gl_pathc; i--; ++pp)
362 if (*pp)
363 free(*pp);
364 free(pglob->gl_pathv);
365 pglob->gl_pathv = NULL;
369 static int
370 g_Ctoc(const gchar *str, char *buf, unsigned int len)
373 while (len--) {
374 if ((*buf++ = *str++) == EOS)
375 return (0);
377 return (1);
380 #ifdef DEBUG_ENABLED
381 static void
382 qprintf(const char *str, Char *s)
384 Char *p;
386 (void)printf("%s:\n", str);
387 for (p = s; *p; p++)
388 (void)printf("%c", CHAR(*p));
389 (void)printf("\n");
390 for (p = s; *p; p++)
391 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
392 (void)printf("\n");
393 for (p = s; *p; p++)
394 (void)printf("%c", ismeta(*p) ? '_' : ' ');
395 (void)printf("\n");
397 #endif