summaryrefslogtreecommitdiff
path: root/lib/fdopendir.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fdopendir.c')
-rw-r--r--lib/fdopendir.c83
1 files changed, 36 insertions, 47 deletions
diff --git a/lib/fdopendir.c b/lib/fdopendir.c
index a654a328dad..bdbb2ea912f 100644
--- a/lib/fdopendir.c
+++ b/lib/fdopendir.c
@@ -25,17 +25,40 @@
#if !HAVE_FDOPENDIR
-# include "openat.h"
-# include "openat-priv.h"
-# include "save-cwd.h"
+# if GNULIB_defined_DIR
+/* We are in control of the file descriptor of a DIR. */
-# if GNULIB_DIRENT_SAFER
-# include "dirent--.h"
-# endif
+# include "dirent-private.h"
-# ifndef REPLACE_FCHDIR
-# define REPLACE_FCHDIR 0
-# endif
+# if !REPLACE_FCHDIR
+# error "unexpected configuration: GNULIB_defined_DIR but fchdir not replaced"
+# endif
+
+DIR *
+fdopendir (int fd)
+{
+ char const *name = _gl_directory_name (fd);
+ DIR *dirp = name ? opendir (name) : NULL;
+ if (dirp != NULL)
+ dirp->fd_to_close = fd;
+ return dirp;
+}
+
+# else
+/* We are not in control of the file descriptor of a DIR, and therefore have to
+ play tricks with file descriptors before and after a call to opendir(). */
+
+# include "openat.h"
+# include "openat-priv.h"
+# include "save-cwd.h"
+
+# if GNULIB_DIRENT_SAFER
+# include "dirent--.h"
+# endif
+
+# ifndef REPLACE_FCHDIR
+# define REPLACE_FCHDIR 0
+# endif
static DIR *fdopendir_with_dup (int, int, struct saved_cwd const *);
static DIR *fd_clone_opendir (int, struct saved_cwd const *);
@@ -62,41 +85,6 @@ static DIR *fd_clone_opendir (int, struct saved_cwd const *);
If this function returns successfully, FD is under control of the
dirent.h system, and the caller should not close or modify the state of
FD other than by the dirent.h functions. */
-# ifdef __KLIBC__
-# include <InnoTekLIBC/backend.h>
-
-DIR *
-fdopendir (int fd)
-{
- char path[_MAX_PATH];
- DIR *dirp;
-
- /* Get a path from fd */
- if (__libc_Back_ioFHToPath (fd, path, sizeof (path)))
- return NULL;
-
- dirp = opendir (path);
- if (!dirp)
- return NULL;
-
- /* Unregister fd registered by opendir() */
- _gl_unregister_dirp_fd (dirfd (dirp));
-
- /* Register our fd */
- if (_gl_register_dirp_fd (fd, dirp))
- {
- int saved_errno = errno;
-
- closedir (dirp);
-
- errno = saved_errno;
-
- dirp = NULL;
- }
-
- return dirp;
-}
-# else
DIR *
fdopendir (int fd)
{
@@ -119,7 +107,6 @@ fdopendir (int fd)
return dir;
}
-# endif
/* Like fdopendir, except that if OLDER_DUPFD is not -1, it is known
to be a dup of FD which is less than FD - 1 and which will be
@@ -188,7 +175,7 @@ fd_clone_opendir (int fd, struct saved_cwd const *cwd)
if (proc_file != buf)
free (proc_file);
}
-# if REPLACE_FCHDIR
+# if REPLACE_FCHDIR
if (! dir && EXPECTED_ERRNO (saved_errno))
{
char const *name = _gl_directory_name (fd);
@@ -203,7 +190,7 @@ fd_clone_opendir (int fd, struct saved_cwd const *cwd)
return dp;
}
-# endif
+# endif
errno = saved_errno;
return dir;
}
@@ -223,6 +210,8 @@ fd_clone_opendir (int fd, struct saved_cwd const *cwd)
}
}
+# endif
+
#else /* HAVE_FDOPENDIR */
# include <errno.h>