Cross platform way of testing whether a file is a directory Cross platform way of testing whether a file is a directory unix unix

Cross platform way of testing whether a file is a directory


Ref http://www.nexenta.org/os/Porting_Codefixes:

The struct dirent definition in solaris does not contain the d_type field. You would need to make the changes as follows

if (de->d_type == DT_DIR){   return 0;}

changes to

struct stat s; /*include sys/stat.h if necessary */....stat(de->d_name, &s);if (s.st_mode & S_IFDIR){  return 0;}

Since stat is also POSIX standard it should be more cross-platform. But you may want to use if ((s.st_mode & S_IFMT) == S_IFDIR) to follow the standard.