15af32e75SAxel Dörfler /*
25af32e75SAxel Dörfler * Copyright (c) 1989, 1993
35af32e75SAxel Dörfler * The Regents of the University of California. All rights reserved.
45af32e75SAxel Dörfler *
55af32e75SAxel Dörfler * This code is derived from software contributed to Berkeley by
65af32e75SAxel Dörfler * Guido van Rossum.
75af32e75SAxel Dörfler *
85af32e75SAxel Dörfler * Redistribution and use in source and binary forms, with or without
95af32e75SAxel Dörfler * modification, are permitted provided that the following conditions
105af32e75SAxel Dörfler * are met:
115af32e75SAxel Dörfler * 1. Redistributions of source code must retain the above copyright
125af32e75SAxel Dörfler * notice, this list of conditions and the following disclaimer.
135af32e75SAxel Dörfler * 2. Redistributions in binary form must reproduce the above copyright
145af32e75SAxel Dörfler * notice, this list of conditions and the following disclaimer in the
155af32e75SAxel Dörfler * documentation and/or other materials provided with the distribution.
165af32e75SAxel Dörfler * 4. Neither the name of the University nor the names of its contributors
175af32e75SAxel Dörfler * may be used to endorse or promote products derived from this software
185af32e75SAxel Dörfler * without specific prior written permission.
195af32e75SAxel Dörfler *
205af32e75SAxel Dörfler * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
215af32e75SAxel Dörfler * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
225af32e75SAxel Dörfler * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
235af32e75SAxel Dörfler * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
245af32e75SAxel Dörfler * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
255af32e75SAxel Dörfler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
265af32e75SAxel Dörfler * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
275af32e75SAxel Dörfler * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
285af32e75SAxel Dörfler * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
295af32e75SAxel Dörfler * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
305af32e75SAxel Dörfler * SUCH DAMAGE.
315af32e75SAxel Dörfler */
325af32e75SAxel Dörfler
336e036f04SAxel Dörfler #if defined(LIBC_SCCS) && !defined(lint)
346e036f04SAxel Dörfler static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
356e036f04SAxel Dörfler #endif /* LIBC_SCCS and not lint */
366e036f04SAxel Dörfler #include <sys/cdefs.h>
376e036f04SAxel Dörfler //__FBSDID("$FreeBSD: src/lib/libc/gen/glob.c,v 1.27 2008/06/26 07:12:35 mtm Exp $");
386e036f04SAxel Dörfler
395af32e75SAxel Dörfler /*
405af32e75SAxel Dörfler * glob(3) -- a superset of the one defined in POSIX 1003.2.
415af32e75SAxel Dörfler *
425af32e75SAxel Dörfler * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
435af32e75SAxel Dörfler *
445af32e75SAxel Dörfler * Optional extra services, controlled by flags not defined by POSIX:
455af32e75SAxel Dörfler *
465af32e75SAxel Dörfler * GLOB_QUOTE:
475af32e75SAxel Dörfler * Escaping convention: \ inhibits any special meaning the following
485af32e75SAxel Dörfler * character might have (except \ at end of string is retained).
495af32e75SAxel Dörfler * GLOB_MAGCHAR:
505af32e75SAxel Dörfler * Set in gl_flags if pattern contained a globbing character.
515af32e75SAxel Dörfler * GLOB_NOMAGIC:
525af32e75SAxel Dörfler * Same as GLOB_NOCHECK, but it will only append pattern if it did
535af32e75SAxel Dörfler * not contain any magic characters. [Used in csh style globbing]
545af32e75SAxel Dörfler * GLOB_ALTDIRFUNC:
555af32e75SAxel Dörfler * Use alternately specified directory access functions.
565af32e75SAxel Dörfler * GLOB_TILDE:
575af32e75SAxel Dörfler * expand ~user/foo to the /home/dir/of/user/foo
585af32e75SAxel Dörfler * GLOB_BRACE:
595af32e75SAxel Dörfler * expand {1,2}{a,b} to 1a 1b 2a 2b
605af32e75SAxel Dörfler * gl_matchc:
615af32e75SAxel Dörfler * Number of matches in the current invocation of glob.
625af32e75SAxel Dörfler */
635af32e75SAxel Dörfler
646e036f04SAxel Dörfler /*
656e036f04SAxel Dörfler * Some notes on multibyte character support:
666e036f04SAxel Dörfler * 1. Patterns with illegal byte sequences match nothing - even if
676e036f04SAxel Dörfler * GLOB_NOCHECK is specified.
686e036f04SAxel Dörfler * 2. Illegal byte sequences in filenames are handled by treating them as
696e036f04SAxel Dörfler * single-byte characters with a value of the first byte of the sequence
706e036f04SAxel Dörfler * cast to wchar_t.
716e036f04SAxel Dörfler * 3. State-dependent encodings are not currently supported.
726e036f04SAxel Dörfler */
736e036f04SAxel Dörfler
745af32e75SAxel Dörfler #include <sys/param.h>
755af32e75SAxel Dörfler #include <sys/stat.h>
765af32e75SAxel Dörfler
775af32e75SAxel Dörfler #include <ctype.h>
785af32e75SAxel Dörfler #include <dirent.h>
795af32e75SAxel Dörfler #include <errno.h>
805af32e75SAxel Dörfler #include <glob.h>
816e036f04SAxel Dörfler #include <limits.h>
825af32e75SAxel Dörfler #include <pwd.h>
836e036f04SAxel Dörfler #include <stdint.h>
845af32e75SAxel Dörfler #include <stdio.h>
855af32e75SAxel Dörfler #include <stdlib.h>
865af32e75SAxel Dörfler #include <string.h>
875af32e75SAxel Dörfler #include <unistd.h>
885af32e75SAxel Dörfler
89ae901935SOliver Tappe #include <errno_private.h>
90*5de93d5bSOliver Tappe #include <wchar_private.h>
91ae901935SOliver Tappe
926e036f04SAxel Dörfler #ifndef __HAIKU__
936e036f04SAxel Dörfler # include "collate.h"
946e036f04SAxel Dörfler #endif
955af32e75SAxel Dörfler
965af32e75SAxel Dörfler #define DOLLAR '$'
975af32e75SAxel Dörfler #define DOT '.'
985af32e75SAxel Dörfler #define EOS '\0'
995af32e75SAxel Dörfler #define LBRACKET '['
1005af32e75SAxel Dörfler #define NOT '!'
1015af32e75SAxel Dörfler #define QUESTION '?'
1025af32e75SAxel Dörfler #define QUOTE '\\'
1035af32e75SAxel Dörfler #define RANGE '-'
1045af32e75SAxel Dörfler #define RBRACKET ']'
1055af32e75SAxel Dörfler #define SEP '/'
1065af32e75SAxel Dörfler #define STAR '*'
1075af32e75SAxel Dörfler #define TILDE '~'
1085af32e75SAxel Dörfler #define UNDERSCORE '_'
1095af32e75SAxel Dörfler #define LBRACE '{'
1105af32e75SAxel Dörfler #define RBRACE '}'
1115af32e75SAxel Dörfler #define SLASH '/'
1125af32e75SAxel Dörfler #define COMMA ','
1135af32e75SAxel Dörfler
11411dbc6cfSStefano Ceccherini #ifndef DEBUG
1155af32e75SAxel Dörfler
1166e036f04SAxel Dörfler #define M_QUOTE 0x8000000000ULL
1176e036f04SAxel Dörfler #define M_PROTECT 0x4000000000ULL
1186e036f04SAxel Dörfler #define M_MASK 0xffffffffffULL
1196e036f04SAxel Dörfler #define M_CHAR 0x00ffffffffULL
1205af32e75SAxel Dörfler
1216e036f04SAxel Dörfler typedef uint_fast64_t Char;
1225af32e75SAxel Dörfler
1235af32e75SAxel Dörfler #else
1245af32e75SAxel Dörfler
1255af32e75SAxel Dörfler #define M_QUOTE 0x80
1265af32e75SAxel Dörfler #define M_PROTECT 0x40
1275af32e75SAxel Dörfler #define M_MASK 0xff
1286e036f04SAxel Dörfler #define M_CHAR 0x7f
1295af32e75SAxel Dörfler
1305af32e75SAxel Dörfler typedef char Char;
1315af32e75SAxel Dörfler
1325af32e75SAxel Dörfler #endif
1335af32e75SAxel Dörfler
1345af32e75SAxel Dörfler
1356e036f04SAxel Dörfler #define CHAR(c) ((Char)((c)&M_CHAR))
1365af32e75SAxel Dörfler #define META(c) ((Char)((c)|M_QUOTE))
1375af32e75SAxel Dörfler #define M_ALL META('*')
1385af32e75SAxel Dörfler #define M_END META(']')
1395af32e75SAxel Dörfler #define M_NOT META('!')
1405af32e75SAxel Dörfler #define M_ONE META('?')
1415af32e75SAxel Dörfler #define M_RNG META('-')
1425af32e75SAxel Dörfler #define M_SET META('[')
1435af32e75SAxel Dörfler #define ismeta(c) (((c)&M_QUOTE) != 0)
1445af32e75SAxel Dörfler
1455af32e75SAxel Dörfler
1465af32e75SAxel Dörfler static int compare(const void *, const void *);
1476e036f04SAxel Dörfler static int g_Ctoc(const Char *, char *, size_t);
1485af32e75SAxel Dörfler static int g_lstat(Char *, struct stat *, glob_t *);
1495af32e75SAxel Dörfler static DIR *g_opendir(Char *, glob_t *);
1506e036f04SAxel Dörfler static const Char *g_strchr(const Char *, wchar_t);
1515af32e75SAxel Dörfler #ifdef notdef
1525af32e75SAxel Dörfler static Char *g_strcat(Char *, const Char *);
1535af32e75SAxel Dörfler #endif
1545af32e75SAxel Dörfler static int g_stat(Char *, struct stat *, glob_t *);
1556e036f04SAxel Dörfler static int glob0(const Char *, glob_t *, size_t *);
1566e036f04SAxel Dörfler static int glob1(Char *, glob_t *, size_t *);
1576e036f04SAxel Dörfler static int glob2(Char *, Char *, Char *, Char *, glob_t *, size_t *);
1586e036f04SAxel Dörfler static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, size_t *);
1596e036f04SAxel Dörfler static int globextend(const Char *, glob_t *, size_t *);
1605af32e75SAxel Dörfler static const Char *
1615af32e75SAxel Dörfler globtilde(const Char *, Char *, size_t, glob_t *);
1626e036f04SAxel Dörfler static int globexp1(const Char *, glob_t *, size_t *);
1636e036f04SAxel Dörfler static int globexp2(const Char *, const Char *, glob_t *, int *, size_t *);
1645af32e75SAxel Dörfler static int match(Char *, Char *, Char *);
1655af32e75SAxel Dörfler #ifdef DEBUG
1665af32e75SAxel Dörfler static void qprintf(const char *, Char *);
1675af32e75SAxel Dörfler #endif
1685af32e75SAxel Dörfler
1695074333dSAxel Dörfler
1705074333dSAxel Dörfler
1715af32e75SAxel Dörfler int
glob(const char * pattern,int flags,int (* errfunc)(const char *,int),glob_t * pglob)1725af32e75SAxel Dörfler glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob)
1735af32e75SAxel Dörfler {
1746e036f04SAxel Dörfler const char *patnext;
1756e036f04SAxel Dörfler size_t limit;
1766e036f04SAxel Dörfler Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
1776e036f04SAxel Dörfler mbstate_t mbs;
1786e036f04SAxel Dörfler wchar_t wc;
1796e036f04SAxel Dörfler size_t clen;
1805af32e75SAxel Dörfler
1816e036f04SAxel Dörfler patnext = pattern;
1825af32e75SAxel Dörfler if (!(flags & GLOB_APPEND)) {
1835af32e75SAxel Dörfler pglob->gl_pathc = 0;
1845af32e75SAxel Dörfler pglob->gl_pathv = NULL;
1855af32e75SAxel Dörfler if (!(flags & GLOB_DOOFFS))
1865af32e75SAxel Dörfler pglob->gl_offs = 0;
1875af32e75SAxel Dörfler }
1885af32e75SAxel Dörfler if (flags & GLOB_LIMIT) {
1895af32e75SAxel Dörfler limit = pglob->gl_matchc;
1905af32e75SAxel Dörfler if (limit == 0)
1915af32e75SAxel Dörfler limit = ARG_MAX;
1925af32e75SAxel Dörfler } else
1935af32e75SAxel Dörfler limit = 0;
1945af32e75SAxel Dörfler pglob->gl_flags = flags & ~GLOB_MAGCHAR;
1955af32e75SAxel Dörfler pglob->gl_errfunc = errfunc;
1965af32e75SAxel Dörfler pglob->gl_matchc = 0;
1975af32e75SAxel Dörfler
1985af32e75SAxel Dörfler bufnext = patbuf;
1995af32e75SAxel Dörfler bufend = bufnext + MAXPATHLEN - 1;
2005af32e75SAxel Dörfler if (flags & GLOB_NOESCAPE) {
2016e036f04SAxel Dörfler memset(&mbs, 0, sizeof(mbs));
2026e036f04SAxel Dörfler while (bufend - bufnext >= MB_CUR_MAX) {
203*5de93d5bSOliver Tappe clen = __mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
2046e036f04SAxel Dörfler if (clen == (size_t)-1 || clen == (size_t)-2)
2056e036f04SAxel Dörfler return (GLOB_NOMATCH);
2066e036f04SAxel Dörfler else if (clen == 0)
2076e036f04SAxel Dörfler break;
2086e036f04SAxel Dörfler *bufnext++ = wc;
2096e036f04SAxel Dörfler patnext += clen;
2106e036f04SAxel Dörfler }
2115af32e75SAxel Dörfler } else {
2125af32e75SAxel Dörfler /* Protect the quoted characters. */
2136e036f04SAxel Dörfler memset(&mbs, 0, sizeof(mbs));
2146e036f04SAxel Dörfler while (bufend - bufnext >= MB_CUR_MAX) {
2156e036f04SAxel Dörfler if (*patnext == QUOTE) {
2166e036f04SAxel Dörfler if (*++patnext == EOS) {
2176e036f04SAxel Dörfler *bufnext++ = QUOTE | M_PROTECT;
2186e036f04SAxel Dörfler continue;
2195af32e75SAxel Dörfler }
2206e036f04SAxel Dörfler prot = M_PROTECT;
2216e036f04SAxel Dörfler } else
2226e036f04SAxel Dörfler prot = 0;
223*5de93d5bSOliver Tappe clen = __mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
2246e036f04SAxel Dörfler if (clen == (size_t)-1 || clen == (size_t)-2)
2256e036f04SAxel Dörfler return (GLOB_NOMATCH);
2266e036f04SAxel Dörfler else if (clen == 0)
2276e036f04SAxel Dörfler break;
2286e036f04SAxel Dörfler *bufnext++ = wc | prot;
2296e036f04SAxel Dörfler patnext += clen;
2305af32e75SAxel Dörfler }
2315af32e75SAxel Dörfler }
2325af32e75SAxel Dörfler *bufnext = EOS;
2335af32e75SAxel Dörfler
2345af32e75SAxel Dörfler if (flags & GLOB_BRACE)
2355af32e75SAxel Dörfler return globexp1(patbuf, pglob, &limit);
2366e036f04SAxel Dörfler else
2375af32e75SAxel Dörfler return glob0(patbuf, pglob, &limit);
2385af32e75SAxel Dörfler }
2395af32e75SAxel Dörfler
2405af32e75SAxel Dörfler /*
2415af32e75SAxel Dörfler * Expand recursively a glob {} pattern. When there is no more expansion
2425af32e75SAxel Dörfler * invoke the standard globbing routine to glob the rest of the magic
2435af32e75SAxel Dörfler * characters
2445af32e75SAxel Dörfler */
2455af32e75SAxel Dörfler static int
globexp1(const Char * pattern,glob_t * pglob,size_t * limit)2466e036f04SAxel Dörfler globexp1(const Char *pattern, glob_t *pglob, size_t *limit)
2475af32e75SAxel Dörfler {
2485af32e75SAxel Dörfler const Char* ptr = pattern;
2495af32e75SAxel Dörfler int rv;
2505af32e75SAxel Dörfler
2515af32e75SAxel Dörfler /* Protect a single {}, for find(1), like csh */
2525af32e75SAxel Dörfler if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
2535af32e75SAxel Dörfler return glob0(pattern, pglob, limit);
2545af32e75SAxel Dörfler
2556e036f04SAxel Dörfler while ((ptr = g_strchr(ptr, LBRACE)) != NULL)
2565af32e75SAxel Dörfler if (!globexp2(ptr, pattern, pglob, &rv, limit))
2575af32e75SAxel Dörfler return rv;
2585af32e75SAxel Dörfler
2595af32e75SAxel Dörfler return glob0(pattern, pglob, limit);
2605af32e75SAxel Dörfler }
2615af32e75SAxel Dörfler
2625af32e75SAxel Dörfler
2635af32e75SAxel Dörfler /*
2645af32e75SAxel Dörfler * Recursive brace globbing helper. Tries to expand a single brace.
2655af32e75SAxel Dörfler * If it succeeds then it invokes globexp1 with the new pattern.
2665af32e75SAxel Dörfler * If it fails then it tries to glob the rest of the pattern and returns.
2675af32e75SAxel Dörfler */
2685af32e75SAxel Dörfler static int
globexp2(const Char * ptr,const Char * pattern,glob_t * pglob,int * rv,size_t * limit)2696e036f04SAxel Dörfler globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, size_t *limit)
2705af32e75SAxel Dörfler {
2715af32e75SAxel Dörfler int i;
2725af32e75SAxel Dörfler Char *lm, *ls;
2736e036f04SAxel Dörfler const Char *pe, *pm, *pm1, *pl;
2745af32e75SAxel Dörfler Char patbuf[MAXPATHLEN];
2755af32e75SAxel Dörfler
2765af32e75SAxel Dörfler /* copy part up to the brace */
2775af32e75SAxel Dörfler for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
2785af32e75SAxel Dörfler continue;
2795af32e75SAxel Dörfler *lm = EOS;
2805af32e75SAxel Dörfler ls = lm;
2815af32e75SAxel Dörfler
2825af32e75SAxel Dörfler /* Find the balanced brace */
2836e036f04SAxel Dörfler for (i = 0, pe = ++ptr; *pe; pe++)
2845af32e75SAxel Dörfler if (*pe == LBRACKET) {
2855af32e75SAxel Dörfler /* Ignore everything between [] */
2865af32e75SAxel Dörfler for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
2875af32e75SAxel Dörfler continue;
2885af32e75SAxel Dörfler if (*pe == EOS) {
2895af32e75SAxel Dörfler /*
2905af32e75SAxel Dörfler * We could not find a matching RBRACKET.
2915af32e75SAxel Dörfler * Ignore and just look for RBRACE
2925af32e75SAxel Dörfler */
2935af32e75SAxel Dörfler pe = pm;
2945af32e75SAxel Dörfler }
2955af32e75SAxel Dörfler }
2965af32e75SAxel Dörfler else if (*pe == LBRACE)
2975af32e75SAxel Dörfler i++;
2985af32e75SAxel Dörfler else if (*pe == RBRACE) {
2995af32e75SAxel Dörfler if (i == 0)
3005af32e75SAxel Dörfler break;
3015af32e75SAxel Dörfler i--;
3025af32e75SAxel Dörfler }
3035af32e75SAxel Dörfler
3045af32e75SAxel Dörfler /* Non matching braces; just glob the pattern */
3055af32e75SAxel Dörfler if (i != 0 || *pe == EOS) {
3065af32e75SAxel Dörfler *rv = glob0(patbuf, pglob, limit);
3075af32e75SAxel Dörfler return 0;
3085af32e75SAxel Dörfler }
3095af32e75SAxel Dörfler
3106e036f04SAxel Dörfler for (i = 0, pl = pm = ptr; pm <= pe; pm++)
3115af32e75SAxel Dörfler switch (*pm) {
3125af32e75SAxel Dörfler case LBRACKET:
3135af32e75SAxel Dörfler /* Ignore everything between [] */
3146e036f04SAxel Dörfler for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
3155af32e75SAxel Dörfler continue;
3165af32e75SAxel Dörfler if (*pm == EOS) {
3175af32e75SAxel Dörfler /*
3185af32e75SAxel Dörfler * We could not find a matching RBRACKET.
3195af32e75SAxel Dörfler * Ignore and just look for RBRACE
3205af32e75SAxel Dörfler */
3216e036f04SAxel Dörfler pm = pm1;
3225af32e75SAxel Dörfler }
3235af32e75SAxel Dörfler break;
3245af32e75SAxel Dörfler
3255af32e75SAxel Dörfler case LBRACE:
3265af32e75SAxel Dörfler i++;
3275af32e75SAxel Dörfler break;
3285af32e75SAxel Dörfler
3295af32e75SAxel Dörfler case RBRACE:
3305af32e75SAxel Dörfler if (i) {
3315af32e75SAxel Dörfler i--;
3325af32e75SAxel Dörfler break;
3335af32e75SAxel Dörfler }
3345af32e75SAxel Dörfler /* FALLTHROUGH */
3355af32e75SAxel Dörfler case COMMA:
3365af32e75SAxel Dörfler if (i && *pm == COMMA)
3375af32e75SAxel Dörfler break;
3385af32e75SAxel Dörfler else {
3395af32e75SAxel Dörfler /* Append the current string */
3405af32e75SAxel Dörfler for (lm = ls; (pl < pm); *lm++ = *pl++)
3415af32e75SAxel Dörfler continue;
3425af32e75SAxel Dörfler /*
3435af32e75SAxel Dörfler * Append the rest of the pattern after the
3445af32e75SAxel Dörfler * closing brace
3455af32e75SAxel Dörfler */
3465af32e75SAxel Dörfler for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
3475af32e75SAxel Dörfler continue;
3485af32e75SAxel Dörfler
3495af32e75SAxel Dörfler /* Expand the current pattern */
3505af32e75SAxel Dörfler #ifdef DEBUG
3515af32e75SAxel Dörfler qprintf("globexp2:", patbuf);
3525af32e75SAxel Dörfler #endif
3535af32e75SAxel Dörfler *rv = globexp1(patbuf, pglob, limit);
3545af32e75SAxel Dörfler
3555af32e75SAxel Dörfler /* move after the comma, to the next string */
3565af32e75SAxel Dörfler pl = pm + 1;
3575af32e75SAxel Dörfler }
3585af32e75SAxel Dörfler break;
3595af32e75SAxel Dörfler
3605af32e75SAxel Dörfler default:
3615af32e75SAxel Dörfler break;
3625af32e75SAxel Dörfler }
3635af32e75SAxel Dörfler *rv = 0;
3645af32e75SAxel Dörfler return 0;
3655af32e75SAxel Dörfler }
3665af32e75SAxel Dörfler
3675af32e75SAxel Dörfler
3686e036f04SAxel Dörfler
3695af32e75SAxel Dörfler /*
3705af32e75SAxel Dörfler * expand tilde from the passwd file.
3715af32e75SAxel Dörfler */
3725af32e75SAxel Dörfler static const Char *
globtilde(const Char * pattern,Char * patbuf,size_t patbuf_len,glob_t * pglob)3735af32e75SAxel Dörfler globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
3745af32e75SAxel Dörfler {
3755af32e75SAxel Dörfler struct passwd *pwd;
3765af32e75SAxel Dörfler char *h;
3775af32e75SAxel Dörfler const Char *p;
3785af32e75SAxel Dörfler Char *b, *eb;
3795af32e75SAxel Dörfler
3805af32e75SAxel Dörfler if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
3815af32e75SAxel Dörfler return pattern;
3825af32e75SAxel Dörfler
3835af32e75SAxel Dörfler /*
3845af32e75SAxel Dörfler * Copy up to the end of the string or /
3855af32e75SAxel Dörfler */
3865af32e75SAxel Dörfler eb = &patbuf[patbuf_len - 1];
3875af32e75SAxel Dörfler for (p = pattern + 1, h = (char *) patbuf;
3885af32e75SAxel Dörfler h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
3895af32e75SAxel Dörfler continue;
3905af32e75SAxel Dörfler
3915af32e75SAxel Dörfler *h = EOS;
3925af32e75SAxel Dörfler
3935af32e75SAxel Dörfler if (((char *) patbuf)[0] == EOS) {
3945af32e75SAxel Dörfler /*
3955af32e75SAxel Dörfler * handle a plain ~ or ~/ by expanding $HOME first (iff
3965af32e75SAxel Dörfler * we're not running setuid or setgid) and then trying
3975af32e75SAxel Dörfler * the password file
3985af32e75SAxel Dörfler */
3995af32e75SAxel Dörfler if (
4006e036f04SAxel Dörfler #ifndef __HAIKU__
4016e036f04SAxel Dörfler issetugid() != 0 ||
4026e036f04SAxel Dörfler #endif
4035af32e75SAxel Dörfler (h = getenv("HOME")) == NULL) {
4045af32e75SAxel Dörfler if (((h = getlogin()) != NULL &&
4055af32e75SAxel Dörfler (pwd = getpwnam(h)) != NULL) ||
4065af32e75SAxel Dörfler (pwd = getpwuid(getuid())) != NULL)
4075af32e75SAxel Dörfler h = pwd->pw_dir;
4085af32e75SAxel Dörfler else
4095af32e75SAxel Dörfler return pattern;
4105af32e75SAxel Dörfler }
4115af32e75SAxel Dörfler }
4125af32e75SAxel Dörfler else {
4135af32e75SAxel Dörfler /*
4145af32e75SAxel Dörfler * Expand a ~user
4155af32e75SAxel Dörfler */
4165af32e75SAxel Dörfler if ((pwd = getpwnam((char*) patbuf)) == NULL)
4175af32e75SAxel Dörfler return pattern;
4185af32e75SAxel Dörfler else
4195af32e75SAxel Dörfler h = pwd->pw_dir;
4205af32e75SAxel Dörfler }
4215af32e75SAxel Dörfler
4225af32e75SAxel Dörfler /* Copy the home directory */
4235af32e75SAxel Dörfler for (b = patbuf; b < eb && *h; *b++ = *h++)
4245af32e75SAxel Dörfler continue;
4255af32e75SAxel Dörfler
4265af32e75SAxel Dörfler /* Append the rest of the pattern */
4275af32e75SAxel Dörfler while (b < eb && (*b++ = *p++) != EOS)
4285af32e75SAxel Dörfler continue;
4295af32e75SAxel Dörfler *b = EOS;
4305af32e75SAxel Dörfler
4315af32e75SAxel Dörfler return patbuf;
4325af32e75SAxel Dörfler }
4335af32e75SAxel Dörfler
4345af32e75SAxel Dörfler
4355af32e75SAxel Dörfler /*
4365af32e75SAxel Dörfler * The main glob() routine: compiles the pattern (optionally processing
4375af32e75SAxel Dörfler * quotes), calls glob1() to do the real pattern matching, and finally
4385af32e75SAxel Dörfler * sorts the list (unless unsorted operation is requested). Returns 0
4395af32e75SAxel Dörfler * if things went well, nonzero if errors occurred.
4405af32e75SAxel Dörfler */
4415af32e75SAxel Dörfler static int
glob0(const Char * pattern,glob_t * pglob,size_t * limit)4426e036f04SAxel Dörfler glob0(const Char *pattern, glob_t *pglob, size_t *limit)
4435af32e75SAxel Dörfler {
4445af32e75SAxel Dörfler const Char *qpatnext;
4456e036f04SAxel Dörfler int c, err;
4466e036f04SAxel Dörfler size_t oldpathc;
4475af32e75SAxel Dörfler Char *bufnext, patbuf[MAXPATHLEN];
4485af32e75SAxel Dörfler
4495af32e75SAxel Dörfler qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
4505af32e75SAxel Dörfler oldpathc = pglob->gl_pathc;
4515af32e75SAxel Dörfler bufnext = patbuf;
4525af32e75SAxel Dörfler
4535af32e75SAxel Dörfler /* We don't need to check for buffer overflow any more. */
4545af32e75SAxel Dörfler while ((c = *qpatnext++) != EOS) {
4555af32e75SAxel Dörfler switch (c) {
4565af32e75SAxel Dörfler case LBRACKET:
4575af32e75SAxel Dörfler c = *qpatnext;
4585af32e75SAxel Dörfler if (c == NOT)
4595af32e75SAxel Dörfler ++qpatnext;
4605af32e75SAxel Dörfler if (*qpatnext == EOS ||
4616e036f04SAxel Dörfler g_strchr(qpatnext+1, RBRACKET) == NULL) {
4625af32e75SAxel Dörfler *bufnext++ = LBRACKET;
4635af32e75SAxel Dörfler if (c == NOT)
4645af32e75SAxel Dörfler --qpatnext;
4655af32e75SAxel Dörfler break;
4665af32e75SAxel Dörfler }
4675af32e75SAxel Dörfler *bufnext++ = M_SET;
4685af32e75SAxel Dörfler if (c == NOT)
4695af32e75SAxel Dörfler *bufnext++ = M_NOT;
4705af32e75SAxel Dörfler c = *qpatnext++;
4715af32e75SAxel Dörfler do {
4725af32e75SAxel Dörfler *bufnext++ = CHAR(c);
4735af32e75SAxel Dörfler if (*qpatnext == RANGE &&
4745af32e75SAxel Dörfler (c = qpatnext[1]) != RBRACKET) {
4755af32e75SAxel Dörfler *bufnext++ = M_RNG;
4765af32e75SAxel Dörfler *bufnext++ = CHAR(c);
4775af32e75SAxel Dörfler qpatnext += 2;
4785af32e75SAxel Dörfler }
4795af32e75SAxel Dörfler } while ((c = *qpatnext++) != RBRACKET);
4805af32e75SAxel Dörfler pglob->gl_flags |= GLOB_MAGCHAR;
4815af32e75SAxel Dörfler *bufnext++ = M_END;
4825af32e75SAxel Dörfler break;
4835af32e75SAxel Dörfler case QUESTION:
4845af32e75SAxel Dörfler pglob->gl_flags |= GLOB_MAGCHAR;
4855af32e75SAxel Dörfler *bufnext++ = M_ONE;
4865af32e75SAxel Dörfler break;
4875af32e75SAxel Dörfler case STAR:
4885af32e75SAxel Dörfler pglob->gl_flags |= GLOB_MAGCHAR;
4895af32e75SAxel Dörfler /* collapse adjacent stars to one,
4905af32e75SAxel Dörfler * to avoid exponential behavior
4915af32e75SAxel Dörfler */
4925af32e75SAxel Dörfler if (bufnext == patbuf || bufnext[-1] != M_ALL)
4935af32e75SAxel Dörfler *bufnext++ = M_ALL;
4945af32e75SAxel Dörfler break;
4955af32e75SAxel Dörfler default:
4965af32e75SAxel Dörfler *bufnext++ = CHAR(c);
4975af32e75SAxel Dörfler break;
4985af32e75SAxel Dörfler }
4995af32e75SAxel Dörfler }
5005af32e75SAxel Dörfler *bufnext = EOS;
5015af32e75SAxel Dörfler #ifdef DEBUG
5025af32e75SAxel Dörfler qprintf("glob0:", patbuf);
5035af32e75SAxel Dörfler #endif
5045af32e75SAxel Dörfler
5055af32e75SAxel Dörfler if ((err = glob1(patbuf, pglob, limit)) != 0)
5065af32e75SAxel Dörfler return(err);
5075af32e75SAxel Dörfler
5085af32e75SAxel Dörfler /*
5095af32e75SAxel Dörfler * If there was no match we are going to append the pattern
5105af32e75SAxel Dörfler * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
5115af32e75SAxel Dörfler * and the pattern did not contain any magic characters
5125af32e75SAxel Dörfler * GLOB_NOMAGIC is there just for compatibility with csh.
5135af32e75SAxel Dörfler */
5145af32e75SAxel Dörfler if (pglob->gl_pathc == oldpathc) {
5155af32e75SAxel Dörfler if (((pglob->gl_flags & GLOB_NOCHECK) ||
5165af32e75SAxel Dörfler ((pglob->gl_flags & GLOB_NOMAGIC) &&
5175af32e75SAxel Dörfler !(pglob->gl_flags & GLOB_MAGCHAR))))
5186e036f04SAxel Dörfler return(globextend(pattern, pglob, limit));
5195af32e75SAxel Dörfler else
5206e036f04SAxel Dörfler return(GLOB_NOMATCH);
5215af32e75SAxel Dörfler }
5225af32e75SAxel Dörfler if (!(pglob->gl_flags & GLOB_NOSORT))
5235af32e75SAxel Dörfler qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
5245af32e75SAxel Dörfler pglob->gl_pathc - oldpathc, sizeof(char *), compare);
5256e036f04SAxel Dörfler return(0);
5265af32e75SAxel Dörfler }
5275af32e75SAxel Dörfler
5285af32e75SAxel Dörfler static int
compare(const void * p,const void * q)5295af32e75SAxel Dörfler compare(const void *p, const void *q)
5305af32e75SAxel Dörfler {
5316e036f04SAxel Dörfler return(strcmp(*(char **)p, *(char **)q));
5325af32e75SAxel Dörfler }
5335af32e75SAxel Dörfler
5345af32e75SAxel Dörfler static int
glob1(Char * pattern,glob_t * pglob,size_t * limit)5356e036f04SAxel Dörfler glob1(Char *pattern, glob_t *pglob, size_t *limit)
5365af32e75SAxel Dörfler {
5375af32e75SAxel Dörfler Char pathbuf[MAXPATHLEN];
5385af32e75SAxel Dörfler
5395af32e75SAxel Dörfler /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
5405af32e75SAxel Dörfler if (*pattern == EOS)
5416e036f04SAxel Dörfler return(0);
5426e036f04SAxel Dörfler return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
5436e036f04SAxel Dörfler pattern, pglob, limit));
5445af32e75SAxel Dörfler }
5455af32e75SAxel Dörfler
5465af32e75SAxel Dörfler /*
5475af32e75SAxel Dörfler * The functions glob2 and glob3 are mutually recursive; there is one level
5485af32e75SAxel Dörfler * of recursion for each segment in the pattern that contains one or more
5495af32e75SAxel Dörfler * meta characters.
5505af32e75SAxel Dörfler */
5515af32e75SAxel Dörfler static int
glob2(Char * pathbuf,Char * pathend,Char * pathend_last,Char * pattern,glob_t * pglob,size_t * limit)5526e036f04SAxel Dörfler glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
5536e036f04SAxel Dörfler glob_t *pglob, size_t *limit)
5545af32e75SAxel Dörfler {
5555af32e75SAxel Dörfler struct stat sb;
5565af32e75SAxel Dörfler Char *p, *q;
5575af32e75SAxel Dörfler int anymeta;
5585af32e75SAxel Dörfler
5595af32e75SAxel Dörfler /*
5605af32e75SAxel Dörfler * Loop over pattern segments until end of pattern or until
5615af32e75SAxel Dörfler * segment with meta character found.
5625af32e75SAxel Dörfler */
5635af32e75SAxel Dörfler for (anymeta = 0;;) {
5645af32e75SAxel Dörfler if (*pattern == EOS) { /* End of pattern? */
5655af32e75SAxel Dörfler *pathend = EOS;
5665af32e75SAxel Dörfler if (g_lstat(pathbuf, &sb, pglob))
5675af32e75SAxel Dörfler return(0);
5685af32e75SAxel Dörfler
5695af32e75SAxel Dörfler if (((pglob->gl_flags & GLOB_MARK) &&
5705af32e75SAxel Dörfler pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
5715af32e75SAxel Dörfler || (S_ISLNK(sb.st_mode) &&
5725af32e75SAxel Dörfler (g_stat(pathbuf, &sb, pglob) == 0) &&
5735af32e75SAxel Dörfler S_ISDIR(sb.st_mode)))) {
5745af32e75SAxel Dörfler if (pathend + 1 > pathend_last)
5756e036f04SAxel Dörfler return (GLOB_ABORTED);
5765af32e75SAxel Dörfler *pathend++ = SEP;
5775af32e75SAxel Dörfler *pathend = EOS;
5785af32e75SAxel Dörfler }
5795af32e75SAxel Dörfler ++pglob->gl_matchc;
5806e036f04SAxel Dörfler return(globextend(pathbuf, pglob, limit));
5815af32e75SAxel Dörfler }
5825af32e75SAxel Dörfler
5835af32e75SAxel Dörfler /* Find end of next segment, copy tentatively to pathend. */
5845af32e75SAxel Dörfler q = pathend;
5855af32e75SAxel Dörfler p = pattern;
5865af32e75SAxel Dörfler while (*p != EOS && *p != SEP) {
5875af32e75SAxel Dörfler if (ismeta(*p))
5885af32e75SAxel Dörfler anymeta = 1;
5895af32e75SAxel Dörfler if (q + 1 > pathend_last)
5906e036f04SAxel Dörfler return (GLOB_ABORTED);
5915af32e75SAxel Dörfler *q++ = *p++;
5925af32e75SAxel Dörfler }
5935af32e75SAxel Dörfler
5945af32e75SAxel Dörfler if (!anymeta) { /* No expansion, do next segment. */
5955af32e75SAxel Dörfler pathend = q;
5965af32e75SAxel Dörfler pattern = p;
5975af32e75SAxel Dörfler while (*pattern == SEP) {
5985af32e75SAxel Dörfler if (pathend + 1 > pathend_last)
5996e036f04SAxel Dörfler return (GLOB_ABORTED);
6005af32e75SAxel Dörfler *pathend++ = *pattern++;
6015af32e75SAxel Dörfler }
6025af32e75SAxel Dörfler } else /* Need expansion, recurse. */
6036e036f04SAxel Dörfler return(glob3(pathbuf, pathend, pathend_last, pattern, p,
6046e036f04SAxel Dörfler pglob, limit));
6055af32e75SAxel Dörfler }
6065af32e75SAxel Dörfler /* NOTREACHED */
6075af32e75SAxel Dörfler }
6085af32e75SAxel Dörfler
6095af32e75SAxel Dörfler static int
glob3(Char * pathbuf,Char * pathend,Char * pathend_last,Char * pattern,Char * restpattern,glob_t * pglob,size_t * limit)6106e036f04SAxel Dörfler glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
6116e036f04SAxel Dörfler Char *pattern, Char *restpattern,
6126e036f04SAxel Dörfler glob_t *pglob, size_t *limit)
6135af32e75SAxel Dörfler {
6145af32e75SAxel Dörfler struct dirent *dp;
6155af32e75SAxel Dörfler DIR *dirp;
6165af32e75SAxel Dörfler int err;
6175af32e75SAxel Dörfler char buf[MAXPATHLEN];
6185af32e75SAxel Dörfler
6195af32e75SAxel Dörfler /*
6205af32e75SAxel Dörfler * The readdirfunc declaration can't be prototyped, because it is
6215af32e75SAxel Dörfler * assigned, below, to two functions which are prototyped in glob.h
6225af32e75SAxel Dörfler * and dirent.h as taking pointers to differently typed opaque
6235af32e75SAxel Dörfler * structures.
6245af32e75SAxel Dörfler */
6255af32e75SAxel Dörfler struct dirent *(*readdirfunc)();
6265af32e75SAxel Dörfler
6275af32e75SAxel Dörfler if (pathend > pathend_last)
6286e036f04SAxel Dörfler return (GLOB_ABORTED);
6295af32e75SAxel Dörfler *pathend = EOS;
630ae901935SOliver Tappe __set_errno(0);
6315af32e75SAxel Dörfler
6325af32e75SAxel Dörfler if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
6335af32e75SAxel Dörfler /* TODO: don't call for ENOENT or ENOTDIR? */
6345af32e75SAxel Dörfler if (pglob->gl_errfunc) {
6355af32e75SAxel Dörfler if (g_Ctoc(pathbuf, buf, sizeof(buf)))
6366e036f04SAxel Dörfler return (GLOB_ABORTED);
6375af32e75SAxel Dörfler if (pglob->gl_errfunc(buf, errno) ||
6385af32e75SAxel Dörfler pglob->gl_flags & GLOB_ERR)
6396e036f04SAxel Dörfler return (GLOB_ABORTED);
6405af32e75SAxel Dörfler }
6416e036f04SAxel Dörfler return(0);
6425af32e75SAxel Dörfler }
6435af32e75SAxel Dörfler
6445af32e75SAxel Dörfler err = 0;
6455af32e75SAxel Dörfler
6465af32e75SAxel Dörfler /* Search directory for matching names. */
6475af32e75SAxel Dörfler if (pglob->gl_flags & GLOB_ALTDIRFUNC)
6485af32e75SAxel Dörfler readdirfunc = pglob->gl_readdir;
6495af32e75SAxel Dörfler else
6505af32e75SAxel Dörfler readdirfunc = readdir;
6515af32e75SAxel Dörfler while ((dp = (*readdirfunc)(dirp))) {
6526e036f04SAxel Dörfler char *sc;
6535af32e75SAxel Dörfler Char *dc;
6546e036f04SAxel Dörfler wchar_t wc;
6556e036f04SAxel Dörfler size_t clen;
6566e036f04SAxel Dörfler mbstate_t mbs;
6575af32e75SAxel Dörfler
6585af32e75SAxel Dörfler /* Initial DOT must be matched literally. */
6595af32e75SAxel Dörfler if (dp->d_name[0] == DOT && *pattern != DOT)
6605af32e75SAxel Dörfler continue;
6616e036f04SAxel Dörfler memset(&mbs, 0, sizeof(mbs));
6625af32e75SAxel Dörfler dc = pathend;
6636e036f04SAxel Dörfler sc = dp->d_name;
6646e036f04SAxel Dörfler while (dc < pathend_last) {
665*5de93d5bSOliver Tappe clen = __mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
6666e036f04SAxel Dörfler if (clen == (size_t)-1 || clen == (size_t)-2) {
6676e036f04SAxel Dörfler wc = *sc;
6686e036f04SAxel Dörfler clen = 1;
6696e036f04SAxel Dörfler memset(&mbs, 0, sizeof(mbs));
6706e036f04SAxel Dörfler }
6716e036f04SAxel Dörfler if ((*dc++ = wc) == EOS)
6726e036f04SAxel Dörfler break;
6736e036f04SAxel Dörfler sc += clen;
6746e036f04SAxel Dörfler }
6755af32e75SAxel Dörfler if (!match(pathend, pattern, restpattern)) {
6765af32e75SAxel Dörfler *pathend = EOS;
6775af32e75SAxel Dörfler continue;
6785af32e75SAxel Dörfler }
6795af32e75SAxel Dörfler err = glob2(pathbuf, --dc, pathend_last, restpattern,
6805af32e75SAxel Dörfler pglob, limit);
6815af32e75SAxel Dörfler if (err)
6825af32e75SAxel Dörfler break;
6835af32e75SAxel Dörfler }
6845af32e75SAxel Dörfler
6855af32e75SAxel Dörfler if (pglob->gl_flags & GLOB_ALTDIRFUNC)
6865af32e75SAxel Dörfler (*pglob->gl_closedir)(dirp);
6875af32e75SAxel Dörfler else
6885af32e75SAxel Dörfler closedir(dirp);
6896e036f04SAxel Dörfler return(err);
6905af32e75SAxel Dörfler }
6915af32e75SAxel Dörfler
6925af32e75SAxel Dörfler
6935af32e75SAxel Dörfler /*
6945af32e75SAxel Dörfler * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
6955af32e75SAxel Dörfler * add the new item, and update gl_pathc.
6965af32e75SAxel Dörfler *
6975af32e75SAxel Dörfler * This assumes the BSD realloc, which only copies the block when its size
6985af32e75SAxel Dörfler * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
6995af32e75SAxel Dörfler * behavior.
7005af32e75SAxel Dörfler *
7015af32e75SAxel Dörfler * Return 0 if new item added, error code if memory couldn't be allocated.
7025af32e75SAxel Dörfler *
7035af32e75SAxel Dörfler * Invariant of the glob_t structure:
7045af32e75SAxel Dörfler * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
7055af32e75SAxel Dörfler * gl_pathv points to (gl_offs + gl_pathc + 1) items.
7065af32e75SAxel Dörfler */
7075af32e75SAxel Dörfler static int
globextend(const Char * path,glob_t * pglob,size_t * limit)7086e036f04SAxel Dörfler globextend(const Char *path, glob_t *pglob, size_t *limit)
7095af32e75SAxel Dörfler {
7105af32e75SAxel Dörfler char **pathv;
7116e036f04SAxel Dörfler size_t i, newsize, len;
7125af32e75SAxel Dörfler char *copy;
7135af32e75SAxel Dörfler const Char *p;
7145af32e75SAxel Dörfler
7155af32e75SAxel Dörfler if (*limit && pglob->gl_pathc > *limit) {
716ae901935SOliver Tappe __set_errno(0);
7176e036f04SAxel Dörfler return (GLOB_NOSPACE);
7185af32e75SAxel Dörfler }
7195af32e75SAxel Dörfler
7205af32e75SAxel Dörfler newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
7215af32e75SAxel Dörfler pathv = pglob->gl_pathv ?
7225af32e75SAxel Dörfler realloc((char *)pglob->gl_pathv, newsize) :
7235af32e75SAxel Dörfler malloc(newsize);
7245af32e75SAxel Dörfler if (pathv == NULL) {
7255af32e75SAxel Dörfler if (pglob->gl_pathv) {
7265af32e75SAxel Dörfler free(pglob->gl_pathv);
7275af32e75SAxel Dörfler pglob->gl_pathv = NULL;
7285af32e75SAxel Dörfler }
7296e036f04SAxel Dörfler return(GLOB_NOSPACE);
7305af32e75SAxel Dörfler }
7315af32e75SAxel Dörfler
7325af32e75SAxel Dörfler if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
7335af32e75SAxel Dörfler /* first time around -- clear initial gl_offs items */
7345af32e75SAxel Dörfler pathv += pglob->gl_offs;
7356e036f04SAxel Dörfler for (i = pglob->gl_offs + 1; --i > 0; )
7365af32e75SAxel Dörfler *--pathv = NULL;
7375af32e75SAxel Dörfler }
7385af32e75SAxel Dörfler pglob->gl_pathv = pathv;
7395af32e75SAxel Dörfler
7405af32e75SAxel Dörfler for (p = path; *p++;)
7415af32e75SAxel Dörfler continue;
7426e036f04SAxel Dörfler len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
7435af32e75SAxel Dörfler if ((copy = malloc(len)) != NULL) {
7445af32e75SAxel Dörfler if (g_Ctoc(path, copy, len)) {
7455af32e75SAxel Dörfler free(copy);
7466e036f04SAxel Dörfler return (GLOB_NOSPACE);
7475af32e75SAxel Dörfler }
7485af32e75SAxel Dörfler pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
7495af32e75SAxel Dörfler }
7505af32e75SAxel Dörfler pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
7516e036f04SAxel Dörfler return(copy == NULL ? GLOB_NOSPACE : 0);
7525af32e75SAxel Dörfler }
7535af32e75SAxel Dörfler
7545af32e75SAxel Dörfler /*
7555af32e75SAxel Dörfler * pattern matching function for filenames. Each occurrence of the *
7565af32e75SAxel Dörfler * pattern causes a recursion level.
7575af32e75SAxel Dörfler */
7585af32e75SAxel Dörfler static int
match(Char * name,Char * pat,Char * patend)7595af32e75SAxel Dörfler match(Char *name, Char *pat, Char *patend)
7605af32e75SAxel Dörfler {
7615af32e75SAxel Dörfler int ok, negate_range;
7625af32e75SAxel Dörfler Char c, k;
7635af32e75SAxel Dörfler
7645af32e75SAxel Dörfler while (pat < patend) {
7655af32e75SAxel Dörfler c = *pat++;
7665af32e75SAxel Dörfler switch (c & M_MASK) {
7675af32e75SAxel Dörfler case M_ALL:
7685af32e75SAxel Dörfler if (pat == patend)
7696e036f04SAxel Dörfler return(1);
7705af32e75SAxel Dörfler do
7715af32e75SAxel Dörfler if (match(name, pat, patend))
7726e036f04SAxel Dörfler return(1);
7735af32e75SAxel Dörfler while (*name++ != EOS);
7746e036f04SAxel Dörfler return(0);
7755af32e75SAxel Dörfler case M_ONE:
7765af32e75SAxel Dörfler if (*name++ == EOS)
7776e036f04SAxel Dörfler return(0);
7785af32e75SAxel Dörfler break;
7795af32e75SAxel Dörfler case M_SET:
7805af32e75SAxel Dörfler ok = 0;
7815af32e75SAxel Dörfler if ((k = *name++) == EOS)
7826e036f04SAxel Dörfler return(0);
7835af32e75SAxel Dörfler if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
7845af32e75SAxel Dörfler ++pat;
7855af32e75SAxel Dörfler while (((c = *pat++) & M_MASK) != M_END)
7865af32e75SAxel Dörfler if ((*pat & M_MASK) == M_RNG) {
7875af32e75SAxel Dörfler if (
7886e036f04SAxel Dörfler #ifndef __HAIKU__
7896e036f04SAxel Dörfler __collate_load_error ?
7906e036f04SAxel Dörfler #endif
7915af32e75SAxel Dörfler CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1])
7926e036f04SAxel Dörfler #ifndef __HAIKU__
7936e036f04SAxel Dörfler :
7946e036f04SAxel Dörfler __collate_range_cmp(CHAR(c), CHAR(k)) <= 0
7956e036f04SAxel Dörfler && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0
7966e036f04SAxel Dörfler #endif
7975af32e75SAxel Dörfler )
7985af32e75SAxel Dörfler ok = 1;
7995af32e75SAxel Dörfler pat += 2;
8005af32e75SAxel Dörfler } else if (c == k)
8015af32e75SAxel Dörfler ok = 1;
8025af32e75SAxel Dörfler if (ok == negate_range)
8036e036f04SAxel Dörfler return(0);
8045af32e75SAxel Dörfler break;
8055af32e75SAxel Dörfler default:
8065af32e75SAxel Dörfler if (*name++ != c)
8076e036f04SAxel Dörfler return(0);
8085af32e75SAxel Dörfler break;
8095af32e75SAxel Dörfler }
8105af32e75SAxel Dörfler }
8116e036f04SAxel Dörfler return(*name == EOS);
8125af32e75SAxel Dörfler }
8135af32e75SAxel Dörfler
8145af32e75SAxel Dörfler /* Free allocated data belonging to a glob_t structure. */
8155af32e75SAxel Dörfler void
globfree(glob_t * pglob)8165af32e75SAxel Dörfler globfree(glob_t *pglob)
8175af32e75SAxel Dörfler {
8186e036f04SAxel Dörfler size_t i;
8195af32e75SAxel Dörfler char **pp;
8205af32e75SAxel Dörfler
8215af32e75SAxel Dörfler if (pglob->gl_pathv != NULL) {
8225af32e75SAxel Dörfler pp = pglob->gl_pathv + pglob->gl_offs;
8235af32e75SAxel Dörfler for (i = pglob->gl_pathc; i--; ++pp)
8245af32e75SAxel Dörfler if (*pp)
8255af32e75SAxel Dörfler free(*pp);
8265af32e75SAxel Dörfler free(pglob->gl_pathv);
8275af32e75SAxel Dörfler pglob->gl_pathv = NULL;
8285af32e75SAxel Dörfler }
8295af32e75SAxel Dörfler }
8305af32e75SAxel Dörfler
8315af32e75SAxel Dörfler static DIR *
g_opendir(Char * str,glob_t * pglob)8325af32e75SAxel Dörfler g_opendir(Char *str, glob_t *pglob)
8335af32e75SAxel Dörfler {
8345af32e75SAxel Dörfler char buf[MAXPATHLEN];
8355af32e75SAxel Dörfler
8365af32e75SAxel Dörfler if (!*str)
8375af32e75SAxel Dörfler strcpy(buf, ".");
8385af32e75SAxel Dörfler else {
8395af32e75SAxel Dörfler if (g_Ctoc(str, buf, sizeof(buf)))
8406e036f04SAxel Dörfler return (NULL);
8415af32e75SAxel Dörfler }
8425af32e75SAxel Dörfler
8435af32e75SAxel Dörfler if (pglob->gl_flags & GLOB_ALTDIRFUNC)
8446e036f04SAxel Dörfler return((*pglob->gl_opendir)(buf));
8455af32e75SAxel Dörfler
8466e036f04SAxel Dörfler return(opendir(buf));
8475af32e75SAxel Dörfler }
8485af32e75SAxel Dörfler
8495af32e75SAxel Dörfler static int
g_lstat(Char * fn,struct stat * sb,glob_t * pglob)8505af32e75SAxel Dörfler g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
8515af32e75SAxel Dörfler {
8525af32e75SAxel Dörfler char buf[MAXPATHLEN];
8535af32e75SAxel Dörfler
8545af32e75SAxel Dörfler if (g_Ctoc(fn, buf, sizeof(buf))) {
855ae901935SOliver Tappe __set_errno(ENAMETOOLONG);
8566e036f04SAxel Dörfler return (-1);
8575af32e75SAxel Dörfler }
8585af32e75SAxel Dörfler if (pglob->gl_flags & GLOB_ALTDIRFUNC)
8596e036f04SAxel Dörfler return((*pglob->gl_lstat)(buf, sb));
8606e036f04SAxel Dörfler return(lstat(buf, sb));
8615af32e75SAxel Dörfler }
8625af32e75SAxel Dörfler
8635af32e75SAxel Dörfler static int
g_stat(Char * fn,struct stat * sb,glob_t * pglob)8645af32e75SAxel Dörfler g_stat(Char *fn, struct stat *sb, glob_t *pglob)
8655af32e75SAxel Dörfler {
8665af32e75SAxel Dörfler char buf[MAXPATHLEN];
8675af32e75SAxel Dörfler
8685af32e75SAxel Dörfler if (g_Ctoc(fn, buf, sizeof(buf))) {
869ae901935SOliver Tappe __set_errno(ENAMETOOLONG);
8706e036f04SAxel Dörfler return (-1);
8715af32e75SAxel Dörfler }
8725af32e75SAxel Dörfler if (pglob->gl_flags & GLOB_ALTDIRFUNC)
8736e036f04SAxel Dörfler return((*pglob->gl_stat)(buf, sb));
8746e036f04SAxel Dörfler return(stat(buf, sb));
8755af32e75SAxel Dörfler }
8765af32e75SAxel Dörfler
8776e036f04SAxel Dörfler static const Char *
g_strchr(const Char * str,wchar_t ch)8786e036f04SAxel Dörfler g_strchr(const Char *str, wchar_t ch)
8795af32e75SAxel Dörfler {
8806e036f04SAxel Dörfler
8815af32e75SAxel Dörfler do {
8825af32e75SAxel Dörfler if (*str == ch)
8836e036f04SAxel Dörfler return (str);
8845af32e75SAxel Dörfler } while (*str++);
8856e036f04SAxel Dörfler return (NULL);
8865af32e75SAxel Dörfler }
8875af32e75SAxel Dörfler
8885af32e75SAxel Dörfler static int
g_Ctoc(const Char * str,char * buf,size_t len)8896e036f04SAxel Dörfler g_Ctoc(const Char *str, char *buf, size_t len)
8905af32e75SAxel Dörfler {
8916e036f04SAxel Dörfler mbstate_t mbs;
8926e036f04SAxel Dörfler size_t clen;
8935af32e75SAxel Dörfler
8946e036f04SAxel Dörfler memset(&mbs, 0, sizeof(mbs));
8956e036f04SAxel Dörfler while (len >= MB_CUR_MAX) {
896*5de93d5bSOliver Tappe clen = __wcrtomb(buf, *str, &mbs);
8976e036f04SAxel Dörfler if (clen == (size_t)-1)
8986e036f04SAxel Dörfler return (1);
8996e036f04SAxel Dörfler if (*str == L'\0')
9006e036f04SAxel Dörfler return (0);
9016e036f04SAxel Dörfler str++;
9026e036f04SAxel Dörfler buf += clen;
9036e036f04SAxel Dörfler len -= clen;
9045af32e75SAxel Dörfler }
9056e036f04SAxel Dörfler return (1);
9065af32e75SAxel Dörfler }
9075af32e75SAxel Dörfler
9085af32e75SAxel Dörfler #ifdef DEBUG
9095af32e75SAxel Dörfler static void
qprintf(const char * str,Char * s)9105af32e75SAxel Dörfler qprintf(const char *str, Char *s)
9115af32e75SAxel Dörfler {
9125af32e75SAxel Dörfler Char *p;
9135af32e75SAxel Dörfler
9146e036f04SAxel Dörfler (void)printf("%s:\n", str);
9155af32e75SAxel Dörfler for (p = s; *p; p++)
9166e036f04SAxel Dörfler (void)printf("%c", CHAR(*p));
9176e036f04SAxel Dörfler (void)printf("\n");
9185af32e75SAxel Dörfler for (p = s; *p; p++)
9196e036f04SAxel Dörfler (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
9206e036f04SAxel Dörfler (void)printf("\n");
9215af32e75SAxel Dörfler for (p = s; *p; p++)
9226e036f04SAxel Dörfler (void)printf("%c", ismeta(*p) ? '_' : ' ');
9236e036f04SAxel Dörfler (void)printf("\n");
9245af32e75SAxel Dörfler }
9255af32e75SAxel Dörfler #endif
926