79 #define WIN32_LEAN_AND_MEAN 83 #include <sys/types.h> 88 #if !defined(FILE_ATTRIBUTE_DEVICE) 89 # define FILE_ATTRIBUTE_DEVICE 0x40 93 #if defined(_MSC_VER) && !defined(S_IREAD) 94 # define S_IFMT _S_IFMT 95 # define S_IFDIR _S_IFDIR 96 # define S_IFCHR _S_IFCHR 97 # define S_IFFIFO _S_IFFIFO 98 # define S_IFREG _S_IFREG 99 # define S_IREAD _S_IREAD 100 # define S_IWRITE _S_IWRITE 101 # define S_IEXEC _S_IEXEC 107 #if defined(_MSC_VER) 108 # define S_IRUSR S_IREAD 109 # define S_IWUSR S_IWRITE 120 #define _DIRENT_HAVE_D_TYPE 124 #define DT_REG S_IFREG 125 #define DT_DIR S_IFDIR 126 #define DT_FIFO S_IFFIFO 127 #define DT_SOCK S_IFSOCK 128 #define DT_CHR S_IFCHR 129 #define DT_BLK S_IFBLK 132 #define IFTODT(mode) ((mode) & S_IFMT) 133 #define DTTOIF(type) (type) 141 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFFIFO) 142 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) 143 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) 144 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) 145 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) 146 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) 147 #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) 154 typedef struct dirent
156 char d_name[MAX_PATH + 1];
165 WIN32_FIND_DATAA find_data;
167 HANDLE search_handle;
168 char patt[MAX_PATH + 3];
173 static DIR *opendir(
const char *dirname);
174 static struct dirent *readdir(DIR *dirp);
175 static int closedir(DIR *dirp);
176 static void rewinddir(DIR* dirp);
180 #if defined(_MSC_VER) && _MSC_VER >= 1400 181 # define DIRENT_STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE) 183 # define DIRENT_STRNCPY(dest,src,size) strncpy((dest),(src),(size)) 187 #if defined(_MSC_VER) 188 #define DIRENT_SET_ERRNO(x) _set_errno (x) 190 #define DIRENT_SET_ERRNO(x) (errno = (x)) 199 static DIR *opendir(
const char *dirname)
204 if (dirname == NULL) {
205 DIRENT_SET_ERRNO (ENOENT);
208 if (strlen (dirname) + 3 >= MAX_PATH) {
209 DIRENT_SET_ERRNO (ENAMETOOLONG);
214 dirp = (DIR*) malloc (
sizeof (
struct DIR));
223 if (GetFullPathNameA (dirname, MAX_PATH, dirp->patt, NULL)) {
227 p = strchr (dirp->patt,
'\0');
228 if (dirp->patt < p && *(p-1) !=
'\\' && *(p-1) !=
':') {
235 dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->find_data);
236 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
242 DIRENT_SET_ERRNO (ENOENT);
247 DIRENT_SET_ERRNO (ENOMEM);
268 static struct dirent *readdir(DIR *dirp)
273 DIRENT_SET_ERRNO (EBADF);
278 if (dirp->cached != 0) {
283 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
286 if (FindNextFileA (dirp->search_handle, &dirp->find_data) == FALSE) {
288 FindClose (dirp->search_handle);
289 dirp->search_handle = INVALID_HANDLE_VALUE;
295 DIRENT_STRNCPY ( dirp->curentry.d_name,
296 dirp->find_data.cFileName,
297 sizeof(dirp->curentry.d_name) );
298 dirp->curentry.d_name[MAX_PATH] =
'\0';
301 dirp->curentry.d_namlen = strlen (dirp->curentry.d_name);
304 attr = dirp->find_data.dwFileAttributes;
305 if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
306 dirp->curentry.d_type = DT_CHR;
307 }
else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
308 dirp->curentry.d_type = DT_DIR;
310 dirp->curentry.d_type = DT_REG;
312 return &dirp->curentry;
321 static int closedir(DIR *dirp)
325 DIRENT_SET_ERRNO (EBADF);
330 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
331 FindClose (dirp->search_handle);
332 dirp->search_handle = INVALID_HANDLE_VALUE;
348 static void rewinddir(DIR* dirp)
352 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
353 FindClose (dirp->search_handle);
357 dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->find_data);
358 if (dirp->search_handle != INVALID_HANDLE_VALUE) {