Include <stddef.h>, for size_t.
[gnulib.git] / lib / glob.c
index f2db01b..a0af922 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991-2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+/* Copyright (C) 1991-2002,2003,2004,2005,2006,2007 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -16,7 +16,7 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
-#ifdef HAVE_CONFIG_H
+#ifndef _LIBC
 # include <config.h>
 #endif
 
 #include <stdio.h>             /* Needed on stupid SunOS for assert.  */
 
 #if !defined _LIBC || !defined GLOB_ONLY_P
-#if defined HAVE_UNISTD_H || defined _LIBC
-# include <unistd.h>
-# ifndef POSIX
-#  ifdef _POSIX_VERSION
-#   define POSIX
-#  endif
-# endif
+
+#include <unistd.h>
+#if !defined POSIX && defined _POSIX_VERSION
+# define POSIX
 #endif
 
 #include <pwd.h>
 # define __set_errno(val) errno = (val)
 #endif
 
-#if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
-# include <dirent.h>
-# define NAMLEN(dirent) strlen((dirent)->d_name)
-#else
-# define dirent direct
-# define NAMLEN(dirent) (dirent)->d_namlen
-# ifdef HAVE_SYS_NDIR_H
-#  include <sys/ndir.h>
-# endif
-# ifdef HAVE_SYS_DIR_H
-#  include <sys/dir.h>
-# endif
-# ifdef HAVE_NDIR_H
-#  include <ndir.h>
-# endif
-# ifdef HAVE_VMSDIR_H
-#  include "vmsdir.h"
-# endif /* HAVE_VMSDIR_H */
-#endif
+#include <dirent.h>
 
 
 /* In GNU systems, <dirent.h> defines this macro for us.  */
-#ifdef _D_NAMLEN
-# undef NAMLEN
-# define NAMLEN(d) _D_NAMLEN(d)
+#ifndef _D_EXACT_NAMLEN
+# define _D_EXACT_NAMLEN(dirent) strlen ((dirent)->d_name)
 #endif
 
 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
 
 /* If the system has the `struct dirent64' type we use it internally.  */
 #if defined _LIBC && !defined COMPILE_GLOB64
-# if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
-#  define CONVERT_D_NAMLEN(d64, d32)
-# else
-#  define CONVERT_D_NAMLEN(d64, d32) \
-  (d64)->d_namlen = (d32)->d_namlen;
-# endif
-
 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
 #  define CONVERT_D_INO(d64, d32)
 # else
 # endif
 
 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
-  memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1);                   \
-  CONVERT_D_NAMLEN (d64, d32)                                                \
+  memcpy ((d64)->d_name, (d32)->d_name, _D_EXACT_NAMLEN (d32) + 1);          \
   CONVERT_D_INO (d64, d32)                                                   \
   CONVERT_D_TYPE (d64, d32)
 #endif
 # endif
 # define struct_stat64         struct stat64
 #else /* !_LIBC */
-# include "getlogin_r.h"
-# include "mempcpy.h"
-# include "stat-macros.h"
-# include "strdup.h"
 # define __stat64(fname, buf)  stat (fname, buf)
 # define struct_stat64         struct stat
 # define __stat(fname, buf)    stat (fname, buf)
@@ -193,6 +159,11 @@ static const char *next_brace_sub (const char *begin, int flags) __THROW;
 
 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
 
+/* The results of opendir() in this file are not used with dirfd and fchdir,
+   therefore save some unnecessary work in fchdir.c.  */
+#undef opendir
+#undef closedir
+
 static int glob_in_dir (const char *pattern, const char *directory,
                        int flags, int (*errfunc) (const char *, int),
                        glob_t *pglob);
@@ -240,9 +211,11 @@ int
 #ifdef GLOB_ATTRIBUTE
 GLOB_ATTRIBUTE
 #endif
-glob (const char *pattern, int flags,
-      int (*errfunc) (const char *, int),
-      glob_t *pglob)
+glob (pattern, flags, errfunc, pglob)
+     const char *pattern;
+     int flags;
+     int (*errfunc) (const char *, int);
+     glob_t *pglob;
 {
   const char *filename;
   const char *dirname;
@@ -320,7 +293,7 @@ glob (const char *pattern, int flags,
          next = next_brace_sub (begin + 1, flags);
          if (next == NULL)
            {
-             /* It is an illegal expression.  */
+             /* It is an invalid expression.  */
 #ifndef __GNUC__
              free (onealt);
 #endif
@@ -334,7 +307,7 @@ glob (const char *pattern, int flags,
              rest = next_brace_sub (rest + 1, flags);
              if (rest == NULL)
                {
-                 /* It is an illegal expression.  */
+                 /* It is an invalid expression.  */
 #ifndef __GNUC__
                  free (onealt);
 #endif
@@ -512,7 +485,6 @@ glob (const char *pattern, int flags,
 
   oldcount = pglob->gl_pathc + pglob->gl_offs;
 
-#ifndef VMS
   if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
     {
       if (dirname[1] == '\0' || dirname[1] == '/')
@@ -531,7 +503,7 @@ glob (const char *pattern, int flags,
            {
              int success;
              char *name;
-             size_t buflen = GET_LOGIN_NAME_MAX() + 1;
+             size_t buflen = GET_LOGIN_NAME_MAX () + 1;
 
              if (buflen == 0)
                /* `sysconf' does not support _SC_LOGIN_NAME_MAX.  Try
@@ -680,7 +652,6 @@ glob (const char *pattern, int flags,
        }
 # endif        /* Not Amiga && not WINDOWS32.  */
     }
-#endif /* Not VMS.  */
 
   /* Now test whether we looked for "~" or "~NAME".  In this case we
      can give the answer now.  */
@@ -888,7 +859,8 @@ libc_hidden_def (glob)
 
 /* Free storage allocated in PGLOB by a previous `glob' call.  */
 void
-globfree (register glob_t *pglob)
+globfree (pglob)
+     register glob_t *pglob;
 {
   if (pglob->gl_pathv != NULL)
     {
@@ -909,8 +881,8 @@ libc_hidden_def (globfree)
 static int
 collated_compare (const void *a, const void *b)
 {
-  const char *const s1 = *(const char *const * const) a;
-  const char *const s2 = *(const char *const * const) b;
+  char *const *ps1 = a; char *s1 = *ps1;
+  char *const *ps2 = b; char *s2 = *ps2;
 
   if (s1 == s2)
     return 0;
@@ -986,7 +958,9 @@ prefix_array (const char *dirname, char **array, size_t n)
 /* Return nonzero if PATTERN contains any metacharacters.
    Metacharacters can be quoted with backslashes if QUOTE is nonzero.  */
 int
-__glob_pattern_p (const char *pattern, int quote)
+__glob_pattern_p (pattern, quote)
+     const char *pattern;
+     int quote;
 {
   register const char *p;
   int open = 0;
@@ -1130,7 +1104,7 @@ glob_in_dir (const char *pattern, const char *directory, int flags,
            {
              int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
                               | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
-#if defined _AMIGA || defined VMS
+#if defined _AMIGA || defined __VMS
                               | FNM_CASEFOLD
 #endif
                               );
@@ -1203,7 +1177,7 @@ glob_in_dir (const char *pattern, const char *directory, int flags,
                          struct globlink *new =
                            __alloca (sizeof (struct globlink));
                          char *p;
-                         len = NAMLEN (d);
+                         len = _D_EXACT_NAMLEN (d);
                          new->name =
                            malloc (len + 1 + ((flags & GLOB_MARK) && isdir));
                          if (new->name == NULL)