Index: trunk/src/sh_dbIO.c
===================================================================
--- trunk/src/sh_dbIO.c	(revision 562)
+++ trunk/src/sh_dbIO.c	(revision 564)
@@ -1512,8 +1512,7 @@
  	}
     }
-	 
-  if (!buf) {
-    memset(&p, 0, sizeof(sh_filestore_t));
-  }
+
+  /* unconditionally initialize the structure */
+  memset(&p, 0, sizeof(sh_filestore_t));
 
   if (buf != NULL) 
Index: trunk/src/sh_files.c
===================================================================
--- trunk/src/sh_files.c	(revision 562)
+++ trunk/src/sh_files.c	(revision 564)
@@ -20,5 +20,35 @@
 #include "config_xor.h"
 
-#if defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
+#if defined(HAVE_PTHREAD_MUTEX_RECURSIVE) && defined(HAVE_DIRENT_H) && defined(HAVE_SCANDIR)
+
+/* Linux */
+#if defined(__linux__)
+#define _XOPEN_SOURCE 700
+#define SH_USE_SCANDIR 1
+
+/* FreeBSD */
+#elif defined(__FreeBSD__)
+
+#if __FreeBSD__ >= 8
+#define __XSI_VISIBLE 700
+#define SH_USE_SCANDIR 1
+#endif
+
+/* OpenBSD */
+#elif defined(__OpenBSD__)
+#define __POSIX_VISIBLE 200809
+#define SH_USE_SCANDIR 1
+
+#elif defined(__NetBSD__)
+#define _NETBSD_SOURCE
+#define SH_USE_SCANDIR 1
+
+/* other os */
+#else
+#define _XOPEN_SOURCE 500
+
+#endif
+
+#elif defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
 #define _XOPEN_SOURCE 500
 #endif
@@ -36,5 +66,4 @@
 #include <sys/types.h>
 #include <unistd.h>
-#include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -2081,4 +2110,8 @@
   DIR *           thisDir = NULL;
   struct dirent * thisEntry;
+#if defined(SH_USE_SCANDIR)
+  struct dirent **entryList;
+  int             entry;
+#endif
   int             status;
   int             dummy = S_FALSE;
@@ -2267,6 +2300,13 @@
   dirlist_orig = NULL;
 
+#if defined(SH_USE_SCANDIR)
+  entry = scandir(iname, &entryList, 0, alphasort);
+  while(entry > 0) { /* scandir() may return -1 on error! */
+      entry--;
+      thisEntry = entryList[entry];
+#else
   do {
       thisEntry = readdir (thisDir);
+#endif
       if (thisEntry != NULL) 
 	{
@@ -2284,6 +2324,9 @@
 	  dirlist = addto_sh_dirlist (thisEntry, dirlist);
 	}
-  } while (thisEntry != NULL);
-
+  }
+#if !defined(SH_USE_SCANDIR)  
+  while (thisEntry != NULL);
+#endif
+  
   SH_MUTEX_UNLOCK(mutex_readdir);
 
