summaryrefslogtreecommitdiffstats
path: root/lib/libc/gen/readdir.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/gen/readdir.c')
-rw-r--r--lib/libc/gen/readdir.c56
1 files changed, 32 insertions, 24 deletions
diff --git a/lib/libc/gen/readdir.c b/lib/libc/gen/readdir.c
index 43c7bc2..9698415 100644
--- a/lib/libc/gen/readdir.c
+++ b/lib/libc/gen/readdir.c
@@ -38,20 +38,21 @@
static char sccsid[] = "@(#)readdir.c 8.3 (Berkeley) 9/29/94";
#endif /* LIBC_SCCS and not lint */
+#include "namespace.h"
#include <sys/param.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
-#ifdef _THREAD_SAFE
#include <pthread.h>
-#include "pthread_private.h"
-#endif /* _THREAD_SAFE */
+#include "un-namespace.h"
+
+#include "libc_private.h"
/*
* get next entry in a directory.
*/
struct dirent *
-readdir(dirp)
+_readdir_unlocked(dirp)
DIR *dirp;
{
struct dirent *dp;
@@ -63,7 +64,7 @@ readdir(dirp)
dirp->dd_loc = 0;
}
if (dirp->dd_loc == 0 && !(dirp->dd_flags & __DTF_READALL)) {
- dirp->dd_size = getdirentries(dirp->dd_fd,
+ dirp->dd_size = _getdirentries(dirp->dd_fd,
dirp->dd_buf, dirp->dd_len, &dirp->dd_seek);
if (dirp->dd_size <= 0)
return (NULL);
@@ -83,6 +84,22 @@ readdir(dirp)
}
}
+struct dirent *
+readdir(dirp)
+ DIR *dirp;
+{
+ struct dirent *dp;
+
+ if (__isthreaded) {
+ _pthread_mutex_lock((pthread_mutex_t *)&dirp->dd_lock);
+ dp = _readdir_unlocked(dirp);
+ _pthread_mutex_unlock((pthread_mutex_t *)&dirp->dd_lock);
+ }
+ else
+ dp = _readdir_unlocked(dirp);
+ return (dp);
+}
+
int
readdir_r(dirp, entry, result)
DIR *dirp;
@@ -91,34 +108,25 @@ readdir_r(dirp, entry, result)
{
struct dirent *dp;
int saved_errno;
-#ifdef _THREAD_SAFE
- int ret;
-
- if ((ret = _FD_LOCK(dirp->dd_fd, FD_READ, NULL)) != 0)
- return (ret);
-#endif
saved_errno = errno;
errno = 0;
- dp = readdir(dirp);
+ if (__isthreaded) {
+ _pthread_mutex_lock((pthread_mutex_t *)&dirp->dd_lock);
+ if ((dp = _readdir_unlocked(dirp)) != NULL)
+ memcpy(entry, dp, sizeof *entry);
+ _pthread_mutex_unlock((pthread_mutex_t *)&dirp->dd_lock);
+ }
+ else if ((dp = _readdir_unlocked(dirp)) != NULL)
+ memcpy(entry, dp, sizeof *entry);
+
if (errno != 0) {
- if (dp == NULL) {
-#ifdef _THREAD_SAFE
- _FD_UNLOCK(dirp->dd_fd, FD_READ);
-#endif
+ if (dp == NULL)
return (errno);
- }
} else
errno = saved_errno;
if (dp != NULL)
- memcpy(entry, dp, sizeof *entry);
-
-#ifdef _THREAD_SAFE
- _FD_UNLOCK(dirp->dd_fd, FD_READ);
-#endif
-
- if (dp != NULL)
*result = entry;
else
*result = NULL;
OpenPOWER on IntegriCloud