3
Implementation of POSIX directory browsing functions and types for Win32.
5
Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
6
History: Created March 1997. Updated June 2003.
7
Rights: See end of file.
15
#if !defined(HAVE_DIRENT_H) && !defined(HAVE_NDIR_H) && !defined(HAVE_SYS_DIR_H) && !defined(HAVE_SYS_NDIR_H)
16
#ifdef HAVE__FINDFIRST
21
#include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
32
long handle; /* -1 for failed rewind */
33
struct _finddata_t info;
34
struct dirent result; /* d_name null iff first time */
35
char *name; /* null-terminated char string */
38
DIR *opendir(const char *name)
44
size_t base_length = strlen(name);
45
const char *all = /* search pattern must end with suitable wildcard */
46
strchr("/\\", name[base_length - 1]) ? "*" : "/*";
48
if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
49
(dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0)
51
strcat(strcpy(dir->name, name), all);
53
if((dir->handle = (long) _findfirst(dir->name, &dir->info)) != -1)
55
dir->result.d_name = 0;
79
int closedir(DIR *dir)
87
result = _findclose(dir->handle);
94
if(result == -1) /* map all errors to EBADF */
102
struct dirent *readdir(DIR *dir)
104
struct dirent *result = 0;
106
if(dir && dir->handle != -1)
108
if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
110
result = &dir->result;
111
result->d_name = dir->info.name;
122
void rewinddir(DIR *dir)
124
if(dir && dir->handle != -1)
126
_findclose(dir->handle);
127
dir->handle = (long) _findfirst(dir->name, &dir->info);
128
dir->result.d_name = 0;
145
Copyright Kevlin Henney, 1997, 2003. All rights reserved.
147
Permission to use, copy, modify, and distribute this software and its
148
documentation for any purpose is hereby granted without fee, provided
149
that this copyright and permissions notice appear in all copies and
152
This software is supplied "as is" without express or implied warranty.
154
But that said, if there are any problems please get in touch.