]> git.sur5r.net Git - openldap/commitdiff
dirent emulation for MSVC
authorHoward Chu <hyc@openldap.org>
Thu, 14 Sep 2006 08:01:05 +0000 (08:01 +0000)
committerHoward Chu <hyc@openldap.org>
Thu, 14 Sep 2006 08:01:05 +0000 (08:01 +0000)
include/ac/dirent.h
libraries/liblutil/utils.c

index bba3a6a4a6cd33f3b9db89fa5beac8f223c4dbe1..e996e7256b7b3b5cbff1fadb9b4019159c6ae7f6 100644 (file)
 #if HAVE_DIRENT_H
 # include <dirent.h>
 # define NAMLEN(dirent) strlen((dirent)->d_name)
+#elif defined(_MSC_VER)
+#include <windows.h>
+#ifndef MAX_PATH
+#define MAX_PATH       260
+#endif
+struct dirent {
+       char *d_name;
+};
+typedef struct DIR {
+       HANDLE dir;
+       struct dirent data;
+       int first;
+       char buf[MAX_PATH+1];
+} DIR;
 #else
 # define dirent direct
 # define NAMLEN(dirent) (dirent)->d_namlen
index c7ae35cd122a6e89de0421810baf8619a4782fbd..e74f9093ced1da465c7d479bbb234232f01a8034 100644 (file)
@@ -314,6 +314,67 @@ int mkstemp( char * template )
 }
 #endif
 
+#ifdef _MSC_VER
+#include <windows.h>
+struct dirent {
+       char *d_name;
+};
+typedef struct DIR {
+       HANDLE dir;
+       struct dirent data;
+       int first;
+       char buf[MAX_PATH+1];
+} DIR;
+DIR *opendir( char *path )
+{
+       char tmp[32768];
+       int len = strlen(path);
+       DIR *d;
+       HANDLE h;
+       WIN32_FIND_DATA data;
+       
+       if (len+3 >= sizeof(tmp))
+               return NULL;
+
+       strcpy(tmp, path);
+       tmp[len++] = '\\';
+       tmp[len++] = '*';
+       tmp[len] = '\0';
+
+       h = FindFirstFile( tmp, &data );
+       
+       if ( h == INVALID_HANDLE_VALUE )
+               return NULL;
+
+       d = ber_memalloc( sizeof(DIR) );
+       if ( !d )
+               return NULL;
+       d->dir = h;
+       d->data.d_name = d->buf;
+       d->first = 1;
+       strcpy(d->data.d_name, data.cFileName);
+       return d;
+}
+struct dirent *readdir(DIR *dir)
+{
+       WIN32_FIND_DATA data;
+
+       if (dir->first) {
+               dir->first = 0;
+       } else {
+               if (!FindNextFile(dir->dir, &data))
+                       return NULL;
+               strcpy(dir->data.d_name, data.cFileName);
+       }
+       return &dir->data;
+}
+void closedir(DIR *dir)
+{
+       FindClose(dir->dir);
+       ber_memfree(dir);
+}
+#endif
+
 /*
  * Memory Reverse Search
  */