summaryrefslogtreecommitdiff
path: root/lib/closedir.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/closedir.c')
-rw-r--r--lib/closedir.c28
1 files changed, 19 insertions, 9 deletions
diff --git a/lib/closedir.c b/lib/closedir.c
index 0e004afeca..3777e9f70d 100644
--- a/lib/closedir.c
+++ b/lib/closedir.c
@@ -23,31 +23,37 @@
# include <unistd.h>
#endif
+#include <stdlib.h>
+
#if HAVE_CLOSEDIR
/* Override closedir(), to keep track of the open file descriptors.
Needed because there is a function dirfd(). */
-#else
-
-# include <stdlib.h>
+#endif
+#if GNULIB_defined_DIR
# include "dirent-private.h"
-
#endif
int
closedir (DIR *dirp)
+#undef closedir
{
-# if REPLACE_FCHDIR || REPLACE_DIRFD
+#if GNULIB_defined_DIR || REPLACE_FCHDIR || defined __KLIBC__
int fd = dirfd (dirp);
-# endif
+#endif
int retval;
-#if HAVE_CLOSEDIR
-# undef closedir
+#if HAVE_DIRENT_H /* equivalent to HAVE_CLOSEDIR */
+# if GNULIB_defined_DIR
+ retval = closedir (dirp->real_dirp);
+ if (retval >= 0)
+ free (dirp);
+# else
retval = closedir (dirp);
+# endif
# ifdef __KLIBC__
if (!retval)
@@ -63,9 +69,13 @@ closedir (DIR *dirp)
#endif
-#if REPLACE_FCHDIR
+#if GNULIB_defined_DIR
+ if (retval >= 0)
+ close (fd);
+#elif REPLACE_FCHDIR
if (retval >= 0)
_gl_unregister_fd (fd);
#endif
+
return retval;
}