summaryrefslogtreecommitdiff
path: root/libc/misc
diff options
context:
space:
mode:
Diffstat (limited to 'libc/misc')
-rw-r--r--libc/misc/dirent/scandir.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/libc/misc/dirent/scandir.c b/libc/misc/dirent/scandir.c
index aba63f20b..ad997b121 100644
--- a/libc/misc/dirent/scandir.c
+++ b/libc/misc/dirent/scandir.c
@@ -35,8 +35,21 @@ int scandir(const char *dir, struct dirent ***namelist,
__set_errno (0);
pos = 0;
- while ((current = readdir (dp)) != NULL)
- if (selector == NULL || (*selector) (current))
+ while ((current = readdir (dp)) != NULL) {
+ int use_it = selector == NULL;
+
+ if (! use_it)
+ {
+ use_it = (*selector) (current);
+ /*
+ * The selector function might have changed errno.
+ * It was zero before and it need to be again to make
+ * the latter tests work.
+ */
+ if (! use_it)
+ __set_errno (0);
+ }
+ if (use_it)
{
struct dirent *vnew;
size_t dsize;
@@ -64,6 +77,7 @@ int scandir(const char *dir, struct dirent ***namelist,
names[pos++] = (struct dirent *) memcpy (vnew, current, dsize);
}
+ }
if (unlikely(errno != 0))
{