--- awhttpd.old/fileserver.c	2003-06-10 20:23:09.000000000 +0200
+++ awhttpd/fileserver.c	2003-06-10 13:08:07.000000000 +0200
@@ -14,14 +14,14 @@
 
 void procfileserver(char *thedir, int i) {
 
-  DIR *dirp;
-  struct dirent *dp;
   char tpbuf[TPBUFLEN];
   int j=0;
   int isdir;
-
-  dirp = opendir(thedir);
-  if (dirp == NULL) {
+  struct dirent **namelist;
+  int n,c=0;
+  
+  n=scandir(thedir,&namelist,0,alphasort);
+  if(n<0) {
     send404(i);
     discon(i);
     return;
@@ -32,34 +32,32 @@
   bufferandsendstr(tpbuf,infd[i]);
 
   /* Get rid of directory ".", and get rid of ".." if we're at the root.  */
-  dp = readdir(dirp);
-  if (strlen(thedir+strlen(cnfdir)) == 1) dp = readdir(dirp);
-
-  while((dp = readdir(dirp)) != NULL) {
 
+  while(c<n) {
     /* Make sure privacy settings are honored. */
     #ifdef HIDEFILESSTARTINGWITHADOT
-    if ((strncmp(dp->d_name, ".", 1) != 0) &&
-        (strncmp(dp->d_name, "..", 2) != 0) &&
-        (dp->d_name[0] == '.')) continue;
+    if ((strncmp(namelist[c]->d_name, ".", 1) != 0) &&
+        (strncmp(namelist[c]->d_name, "..", 2) != 0) &&
+        (namelist[c]->d_name[0] == '.')) continue;
     #endif
     #ifdef HIDDENPREFIX
-    if (strncmp(HIDDENPREFIX, dp->d_name, strlen(HIDDENPREFIX))==0) continue;
+    if (strncmp(HIDDENPREFIX, namelist[c]->d_name, strlen(HIDDENPREFIX))==0) continue;
     #endif
 
     memset(tpbuf, '\0', TPBUFLEN);
-    snprintf(tpbuf, TPBUFLEN, "%s%s", thedir, dp->d_name);
+    snprintf(tpbuf, TPBUFLEN, "%s%s", thedir, namelist[c]->d_name);
     isdir = isadir(tpbuf);
     memset(tpbuf, '\0', TPBUFLEN);
-    snprintf(tpbuf, TPBUFLEN, "<A HREF=\"%s%s\">%s%s</A><BR>\n", dp->d_name, isdir ? "/" : "", dp->d_name, isdir ? "/" : "");
+    snprintf(tpbuf, TPBUFLEN, "<A HREF=\"%s%s\">%s%s</A><BR>\n", namelist[c]->d_name, isdir ? "/" : "", namelist[c]->d_name, isdir ? "/" : "");
     j += strlen(tpbuf);
     bufferandsendstr(tpbuf,infd[i]);
-
+    free(namelist[c]);
+    c++;
   }
 
   bufferandsendstr("<BR><BR>End of Anti-Web directory listing.</BODY></HTML>\n",infd[i]);
   sendbuffer(infd[i]);
-  closedir(dirp);
+  free(namelist);
   discon(i);
 
 }
