You may also use access
in combination with opendir
to determine if the directory exists, and, if the name exists, but is not a directory. For example:
/* test that dir exists (1 success, -1 does not exist, -2 not dir) */
int
xis_dir (const char *d)
{
DIR *dirptr;
if (access ( d, F_OK ) != -1 ) {
// file exists
if ((dirptr = opendir (d)) != NULL) {
closedir (dirptr); /* d exists and is a directory */
} else {
return -2; /* d exists but is not a directory */
}
} else {
return -1; /* d does not exist */
}
return 1;
}