As it turns out, glob is also an offender...
authorDaniel Hokka Zakrisson <dhokka@cs.princeton.edu>
Tue, 1 Jul 2008 19:40:41 +0000 (19:40 +0000)
committerDaniel Hokka Zakrisson <dhokka@cs.princeton.edu>
Tue, 1 Jul 2008 19:40:41 +0000 (19:40 +0000)
source/libc-opendir-hack.c

index 95786ef..ea735fa 100644 (file)
@@ -1,11 +1,16 @@
 #define _GNU_SOURCE 1
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <dlfcn.h>
 #include <sys/types.h>
 #include <fcntl.h>
 #include <dirent.h>
+#include <glob.h>
 
-#define NAME   "O_CLOEXEC-vs-O_ATOMICLOOKUP"
+static int (*real_glob)(const char *pattern, int flags,
+         int (*errfunc) (const char *epath, int eerrno),
+         glob_t *pglob);
 
 DIR *opendir(const char *name)
 {
@@ -14,3 +19,33 @@ DIR *opendir(const char *name)
     return NULL;
   return fdopendir(fd);
 }
+
+DIR *__opendir(const char *name)
+{
+  return opendir(name);
+}
+
+int glob(const char *pattern, int flags,
+         int (*errfunc) (const char *epath, int eerrno),
+         glob_t *pglob)
+{
+  if (!(flags & GLOB_ALTDIRFUNC)) {
+    pglob->gl_closedir = closedir;
+    pglob->gl_readdir = readdir;
+    pglob->gl_opendir = opendir;
+    pglob->gl_lstat = lstat;
+    pglob->gl_stat = stat;
+    flags |= GLOB_ALTDIRFUNC;
+  }
+  return real_glob(pattern, flags, errfunc, pglob);
+}
+
+static void _init() __attribute__((constructor));
+static void _init()
+{
+  real_glob = dlsym(RTLD_NEXT, "glob");
+  if (!real_glob) {
+    fprintf(stderr, "Would the real glob please stand up? %s\n", dlerror());
+    exit(1);
+  }
+}