Since I found that the above approved answer lacks some clarity and the op provides an incorrect solution that he/she will use. I therefore hope that the below example will help others. The solution is more or less portable as well.
/******************************************************************************
* Checks to see if a directory exists. Note: This method only checks the
* existence of the full path AND if path leaf is a dir.
*
* @return >0 if dir exists AND is a dir,
* 0 if dir does not exist OR exists but not a dir,
* <0 if an error occurred (errno is also set)
*****************************************************************************/
int dirExists(const char* const path)
{
struct stat info;
int statRC = stat( path, &info );
if( statRC != 0 )
{
if (errno == ENOENT) { return 0; } // something along the path does not exist
if (errno == ENOTDIR) { return 0; } // something in path prefix is not a dir
return -1;
}
return ( info.st_mode & S_IFDIR ) ? 1 : 0;
}