ea735fa5a5979cd39007ccfb91e20d320d8d3cc1
[bootmanager.git] / source / libc-opendir-hack.c
1 #define _GNU_SOURCE 1
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <dlfcn.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8 #include <dirent.h>
9 #include <glob.h>
10
11 static int (*real_glob)(const char *pattern, int flags,
12          int (*errfunc) (const char *epath, int eerrno),
13          glob_t *pglob);
14
15 DIR *opendir(const char *name)
16 {
17   int fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_LARGEFILE);
18   if (fd == -1)
19     return NULL;
20   return fdopendir(fd);
21 }
22
23 DIR *__opendir(const char *name)
24 {
25   return opendir(name);
26 }
27
28 int glob(const char *pattern, int flags,
29          int (*errfunc) (const char *epath, int eerrno),
30          glob_t *pglob)
31 {
32   if (!(flags & GLOB_ALTDIRFUNC)) {
33     pglob->gl_closedir = closedir;
34     pglob->gl_readdir = readdir;
35     pglob->gl_opendir = opendir;
36     pglob->gl_lstat = lstat;
37     pglob->gl_stat = stat;
38     flags |= GLOB_ALTDIRFUNC;
39   }
40   return real_glob(pattern, flags, errfunc, pglob);
41 }
42
43 static void _init() __attribute__((constructor));
44 static void _init()
45 {
46   real_glob = dlsym(RTLD_NEXT, "glob");
47   if (!real_glob) {
48     fprintf(stderr, "Would the real glob please stand up? %s\n", dlerror());
49     exit(1);
50   }
51 }