1 /* $OpenBSD: glob.c,v 1.26 2005/11/28 17:50:12 deraadt Exp $ */
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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
35 * mono_w32file_unix_glob(3) -- a subset of the one defined in POSIX 1003.2.
37 * Optional extra services, controlled by flags not defined by POSIX:
40 * Set in gl_flags if pattern contained a globbing character.
42 #include <sys/types.h>
53 #include "w32file-unix-glob.h"
63 #define M_QUOTE 0x8000
64 #define M_PROTECT 0x4000
66 #define M_ASCII 0x00ff
68 typedef unsigned short Char;
73 #define M_PROTECT 0x40
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)
90 g_Ctoc(const gchar *, char *, unsigned int);
93 glob0(GDir *dir, const gchar *, mono_w32file_unix_glob_t *, gboolean, gboolean);
95 glob1(GDir *dir, gchar *, gchar *, mono_w32file_unix_glob_t *, size_t *, gboolean, gboolean);
98 glob3(GDir *dir, gchar *, gchar *, mono_w32file_unix_glob_t *, size_t *, gboolean, gboolean);
101 globextend(const gchar *, mono_w32file_unix_glob_t *, size_t *);
104 match(const gchar *, gchar *, gchar *, gboolean);
107 static void qprintf(const char *, Char *);
111 mono_w32file_unix_glob(GDir *dir, const char *pattern, int flags, mono_w32file_unix_glob_t *pglob)
113 const unsigned char *patnext;
115 gchar *bufnext, *bufend, patbuf[PATH_MAX];
117 patnext = (unsigned char *) pattern;
118 if (!(flags & W32FILE_UNIX_GLOB_APPEND)) {
120 pglob->gl_pathv = NULL;
123 pglob->gl_flags = flags & ~W32FILE_UNIX_GLOB_MAGCHAR;
126 bufend = bufnext + PATH_MAX - 1;
128 /* Protect the quoted characters. */
129 while (bufnext < bufend && (c = *patnext++) != EOS)
131 if ((c = *patnext++) == EOS) {
135 *bufnext++ = c | M_PROTECT;
141 return glob0(dir, patbuf, pglob, flags & W32FILE_UNIX_GLOB_IGNORECASE,
142 flags & W32FILE_UNIX_GLOB_UNIQUE);
146 * The main glob() routine: compiles the pattern (optionally processing
147 * quotes), calls glob1() to do the real pattern matching, and finally
148 * sorts the list (unless unsorted operation is requested). Returns 0
149 * if things went well, nonzero if errors occurred. It is not an error
150 * to find no matches.
153 glob0(GDir *dir, const gchar *pattern, mono_w32file_unix_glob_t *pglob, gboolean ignorecase,
156 const gchar *qpatnext;
157 int c, err, oldpathc;
158 gchar *bufnext, patbuf[PATH_MAX];
162 oldpathc = pglob->gl_pathc;
165 /* We don't need to check for buffer overflow any more. */
166 while ((c = *qpatnext++) != EOS) {
169 pglob->gl_flags |= W32FILE_UNIX_GLOB_MAGCHAR;
173 pglob->gl_flags |= W32FILE_UNIX_GLOB_MAGCHAR;
174 /* collapse adjacent stars to one,
175 * to avoid exponential behavior
177 if (bufnext == patbuf || bufnext[-1] != M_ALL)
181 *bufnext++ = CHAR(c);
187 qprintf("glob0:", patbuf);
190 if ((err = glob1(dir, patbuf, patbuf+PATH_MAX-1, pglob, &limit,
191 ignorecase, unique)) != 0)
194 if (pglob->gl_pathc == oldpathc) {
195 return(W32FILE_UNIX_GLOB_NOMATCH);
202 glob1(GDir *dir, gchar *pattern, gchar *pattern_last, mono_w32file_unix_glob_t *pglob,
203 size_t *limitp, gboolean ignorecase, gboolean unique)
205 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
208 return(glob3(dir, pattern, pattern_last, pglob, limitp, ignorecase,
212 static gboolean contains (mono_w32file_unix_glob_t *pglob, const gchar *name)
217 if (pglob->gl_pathv != NULL) {
218 pp = pglob->gl_pathv + pglob->gl_offs;
219 for (i = pglob->gl_pathc; i--; ++pp) {
221 if (!strcmp (*pp, name)) {
232 glob3(GDir *dir, gchar *pattern, gchar *pattern_last, mono_w32file_unix_glob_t *pglob,
233 size_t *limitp, gboolean ignorecase, gboolean unique)
237 /* Search directory for matching names. */
238 while ((name = g_dir_read_name(dir))) {
239 if (!match(name, pattern, pattern + strlen (pattern),
244 !contains (pglob, name)) {
245 globextend (name, pglob, limitp);
254 * Extend the gl_pathv member of a mono_w32file_unix_glob_t structure to accommodate a new item,
255 * add the new item, and update gl_pathc.
257 * This assumes the BSD realloc, which only copies the block when its size
258 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
261 * Return 0 if new item added, error code if memory couldn't be allocated.
263 * Invariant of the mono_w32file_unix_glob_t structure:
264 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
265 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
268 globextend(const gchar *path, mono_w32file_unix_glob_t *pglob, size_t *limitp)
272 unsigned int newsize, len;
276 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
277 /* FIXME: Can just use realloc(). */
278 pathv = (char **)(pglob->gl_pathv ? g_realloc ((char *)pglob->gl_pathv, newsize) :
281 if (pglob->gl_pathv) {
282 g_free (pglob->gl_pathv);
283 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; )
294 pglob->gl_pathv = pathv;
296 for (p = path; *p++;)
298 len = (size_t)(p - path);
300 if ((copy = (char *)malloc(len)) != NULL) {
301 if (g_Ctoc(path, copy, len)) {
303 return(W32FILE_UNIX_GLOB_NOSPACE);
305 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
307 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
310 /* Broken on opensuse 11 */
311 if ((pglob->gl_flags & W32FILE_UNIX_GLOB_LIMIT) &&
312 newsize + *limitp >= ARG_MAX) {
314 return(W32FILE_UNIX_GLOB_NOSPACE);
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.
327 match(const gchar *name, gchar *pat, gchar *patend, gboolean ignorecase)
331 while (pat < patend) {
333 switch (c & M_MASK) {
338 if (match(name, pat, patend, ignorecase))
340 } while (*name++ != EOS);
348 if (g_ascii_tolower (*name++) != g_ascii_tolower (c))
358 return(*name == EOS);
361 /* Free allocated data belonging to a mono_w32file_unix_glob_t structure. */
363 mono_w32file_unix_globfree(mono_w32file_unix_glob_t *pglob)
368 if (pglob->gl_pathv != NULL) {
369 pp = pglob->gl_pathv + pglob->gl_offs;
370 for (i = pglob->gl_pathc; i--; ++pp)
373 g_free (pglob->gl_pathv);
374 pglob->gl_pathv = NULL;
379 g_Ctoc(const gchar *str, char *buf, unsigned int len)
383 if ((*buf++ = *str++) == EOS)
391 qprintf(const char *str, Char *s)
395 (void)printf("%s:\n", str);
397 (void)printf("%c", CHAR(*p));
400 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
403 (void)printf("%c", ismeta(*p) ? '_' : ' ');