OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libc/] [posix/] [glob.c] - Blame information for rev 816

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.