readdir()
is not documented to return REREMOTEIO
, so most likely sterror()
gives misleading information.
Set errno
to 0
before entering the while()
loop, that is before calling readdir()
.
From man readdir
:
If the end of the directory stream is reached, NULL is returned and errno is not
changed. If an error occurs, NULL is returned and errno is set appropriately. To distinguish end of stream and from an
error, set errno to zero before calling readdir() and then check the
value of errno if NULL is returned.
To test these two cases when readdir()
returns NULL
you might like to modify your code like this:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
...
DIR * dir_ptr = opendir("/tmp");
if (NULL != dir_ptr)
{
perror("opendir() failed");
}
else
{
struct dirent * dir_entery;
errno = 0;
while ((dir_entery = readdir(dir_ptr))) /* an extra pair of parenthesis here to silence GCC */
{
printf("%s\n", dir_entery->d_name);
}
if (0 != errno)
{
perror("readdir() failed");
}
else
{
printf("No more entries.\n");
}
}
...