1 /* $OpenBSD: glob.c,v 1.26 2005/11/28 17:50:12 deraadt Exp $ */
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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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:
41 * Set in gl_flags if pattern contained a globbing character.
43 #include <sys/types.h>
54 #include "w32file-unix-glob.h"
64 #define M_QUOTE 0x8000
65 #define M_PROTECT 0x4000
67 #define M_ASCII 0x00ff
69 typedef unsigned short Char;
74 #define M_PROTECT 0x40
83 #define CHAR(c) ((gchar)((c)&M_ASCII))
84 #define META(c) ((gchar)((c)|M_QUOTE))
85 #define M_ALL META('*')
86 #define M_ONE META('?')
87 #define ismeta(c) (((c)&M_QUOTE) != 0)
91 g_Ctoc(const gchar *, char *, unsigned int);
94 glob0(GDir *dir, const gchar *, mono_w32file_unix_glob_t *, gboolean, gboolean);
96 glob1(GDir *dir, gchar *, gchar *, mono_w32file_unix_glob_t *, size_t *, gboolean, gboolean);
99 glob3(GDir *dir, gchar *, gchar *, mono_w32file_unix_glob_t *, size_t *, gboolean, gboolean);
102 globextend(const gchar *, mono_w32file_unix_glob_t *, size_t *);
105 match(const gchar *, gchar *, gchar *, gboolean);
108 static void qprintf(const char *, Char *);
112 mono_w32file_unix_glob(GDir *dir, const char *pattern, int flags, mono_w32file_unix_glob_t *pglob)
114 const unsigned char *patnext;
116 gchar *bufnext, *bufend, patbuf[PATH_MAX];
118 patnext = (unsigned char *) pattern;
119 if (!(flags & W32FILE_UNIX_GLOB_APPEND)) {
121 pglob->gl_pathv = NULL;
124 pglob->gl_flags = flags & ~W32FILE_UNIX_GLOB_MAGCHAR;
127 bufend = bufnext + PATH_MAX - 1;
129 /* Protect the quoted characters. */
130 while (bufnext < bufend && (c = *patnext++) != EOS)
132 if ((c = *patnext++) == EOS) {
136 *bufnext++ = c | M_PROTECT;
142 return glob0(dir, patbuf, pglob, flags & W32FILE_UNIX_GLOB_IGNORECASE,
143 flags & W32FILE_UNIX_GLOB_UNIQUE);
147 * The main glob() routine: compiles the pattern (optionally processing
148 * quotes), calls glob1() to do the real pattern matching, and finally
149 * sorts the list (unless unsorted operation is requested). Returns 0
150 * if things went well, nonzero if errors occurred. It is not an error
151 * to find no matches.
154 glob0(GDir *dir, const gchar *pattern, mono_w32file_unix_glob_t *pglob, gboolean ignorecase,
157 const gchar *qpatnext;
158 int c, err, oldpathc;
159 gchar *bufnext, patbuf[PATH_MAX];
163 oldpathc = pglob->gl_pathc;
166 /* We don't need to check for buffer overflow any more. */
167 while ((c = *qpatnext++) != EOS) {
170 pglob->gl_flags |= W32FILE_UNIX_GLOB_MAGCHAR;
174 pglob->gl_flags |= W32FILE_UNIX_GLOB_MAGCHAR;
175 /* collapse adjacent stars to one,
176 * to avoid exponential behavior
178 if (bufnext == patbuf || bufnext[-1] != M_ALL)
182 *bufnext++ = CHAR(c);
188 qprintf("glob0:", patbuf);
191 if ((err = glob1(dir, patbuf, patbuf+PATH_MAX-1, pglob, &limit,
192 ignorecase, unique)) != 0)
195 if (pglob->gl_pathc == oldpathc) {
196 return(W32FILE_UNIX_GLOB_NOMATCH);
203 glob1(GDir *dir, gchar *pattern, gchar *pattern_last, mono_w32file_unix_glob_t *pglob,
204 size_t *limitp, gboolean ignorecase, gboolean unique)
206 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
209 return(glob3(dir, pattern, pattern_last, pglob, limitp, ignorecase,
213 static gboolean contains (mono_w32file_unix_glob_t *pglob, const gchar *name)
218 if (pglob->gl_pathv != NULL) {
219 pp = pglob->gl_pathv + pglob->gl_offs;
220 for (i = pglob->gl_pathc; i--; ++pp) {
222 if (!strcmp (*pp, name)) {
233 glob3(GDir *dir, gchar *pattern, gchar *pattern_last, mono_w32file_unix_glob_t *pglob,
234 size_t *limitp, gboolean ignorecase, gboolean unique)
238 /* Search directory for matching names. */
239 while ((name = g_dir_read_name(dir))) {
240 if (!match(name, pattern, pattern + strlen (pattern),
245 !contains (pglob, name)) {
246 globextend (name, pglob, limitp);
255 * Extend the gl_pathv member of a mono_w32file_unix_glob_t structure to accommodate a new item,
256 * add the new item, and update gl_pathc.
258 * This assumes the BSD realloc, which only copies the block when its size
259 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
262 * Return 0 if new item added, error code if memory couldn't be allocated.
264 * Invariant of the mono_w32file_unix_glob_t structure:
265 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
266 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
269 globextend(const gchar *path, mono_w32file_unix_glob_t *pglob, size_t *limitp)
273 unsigned int newsize, len;
277 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
278 /* FIXME: Can just use realloc(). */
279 pathv = (char **)(pglob->gl_pathv ? g_realloc ((char *)pglob->gl_pathv, newsize) :
282 if (pglob->gl_pathv) {
283 g_free (pglob->gl_pathv);
284 pglob->gl_pathv = NULL;
286 return(W32FILE_UNIX_GLOB_NOSPACE);
289 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
290 /* first time around -- clear initial gl_offs items */
291 pathv += pglob->gl_offs;
292 for (i = pglob->gl_offs; --i >= 0; )
295 pglob->gl_pathv = pathv;
297 for (p = path; *p++;)
299 len = (size_t)(p - path);
301 if ((copy = (char *)malloc(len)) != NULL) {
302 if (g_Ctoc(path, copy, len)) {
304 return(W32FILE_UNIX_GLOB_NOSPACE);
306 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
308 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
311 /* Broken on opensuse 11 */
312 if ((pglob->gl_flags & W32FILE_UNIX_GLOB_LIMIT) &&
313 newsize + *limitp >= ARG_MAX) {
315 return(W32FILE_UNIX_GLOB_NOSPACE);
319 return(copy == NULL ? W32FILE_UNIX_GLOB_NOSPACE : 0);
324 * pattern matching function for filenames. Each occurrence of the *
325 * pattern causes a recursion level.
328 match(const gchar *name, gchar *pat, gchar *patend, gboolean ignorecase)
332 while (pat < patend) {
334 switch (c & M_MASK) {
339 if (match(name, pat, patend, ignorecase))
341 } while (*name++ != EOS);
349 if (g_ascii_tolower (*name++) != g_ascii_tolower (c))
359 return(*name == EOS);
362 /* Free allocated data belonging to a mono_w32file_unix_glob_t structure. */
364 mono_w32file_unix_globfree(mono_w32file_unix_glob_t *pglob)
369 if (pglob->gl_pathv != NULL) {
370 pp = pglob->gl_pathv + pglob->gl_offs;
371 for (i = pglob->gl_pathc; i--; ++pp)
374 g_free (pglob->gl_pathv);
375 pglob->gl_pathv = NULL;
380 g_Ctoc(const gchar *str, char *buf, unsigned int len)
384 if ((*buf++ = *str++) == EOS)
392 qprintf(const char *str, Char *s)
396 (void)printf("%s:\n", str);
398 (void)printf("%c", CHAR(*p));
401 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
404 (void)printf("%c", ismeta(*p) ? '_' : ' ');