1 /****************************************************************************
3 Declaration 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 Reviewed by Ramon Santamaria for raylib on January 2020.
9 Copyright Kevlin Henney, 1997, 2003. All rights reserved.
11 Permission to use, copy, modify, and distribute this software and its
12 documentation for any purpose is hereby granted without fee, provided
13 that this copyright and permissions notice appear in all copies and
16 This software is supplied "as is" without express or implied warranty.
18 But that said, if there are any problems please get in touch.
20 ****************************************************************************/
25 // Allow custom memory allocators
27 #define DIRENT_MALLOC(sz) malloc(sz)
30 #define DIRENT_FREE(p) free(p)
33 //----------------------------------------------------------------------------------
34 // Types and Structures Definition
35 //----------------------------------------------------------------------------------
37 // Fordward declaration of DIR, implementation below
38 typedef struct DIR DIR;
48 //------------------------------------------------------------------------------------
49 // Functions Declaration
50 //------------------------------------------------------------------------------------
51 DIR *opendir(const char *name);
52 int closedir(DIR *dir);
53 struct dirent *readdir(DIR *dir);
54 void rewinddir(DIR *dir);
62 /****************************************************************************
64 Implementation of POSIX directory browsing functions and types for Win32.
66 Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
67 History: Created March 1997. Updated June 2003.
68 Reviewed by Ramon Santamaria for raylib on January 2020.
70 Copyright Kevlin Henney, 1997, 2003. All rights reserved.
72 Permission to use, copy, modify, and distribute this software and its
73 documentation for any purpose is hereby granted without fee, provided
74 that this copyright and permissions notice appear in all copies and
77 This software is supplied "as is" without express or implied warranty.
79 But that said, if there are any problems please get in touch.
81 ****************************************************************************/
83 #include <io.h> // _findfirst and _findnext set errno iff they return -1
88 //----------------------------------------------------------------------------------
89 // Types and Structures Definition
90 //----------------------------------------------------------------------------------
91 typedef ptrdiff_t handle_type; // C99's intptr_t not sufficiently portable
94 handle_type handle; // -1 for failed rewind
95 struct _finddata_t info;
96 struct dirent result; // d_name null iff first time
97 char *name; // null-terminated char string
100 DIR *opendir(const char *name)
106 size_t base_length = strlen(name);
108 // Search pattern must end with suitable wildcard
109 const char *all = strchr("/\\", name[base_length - 1]) ? "*" : "/*";
111 if ((dir = (DIR *)DIRENT_MALLOC(sizeof *dir)) != 0 &&
112 (dir->name = (char *)DIRENT_MALLOC(base_length + strlen(all) + 1)) != 0)
114 strcat(strcpy(dir->name, name), all);
116 if ((dir->handle = (handle_type) _findfirst(dir->name, &dir->info)) != -1)
118 dir->result.d_name = 0;
122 DIRENT_FREE(dir->name);
139 int closedir(DIR *dir)
145 if (dir->handle != -1) result = _findclose(dir->handle);
147 DIRENT_FREE(dir->name);
151 // NOTE: All errors ampped to EBADF
152 if (result == -1) errno = EBADF;
157 struct dirent *readdir(DIR *dir)
159 struct dirent *result = 0;
161 if (dir && dir->handle != -1)
163 if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
165 result = &dir->result;
166 result->d_name = dir->info.name;
174 void rewinddir(DIR *dir)
176 if (dir && dir->handle != -1)
178 _findclose(dir->handle);
179 dir->handle = (handle_type) _findfirst(dir->name, &dir->info);
180 dir->result.d_name = 0;