Don't use readdir_r

readdir_r can easily lead to stack overflows if the path is longer than
the space reserved for d_name in dirent. While some OSes use a length of
MAXNAMLEN + 1, others use just 1. This could be worked around by always
allocating a buffer of sizeof(struct dirent) + MAXNAMLEN (which would
work only in case MAXNAMLEN is actually the longest readdir_r can
return), but is probably not worth the risk, especially as glibc has
entirely deprecated readdir_r and it is expected to be removed from
POSIX.