/* This file is part of Lcrzo.
*/

int lcrzo_dir_exist(const char *dirname,
		    lcrzo_bool *pyes)
{ int ret;
  struct stat st;
  lcrzo_bool fileexists;
  lcrzo_string cleandirname;

  fileexists=LCRZO_FALSE;

  lcrzo_er(lcrzo_priv_dirname_removelastslash(dirname, &cleandirname));
  ret=lcrzo_priv_filename_stat_get(cleandirname, &st);
  lcrzo_string_free(cleandirname);
  if (ret==LCRZO_ERR_OK)
  { 
#if defined LCRZODEF_SYSTEM_Unix
    if (S_ISDIR(st.st_mode))
#elif defined LCRZODEF_SYSTEM_Windows
    if ( st.st_mode & _S_IFDIR )
#else
 #error "Unknown value for LCRZODEF_SYSTEM"
#endif
    { fileexists=LCRZO_TRUE;
    }
  }
  else if (ret!=LCRZO_ERR_OKFILENOTFOUND )
  { return(ret);
  }
  lcrzo_err_purge();

  if (pyes!=NULL)
  { *pyes=fileexists;
  }

  return(LCRZO_ERR_OK);
}

