(linenum→info "unix/slp.c:2238")

glibc/2.7/posix/glob.c

    1: /* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006, 2007
    2:    Free Software Foundation, Inc.
    3:    This file is part of the GNU C Library.
    4: 
    5:    The GNU C Library is free software; you can redistribute it and/or
    6:    modify it under the terms of the GNU Lesser General Public
    7:    License as published by the Free Software Foundation; either
    8:    version 2.1 of the License, or (at your option) any later version.
    9: 
   10:    The GNU C Library is distributed in the hope that it will be useful,
   11:    but WITHOUT ANY WARRANTY; without even the implied warranty of
   12:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13:    Lesser General Public License for more details.
   14: 
   15:    You should have received a copy of the GNU Lesser General Public
   16:    License along with the GNU C Library; if not, write to the Free
   17:    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   18:    02111-1307 USA.  */
   19: 
   20: #ifdef  HAVE_CONFIG_H
   21: # include <config.h>
   22: #endif
   23: 
   24: #include <glob.h>
   25: 
   26: #include <errno.h>
   27: #include <sys/types.h>
   28: #include <sys/stat.h>
   29: #include <stddef.h>
   30: 
   31: /* Outcomment the following line for production quality code.  */
   32: /* #define NDEBUG 1 */
   33: #include <assert.h>
   34: 
   35: #include <stdio.h>              /* Needed on stupid SunOS for assert.  */
   36: 
   37: #if !defined _LIBC || !defined GLOB_ONLY_P
   38: #if defined HAVE_UNISTD_H || defined _LIBC
   39: # include <unistd.h>
   40: # ifndef POSIX
   41: #  ifdef _POSIX_VERSION
   42: #   define POSIX
   43: #  endif
   44: # endif
   45: #endif
   46: 
   47: #include <pwd.h>
   48: 
   49: #include <errno.h>
   50: #ifndef __set_errno
   51: # define __set_errno(val) errno = (val)
   52: #endif
   53: 
   54: #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
   55: # include <dirent.h>
   56: # define NAMLEN(dirent) strlen((dirent)->d_name)
   57: #else
   58: # define dirent direct
   59: # define NAMLEN(dirent) (dirent)->d_namlen
   60: # ifdef HAVE_SYS_NDIR_H
   61: #  include <sys/ndir.h>
   62: # endif
   63: # ifdef HAVE_SYS_DIR_H
   64: #  include <sys/dir.h>
   65: # endif
   66: # ifdef HAVE_NDIR_H
   67: #  include <ndir.h>
   68: # endif
   69: # ifdef HAVE_VMSDIR_H
   70: #  include "vmsdir.h"
   71: # endif /* HAVE_VMSDIR_H */
   72: #endif
   73: 
   74: 
   75: /* In GNU systems, <dirent.h> defines this macro for us.  */
   76: #ifdef _D_NAMLEN
   77: # undef NAMLEN
   78: # define NAMLEN(d) _D_NAMLEN(d)
   79: #endif
   80: 
   81: /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
   82:    if the `d_type' member for `struct dirent' is available.
   83:    HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB.  */
   84: #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
   85: /* True if the directory entry D must be of type T.  */
   86: # define DIRENT_MUST_BE(d, t)   ((d)->d_type == (t))
   87: 
   88: /* True if the directory entry D might be a symbolic link.  */
   89: # define DIRENT_MIGHT_BE_SYMLINK(d) \
   90:     ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
   91: 
   92: /* True if the directory entry D might be a directory.  */
   93: # define DIRENT_MIGHT_BE_DIR(d)  \
   94:     ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
   95: 
   96: #else /* !HAVE_D_TYPE */
   97: # define DIRENT_MUST_BE(d, t)           false
   98: # define DIRENT_MIGHT_BE_SYMLINK(d)     true
   99: # define DIRENT_MIGHT_BE_DIR(d)         true
  100: #endif /* HAVE_D_TYPE */
  101: 
  102: /* If the system has the `struct dirent64' type we use it internally.  */
  103: #if defined _LIBC && !defined COMPILE_GLOB64
  104: # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
  105: #  define CONVERT_D_NAMLEN(d64, d32)
  106: # else
  107: #  define CONVERT_D_NAMLEN(d64, d32) \
  108:   (d64)->d_namlen = (d32)->d_namlen;
  109: # endif
  110: 
  111: # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
  112: #  define CONVERT_D_INO(d64, d32)
  113: # else
  114: #  define CONVERT_D_INO(d64, d32) \
  115:   (d64)->d_ino = (d32)->d_ino;
  116: # endif
  117: 
  118: # ifdef _DIRENT_HAVE_D_TYPE
  119: #  define CONVERT_D_TYPE(d64, d32) \
  120:   (d64)->d_type = (d32)->d_type;
  121: # else
  122: #  define CONVERT_D_TYPE(d64, d32)
  123: # endif
  124: 
  125: # define CONVERT_DIRENT_DIRENT64(d64, d32) \
  126:   memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1);                    \
  127:   CONVERT_D_NAMLEN (d64, d32)                                                 \
  128:   CONVERT_D_INO (d64, d32)                                                    \
  129:   CONVERT_D_TYPE (d64, d32)
  130: #endif
  131: 
  132: 
  133: #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
  134: /* Posix does not require that the d_ino field be present, and some
  135:    systems do not provide it. */
  136: # define REAL_DIR_ENTRY(dp) 1
  137: #else
  138: # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
  139: #endif /* POSIX */
  140: 
  141: #include <stdlib.h>
  142: #include <string.h>
  143: 
  144: /* NAME_MAX is usually defined in <dirent.h> or <limits.h>.  */
  145: #include <limits.h>
  146: #ifndef NAME_MAX
  147: # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
  148: #endif
  149: 
  150: #include <alloca.h>
  151: 
  152: #ifdef _LIBC
  153: # undef strdup
  154: # define strdup(str) __strdup (str)
  155: # define sysconf(id) __sysconf (id)
  156: # define closedir(dir) __closedir (dir)
  157: # define opendir(name) __opendir (name)
  158: # define readdir(str) __readdir64 (str)
  159: # define getpwnam_r(name, bufp, buf, len, res) \
  160:    __getpwnam_r (name, bufp, buf, len, res)
  161: # ifndef __stat64
  162: #  define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
  163: # endif
  164: # define struct_stat64          struct stat64
  165: #else /* !_LIBC */
  166: # include "getlogin_r.h"
  167: # include "mempcpy.h"
  168: # include "stat-macros.h"
  169: # include "strdup.h"
  170: # define __stat64(fname, buf)   stat (fname, buf)
  171: # define struct_stat64          struct stat
  172: # define __stat(fname, buf)     stat (fname, buf)
  173: # define __alloca               alloca
  174: # define __readdir              readdir
  175: # define __readdir64            readdir64
  176: # define __glob_pattern_p       glob_pattern_p
  177: #endif /* _LIBC */
  178: 
  179: #include <fnmatch.h>
  180: 
  181: #ifdef _SC_GETPW_R_SIZE_MAX
  182: # define GETPW_R_SIZE_MAX()     sysconf (_SC_GETPW_R_SIZE_MAX)
  183: #else
  184: # define GETPW_R_SIZE_MAX()     (-1)
  185: #endif
  186: #ifdef _SC_LOGIN_NAME_MAX
  187: # define GET_LOGIN_NAME_MAX()   sysconf (_SC_LOGIN_NAME_MAX)
  188: #else
  189: # define GET_LOGIN_NAME_MAX()   (-1)
  190: #endif
  191: ^L
  192: static const char *next_brace_sub (const char *begin, int flags) __THROW;
  193: 
  194: #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
  195: 
  196: #ifndef attribute_hidden
  197: # define attribute_hidden
  198: #endif
  199: 
  200: static int glob_in_dir (const char *pattern, const char *directory,
  201:                         int flags, int (*errfunc) (const char *, int),
  202:                         glob_t *pglob);
  203: extern int __glob_pattern_type (const char *pattern, int quote)
  204:     attribute_hidden;
  205: 
  206: #if !defined _LIBC || !defined GLOB_ONLY_P
  207: static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
  208: static int collated_compare (const void *, const void *) __THROW;
  209: 
  210: 
  211: /* Find the end of the sub-pattern in a brace expression.  */
  212: static const char *
  213: next_brace_sub (const char *cp, int flags)
  214: {
  215:   unsigned int depth = 0;
  216:   while (*cp != '\0')
  217:     if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
  218:       {
  219:         if (*++cp == '\0')
  220:           break;
  221:         ++cp;
  222:       }
  223:     else
  224:       {
  225:         if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
  226:           break;
  227: 
  228:         if (*cp++ == '{')
  229:           depth++;
  230:       }
  231: 
  232:   return *cp != '\0' ? cp : NULL;
  233: }
  234: 
  235: #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
  236: 
  237: /* Do glob searching for PATTERN, placing results in PGLOB.
  238:    The bits defined above may be set in FLAGS.
  239:    If a directory cannot be opened or read and ERRFUNC is not nil,
  240:    it is called with the pathname that caused the error, and the
  241:    `errno' value from the failing call; if it returns non-zero
  242:    `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
  243:    If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
  244:    Otherwise, `glob' returns zero.  */
  245: int
  246: #ifdef GLOB_ATTRIBUTE
  247: GLOB_ATTRIBUTE
  248: #endif
  249: glob (pattern, flags, errfunc, pglob)
  250:      const char *pattern;
  251:      int flags;
  252:      int (*errfunc) (const char *, int);
  253:      glob_t *pglob;
  254: {
  255:   const char *filename;
  256:   const char *dirname;
  257:   size_t dirlen;
  258:   int status;
  259:   size_t oldcount;
  260:   int meta;
  261:   int dirname_modified;
  262:   glob_t dirs;
  263: 
  264:   if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
  265:     {
  266:       __set_errno (EINVAL);
  267:       return -1;
  268:     }
  269: 
  270:   if (!(flags & GLOB_DOOFFS))
  271:     /* Have to do this so `globfree' knows where to start freeing.  It
  272:        also makes all the code that uses gl_offs simpler. */
  273:     pglob->gl_offs = 0;
  274: 
  275:   if (flags & GLOB_BRACE)
  276:     {
  277:       const char *begin;
  278: 
  279:       if (flags & GLOB_NOESCAPE)
  280:         begin = strchr (pattern, '{');
  281:       else
  282:         {
  283:           begin = pattern;
  284:           while (1)
  285:             {
  286:               if (*begin == '\0')
  287:                 {
  288:                   begin = NULL;
  289:                   break;
  290:                 }
  291: 
  292:               if (*begin == '\\' && begin[1] != '\0')
  293:                 ++begin;
  294:               else if (*begin == '{')
  295:                 break;
  296: 
  297:               ++begin;
  298:             }
  299:         }
  300: 
  301:       if (begin != NULL)
  302:         {
  303:           /* Allocate working buffer large enough for our work.  Note that
  304:             we have at least an opening and closing brace.  */
  305:           size_t firstc;
  306:           char *alt_start;
  307:           const char *p;
  308:           const char *next;
  309:           const char *rest;
  310:           size_t rest_len;
  311: #ifdef __GNUC__
  312:           char onealt[strlen (pattern) - 1];
  313: #else
  314:           char *onealt = (char *) malloc (strlen (pattern) - 1);
  315:           if (onealt == NULL)
  316:             {
  317:               if (!(flags & GLOB_APPEND))
  318:                 {
  319:                   pglob->gl_pathc = 0;
  320:                   pglob->gl_pathv = NULL;
  321:                 }
  322:               return GLOB_NOSPACE;
  323:             }
  324: #endif
  325: 
  326:           /* We know the prefix for all sub-patterns.  */
  327:           alt_start = mempcpy (onealt, pattern, begin - pattern);
  328: 
  329:           /* Find the first sub-pattern and at the same time find the
  330:              rest after the closing brace.  */
  331:           next = next_brace_sub (begin + 1, flags);
  332:           if (next == NULL)
  333:             {
  334:               /* It is an illegal expression.  */
  335: #ifndef __GNUC__
  336:               free (onealt);
  337: #endif
  338:               return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
  339:             }
  340: 
  341:           /* Now find the end of the whole brace expression.  */
  342:           rest = next;
  343:           while (*rest != '}')
  344:             {
  345:               rest = next_brace_sub (rest + 1, flags);
  346:               if (rest == NULL)
  347:                 {
  348:                   /* It is an illegal expression.  */
  349: #ifndef __GNUC__
  350:                   free (onealt);
  351: #endif
  352:                   return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
  353:                 }
  354:             }
  355:           /* Please note that we now can be sure the brace expression
  356:              is well-formed.  */
  357:           rest_len = strlen (++rest) + 1;
  358: 
  359:           /* We have a brace expression.  BEGIN points to the opening {,
  360:              NEXT points past the terminator of the first element, and END
  361:              points past the final }.  We will accumulate result names from
  362:              recursive runs for each brace alternative in the buffer using
  363:              GLOB_APPEND.  */
  364: 
  365:           if (!(flags & GLOB_APPEND))
  366:             {
  367:               /* This call is to set a new vector, so clear out the
  368:                  vector so we can append to it.  */
  369:               pglob->gl_pathc = 0;
  370:               pglob->gl_pathv = NULL;
  371:             }
  372:           firstc = pglob->gl_pathc;
  373: 
  374:           p = begin + 1;
  375:           while (1)
  376:             {
  377:               int result;
  378: 
  379:               /* Construct the new glob expression.  */
  380:               mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
  381: 
  382:               result = glob (onealt,
  383:                              ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
  384:                               | GLOB_APPEND), errfunc, pglob);
  385: 
  386:               /* If we got an error, return it.  */
  387:               if (result && result != GLOB_NOMATCH)
  388:                 {
  389: #ifndef __GNUC__
  390:                   free (onealt);
  391: #endif
  392:                   if (!(flags & GLOB_APPEND))
  393:                     {
  394:                       globfree (pglob);
  395:                       pglob->gl_pathc = 0;
  396:                     }
  397:                   return result;
  398:                 }
  399: 
  400:               if (*next == '}')
  401:                 /* We saw the last entry.  */
  402:                 break;
  403: 
  404:               p = next + 1;
  405:               next = next_brace_sub (p, flags);
  406:               assert (next != NULL);
  407:             }
  408: 
  409: #ifndef __GNUC__
  410:           free (onealt);
  411: #endif
  412: 
  413:           if (pglob->gl_pathc != firstc)
  414:             /* We found some entries.  */
  415:             return 0;
  416:           else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
  417:             return GLOB_NOMATCH;
  418:         }
  419:     }
  420: 
  421:   /* Find the filename.  */
  422:   filename = strrchr (pattern, '/');
  423: #if defined __MSDOS__ || defined WINDOWS32
  424:   /* The case of "d:pattern".  Since `:' is not allowed in
  425:      file names, we can safely assume that wherever it
  426:      happens in pattern, it signals the filename part.  This
  427:      is so we could some day support patterns like "[a-z]:foo".  */
  428:   if (filename == NULL)
  429:     filename = strchr (pattern, ':');
  430: #endif /* __MSDOS__ || WINDOWS32 */
  431:   dirname_modified = 0;
  432:   if (filename == NULL)
  433:     {
  434:       /* This can mean two things: a simple name or "~name".  The latter
  435:          case is nothing but a notation for a directory.  */
  436:       if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
  437:         {
  438:           dirname = pattern;
  439:           dirlen = strlen (pattern);
  440: 
  441:           /* Set FILENAME to NULL as a special flag.  This is ugly but
  442:              other solutions would require much more code.  We test for
  443:              this special case below.  */
  444:           filename = NULL;
  445:         }
  446:       else
  447:         {
  448:           filename = pattern;
  449: #ifdef _AMIGA
  450:           dirname = "";
  451: #else
  452:           dirname = ".";
  453: #endif
  454:           dirlen = 0;
  455:         }
  456:     }
  457:   else if (filename == pattern
  458:            || (filename == pattern + 1 && pattern[0] == '\\'
  459:                && (flags & GLOB_NOESCAPE) == 0))
  460:     {
  461:       /* "/pattern" or "\\/pattern".  */
  462:       dirname = "/";
  463:       dirlen = 1;
  464:       ++filename;
  465:     }
  466:   else
  467:     {
  468:       char *newp;
  469:       dirlen = filename - pattern;
  470: #if defined __MSDOS__ || defined WINDOWS32
  471:       if (*filename == ':'
  472:           || (filename > pattern + 1 && filename[-1] == ':'))
  473:         {
  474:           char *drive_spec;
  475: 
  476:           ++dirlen;
  477:           drive_spec = (char *) __alloca (dirlen + 1);
  478:           *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
  479:           /* For now, disallow wildcards in the drive spec, to
  480:              prevent infinite recursion in glob.  */
  481:           if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
  482:             return GLOB_NOMATCH;
  483:           /* If this is "d:pattern", we need to copy `:' to DIRNAME
  484:              as well.  If it's "d:/pattern", don't remove the slash
  485:              from "d:/", since "d:" and "d:/" are not the same.*/
  486:         }
  487: #endif
  488:       newp = (char *) __alloca (dirlen + 1);
  489:       *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
  490:       dirname = newp;
  491:       ++filename;
  492: 
  493:       if (filename[0] == '\0'
  494: #if defined __MSDOS__ || defined WINDOWS32
  495:           && dirname[dirlen - 1] != ':'
  496:           && (dirlen < 3 || dirname[dirlen - 2] != ':'
  497:               || dirname[dirlen - 1] != '/')
  498: #endif
  499:           && dirlen > 1)
  500:         /* "pattern/".  Expand "pattern", appending slashes.  */
  501:         {
  502:           int orig_flags = flags;
  503:           if (!(flags & GLOB_NOESCAPE) && dirname[dirlen - 1] == '\\')
  504:             {
  505:               /* "pattern\\/".  Remove the final backslash if it hasn't
  506:                  been quoted.  */
  507:               char *p = (char *) &dirname[dirlen - 1];
  508: 
  509:               while (p > dirname && p[-1] == '\\') --p;
  510:               if ((&dirname[dirlen] - p) & 1)
  511:                 {
  512:                   *(char *) &dirname[--dirlen] = '\0';
  513:                   flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
  514:                 }
  515:             }
  516:           int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
  517:           if (val == 0)
  518:             pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
  519:                                | (flags & GLOB_MARK));
  520:           else if (val == GLOB_NOMATCH && flags != orig_flags)
  521:             {
  522:               /* Make sure globfree (&dirs); is a nop.  */
  523:               dirs.gl_pathv = NULL;
  524:               flags = orig_flags;
  525:               oldcount = pglob->gl_pathc + pglob->gl_offs;
  526:               goto no_matches;
  527:             }
  528:           return val;
  529:         }
  530:     }
  531: 
  532:   if (!(flags & GLOB_APPEND))
  533:     {
  534:       pglob->gl_pathc = 0;
  535:       if (!(flags & GLOB_DOOFFS))
  536:         pglob->gl_pathv = NULL;
  537:       else
  538:         {
  539:           size_t i;
  540:           pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
  541:                                               * sizeof (char *));
  542:           if (pglob->gl_pathv == NULL)
  543:             return GLOB_NOSPACE;
  544: 
  545:           for (i = 0; i <= pglob->gl_offs; ++i)
  546:             pglob->gl_pathv[i] = NULL;
  547:         }
  548:     }
  549: 
  550:   oldcount = pglob->gl_pathc + pglob->gl_offs;