Fix for filesystems without working d_type

If the filesystem doesn't properly suppport dirent's d_type field and uses
D_UNKNOWN, use stat() to check whether a file is a directory or not.

At the same time, put d_type behind an #ifdef so the code should compile also on
system that don't have d_type in the first place.