From 2cc356588e586809287fa6f3c92b437e3a32c11a Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Thu, 14 Sep 2006 08:01:05 +0000 Subject: [PATCH] dirent emulation for MSVC --- include/ac/dirent.h | 14 +++++++++ libraries/liblutil/utils.c | 61 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/include/ac/dirent.h b/include/ac/dirent.h index bba3a6a4a6..e996e7256b 100644 --- a/include/ac/dirent.h +++ b/include/ac/dirent.h @@ -20,6 +20,20 @@ #if HAVE_DIRENT_H # include # define NAMLEN(dirent) strlen((dirent)->d_name) +#elif defined(_MSC_VER) +#include +#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 diff --git a/libraries/liblutil/utils.c b/libraries/liblutil/utils.c index c7ae35cd12..e74f9093ce 100644 --- a/libraries/liblutil/utils.c +++ b/libraries/liblutil/utils.c @@ -314,6 +314,67 @@ int mkstemp( char * template ) } #endif +#ifdef _MSC_VER +#include +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 */ -- 2.39.5