Pass the --clr-memory-model flag on the command line instead of MONO_DEBUG so its...
[mono-project.git] / mono / metadata / w32file-unix-glob.c
blobd07a7dc8297f2230a359264d6e3174cee03d4e72
1 /* $OpenBSD: glob.c,v 1.26 2005/11/28 17:50:12 deraadt Exp $ */
2 /**
3 * \file
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Guido van Rossum.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
36 * mono_w32file_unix_glob(3) -- a subset of the one defined in POSIX 1003.2.
38 * Optional extra services, controlled by flags not defined by POSIX:
40 * GLOB_MAGCHAR:
41 * Set in gl_flags if pattern contained a globbing character.
43 #include <sys/types.h>
44 #include <sys/stat.h>
46 #include <glib.h>
47 #include <ctype.h>
48 #include <errno.h>
49 #include <mono/utils/mono-errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
55 #include "w32file-unix-glob.h"
57 #define EOS '\0'
58 #define NOT '!'
59 #define QUESTION '?'
60 #define QUOTE '\\'
61 #define STAR '*'
63 #ifndef DEBUG
65 #define M_QUOTE 0x8000
66 #define M_PROTECT 0x4000
67 #define M_MASK 0xffff
68 #define M_ASCII 0x00ff
70 typedef unsigned short Char;
72 #else
74 #define M_QUOTE 0x80
75 #define M_PROTECT 0x40
76 #define M_MASK 0xff
77 #define M_ASCII 0x7f
79 typedef char Char;
81 #endif
84 #define CHAR(c) ((gchar)((c)&M_ASCII))
85 #define META(c) ((gchar)((c)|M_QUOTE))
86 #define M_ALL META('*')
87 #define M_ONE META('?')
88 #define ismeta(c) (((c)&M_QUOTE) != 0)
91 static int
92 g_Ctoc(const gchar *, char *, unsigned int);
94 static int
95 glob0(GDir *dir, const gchar *, mono_w32file_unix_glob_t *, gboolean, gboolean);
96 static int
97 glob1(GDir *dir, gchar *, gchar *, mono_w32file_unix_glob_t *, size_t *, gboolean, gboolean);
99 static int
100 glob3(GDir *dir, gchar *, gchar *, mono_w32file_unix_glob_t *, size_t *, gboolean, gboolean);
102 static int
103 globextend(const gchar *, mono_w32file_unix_glob_t *, size_t *);
105 static int
106 match(const gchar *, gchar *, gchar *, gboolean);
108 #ifdef DEBUG_ENABLED
109 static void qprintf(const char *, Char *);
110 #endif
113 mono_w32file_unix_glob(GDir *dir, const char *pattern, int flags, mono_w32file_unix_glob_t *pglob)
115 const unsigned char *patnext;
116 int c;
117 gchar *bufnext, *bufend, patbuf[PATH_MAX];
119 patnext = (unsigned char *) pattern;
120 if (!(flags & W32FILE_UNIX_GLOB_APPEND)) {
121 pglob->gl_pathc = 0;
122 pglob->gl_pathv = NULL;
123 pglob->gl_offs = 0;
125 pglob->gl_flags = flags & ~W32FILE_UNIX_GLOB_MAGCHAR;
127 bufnext = patbuf;
128 bufend = bufnext + PATH_MAX - 1;
130 /* Protect the quoted characters. */
131 while (bufnext < bufend && (c = *patnext++) != EOS)
132 if (c == QUOTE) {
133 if ((c = *patnext++) == EOS) {
134 c = QUOTE;
135 --patnext;
137 *bufnext++ = c | M_PROTECT;
138 } else
139 *bufnext++ = c;
141 *bufnext = EOS;
143 return glob0(dir, patbuf, pglob, flags & W32FILE_UNIX_GLOB_IGNORECASE,
144 flags & W32FILE_UNIX_GLOB_UNIQUE);
148 * The main glob() routine: compiles the pattern (optionally processing
149 * quotes), calls glob1() to do the real pattern matching, and finally
150 * sorts the list (unless unsorted operation is requested). Returns 0
151 * if things went well, nonzero if errors occurred. It is not an error
152 * to find no matches.
154 static int
155 glob0(GDir *dir, const gchar *pattern, mono_w32file_unix_glob_t *pglob, gboolean ignorecase,
156 gboolean unique)
158 const gchar *qpatnext;
159 int c, err, oldpathc;
160 gchar *bufnext, patbuf[PATH_MAX];
161 size_t limit = 0;
163 qpatnext = pattern;
164 oldpathc = pglob->gl_pathc;
165 bufnext = patbuf;
167 /* We don't need to check for buffer overflow any more. */
168 while ((c = *qpatnext++) != EOS) {
169 switch (c) {
170 case QUESTION:
171 pglob->gl_flags |= W32FILE_UNIX_GLOB_MAGCHAR;
172 *bufnext++ = M_ONE;
173 break;
174 case STAR:
175 pglob->gl_flags |= W32FILE_UNIX_GLOB_MAGCHAR;
176 /* collapse adjacent stars to one,
177 * to avoid exponential behavior
179 if (bufnext == patbuf || bufnext[-1] != M_ALL)
180 *bufnext++ = M_ALL;
181 break;
182 default:
183 *bufnext++ = CHAR(c);
184 break;
187 *bufnext = EOS;
188 #ifdef DEBUG_ENABLED
189 qprintf("glob0:", patbuf);
190 #endif
192 if ((err = glob1(dir, patbuf, patbuf+PATH_MAX-1, pglob, &limit,
193 ignorecase, unique)) != 0)
194 return(err);
196 if (pglob->gl_pathc == oldpathc) {
197 return(W32FILE_UNIX_GLOB_NOMATCH);
200 return(0);
203 static int
204 glob1(GDir *dir, gchar *pattern, gchar *pattern_last, mono_w32file_unix_glob_t *pglob,
205 size_t *limitp, gboolean ignorecase, gboolean unique)
207 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
208 if (*pattern == EOS)
209 return(0);
210 return(glob3(dir, pattern, pattern_last, pglob, limitp, ignorecase,
211 unique));
214 static gboolean contains (mono_w32file_unix_glob_t *pglob, const gchar *name)
216 int i;
217 char **pp;
219 if (pglob->gl_pathv != NULL) {
220 pp = pglob->gl_pathv + pglob->gl_offs;
221 for (i = pglob->gl_pathc; i--; ++pp) {
222 if (*pp) {
223 if (!strcmp (*pp, name)) {
224 return(TRUE);
230 return(FALSE);
233 static int
234 glob3(GDir *dir, gchar *pattern, gchar *pattern_last, mono_w32file_unix_glob_t *pglob,
235 size_t *limitp, gboolean ignorecase, gboolean unique)
237 const gchar *name;
239 /* Search directory for matching names. */
240 while ((name = g_dir_read_name(dir))) {
241 if (!match(name, pattern, pattern + strlen (pattern),
242 ignorecase)) {
243 continue;
245 if (!unique ||
246 !contains (pglob, name)) {
247 globextend (name, pglob, limitp);
251 return(0);
256 * Extend the gl_pathv member of a mono_w32file_unix_glob_t structure to accommodate a new item,
257 * add the new item, and update gl_pathc.
259 * This assumes the BSD realloc, which only copies the block when its size
260 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
261 * behavior.
263 * Return 0 if new item added, error code if memory couldn't be allocated.
265 * Invariant of the mono_w32file_unix_glob_t structure:
266 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
267 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
269 static int
270 globextend(const gchar *path, mono_w32file_unix_glob_t *pglob, size_t *limitp)
272 char **pathv;
273 int i;
274 unsigned int newsize, len;
275 char *copy;
276 const gchar *p;
278 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
279 /* FIXME: Can just use realloc(). */
280 pathv = pglob->gl_pathv ? (char**)g_realloc (pglob->gl_pathv, newsize) :
281 (char**)g_malloc (newsize);
282 if (pathv == NULL) {
283 g_free (pglob->gl_pathv);
284 pglob->gl_pathv = NULL;
285 return(W32FILE_UNIX_GLOB_NOSPACE);
288 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
289 /* first time around -- clear initial gl_offs items */
290 pathv += pglob->gl_offs;
291 for (i = pglob->gl_offs; --i >= 0; )
292 *--pathv = NULL;
294 pglob->gl_pathv = pathv;
296 for (p = path; *p++;)
298 len = (size_t)(p - path);
299 *limitp += len;
300 if ((copy = (char *)malloc(len)) != NULL) {
301 if (g_Ctoc(path, copy, len)) {
302 g_free (copy);
303 return(W32FILE_UNIX_GLOB_NOSPACE);
305 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
307 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
309 #if 0
310 /* Broken on opensuse 11 */
311 if ((pglob->gl_flags & W32FILE_UNIX_GLOB_LIMIT) &&
312 newsize + *limitp >= ARG_MAX) {
313 mono_set_errno (0);
314 return(W32FILE_UNIX_GLOB_NOSPACE);
316 #endif
318 return(copy == NULL ? W32FILE_UNIX_GLOB_NOSPACE : 0);
323 * pattern matching function for filenames. Each occurrence of the *
324 * pattern causes a recursion level.
326 static int
327 match(const gchar *name, gchar *pat, gchar *patend, gboolean ignorecase)
329 gchar c;
331 while (pat < patend) {
332 c = *pat++;
333 switch (c & M_MASK) {
334 case M_ALL:
335 if (pat == patend)
336 return(1);
337 do {
338 if (match(name, pat, patend, ignorecase))
339 return(1);
340 } while (*name++ != EOS);
341 return(0);
342 case M_ONE:
343 if (*name++ == EOS)
344 return(0);
345 break;
346 default:
347 if (ignorecase) {
348 if (g_ascii_tolower (*name++) != g_ascii_tolower (c))
349 return(0);
350 } else {
351 if (*name++ != c)
352 return(0);
355 break;
358 return(*name == EOS);
361 /* Free allocated data belonging to a mono_w32file_unix_glob_t structure. */
362 void
363 mono_w32file_unix_globfree(mono_w32file_unix_glob_t *pglob)
365 int i;
366 char **pp;
368 if (pglob->gl_pathv != NULL) {
369 pp = pglob->gl_pathv + pglob->gl_offs;
370 for (i = pglob->gl_pathc; i--; ++pp)
371 if (*pp)
372 g_free (*pp);
373 g_free (pglob->gl_pathv);
374 pglob->gl_pathv = NULL;
378 static int
379 g_Ctoc(const gchar *str, char *buf, unsigned int len)
382 while (len--) {
383 if ((*buf++ = *str++) == EOS)
384 return (0);
386 return (1);
389 #ifdef DEBUG_ENABLED
390 static void
391 qprintf(const char *str, Char *s)
393 Char *p;
395 (void)printf("%s:\n", str);
396 for (p = s; *p; p++)
397 (void)printf("%c", CHAR(*p));
398 (void)printf("\n");
399 for (p = s; *p; p++)
400 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
401 (void)printf("\n");
402 for (p = s; *p; p++)
403 (void)printf("%c", ismeta(*p) ? '_' : ' ');
404 (void)printf("\n");
406 #endif