1*0a6a1f1dSLionel Sambuc /* $NetBSD: glob.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1989, 1993
5ebfedea0SLionel Sambuc * The Regents of the University of California. All rights reserved.
6ebfedea0SLionel Sambuc *
7ebfedea0SLionel Sambuc * This code is derived from software contributed to Berkeley by
8ebfedea0SLionel Sambuc * Guido van Rossum.
9ebfedea0SLionel Sambuc *
10ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc * are met:
13ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
19ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
20ebfedea0SLionel Sambuc * without specific prior written permission.
21ebfedea0SLionel Sambuc *
22ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32ebfedea0SLionel Sambuc * SUCH DAMAGE.
33ebfedea0SLionel Sambuc */
34ebfedea0SLionel Sambuc
35ebfedea0SLionel Sambuc /*
36ebfedea0SLionel Sambuc * glob(3) -- a superset of the one defined in POSIX 1003.2.
37ebfedea0SLionel Sambuc *
38ebfedea0SLionel Sambuc * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
39ebfedea0SLionel Sambuc *
40ebfedea0SLionel Sambuc * Optional extra services, controlled by flags not defined by POSIX:
41ebfedea0SLionel Sambuc *
42ebfedea0SLionel Sambuc * GLOB_QUOTE:
43ebfedea0SLionel Sambuc * Escaping convention: \ inhibits any special meaning the following
44ebfedea0SLionel Sambuc * character might have (except \ at end of string is retained).
45ebfedea0SLionel Sambuc * GLOB_MAGCHAR:
46ebfedea0SLionel Sambuc * Set in gl_flags if pattern contained a globbing character.
47ebfedea0SLionel Sambuc * GLOB_NOMAGIC:
48ebfedea0SLionel Sambuc * Same as GLOB_NOCHECK, but it will only append pattern if it did
49ebfedea0SLionel Sambuc * not contain any magic characters. [Used in csh style globbing]
50ebfedea0SLionel Sambuc * GLOB_ALTDIRFUNC:
51ebfedea0SLionel Sambuc * Use alternately specified directory access functions.
52ebfedea0SLionel Sambuc * GLOB_TILDE:
53ebfedea0SLionel Sambuc * expand ~user/foo to the /home/dir/of/user/foo
54ebfedea0SLionel Sambuc * GLOB_BRACE:
55ebfedea0SLionel Sambuc * expand {1,2}{a,b} to 1a 1b 2a 2b
56ebfedea0SLionel Sambuc * gl_matchc:
57ebfedea0SLionel Sambuc * Number of matches in the current invocation of glob.
58ebfedea0SLionel Sambuc */
59ebfedea0SLionel Sambuc
60ebfedea0SLionel Sambuc #include <config.h>
61ebfedea0SLionel Sambuc
62ebfedea0SLionel Sambuc #ifdef HAVE_SYS_PARAM_H
63ebfedea0SLionel Sambuc #include <sys/param.h>
64ebfedea0SLionel Sambuc #endif
65ebfedea0SLionel Sambuc #ifdef HAVE_SYS_TYPES_H
66ebfedea0SLionel Sambuc #include <sys/types.h>
67ebfedea0SLionel Sambuc #endif
68ebfedea0SLionel Sambuc #ifdef HAVE_SYS_STAT_H
69ebfedea0SLionel Sambuc #include <sys/stat.h>
70ebfedea0SLionel Sambuc #endif
71ebfedea0SLionel Sambuc
72ebfedea0SLionel Sambuc #include <ctype.h>
73ebfedea0SLionel Sambuc #ifdef HAVE_DIRENT_H
74ebfedea0SLionel Sambuc #include <dirent.h>
75ebfedea0SLionel Sambuc #endif
76ebfedea0SLionel Sambuc #include <errno.h>
77ebfedea0SLionel Sambuc #ifdef HAVE_PWD_H
78ebfedea0SLionel Sambuc #include <pwd.h>
79ebfedea0SLionel Sambuc #endif
80ebfedea0SLionel Sambuc #include <stdio.h>
81ebfedea0SLionel Sambuc #include <stdlib.h>
82ebfedea0SLionel Sambuc #include <string.h>
83ebfedea0SLionel Sambuc #ifdef HAVE_UNISTD_H
84ebfedea0SLionel Sambuc #include <unistd.h>
85ebfedea0SLionel Sambuc #endif
86ebfedea0SLionel Sambuc #ifdef HAVE_LIMITS_H
87ebfedea0SLionel Sambuc #include <limits.h>
88ebfedea0SLionel Sambuc #endif
89ebfedea0SLionel Sambuc
90ebfedea0SLionel Sambuc #include "glob.h"
91ebfedea0SLionel Sambuc #include <krb5/roken.h>
92ebfedea0SLionel Sambuc
93ebfedea0SLionel Sambuc #ifndef ARG_MAX
94ebfedea0SLionel Sambuc #define ARG_MAX _POSIX_ARG_MAX
95ebfedea0SLionel Sambuc #endif
96ebfedea0SLionel Sambuc
97ebfedea0SLionel Sambuc #define CHAR_DOLLAR '$'
98ebfedea0SLionel Sambuc #define CHAR_DOT '.'
99ebfedea0SLionel Sambuc #define CHAR_EOS '\0'
100ebfedea0SLionel Sambuc #define CHAR_LBRACKET '['
101ebfedea0SLionel Sambuc #define CHAR_NOT '!'
102ebfedea0SLionel Sambuc #define CHAR_QUESTION '?'
103ebfedea0SLionel Sambuc #define CHAR_QUOTE '\\'
104ebfedea0SLionel Sambuc #define CHAR_RANGE '-'
105ebfedea0SLionel Sambuc #define CHAR_RBRACKET ']'
106ebfedea0SLionel Sambuc #define CHAR_SEP '/'
107ebfedea0SLionel Sambuc #define CHAR_STAR '*'
108ebfedea0SLionel Sambuc #define CHAR_TILDE '~'
109ebfedea0SLionel Sambuc #define CHAR_UNDERSCORE '_'
110ebfedea0SLionel Sambuc #define CHAR_LBRACE '{'
111ebfedea0SLionel Sambuc #define CHAR_RBRACE '}'
112ebfedea0SLionel Sambuc #define CHAR_SLASH '/'
113ebfedea0SLionel Sambuc #define CHAR_COMMA ','
114ebfedea0SLionel Sambuc
115ebfedea0SLionel Sambuc #ifndef DEBUG
116ebfedea0SLionel Sambuc
117ebfedea0SLionel Sambuc #define M_QUOTE 0x8000
118ebfedea0SLionel Sambuc #define M_PROTECT 0x4000
119ebfedea0SLionel Sambuc #define M_MASK 0xffff
120ebfedea0SLionel Sambuc #define M_ASCII 0x00ff
121ebfedea0SLionel Sambuc
122ebfedea0SLionel Sambuc typedef u_short Char;
123ebfedea0SLionel Sambuc
124ebfedea0SLionel Sambuc #else
125ebfedea0SLionel Sambuc
126ebfedea0SLionel Sambuc #define M_QUOTE 0x80
127ebfedea0SLionel Sambuc #define M_PROTECT 0x40
128ebfedea0SLionel Sambuc #define M_MASK 0xff
129ebfedea0SLionel Sambuc #define M_ASCII 0x7f
130ebfedea0SLionel Sambuc
131ebfedea0SLionel Sambuc typedef char Char;
132ebfedea0SLionel Sambuc
133ebfedea0SLionel Sambuc #endif
134ebfedea0SLionel Sambuc
135ebfedea0SLionel Sambuc
136ebfedea0SLionel Sambuc #define CHAR(c) ((Char)((c)&M_ASCII))
137ebfedea0SLionel Sambuc #define META(c) ((Char)((c)|M_QUOTE))
138ebfedea0SLionel Sambuc #define M_ALL META('*')
139ebfedea0SLionel Sambuc #define M_END META(']')
140ebfedea0SLionel Sambuc #define M_NOT META('!')
141ebfedea0SLionel Sambuc #define M_ONE META('?')
142ebfedea0SLionel Sambuc #define M_RNG META('-')
143ebfedea0SLionel Sambuc #define M_SET META('[')
144ebfedea0SLionel Sambuc #define ismeta(c) (((c)&M_QUOTE) != 0)
145ebfedea0SLionel Sambuc
146ebfedea0SLionel Sambuc
147ebfedea0SLionel Sambuc static int compare (const void *, const void *);
148ebfedea0SLionel Sambuc static void g_Ctoc (const Char *, char *);
149ebfedea0SLionel Sambuc static int g_lstat (Char *, struct stat *, glob_t *);
150ebfedea0SLionel Sambuc static DIR *g_opendir (Char *, glob_t *);
151ebfedea0SLionel Sambuc static Char *g_strchr (const Char *, int);
152ebfedea0SLionel Sambuc #ifdef notdef
153ebfedea0SLionel Sambuc static Char *g_strcat (Char *, const Char *);
154ebfedea0SLionel Sambuc #endif
155ebfedea0SLionel Sambuc static int g_stat (Char *, struct stat *, glob_t *);
156ebfedea0SLionel Sambuc static int glob0 (const Char *, glob_t *);
157ebfedea0SLionel Sambuc static int glob1 (Char *, glob_t *, size_t *);
158ebfedea0SLionel Sambuc static int glob2 (Char *, Char *, Char *, glob_t *, size_t *);
159ebfedea0SLionel Sambuc static int glob3 (Char *, Char *, Char *, Char *, glob_t *, size_t *);
160ebfedea0SLionel Sambuc static int globextend (const Char *, glob_t *, size_t *);
161ebfedea0SLionel Sambuc static const Char * globtilde (const Char *, Char *, glob_t *);
162ebfedea0SLionel Sambuc static int globexp1 (const Char *, glob_t *);
163ebfedea0SLionel Sambuc static int globexp2 (const Char *, const Char *, glob_t *, int *);
164ebfedea0SLionel Sambuc static int match (Char *, Char *, Char *);
165ebfedea0SLionel Sambuc #ifdef DEBUG
166ebfedea0SLionel Sambuc static void qprintf (const char *, Char *);
167ebfedea0SLionel Sambuc #endif
168ebfedea0SLionel Sambuc
169ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
glob(const char * pattern,int flags,int (* errfunc)(const char *,int),glob_t * pglob)170ebfedea0SLionel Sambuc glob(const char *pattern,
171ebfedea0SLionel Sambuc int flags,
172ebfedea0SLionel Sambuc int (*errfunc)(const char *, int),
173ebfedea0SLionel Sambuc glob_t *pglob)
174ebfedea0SLionel Sambuc {
175ebfedea0SLionel Sambuc const u_char *patnext;
176ebfedea0SLionel Sambuc int c;
177ebfedea0SLionel Sambuc Char *bufnext, *bufend, patbuf[MaxPathLen+1];
178ebfedea0SLionel Sambuc
179ebfedea0SLionel Sambuc patnext = (const u_char *) pattern;
180ebfedea0SLionel Sambuc if (!(flags & GLOB_APPEND)) {
181ebfedea0SLionel Sambuc pglob->gl_pathc = 0;
182ebfedea0SLionel Sambuc pglob->gl_pathv = NULL;
183ebfedea0SLionel Sambuc if (!(flags & GLOB_DOOFFS))
184ebfedea0SLionel Sambuc pglob->gl_offs = 0;
185ebfedea0SLionel Sambuc }
186ebfedea0SLionel Sambuc pglob->gl_flags = flags & ~GLOB_MAGCHAR;
187ebfedea0SLionel Sambuc pglob->gl_errfunc = errfunc;
188ebfedea0SLionel Sambuc pglob->gl_matchc = 0;
189ebfedea0SLionel Sambuc
190ebfedea0SLionel Sambuc bufnext = patbuf;
191ebfedea0SLionel Sambuc bufend = bufnext + MaxPathLen;
192ebfedea0SLionel Sambuc if (flags & GLOB_QUOTE) {
193ebfedea0SLionel Sambuc /* Protect the quoted characters. */
194ebfedea0SLionel Sambuc while (bufnext < bufend && (c = *patnext++) != CHAR_EOS)
195ebfedea0SLionel Sambuc if (c == CHAR_QUOTE) {
196ebfedea0SLionel Sambuc if ((c = *patnext++) == CHAR_EOS) {
197ebfedea0SLionel Sambuc c = CHAR_QUOTE;
198ebfedea0SLionel Sambuc --patnext;
199ebfedea0SLionel Sambuc }
200ebfedea0SLionel Sambuc *bufnext++ = c | M_PROTECT;
201ebfedea0SLionel Sambuc }
202ebfedea0SLionel Sambuc else
203ebfedea0SLionel Sambuc *bufnext++ = c;
204ebfedea0SLionel Sambuc }
205ebfedea0SLionel Sambuc else
206ebfedea0SLionel Sambuc while (bufnext < bufend && (c = *patnext++) != CHAR_EOS)
207ebfedea0SLionel Sambuc *bufnext++ = c;
208ebfedea0SLionel Sambuc *bufnext = CHAR_EOS;
209ebfedea0SLionel Sambuc
210ebfedea0SLionel Sambuc if (flags & GLOB_BRACE)
211ebfedea0SLionel Sambuc return globexp1(patbuf, pglob);
212ebfedea0SLionel Sambuc else
213ebfedea0SLionel Sambuc return glob0(patbuf, pglob);
214ebfedea0SLionel Sambuc }
215ebfedea0SLionel Sambuc
216ebfedea0SLionel Sambuc /*
217ebfedea0SLionel Sambuc * Expand recursively a glob {} pattern. When there is no more expansion
218ebfedea0SLionel Sambuc * invoke the standard globbing routine to glob the rest of the magic
219ebfedea0SLionel Sambuc * characters
220ebfedea0SLionel Sambuc */
globexp1(const Char * pattern,glob_t * pglob)221ebfedea0SLionel Sambuc static int globexp1(const Char *pattern, glob_t *pglob)
222ebfedea0SLionel Sambuc {
223ebfedea0SLionel Sambuc const Char* ptr = pattern;
224ebfedea0SLionel Sambuc int rv;
225ebfedea0SLionel Sambuc
226ebfedea0SLionel Sambuc /* Protect a single {}, for find(1), like csh */
227ebfedea0SLionel Sambuc if (pattern[0] == CHAR_LBRACE && pattern[1] == CHAR_RBRACE && pattern[2] == CHAR_EOS)
228ebfedea0SLionel Sambuc return glob0(pattern, pglob);
229ebfedea0SLionel Sambuc
230ebfedea0SLionel Sambuc while ((ptr = (const Char *) g_strchr(ptr, CHAR_LBRACE)) != NULL)
231ebfedea0SLionel Sambuc if (!globexp2(ptr, pattern, pglob, &rv))
232ebfedea0SLionel Sambuc return rv;
233ebfedea0SLionel Sambuc
234ebfedea0SLionel Sambuc return glob0(pattern, pglob);
235ebfedea0SLionel Sambuc }
236ebfedea0SLionel Sambuc
237ebfedea0SLionel Sambuc
238ebfedea0SLionel Sambuc /*
239ebfedea0SLionel Sambuc * Recursive brace globbing helper. Tries to expand a single brace.
240ebfedea0SLionel Sambuc * If it succeeds then it invokes globexp1 with the new pattern.
241ebfedea0SLionel Sambuc * If it fails then it tries to glob the rest of the pattern and returns.
242ebfedea0SLionel Sambuc */
globexp2(const Char * ptr,const Char * pattern,glob_t * pglob,int * rv)243ebfedea0SLionel Sambuc static int globexp2(const Char *ptr, const Char *pattern,
244ebfedea0SLionel Sambuc glob_t *pglob, int *rv)
245ebfedea0SLionel Sambuc {
246ebfedea0SLionel Sambuc int i;
247ebfedea0SLionel Sambuc Char *lm, *ls;
248ebfedea0SLionel Sambuc const Char *pe, *pm, *pl;
249ebfedea0SLionel Sambuc Char patbuf[MaxPathLen + 1];
250ebfedea0SLionel Sambuc
251ebfedea0SLionel Sambuc /* copy part up to the brace */
252ebfedea0SLionel Sambuc for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
253ebfedea0SLionel Sambuc continue;
254ebfedea0SLionel Sambuc ls = lm;
255ebfedea0SLionel Sambuc
256ebfedea0SLionel Sambuc /* Find the balanced brace */
257ebfedea0SLionel Sambuc for (i = 0, pe = ++ptr; *pe; pe++)
258ebfedea0SLionel Sambuc if (*pe == CHAR_LBRACKET) {
259ebfedea0SLionel Sambuc /* Ignore everything between [] */
260ebfedea0SLionel Sambuc for (pm = pe++; *pe != CHAR_RBRACKET && *pe != CHAR_EOS; pe++)
261ebfedea0SLionel Sambuc continue;
262ebfedea0SLionel Sambuc if (*pe == CHAR_EOS) {
263ebfedea0SLionel Sambuc /*
264ebfedea0SLionel Sambuc * We could not find a matching CHAR_RBRACKET.
265ebfedea0SLionel Sambuc * Ignore and just look for CHAR_RBRACE
266ebfedea0SLionel Sambuc */
267ebfedea0SLionel Sambuc pe = pm;
268ebfedea0SLionel Sambuc }
269ebfedea0SLionel Sambuc }
270ebfedea0SLionel Sambuc else if (*pe == CHAR_LBRACE)
271ebfedea0SLionel Sambuc i++;
272ebfedea0SLionel Sambuc else if (*pe == CHAR_RBRACE) {
273ebfedea0SLionel Sambuc if (i == 0)
274ebfedea0SLionel Sambuc break;
275ebfedea0SLionel Sambuc i--;
276ebfedea0SLionel Sambuc }
277ebfedea0SLionel Sambuc
278ebfedea0SLionel Sambuc /* Non matching braces; just glob the pattern */
279ebfedea0SLionel Sambuc if (i != 0 || *pe == CHAR_EOS) {
280ebfedea0SLionel Sambuc *rv = glob0(patbuf, pglob);
281ebfedea0SLionel Sambuc return 0;
282ebfedea0SLionel Sambuc }
283ebfedea0SLionel Sambuc
284ebfedea0SLionel Sambuc for (i = 0, pl = pm = ptr; pm <= pe; pm++)
285ebfedea0SLionel Sambuc switch (*pm) {
286ebfedea0SLionel Sambuc case CHAR_LBRACKET:
287ebfedea0SLionel Sambuc /* Ignore everything between [] */
288ebfedea0SLionel Sambuc for (pl = pm++; *pm != CHAR_RBRACKET && *pm != CHAR_EOS; pm++)
289ebfedea0SLionel Sambuc continue;
290ebfedea0SLionel Sambuc if (*pm == CHAR_EOS) {
291ebfedea0SLionel Sambuc /*
292ebfedea0SLionel Sambuc * We could not find a matching CHAR_RBRACKET.
293ebfedea0SLionel Sambuc * Ignore and just look for CHAR_RBRACE
294ebfedea0SLionel Sambuc */
295ebfedea0SLionel Sambuc pm = pl;
296ebfedea0SLionel Sambuc }
297ebfedea0SLionel Sambuc break;
298ebfedea0SLionel Sambuc
299ebfedea0SLionel Sambuc case CHAR_LBRACE:
300ebfedea0SLionel Sambuc i++;
301ebfedea0SLionel Sambuc break;
302ebfedea0SLionel Sambuc
303ebfedea0SLionel Sambuc case CHAR_RBRACE:
304ebfedea0SLionel Sambuc if (i) {
305ebfedea0SLionel Sambuc i--;
306ebfedea0SLionel Sambuc break;
307ebfedea0SLionel Sambuc }
308ebfedea0SLionel Sambuc /* FALLTHROUGH */
309ebfedea0SLionel Sambuc case CHAR_COMMA:
310ebfedea0SLionel Sambuc if (i && *pm == CHAR_COMMA)
311ebfedea0SLionel Sambuc break;
312ebfedea0SLionel Sambuc else {
313ebfedea0SLionel Sambuc /* Append the current string */
314ebfedea0SLionel Sambuc for (lm = ls; (pl < pm); *lm++ = *pl++)
315ebfedea0SLionel Sambuc continue;
316ebfedea0SLionel Sambuc /*
317ebfedea0SLionel Sambuc * Append the rest of the pattern after the
318ebfedea0SLionel Sambuc * closing brace
319ebfedea0SLionel Sambuc */
320ebfedea0SLionel Sambuc for (pl = pe + 1; (*lm++ = *pl++) != CHAR_EOS;)
321ebfedea0SLionel Sambuc continue;
322ebfedea0SLionel Sambuc
323ebfedea0SLionel Sambuc /* Expand the current pattern */
324ebfedea0SLionel Sambuc #ifdef DEBUG
325ebfedea0SLionel Sambuc qprintf("globexp2:", patbuf);
326ebfedea0SLionel Sambuc #endif
327ebfedea0SLionel Sambuc *rv = globexp1(patbuf, pglob);
328ebfedea0SLionel Sambuc
329ebfedea0SLionel Sambuc /* move after the comma, to the next string */
330ebfedea0SLionel Sambuc pl = pm + 1;
331ebfedea0SLionel Sambuc }
332ebfedea0SLionel Sambuc break;
333ebfedea0SLionel Sambuc
334ebfedea0SLionel Sambuc default:
335ebfedea0SLionel Sambuc break;
336ebfedea0SLionel Sambuc }
337ebfedea0SLionel Sambuc *rv = 0;
338ebfedea0SLionel Sambuc return 0;
339ebfedea0SLionel Sambuc }
340ebfedea0SLionel Sambuc
341ebfedea0SLionel Sambuc
342ebfedea0SLionel Sambuc
343ebfedea0SLionel Sambuc /*
344ebfedea0SLionel Sambuc * expand tilde from the passwd file.
345ebfedea0SLionel Sambuc */
346ebfedea0SLionel Sambuc static const Char *
globtilde(const Char * pattern,Char * patbuf,glob_t * pglob)347ebfedea0SLionel Sambuc globtilde(const Char *pattern, Char *patbuf, glob_t *pglob)
348ebfedea0SLionel Sambuc {
349ebfedea0SLionel Sambuc struct passwd *pwd;
350ebfedea0SLionel Sambuc char *h;
351ebfedea0SLionel Sambuc const Char *p;
352ebfedea0SLionel Sambuc Char *b;
353ebfedea0SLionel Sambuc
354ebfedea0SLionel Sambuc if (*pattern != CHAR_TILDE || !(pglob->gl_flags & GLOB_TILDE))
355ebfedea0SLionel Sambuc return pattern;
356ebfedea0SLionel Sambuc
357ebfedea0SLionel Sambuc /* Copy up to the end of the string or / */
358ebfedea0SLionel Sambuc for (p = pattern + 1, h = (char *) patbuf; *p && *p != CHAR_SLASH;
359ebfedea0SLionel Sambuc *h++ = *p++)
360ebfedea0SLionel Sambuc continue;
361ebfedea0SLionel Sambuc
362ebfedea0SLionel Sambuc *h = CHAR_EOS;
363ebfedea0SLionel Sambuc
364ebfedea0SLionel Sambuc if (((char *) patbuf)[0] == CHAR_EOS) {
365ebfedea0SLionel Sambuc /*
366ebfedea0SLionel Sambuc * handle a plain ~ or ~/ by expanding $HOME
367ebfedea0SLionel Sambuc * first and then trying the password file
368ebfedea0SLionel Sambuc */
369ebfedea0SLionel Sambuc if ((h = getenv("HOME")) == NULL) {
370ebfedea0SLionel Sambuc if ((pwd = k_getpwuid(getuid())) == NULL)
371ebfedea0SLionel Sambuc return pattern;
372ebfedea0SLionel Sambuc else
373ebfedea0SLionel Sambuc h = pwd->pw_dir;
374ebfedea0SLionel Sambuc }
375ebfedea0SLionel Sambuc }
376ebfedea0SLionel Sambuc else {
377ebfedea0SLionel Sambuc /*
378ebfedea0SLionel Sambuc * Expand a ~user
379ebfedea0SLionel Sambuc */
380ebfedea0SLionel Sambuc if ((pwd = k_getpwnam((char*) patbuf)) == NULL)
381ebfedea0SLionel Sambuc return pattern;
382ebfedea0SLionel Sambuc else
383ebfedea0SLionel Sambuc h = pwd->pw_dir;
384ebfedea0SLionel Sambuc }
385ebfedea0SLionel Sambuc
386ebfedea0SLionel Sambuc /* Copy the home directory */
387ebfedea0SLionel Sambuc for (b = patbuf; *h; *b++ = *h++)
388ebfedea0SLionel Sambuc continue;
389ebfedea0SLionel Sambuc
390ebfedea0SLionel Sambuc /* Append the rest of the pattern */
391ebfedea0SLionel Sambuc while ((*b++ = *p++) != CHAR_EOS)
392ebfedea0SLionel Sambuc continue;
393ebfedea0SLionel Sambuc
394ebfedea0SLionel Sambuc return patbuf;
395ebfedea0SLionel Sambuc }
396ebfedea0SLionel Sambuc
397ebfedea0SLionel Sambuc
398ebfedea0SLionel Sambuc /*
399ebfedea0SLionel Sambuc * The main glob() routine: compiles the pattern (optionally processing
400ebfedea0SLionel Sambuc * quotes), calls glob1() to do the real pattern matching, and finally
401ebfedea0SLionel Sambuc * sorts the list (unless unsorted operation is requested). Returns 0
402ebfedea0SLionel Sambuc * if things went well, nonzero if errors occurred. It is not an error
403ebfedea0SLionel Sambuc * to find no matches.
404ebfedea0SLionel Sambuc */
405ebfedea0SLionel Sambuc static int
glob0(const Char * pattern,glob_t * pglob)406ebfedea0SLionel Sambuc glob0(const Char *pattern, glob_t *pglob)
407ebfedea0SLionel Sambuc {
408ebfedea0SLionel Sambuc const Char *qpatnext;
409ebfedea0SLionel Sambuc int c, err, oldpathc;
410ebfedea0SLionel Sambuc Char *bufnext, patbuf[MaxPathLen+1];
411ebfedea0SLionel Sambuc size_t limit = 0;
412ebfedea0SLionel Sambuc
413ebfedea0SLionel Sambuc qpatnext = globtilde(pattern, patbuf, pglob);
414ebfedea0SLionel Sambuc oldpathc = pglob->gl_pathc;
415ebfedea0SLionel Sambuc bufnext = patbuf;
416ebfedea0SLionel Sambuc
417ebfedea0SLionel Sambuc /* We don't need to check for buffer overflow any more. */
418ebfedea0SLionel Sambuc while ((c = *qpatnext++) != CHAR_EOS) {
419ebfedea0SLionel Sambuc switch (c) {
420ebfedea0SLionel Sambuc case CHAR_LBRACKET:
421ebfedea0SLionel Sambuc c = *qpatnext;
422ebfedea0SLionel Sambuc if (c == CHAR_NOT)
423ebfedea0SLionel Sambuc ++qpatnext;
424ebfedea0SLionel Sambuc if (*qpatnext == CHAR_EOS ||
425ebfedea0SLionel Sambuc g_strchr(qpatnext+1, CHAR_RBRACKET) == NULL) {
426ebfedea0SLionel Sambuc *bufnext++ = CHAR_LBRACKET;
427ebfedea0SLionel Sambuc if (c == CHAR_NOT)
428ebfedea0SLionel Sambuc --qpatnext;
429ebfedea0SLionel Sambuc break;
430ebfedea0SLionel Sambuc }
431ebfedea0SLionel Sambuc *bufnext++ = M_SET;
432ebfedea0SLionel Sambuc if (c == CHAR_NOT)
433ebfedea0SLionel Sambuc *bufnext++ = M_NOT;
434ebfedea0SLionel Sambuc c = *qpatnext++;
435ebfedea0SLionel Sambuc do {
436ebfedea0SLionel Sambuc *bufnext++ = CHAR(c);
437ebfedea0SLionel Sambuc if (*qpatnext == CHAR_RANGE &&
438ebfedea0SLionel Sambuc (c = qpatnext[1]) != CHAR_RBRACKET) {
439ebfedea0SLionel Sambuc *bufnext++ = M_RNG;
440ebfedea0SLionel Sambuc *bufnext++ = CHAR(c);
441ebfedea0SLionel Sambuc qpatnext += 2;
442ebfedea0SLionel Sambuc }
443ebfedea0SLionel Sambuc } while ((c = *qpatnext++) != CHAR_RBRACKET);
444ebfedea0SLionel Sambuc pglob->gl_flags |= GLOB_MAGCHAR;
445ebfedea0SLionel Sambuc *bufnext++ = M_END;
446ebfedea0SLionel Sambuc break;
447ebfedea0SLionel Sambuc case CHAR_QUESTION:
448ebfedea0SLionel Sambuc pglob->gl_flags |= GLOB_MAGCHAR;
449ebfedea0SLionel Sambuc *bufnext++ = M_ONE;
450ebfedea0SLionel Sambuc break;
451ebfedea0SLionel Sambuc case CHAR_STAR:
452ebfedea0SLionel Sambuc pglob->gl_flags |= GLOB_MAGCHAR;
453ebfedea0SLionel Sambuc /* collapse adjacent stars to one,
454ebfedea0SLionel Sambuc * to avoid exponential behavior
455ebfedea0SLionel Sambuc */
456ebfedea0SLionel Sambuc if (bufnext == patbuf || bufnext[-1] != M_ALL)
457ebfedea0SLionel Sambuc *bufnext++ = M_ALL;
458ebfedea0SLionel Sambuc break;
459ebfedea0SLionel Sambuc default:
460ebfedea0SLionel Sambuc *bufnext++ = CHAR(c);
461ebfedea0SLionel Sambuc break;
462ebfedea0SLionel Sambuc }
463ebfedea0SLionel Sambuc }
464ebfedea0SLionel Sambuc *bufnext = CHAR_EOS;
465ebfedea0SLionel Sambuc #ifdef DEBUG
466ebfedea0SLionel Sambuc qprintf("glob0:", patbuf);
467ebfedea0SLionel Sambuc #endif
468ebfedea0SLionel Sambuc
469ebfedea0SLionel Sambuc if ((err = glob1(patbuf, pglob, &limit)) != 0)
470ebfedea0SLionel Sambuc return(err);
471ebfedea0SLionel Sambuc
472ebfedea0SLionel Sambuc /*
473ebfedea0SLionel Sambuc * If there was no match we are going to append the pattern
474ebfedea0SLionel Sambuc * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
475ebfedea0SLionel Sambuc * and the pattern did not contain any magic characters
476ebfedea0SLionel Sambuc * GLOB_NOMAGIC is there just for compatibility with csh.
477ebfedea0SLionel Sambuc */
478ebfedea0SLionel Sambuc if (pglob->gl_pathc == oldpathc &&
479ebfedea0SLionel Sambuc ((pglob->gl_flags & GLOB_NOCHECK) ||
480ebfedea0SLionel Sambuc ((pglob->gl_flags & GLOB_NOMAGIC) &&
481ebfedea0SLionel Sambuc !(pglob->gl_flags & GLOB_MAGCHAR))))
482ebfedea0SLionel Sambuc return(globextend(pattern, pglob, &limit));
483ebfedea0SLionel Sambuc else if (!(pglob->gl_flags & GLOB_NOSORT))
484ebfedea0SLionel Sambuc qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
485ebfedea0SLionel Sambuc pglob->gl_pathc - oldpathc, sizeof(char *), compare);
486ebfedea0SLionel Sambuc return(0);
487ebfedea0SLionel Sambuc }
488ebfedea0SLionel Sambuc
489ebfedea0SLionel Sambuc static int
compare(const void * p,const void * q)490ebfedea0SLionel Sambuc compare(const void *p, const void *q)
491ebfedea0SLionel Sambuc {
492ebfedea0SLionel Sambuc return(strcmp(*(char **)p, *(char **)q));
493ebfedea0SLionel Sambuc }
494ebfedea0SLionel Sambuc
495ebfedea0SLionel Sambuc static int
glob1(Char * pattern,glob_t * pglob,size_t * limit)496ebfedea0SLionel Sambuc glob1(Char *pattern, glob_t *pglob, size_t *limit)
497ebfedea0SLionel Sambuc {
498ebfedea0SLionel Sambuc Char pathbuf[MaxPathLen+1];
499ebfedea0SLionel Sambuc
500ebfedea0SLionel Sambuc /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
501ebfedea0SLionel Sambuc if (*pattern == CHAR_EOS)
502ebfedea0SLionel Sambuc return(0);
503ebfedea0SLionel Sambuc return(glob2(pathbuf, pathbuf, pattern, pglob, limit));
504ebfedea0SLionel Sambuc }
505ebfedea0SLionel Sambuc
506ebfedea0SLionel Sambuc /*
507ebfedea0SLionel Sambuc * The functions glob2 and glob3 are mutually recursive; there is one level
508ebfedea0SLionel Sambuc * of recursion for each segment in the pattern that contains one or more
509ebfedea0SLionel Sambuc * meta characters.
510ebfedea0SLionel Sambuc */
511ebfedea0SLionel Sambuc
512ebfedea0SLionel Sambuc #ifndef S_ISLNK
513ebfedea0SLionel Sambuc #if defined(S_IFLNK) && defined(S_IFMT)
514ebfedea0SLionel Sambuc #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
515ebfedea0SLionel Sambuc #else
516ebfedea0SLionel Sambuc #define S_ISLNK(mode) 0
517ebfedea0SLionel Sambuc #endif
518ebfedea0SLionel Sambuc #endif
519ebfedea0SLionel Sambuc
520ebfedea0SLionel Sambuc static int
glob2(Char * pathbuf,Char * pathend,Char * pattern,glob_t * pglob,size_t * limit)521ebfedea0SLionel Sambuc glob2(Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob,
522ebfedea0SLionel Sambuc size_t *limit)
523ebfedea0SLionel Sambuc {
524ebfedea0SLionel Sambuc struct stat sb;
525ebfedea0SLionel Sambuc Char *p, *q;
526ebfedea0SLionel Sambuc int anymeta;
527ebfedea0SLionel Sambuc
528ebfedea0SLionel Sambuc /*
529ebfedea0SLionel Sambuc * Loop over pattern segments until end of pattern or until
530ebfedea0SLionel Sambuc * segment with meta character found.
531ebfedea0SLionel Sambuc */
532ebfedea0SLionel Sambuc for (anymeta = 0;;) {
533ebfedea0SLionel Sambuc if (*pattern == CHAR_EOS) { /* End of pattern? */
534ebfedea0SLionel Sambuc *pathend = CHAR_EOS;
535ebfedea0SLionel Sambuc if (g_lstat(pathbuf, &sb, pglob))
536ebfedea0SLionel Sambuc return(0);
537ebfedea0SLionel Sambuc
538ebfedea0SLionel Sambuc if (((pglob->gl_flags & GLOB_MARK) &&
539ebfedea0SLionel Sambuc pathend[-1] != CHAR_SEP) && (S_ISDIR(sb.st_mode)
540ebfedea0SLionel Sambuc || (S_ISLNK(sb.st_mode) &&
541ebfedea0SLionel Sambuc (g_stat(pathbuf, &sb, pglob) == 0) &&
542ebfedea0SLionel Sambuc S_ISDIR(sb.st_mode)))) {
543ebfedea0SLionel Sambuc *pathend++ = CHAR_SEP;
544ebfedea0SLionel Sambuc *pathend = CHAR_EOS;
545ebfedea0SLionel Sambuc }
546ebfedea0SLionel Sambuc ++pglob->gl_matchc;
547ebfedea0SLionel Sambuc return(globextend(pathbuf, pglob, limit));
548ebfedea0SLionel Sambuc }
549ebfedea0SLionel Sambuc
550ebfedea0SLionel Sambuc /* Find end of next segment, copy tentatively to pathend. */
551ebfedea0SLionel Sambuc q = pathend;
552ebfedea0SLionel Sambuc p = pattern;
553ebfedea0SLionel Sambuc while (*p != CHAR_EOS && *p != CHAR_SEP) {
554ebfedea0SLionel Sambuc if (ismeta(*p))
555ebfedea0SLionel Sambuc anymeta = 1;
556ebfedea0SLionel Sambuc *q++ = *p++;
557ebfedea0SLionel Sambuc }
558ebfedea0SLionel Sambuc
559ebfedea0SLionel Sambuc if (!anymeta) { /* No expansion, do next segment. */
560ebfedea0SLionel Sambuc pathend = q;
561ebfedea0SLionel Sambuc pattern = p;
562ebfedea0SLionel Sambuc while (*pattern == CHAR_SEP)
563ebfedea0SLionel Sambuc *pathend++ = *pattern++;
564ebfedea0SLionel Sambuc } else /* Need expansion, recurse. */
565ebfedea0SLionel Sambuc return(glob3(pathbuf, pathend, pattern, p, pglob,
566ebfedea0SLionel Sambuc limit));
567ebfedea0SLionel Sambuc }
568ebfedea0SLionel Sambuc /* NOTREACHED */
569ebfedea0SLionel Sambuc }
570ebfedea0SLionel Sambuc
571ebfedea0SLionel Sambuc static int
glob3(Char * pathbuf,Char * pathend,Char * pattern,Char * restpattern,glob_t * pglob,size_t * limit)572ebfedea0SLionel Sambuc glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern,
573ebfedea0SLionel Sambuc glob_t *pglob, size_t *limit)
574ebfedea0SLionel Sambuc {
575ebfedea0SLionel Sambuc struct dirent *dp;
576ebfedea0SLionel Sambuc DIR *dirp;
577ebfedea0SLionel Sambuc int err;
578ebfedea0SLionel Sambuc char buf[MaxPathLen];
579ebfedea0SLionel Sambuc
580ebfedea0SLionel Sambuc /*
581ebfedea0SLionel Sambuc * The readdirfunc declaration can't be prototyped, because it is
582ebfedea0SLionel Sambuc * assigned, below, to two functions which are prototyped in glob.h
583ebfedea0SLionel Sambuc * and dirent.h as taking pointers to differently typed opaque
584ebfedea0SLionel Sambuc * structures.
585ebfedea0SLionel Sambuc */
586ebfedea0SLionel Sambuc struct dirent *(*readdirfunc)(void *);
587ebfedea0SLionel Sambuc
588ebfedea0SLionel Sambuc *pathend = CHAR_EOS;
589ebfedea0SLionel Sambuc errno = 0;
590ebfedea0SLionel Sambuc
591ebfedea0SLionel Sambuc if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
592ebfedea0SLionel Sambuc /* TODO: don't call for ENOENT or ENOTDIR? */
593ebfedea0SLionel Sambuc if (pglob->gl_errfunc) {
594ebfedea0SLionel Sambuc g_Ctoc(pathbuf, buf);
595ebfedea0SLionel Sambuc if (pglob->gl_errfunc(buf, errno) ||
596ebfedea0SLionel Sambuc pglob->gl_flags & GLOB_ERR)
597ebfedea0SLionel Sambuc return (GLOB_ABEND);
598ebfedea0SLionel Sambuc }
599ebfedea0SLionel Sambuc return(0);
600ebfedea0SLionel Sambuc }
601ebfedea0SLionel Sambuc
602ebfedea0SLionel Sambuc err = 0;
603ebfedea0SLionel Sambuc
604ebfedea0SLionel Sambuc /* Search directory for matching names. */
605ebfedea0SLionel Sambuc if (pglob->gl_flags & GLOB_ALTDIRFUNC)
606ebfedea0SLionel Sambuc readdirfunc = pglob->gl_readdir;
607ebfedea0SLionel Sambuc else
608ebfedea0SLionel Sambuc readdirfunc = (struct dirent *(*)(void *))readdir;
609ebfedea0SLionel Sambuc while ((dp = (*readdirfunc)(dirp))) {
610ebfedea0SLionel Sambuc u_char *sc;
611ebfedea0SLionel Sambuc Char *dc;
612ebfedea0SLionel Sambuc
613ebfedea0SLionel Sambuc /* Initial CHAR_DOT must be matched literally. */
614ebfedea0SLionel Sambuc if (dp->d_name[0] == CHAR_DOT && *pattern != CHAR_DOT)
615ebfedea0SLionel Sambuc continue;
616ebfedea0SLionel Sambuc for (sc = (u_char *) dp->d_name, dc = pathend;
617ebfedea0SLionel Sambuc (*dc++ = *sc++) != CHAR_EOS;)
618ebfedea0SLionel Sambuc continue;
619ebfedea0SLionel Sambuc if (!match(pathend, pattern, restpattern)) {
620ebfedea0SLionel Sambuc *pathend = CHAR_EOS;
621ebfedea0SLionel Sambuc continue;
622ebfedea0SLionel Sambuc }
623ebfedea0SLionel Sambuc err = glob2(pathbuf, --dc, restpattern, pglob, limit);
624ebfedea0SLionel Sambuc if (err)
625ebfedea0SLionel Sambuc break;
626ebfedea0SLionel Sambuc }
627ebfedea0SLionel Sambuc
628ebfedea0SLionel Sambuc if (pglob->gl_flags & GLOB_ALTDIRFUNC)
629ebfedea0SLionel Sambuc (*pglob->gl_closedir)(dirp);
630ebfedea0SLionel Sambuc else
631ebfedea0SLionel Sambuc closedir(dirp);
632ebfedea0SLionel Sambuc return(err);
633ebfedea0SLionel Sambuc }
634ebfedea0SLionel Sambuc
635ebfedea0SLionel Sambuc
636ebfedea0SLionel Sambuc /*
637ebfedea0SLionel Sambuc * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
638ebfedea0SLionel Sambuc * add the new item, and update gl_pathc.
639ebfedea0SLionel Sambuc *
640ebfedea0SLionel Sambuc * This assumes the BSD realloc, which only copies the block when its size
641ebfedea0SLionel Sambuc * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
642ebfedea0SLionel Sambuc * behavior.
643ebfedea0SLionel Sambuc *
644ebfedea0SLionel Sambuc * Return 0 if new item added, error code if memory couldn't be allocated.
645ebfedea0SLionel Sambuc *
646ebfedea0SLionel Sambuc * Invariant of the glob_t structure:
647ebfedea0SLionel Sambuc * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
648ebfedea0SLionel Sambuc * gl_pathv points to (gl_offs + gl_pathc + 1) items.
649ebfedea0SLionel Sambuc */
650ebfedea0SLionel Sambuc static int
globextend(const Char * path,glob_t * pglob,size_t * limit)651ebfedea0SLionel Sambuc globextend(const Char *path, glob_t *pglob, size_t *limit)
652ebfedea0SLionel Sambuc {
653ebfedea0SLionel Sambuc char **pathv;
654ebfedea0SLionel Sambuc int i;
655ebfedea0SLionel Sambuc size_t newsize, len;
656ebfedea0SLionel Sambuc char *copy;
657ebfedea0SLionel Sambuc const Char *p;
658ebfedea0SLionel Sambuc
659ebfedea0SLionel Sambuc newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
660ebfedea0SLionel Sambuc pathv = pglob->gl_pathv ?
661ebfedea0SLionel Sambuc realloc(pglob->gl_pathv, newsize) :
662ebfedea0SLionel Sambuc malloc(newsize);
663ebfedea0SLionel Sambuc if (pathv == NULL)
664ebfedea0SLionel Sambuc return(GLOB_NOSPACE);
665ebfedea0SLionel Sambuc
666ebfedea0SLionel Sambuc if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
667ebfedea0SLionel Sambuc /* first time around -- clear initial gl_offs items */
668ebfedea0SLionel Sambuc pathv += pglob->gl_offs;
669ebfedea0SLionel Sambuc for (i = pglob->gl_offs; --i >= 0; )
670ebfedea0SLionel Sambuc *--pathv = NULL;
671ebfedea0SLionel Sambuc }
672ebfedea0SLionel Sambuc pglob->gl_pathv = pathv;
673ebfedea0SLionel Sambuc
674ebfedea0SLionel Sambuc for (p = path; *p++;)
675ebfedea0SLionel Sambuc continue;
676ebfedea0SLionel Sambuc len = (size_t)(p - path);
677ebfedea0SLionel Sambuc *limit += len;
678ebfedea0SLionel Sambuc if ((copy = malloc(len)) != NULL) {
679ebfedea0SLionel Sambuc g_Ctoc(path, copy);
680ebfedea0SLionel Sambuc pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
681ebfedea0SLionel Sambuc }
682ebfedea0SLionel Sambuc pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
683ebfedea0SLionel Sambuc
684ebfedea0SLionel Sambuc if ((pglob->gl_flags & GLOB_LIMIT) && (newsize + *limit) >= ARG_MAX) {
685ebfedea0SLionel Sambuc errno = 0;
686ebfedea0SLionel Sambuc return(GLOB_NOSPACE);
687ebfedea0SLionel Sambuc }
688ebfedea0SLionel Sambuc
689ebfedea0SLionel Sambuc return(copy == NULL ? GLOB_NOSPACE : 0);
690ebfedea0SLionel Sambuc }
691ebfedea0SLionel Sambuc
692ebfedea0SLionel Sambuc
693ebfedea0SLionel Sambuc /*
694ebfedea0SLionel Sambuc * pattern matching function for filenames. Each occurrence of the *
695ebfedea0SLionel Sambuc * pattern causes a recursion level.
696ebfedea0SLionel Sambuc */
697ebfedea0SLionel Sambuc static int
match(Char * name,Char * pat,Char * patend)698ebfedea0SLionel Sambuc match(Char *name, Char *pat, Char *patend)
699ebfedea0SLionel Sambuc {
700ebfedea0SLionel Sambuc int ok, negate_range;
701ebfedea0SLionel Sambuc Char c, k;
702ebfedea0SLionel Sambuc
703ebfedea0SLionel Sambuc while (pat < patend) {
704ebfedea0SLionel Sambuc c = *pat++;
705ebfedea0SLionel Sambuc switch (c & M_MASK) {
706ebfedea0SLionel Sambuc case M_ALL:
707ebfedea0SLionel Sambuc if (pat == patend)
708ebfedea0SLionel Sambuc return(1);
709ebfedea0SLionel Sambuc do
710ebfedea0SLionel Sambuc if (match(name, pat, patend))
711ebfedea0SLionel Sambuc return(1);
712ebfedea0SLionel Sambuc while (*name++ != CHAR_EOS);
713ebfedea0SLionel Sambuc return(0);
714ebfedea0SLionel Sambuc case M_ONE:
715ebfedea0SLionel Sambuc if (*name++ == CHAR_EOS)
716ebfedea0SLionel Sambuc return(0);
717ebfedea0SLionel Sambuc break;
718ebfedea0SLionel Sambuc case M_SET:
719ebfedea0SLionel Sambuc ok = 0;
720ebfedea0SLionel Sambuc if ((k = *name++) == CHAR_EOS)
721ebfedea0SLionel Sambuc return(0);
722ebfedea0SLionel Sambuc if ((negate_range = ((*pat & M_MASK) == M_NOT)) != CHAR_EOS)
723ebfedea0SLionel Sambuc ++pat;
724ebfedea0SLionel Sambuc while (((c = *pat++) & M_MASK) != M_END)
725ebfedea0SLionel Sambuc if ((*pat & M_MASK) == M_RNG) {
726ebfedea0SLionel Sambuc if (c <= k && k <= pat[1])
727ebfedea0SLionel Sambuc ok = 1;
728ebfedea0SLionel Sambuc pat += 2;
729ebfedea0SLionel Sambuc } else if (c == k)
730ebfedea0SLionel Sambuc ok = 1;
731ebfedea0SLionel Sambuc if (ok == negate_range)
732ebfedea0SLionel Sambuc return(0);
733ebfedea0SLionel Sambuc break;
734ebfedea0SLionel Sambuc default:
735ebfedea0SLionel Sambuc if (*name++ != c)
736ebfedea0SLionel Sambuc return(0);
737ebfedea0SLionel Sambuc break;
738ebfedea0SLionel Sambuc }
739ebfedea0SLionel Sambuc }
740ebfedea0SLionel Sambuc return(*name == CHAR_EOS);
741ebfedea0SLionel Sambuc }
742ebfedea0SLionel Sambuc
743ebfedea0SLionel Sambuc /* Free allocated data belonging to a glob_t structure. */
744ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
globfree(glob_t * pglob)745ebfedea0SLionel Sambuc globfree(glob_t *pglob)
746ebfedea0SLionel Sambuc {
747ebfedea0SLionel Sambuc int i;
748ebfedea0SLionel Sambuc char **pp;
749ebfedea0SLionel Sambuc
750ebfedea0SLionel Sambuc if (pglob->gl_pathv != NULL) {
751ebfedea0SLionel Sambuc pp = pglob->gl_pathv + pglob->gl_offs;
752ebfedea0SLionel Sambuc for (i = pglob->gl_pathc; i--; ++pp)
753ebfedea0SLionel Sambuc if (*pp)
754ebfedea0SLionel Sambuc free(*pp);
755ebfedea0SLionel Sambuc free(pglob->gl_pathv);
756ebfedea0SLionel Sambuc pglob->gl_pathv = NULL;
757ebfedea0SLionel Sambuc }
758ebfedea0SLionel Sambuc }
759ebfedea0SLionel Sambuc
760ebfedea0SLionel Sambuc static DIR *
g_opendir(Char * str,glob_t * pglob)761ebfedea0SLionel Sambuc g_opendir(Char *str, glob_t *pglob)
762ebfedea0SLionel Sambuc {
763ebfedea0SLionel Sambuc char buf[MaxPathLen];
764ebfedea0SLionel Sambuc
765ebfedea0SLionel Sambuc if (!*str)
766ebfedea0SLionel Sambuc strlcpy(buf, ".", sizeof(buf));
767ebfedea0SLionel Sambuc else
768ebfedea0SLionel Sambuc g_Ctoc(str, buf);
769ebfedea0SLionel Sambuc
770ebfedea0SLionel Sambuc if (pglob->gl_flags & GLOB_ALTDIRFUNC)
771ebfedea0SLionel Sambuc return((*pglob->gl_opendir)(buf));
772ebfedea0SLionel Sambuc
773ebfedea0SLionel Sambuc return(opendir(buf));
774ebfedea0SLionel Sambuc }
775ebfedea0SLionel Sambuc
776ebfedea0SLionel Sambuc static int
g_lstat(Char * fn,struct stat * sb,glob_t * pglob)777ebfedea0SLionel Sambuc g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
778ebfedea0SLionel Sambuc {
779ebfedea0SLionel Sambuc char buf[MaxPathLen];
780ebfedea0SLionel Sambuc
781ebfedea0SLionel Sambuc g_Ctoc(fn, buf);
782ebfedea0SLionel Sambuc if (pglob->gl_flags & GLOB_ALTDIRFUNC)
783ebfedea0SLionel Sambuc return((*pglob->gl_lstat)(buf, sb));
784ebfedea0SLionel Sambuc return(lstat(buf, sb));
785ebfedea0SLionel Sambuc }
786ebfedea0SLionel Sambuc
787ebfedea0SLionel Sambuc static int
g_stat(Char * fn,struct stat * sb,glob_t * pglob)788ebfedea0SLionel Sambuc g_stat(Char *fn, struct stat *sb, glob_t *pglob)
789ebfedea0SLionel Sambuc {
790ebfedea0SLionel Sambuc char buf[MaxPathLen];
791ebfedea0SLionel Sambuc
792ebfedea0SLionel Sambuc g_Ctoc(fn, buf);
793ebfedea0SLionel Sambuc if (pglob->gl_flags & GLOB_ALTDIRFUNC)
794ebfedea0SLionel Sambuc return((*pglob->gl_stat)(buf, sb));
795ebfedea0SLionel Sambuc return(stat(buf, sb));
796ebfedea0SLionel Sambuc }
797ebfedea0SLionel Sambuc
798ebfedea0SLionel Sambuc static Char *
g_strchr(const Char * str,int ch)799ebfedea0SLionel Sambuc g_strchr(const Char *str, int ch)
800ebfedea0SLionel Sambuc {
801ebfedea0SLionel Sambuc do {
802ebfedea0SLionel Sambuc if (*str == ch)
803ebfedea0SLionel Sambuc return (Char *)str;
804ebfedea0SLionel Sambuc } while (*str++);
805ebfedea0SLionel Sambuc return (NULL);
806ebfedea0SLionel Sambuc }
807ebfedea0SLionel Sambuc
808ebfedea0SLionel Sambuc #ifdef notdef
809ebfedea0SLionel Sambuc static Char *
g_strcat(Char * dst,const Char * src)810ebfedea0SLionel Sambuc g_strcat(Char *dst, const Char *src)
811ebfedea0SLionel Sambuc {
812ebfedea0SLionel Sambuc Char *sdst = dst;
813ebfedea0SLionel Sambuc
814ebfedea0SLionel Sambuc while (*dst++)
815ebfedea0SLionel Sambuc continue;
816ebfedea0SLionel Sambuc --dst;
817ebfedea0SLionel Sambuc while((*dst++ = *src++) != CHAR_EOS)
818ebfedea0SLionel Sambuc continue;
819ebfedea0SLionel Sambuc
820ebfedea0SLionel Sambuc return (sdst);
821ebfedea0SLionel Sambuc }
822ebfedea0SLionel Sambuc #endif
823ebfedea0SLionel Sambuc
824ebfedea0SLionel Sambuc static void
g_Ctoc(const Char * str,char * buf)825ebfedea0SLionel Sambuc g_Ctoc(const Char *str, char *buf)
826ebfedea0SLionel Sambuc {
827ebfedea0SLionel Sambuc char *dc;
828ebfedea0SLionel Sambuc
829ebfedea0SLionel Sambuc for (dc = buf; (*dc++ = *str++) != CHAR_EOS;)
830ebfedea0SLionel Sambuc continue;
831ebfedea0SLionel Sambuc }
832ebfedea0SLionel Sambuc
833ebfedea0SLionel Sambuc #ifdef DEBUG
834ebfedea0SLionel Sambuc static void
qprintf(const Char * str,Char * s)835ebfedea0SLionel Sambuc qprintf(const Char *str, Char *s)
836ebfedea0SLionel Sambuc {
837ebfedea0SLionel Sambuc Char *p;
838ebfedea0SLionel Sambuc
839ebfedea0SLionel Sambuc printf("%s:\n", str);
840ebfedea0SLionel Sambuc for (p = s; *p; p++)
841ebfedea0SLionel Sambuc printf("%c", CHAR(*p));
842ebfedea0SLionel Sambuc printf("\n");
843ebfedea0SLionel Sambuc for (p = s; *p; p++)
844ebfedea0SLionel Sambuc printf("%c", *p & M_PROTECT ? '"' : ' ');
845ebfedea0SLionel Sambuc printf("\n");
846ebfedea0SLionel Sambuc for (p = s; *p; p++)
847ebfedea0SLionel Sambuc printf("%c", ismeta(*p) ? '_' : ' ');
848ebfedea0SLionel Sambuc printf("\n");
849ebfedea0SLionel Sambuc }
850ebfedea0SLionel Sambuc #endif
851