diff options
author | Carmelo Amoroso <carmelo.amoroso@st.com> | 2008-10-06 15:04:46 +0000 |
---|---|---|
committer | Carmelo Amoroso <carmelo.amoroso@st.com> | 2008-10-06 15:04:46 +0000 |
commit | ab490047099069aadbb0c30d7765d4dd00318b85 (patch) | |
tree | 1666d7b16b94a4431cec10c12af5bf0a882793a9 | |
parent | acbd962f90dfe3d5a7cb5614e39bb814310ab86b (diff) |
Fix scandir function to reset the errno when the
selector returns zero(no entries) modifying the errno.
The attached test case implements a dummy filter that returns
alway no entries, but change the errno. scandir is not expected
to fail, just returning 0 entries.
Signed-off-by: Filippo Arcidiacono <filippo.arcidiacono@st.com>
Signed-off-by: Giuseppe Cavallaro <peppe.cavallaro@st.com>
Signed-off-by: Carmelo Amoroso <carmelo.amoroso@st.com>
-rw-r--r-- | libc/misc/dirent/scandir.c | 18 | ||||
-rw-r--r-- | test/misc/tst-scandir.c | 21 |
2 files changed, 37 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)) { diff --git a/test/misc/tst-scandir.c b/test/misc/tst-scandir.c new file mode 100644 index 000000000..6e4646142 --- /dev/null +++ b/test/misc/tst-scandir.c @@ -0,0 +1,21 @@ +#include <dirent.h> +#include <errno.h> + +int skip_all(const struct dirent *dirbuf) +{ + __set_errno(EBADF); + return 0; +} + +int main() +{ + struct dirent **namelist; + int n; + + n = scandir(".", &namelist, skip_all, 0); + if (n < 0) { + perror("scandir"); + return 1; + } + return 0; +} |