changeset 9357:75e43b0e5159

Modify glob.c to use fstatat and dirfd, to simplify it. Suggested by Eric Blake. * lib/glob.c (__fxstatat64) [!_LIBC]: New macro. Don't include <stdbool.h>; not used. (link_exists2_p, glob_in_dir) [!_LIBC]: No longer a special case. (link_exists_p): Simplify implementation, since we can now assume dirfd and fstatat. * modules/glob (Depends-on): Add dirfd, openat. Remove stdbool.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 17 Oct 2007 16:06:45 -0700
parents 23797caac455
children ec6f741a8dc6
files ChangeLog lib/glob.c modules/glob
diffstat 3 files changed, 34 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2007-10-17  Paul Eggert  <eggert@cs.ucla.edu>
+
+	Modify glob.c to use fstatat and dirfd, to simplify it.
+	Suggested by Eric Blake.
+	* lib/glob.c (__fxstatat64) [!_LIBC]: New macro.
+	Don't include <stdbool.h>; not used.
+	(link_exists2_p, glob_in_dir) [!_LIBC]: No longer a special case.
+	(link_exists_p): Simplify implementation, since we can now assume
+	dirfd and fstatat.
+	* modules/glob (Depends-on): Add dirfd, openat.  Remove stdbool.
+
 2007-10-17  Ralf Wildenhues  <Ralf.Wildenhues@gmx.de>
 
 	* gnulib-tool (func_get_dependencies): Fix sed script to
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -134,6 +134,7 @@
 # define struct_stat64		struct stat64
 #else /* !_LIBC */
 # define __stat64(fname, buf)	stat (fname, buf)
+# define __fxstatat64(_, d, f, st, flag) fstatat (d, f, st, flag)
 # define struct_stat64		struct stat
 # define __stat(fname, buf)	stat (fname, buf)
 # define __alloca		alloca
@@ -142,9 +143,13 @@
 # define __glob_pattern_p	glob_pattern_p
 #endif /* _LIBC */
 
-#include <stdbool.h>
 #include <fnmatch.h>
 
+#ifndef _LIBC
+# include "dirfd.h"
+# include "openat.h"
+#endif
+
 #ifdef _SC_GETPW_R_SIZE_MAX
 # define GETPW_R_SIZE_MAX()	sysconf (_SC_GETPW_R_SIZE_MAX)
 #else
@@ -1219,40 +1224,31 @@
 static int
 __attribute_noinline__
 link_exists2_p (const char *dir, size_t dirlen, const char *fname,
-	       glob_t *pglob
-# ifndef _LIBC
-		, int flags
-# endif
-		)
+		glob_t *pglob)
 {
   size_t fnamelen = strlen (fname);
   char *fullname = __alloca (dirlen + 1 + fnamelen + 1);
   struct stat st;
-# ifndef _LIBC
-  struct_stat64 st64;
-# endif
 
   mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
 	   fname, fnamelen + 1);
 
-# ifdef _LIBC
   return (*pglob->gl_stat) (fullname, &st) == 0;
-# else
-  return ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
-	   ? (*pglob->gl_stat) (fullname, &st)
-	   : __stat64 (fullname, &st64)) == 0);
-# endif
 }
-# ifdef _LIBC
-#  define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
-  (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)			      \
-   ? link_exists2_p (dirname, dirnamelen, fname, pglob)			      \
-   : ({ struct stat64 st64;						      \
-       __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0; }))
-# else
-#  define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
-  link_exists2_p (dirname, dirnamelen, fname, pglob, flags)
-# endif
+
+/* Return true if DIR/FNAME exists.  */
+static int
+link_exists_p (int dfd, const char *dir, size_t dirlen, const char *fname,
+	       glob_t *pglob, int flags)
+{
+  if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
+    return link_exists2_p (dir, dirlen, fname, pglob);
+  else
+    {
+      struct_stat64 st64;
+      return __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0;
+    }
+}
 #endif
 
 
@@ -1328,10 +1324,8 @@
 	}
       else
 	{
-#ifdef _LIBC
 	  int dfd = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
 		     ? -1 : dirfd ((DIR *) stream));
-#endif
 	  int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
 			   | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
 #if defined _AMIGA || defined VMS
--- a/modules/glob
+++ b/modules/glob
@@ -11,11 +11,12 @@
 Depends-on:
 alloca
 d-type
+dirfd
 extensions
 fnmatch
 getlogin_r
 mempcpy
-stdbool
+openat
 strdup
 sys_stat
 unistd