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>
49 #include <mono/utils/mono-errno.h>
55 #include "w32file-unix-glob.h"
65 #define M_QUOTE 0x8000
66 #define M_PROTECT 0x4000
68 #define M_ASCII 0x00ff
70 typedef unsigned short Char
;
75 #define M_PROTECT 0x40
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)
92 g_Ctoc(const gchar
*, char *, unsigned int);
95 glob0(GDir
*dir
, const gchar
*, mono_w32file_unix_glob_t
*, gboolean
, gboolean
);
97 glob1(GDir
*dir
, gchar
*, gchar
*, mono_w32file_unix_glob_t
*, size_t *, gboolean
, gboolean
);
100 glob3(GDir
*dir
, gchar
*, gchar
*, mono_w32file_unix_glob_t
*, size_t *, gboolean
, gboolean
);
103 globextend(const gchar
*, mono_w32file_unix_glob_t
*, size_t *);
106 match(const gchar
*, gchar
*, gchar
*, gboolean
);
109 static void qprintf(const char *, Char
*);
113 mono_w32file_unix_glob(GDir
*dir
, const char *pattern
, int flags
, mono_w32file_unix_glob_t
*pglob
)
115 const unsigned char *patnext
;
117 gchar
*bufnext
, *bufend
, patbuf
[PATH_MAX
];
119 patnext
= (unsigned char *) pattern
;
120 if (!(flags
& W32FILE_UNIX_GLOB_APPEND
)) {
122 pglob
->gl_pathv
= NULL
;
125 pglob
->gl_flags
= flags
& ~W32FILE_UNIX_GLOB_MAGCHAR
;
128 bufend
= bufnext
+ PATH_MAX
- 1;
130 /* Protect the quoted characters. */
131 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
133 if ((c
= *patnext
++) == EOS
) {
137 *bufnext
++ = c
| M_PROTECT
;
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.
155 glob0(GDir
*dir
, const gchar
*pattern
, mono_w32file_unix_glob_t
*pglob
, gboolean ignorecase
,
158 const gchar
*qpatnext
;
159 int c
, err
, oldpathc
;
160 gchar
*bufnext
, patbuf
[PATH_MAX
];
164 oldpathc
= pglob
->gl_pathc
;
167 /* We don't need to check for buffer overflow any more. */
168 while ((c
= *qpatnext
++) != EOS
) {
171 pglob
->gl_flags
|= W32FILE_UNIX_GLOB_MAGCHAR
;
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
)
183 *bufnext
++ = CHAR(c
);
189 qprintf("glob0:", patbuf
);
192 if ((err
= glob1(dir
, patbuf
, patbuf
+PATH_MAX
-1, pglob
, &limit
,
193 ignorecase
, unique
)) != 0)
196 if (pglob
->gl_pathc
== oldpathc
) {
197 return(W32FILE_UNIX_GLOB_NOMATCH
);
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. */
210 return(glob3(dir
, pattern
, pattern_last
, pglob
, limitp
, ignorecase
,
214 static gboolean
contains (mono_w32file_unix_glob_t
*pglob
, const gchar
*name
)
219 if (pglob
->gl_pathv
!= NULL
) {
220 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
221 for (i
= pglob
->gl_pathc
; i
--; ++pp
) {
223 if (!strcmp (*pp
, name
)) {
234 glob3(GDir
*dir
, gchar
*pattern
, gchar
*pattern_last
, mono_w32file_unix_glob_t
*pglob
,
235 size_t *limitp
, gboolean ignorecase
, gboolean unique
)
239 /* Search directory for matching names. */
240 while ((name
= g_dir_read_name(dir
))) {
241 if (!match(name
, pattern
, pattern
+ strlen (pattern
),
246 !contains (pglob
, name
)) {
247 globextend (name
, pglob
, limitp
);
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
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.
270 globextend(const gchar
*path
, mono_w32file_unix_glob_t
*pglob
, size_t *limitp
)
274 unsigned int newsize
, len
;
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
);
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; )
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
) ? '_' : ' ');