home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / compat / __old_glob.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-17  |  13.6 KB  |  579 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. /* IGNORE(@ */
  20. #include <ansidecl.h>
  21. /* @) */
  22.  
  23. #include "glob.h"
  24. #include <fnmatch.h>
  25.  
  26. #ifdef    SHELL
  27. #include "config.h"
  28. #endif
  29.  
  30. #include "warning.h"
  31.  
  32. #include <errno.h>
  33. #ifdef    __GNU_LIBRARY__
  34. #include <stddef.h>
  35. #endif
  36.  
  37. #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
  38. extern int errno;
  39. #endif
  40.  
  41. #if defined (USGr3) && !defined (DIRENT)
  42. #define DIRENT
  43. #endif /* USGr3 and not DIRENT.  */
  44. #if defined (Xenix) && !defined (SYSNDIR)
  45. #define SYSNDIR
  46. #endif /* Xenix and not SYSNDIR.  */
  47.  
  48. #if defined (POSIX) || defined (DIRENT) || defined (__GNU_LIBRARY__)
  49. #ifdef    USG
  50. #include <sys/types.h>
  51. #endif
  52. #include <dirent.h>
  53. #else
  54. #if    defined (USG) && !defined (sgi)
  55. #ifdef SYSNDIR
  56. #include <sys/ndir.h>
  57. #else /* not SYSNDIR */
  58. #include "ndir.h"
  59. #endif /* SYSNDIR */
  60. #else /* not USG */
  61. #include <sys/types.h>
  62. #include <sys/dir.h>
  63. #endif /* USG */
  64. #undef    dirent
  65. #define    dirent    direct
  66. #endif /* POSIX or DIRENT or __GNU_LIBRARY__ */
  67.  
  68. #if    defined(__GNU_LIBRARY__) || !defined(POSIX) && !defined(USG)
  69. #ifdef linux
  70. #define    D_NAMLEN(d)    strlen((d)->d_name)
  71. #else
  72. #define    D_NAMLEN(d)    ((d)->d_namlen)
  73. #endif
  74. #else
  75. #define    D_NAMLEN(d)    strlen((d)->d_name)
  76. #endif
  77.  
  78. #ifndef    NULL
  79. #define    NULL    0
  80. #endif
  81.  
  82. #if defined(POSIX) && !defined(__GNU_LIBRARY__)
  83. /* Posix does not require that the d_ino field be present, and some
  84.    systems do not provide it. */
  85. #define REAL_DIR_ENTRY(dp) 1
  86. #else
  87. #define REAL_DIR_ENTRY(dp)    ((dp)->d_ino != 0)
  88. #endif
  89.  
  90. #if    defined(__GNU_LIBRARY__) || defined(POSIX)
  91. #include <unistd.h>
  92. #endif
  93.  
  94. #if defined (STDC_HEADERS) || defined (__GNU_LIBRARY__)
  95.  
  96. #include <stdlib.h>
  97. #include <string.h>
  98.  
  99. #define STDC_STRINGS
  100.  
  101. #else /* Not STDC_HEADERS and not __GNU_LIBRARY__.  */
  102.  
  103. #ifdef    USG
  104. #include <string.h>
  105. #ifndef    POSIX
  106. #include <memory.h>
  107. #endif    /* Not POSIX.  */
  108. #define STDC_STRINGS
  109. #else    /* Not USG.  */
  110. #ifdef    NeXT
  111. #include <string.h>
  112. #else    /* Not NeXT.  */
  113. #include <strings.h>
  114. #endif    /* NeXT.  */
  115. /* Declaring bcopy causes errors on systems whose declarations are different.
  116.    If the declaration is omitted, everything works fine.  */
  117. #endif    /* Not USG.  */
  118.  
  119. extern char *malloc ();
  120. extern char *realloc ();
  121. extern void free ();
  122. extern void qsort ();
  123.  
  124. #ifdef    __GNUC__
  125. __inline
  126. #endif
  127. static char *
  128. my_realloc (p, n)
  129.      char *p;
  130.      unsigned int n;
  131. {
  132.   if (p == NULL)
  133.     return malloc (n);
  134.   return realloc (p, n);
  135. }
  136. #define    realloc    my_realloc
  137.  
  138. #define    strcoll    strcmp
  139.  
  140. #endif    /* Not STDC_HEADERS or __GNU_LIBRARY__.  */
  141.  
  142. #ifndef    STDC_STRINGS
  143. #define    memcpy(d, s, n)    bcopy((s), (d), (n))
  144. #define    strrchr    rindex
  145. /* memset is only used for zero here, but let's be paranoid.  */
  146. #define    memset(s, better_be_zero, n) \
  147.   ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
  148. #endif
  149.  
  150. #if    !defined(__alloca) && !defined(__GNU_LIBRARY__)
  151.  
  152. #ifdef __GNUC__
  153. #define alloca __builtin_alloca
  154. #else /* Not GCC.  */
  155. #ifdef    sparc
  156. #include <alloca.h>
  157. #else    /* Not sparc.  */
  158. extern char *alloca ();
  159. #endif /* sparc.  */
  160. #endif /* GCC.  */
  161.  
  162. #define    __alloca    alloca
  163.  
  164. #endif
  165.  
  166. #ifndef    __STDC__
  167. #undef    size_t
  168. #define    size_t    unsigned int
  169. #endif
  170.  
  171. static int EXFUN(glob_pattern_p, (CONST char *pattern, int quote));
  172. static int EXFUN(glob_in_dir, (CONST char *pattern, CONST char *directory,
  173.                    int flags,
  174.                    int EXFUN((*errfunc), (CONST char *, int)),
  175.                    glob_t *pglob));
  176. static int EXFUN(prefix_array, (CONST char *prefix, char **array, size_t n));
  177. static int EXFUN(collated_compare, (CONST PTR, CONST PTR));
  178.  
  179.  
  180. /* Do glob searching for PATTERN, placing results in PGLOB.
  181.    The bits defined above may be set in FLAGS.
  182.    If a directory cannot be opened or read and ERRFUNC is not nil,
  183.    it is called with the pathname that caused the error, and the
  184.    `errno' value from the failing call; if it returns non-zero
  185.    `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
  186.    If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
  187.    Otherwise, `glob' returns zero.  */
  188. int
  189. DEFUN(__old_glob, (pattern, flags, errfunc, pglob),
  190.       CONST char *pattern AND int flags AND
  191.       int EXFUN((*errfunc), (CONST char *, int)) AND
  192.       glob_t *pglob)
  193. {
  194.   CONST char *filename;
  195.   char *dirname;
  196.   size_t dirlen;
  197.   int status;
  198.   int oldcount;
  199.  
  200.   __LIBC_WARNING (glob);
  201.  
  202.   if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
  203.     {
  204.       errno = EINVAL;
  205.       return -1;
  206.     }
  207.  
  208.   /* Find the filename.  */
  209.   filename = strrchr(pattern, '/');
  210.   if (filename == NULL)
  211.     {
  212.       filename = pattern;
  213.       dirname = (char *) ".";
  214.       dirlen = 0;
  215.     }
  216.   else if (filename == pattern)
  217.     {
  218.       /* "/pattern".  */
  219.       dirname = (char *) "/";
  220.       dirlen = 1;
  221.       ++filename;
  222.     }
  223.   else
  224.     {
  225.       dirlen = filename - pattern;
  226.       dirname = (char *) __alloca(dirlen + 1);
  227.       memcpy(dirname, pattern, dirlen);
  228.       dirname[dirlen] = '\0';
  229.       ++filename;
  230.     }
  231.  
  232.   if (filename[0] == '\0' && dirlen > 1)
  233.     /* "pattern/".  Expand "pattern", appending slashes.  */
  234.     return glob(dirname, flags|GLOB_MARK, errfunc, pglob);
  235.  
  236.   if (!(flags & GLOB_APPEND))
  237.     {
  238.       pglob->gl_pathc = 0;
  239.       pglob->gl_pathv = NULL;
  240.     }
  241.  
  242.   oldcount = pglob->gl_pathc;
  243.  
  244.   if (glob_pattern_p(dirname, !(flags & GLOB_NOESCAPE)))
  245.     {
  246.       /* The directory name contains metacharacters, so we
  247.      have to glob for the directory, and then glob for
  248.      the pattern in each directory found.  */
  249.       glob_t dirs;
  250.       register int i;
  251.  
  252.       status = glob(dirname, ((flags & (GLOB_ERR|GLOB_NOCHECK|GLOB_NOESCAPE)) |
  253.                   GLOB_NOSORT),
  254.             errfunc, &dirs);
  255.       if (status != 0)
  256.     return status;
  257.  
  258.       /* We have successfully globbed the preceding directory name.
  259.      For each name we found, call glob_in_dir on it and FILENAME,
  260.      appending the results to PGLOB.  */
  261.       for (i = 0; i < dirs.gl_pathc; ++i)
  262.     {
  263.       int oldcount;
  264.  
  265. #ifdef    SHELL
  266.       {
  267.         /* Make globbing interruptible in the bash shell. */
  268.         extern int interrupt_state;
  269.  
  270.         if (interrupt_state)
  271.           {
  272.         globfree (&dirs);
  273.         globfree (&files);
  274.         return GLOB_ABEND;
  275.           }
  276.       }
  277. #endif    /* SHELL.  */
  278.  
  279.       oldcount = pglob->gl_pathc;
  280.       status = glob_in_dir(filename, dirs.gl_pathv[i],
  281.                    (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
  282.                    errfunc, pglob);
  283.       if (status == GLOB_NOMATCH)
  284.         /* No matches in this directory.  Try the next.  */
  285.         continue;
  286.  
  287.       if (status != 0)
  288.         {
  289.           globfree (&dirs);
  290.           globfree (pglob);
  291.           return status;
  292.         }
  293.  
  294.       /* Stick the directory on the front of each name.  */
  295.       if (prefix_array(dirs.gl_pathv[i],
  296.                &pglob->gl_pathv[oldcount],
  297.                pglob->gl_pathc - oldcount))
  298.         {
  299.           globfree (&dirs);
  300.           globfree (pglob);
  301.           return GLOB_NOSPACE;
  302.         }
  303.     }
  304.  
  305.       if (pglob->gl_pathc == oldcount)
  306.     /* No matches.  */
  307.     if (flags & GLOB_NOCHECK)
  308.       {
  309.         CONST size_t len = strlen (pattern) + 1;
  310.         char *patcopy = (char *) malloc (len);
  311.         if (patcopy == NULL)
  312.           return GLOB_NOSPACE;
  313.         memcpy (patcopy, pattern, len);
  314.  
  315.         pglob->gl_pathv
  316.           = (char **) realloc(pglob->gl_pathv,
  317.                   (pglob->gl_pathc +
  318.                    ((flags & GLOB_DOOFFS) ?
  319.                     pglob->gl_offs : 0) +
  320.                    1 + 1) *
  321.                   sizeof(char *));
  322.         if (pglob->gl_pathv == NULL)
  323.           {
  324.         free (patcopy);
  325.         return GLOB_NOSPACE;
  326.           }
  327.  
  328.         if (flags & GLOB_DOOFFS)
  329.           while (pglob->gl_pathc < pglob->gl_offs)
  330.         pglob->gl_pathv[pglob->gl_pathc++] = NULL;
  331.  
  332.         pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
  333.         return 0;
  334.       }
  335.     else
  336.       return GLOB_NOMATCH;
  337.     }
  338.   else
  339.     {
  340.       status = glob_in_dir(filename, dirname, flags, errfunc, pglob);
  341.       if (status != 0)
  342.     return status;
  343.  
  344.       if (dirlen > 0)
  345.     {
  346.       /* Stick the directory on the front of each name.  */
  347.       if (prefix_array(dirname,
  348.                &pglob->gl_pathv[oldcount],
  349.                pglob->gl_pathc - oldcount))
  350.         {
  351.           globfree (pglob);
  352.           return GLOB_NOSPACE;
  353.         }
  354.     }
  355.     }
  356.  
  357.   if (!(flags & GLOB_NOSORT))
  358.     qsort((PTR) &pglob->gl_pathv[oldcount], pglob->gl_pathc - oldcount,
  359.       sizeof(char *), collated_compare);
  360.  
  361.   return 0;
  362. }
  363.  
  364.  
  365. /* Free storage allocated in PGLOB by a previous `glob' call.  */
  366. void
  367. DEFUN(__old_globfree, (pglob), register glob_t *pglob)
  368. {
  369.   __LIBC_WARNING (globfree);
  370.  
  371.   if (pglob->gl_pathv != NULL)
  372.     {
  373.       register int i;
  374.       for (i = 0; i < pglob->gl_pathc; ++i)
  375.     if (pglob->gl_pathv[i] != NULL)
  376.       free((PTR) pglob->gl_pathv[i]);
  377.       free((PTR) pglob->gl_pathv);
  378.     }
  379. }
  380.  
  381.  
  382. /* Do a collated comparison of A and B.  */
  383. static int
  384. DEFUN(collated_compare, (a, b), CONST PTR a AND CONST PTR b)
  385. {
  386.   CONST char *CONST s1 = *(CONST char *CONST *CONST) a;
  387.   CONST char *CONST s2 = *(CONST char *CONST *CONST) b;
  388.  
  389.   if (s1 == s2)
  390.     return 0;
  391.   if (s1 == NULL)
  392.     return 1;
  393.   if (s2 == NULL)
  394.     return -1;
  395.   return strcoll (s1, s2);
  396. }
  397.  
  398.  
  399. /* Prepend PREFIX to each of N members of ARRAY, replacing ARRAY's
  400.    elements in place.  Return nonzero if out of memory, zero if successful.
  401.    A slash is inserted between PREFIX and each elt of ARRAY.
  402.    Each old element of ARRAY is freed.  */
  403. static int
  404. DEFUN(prefix_array, (prefix, array, n),
  405.       CONST char *prefix AND char **array AND CONST size_t n)
  406. {
  407.   register size_t i;
  408.   CONST size_t prelen = strlen(prefix);
  409.  
  410.   for (i = 0; i < n; ++i)
  411.     {
  412.       CONST size_t eltlen = strlen(array[i]) + 1;
  413.       char *new = (char *) malloc(prelen + 1 + eltlen);
  414.       if (new == NULL)
  415.     {
  416.       while (i > 0)
  417.         free((PTR) array[--i]);
  418.       return 1;
  419.     }
  420.  
  421.       memcpy(new, prefix, prelen);
  422.       new[prelen] = '/';
  423.       memcpy(&new[prelen + 1], array[i], eltlen);
  424.       free((PTR) array[i]);
  425.       array[i] = new;
  426.     }
  427.  
  428.   return 0;
  429. }
  430.  
  431.  
  432. /* Return nonzero if PATTERN contains any metacharacters.
  433.    Metacharacters can be quoted with backslashes if QUOTE is nonzero.  */
  434. static int
  435. DEFUN(glob_pattern_p, (pattern, quote),
  436.       CONST char *pattern AND CONST int quote)
  437. {
  438.   register CONST char *p;
  439.   int open = 0;
  440.  
  441.   for (p = pattern; *p != '\0'; ++p)
  442.     switch (*p)
  443.       {
  444.       case '?':
  445.       case '*':
  446.     return 1;
  447.  
  448.       case '\\':
  449.     if (quote)
  450.       ++p;
  451.     break;
  452.  
  453.       case '[':
  454.     open = 1;
  455.     break;
  456.  
  457.       case ']':
  458.     if (open)
  459.       return 1;
  460.     break;
  461.       }
  462.  
  463.   return 0;
  464. }
  465.  
  466.  
  467. /* Like `glob', but PATTERN is a final pathname component,
  468.    and matches are searched for in DIRECTORY.
  469.    The GLOB_NOSORT bit in FLAGS is ignored.  No sorting is ever done.
  470.    The GLOB_APPEND flag is assumed to be set (always appends).  */
  471. static int
  472. DEFUN(glob_in_dir, (pattern, directory, flags, errfunc, pglob),
  473.       CONST char *pattern AND CONST char *directory AND int flags AND
  474.       int EXFUN((*errfunc), (CONST char *, int)) AND glob_t *pglob)
  475. {
  476.   register DIR *stream;
  477.   register struct dirent *d;
  478.  
  479.   struct globlink
  480.     {
  481.       struct globlink *next;
  482.       char *name;
  483.     };
  484.   struct globlink *names = NULL;
  485.   size_t nfound = 0;
  486.  
  487.   if (!glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
  488.     {
  489.       stream = NULL;
  490.       flags |= GLOB_NOCHECK;
  491.     }
  492.   else
  493.     {
  494.       stream = opendir(directory);
  495.       if (stream == NULL)
  496.     {
  497.       if ((errfunc != NULL && (*errfunc)(directory, errno)) ||
  498.           (flags & GLOB_ERR))
  499.         return GLOB_ABEND;
  500.     }
  501.       else
  502.     while ((d = readdir(stream)) != NULL)
  503.       if (REAL_DIR_ENTRY (d) &&
  504.           fnmatch(pattern, d->d_name,
  505.               (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
  506.               ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
  507.         {
  508.           struct globlink *new = (struct globlink *)
  509.         __alloca(sizeof (struct globlink));
  510.           size_t len = D_NAMLEN (d);
  511.           new->name = (char *) malloc(len +
  512.                       ((flags & GLOB_MARK) ? 1 : 0) + 1);
  513.           if (new->name == NULL)
  514.         goto memory_error;
  515.           memcpy ((PTR) new->name, d->d_name, len);
  516.           if (flags & GLOB_MARK)
  517.         new->name[len++] = '/';
  518.           new->name[len] = '\0';
  519.           new->next = names;
  520.           names = new;
  521.           ++nfound;
  522.         }
  523.     }
  524.  
  525.   if (nfound == 0 && (flags & GLOB_NOCHECK))
  526.     {
  527.       size_t len = strlen (pattern);
  528.       nfound = 1;
  529.       names = (struct globlink *) __alloca (sizeof (struct globlink));
  530.       names->next = NULL;
  531.       names->name = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
  532.       if (names->name == NULL)
  533.     goto memory_error;
  534.       memcpy (names->name, pattern, len);
  535.       if (flags & GLOB_MARK)
  536.     names->name[len++] = '/';
  537.       names->name[len] = '\0';
  538.     }
  539.  
  540.   pglob->gl_pathv
  541.     = (char **) realloc(pglob->gl_pathv,
  542.             (pglob->gl_pathc +
  543.              ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
  544.              nfound + 1) *
  545.             sizeof(char *));
  546.   if (pglob->gl_pathv == NULL)
  547.     goto memory_error;
  548.  
  549.   if (flags & GLOB_DOOFFS)
  550.     while (pglob->gl_pathc < pglob->gl_offs)
  551.       pglob->gl_pathv[pglob->gl_pathc++] = NULL;
  552.  
  553.   for (; names != NULL; names = names->next)
  554.     pglob->gl_pathv[pglob->gl_pathc++] = names->name;
  555.   pglob->gl_pathv[pglob->gl_pathc] = NULL;
  556.  
  557.   if (stream != NULL)
  558.     {
  559.       int save = errno;
  560.       (void) closedir (stream);
  561.       errno = save;
  562.     }
  563.   return nfound == 0 ? GLOB_NOMATCH : 0;
  564.  
  565.  memory_error:
  566.   {
  567.     int save = errno;
  568.     (void) closedir (stream);
  569.     errno = save;
  570.   }
  571.   while (names != NULL)
  572.     {
  573.       if (names->name != NULL)
  574.     free((PTR) names->name);
  575.       names = names->next;
  576.     }
  577.   return GLOB_NOSPACE;
  578. }
  579.