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");
}
}
...
EREMOTEIO
(Remote I/O error). Perhaps there are permission problems on the target file system. Did you tried your program to run with root (supper user) permission, if you are in Ubuntu try withsudo
– Grijesh Chauhan/tmp
:chmod -R 0777 /tmp
– 0x90ls -la /tmp
? Hide the actual file names if you need to keep them private. – jxh