0/****************************************************************************
2 Declaration of POSIX directory browsing functions and types for Win32.
4 Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
5 History: Created March 1997. Updated June 2003.
6 Reviewed by Ramon Santamaria for raylib on January 2020.
8 Copyright Kevlin Henney, 1997, 2003. All rights reserved.
10 Permission to use, copy, modify, and distribute this software and its
11 documentation for any purpose is hereby granted without fee, provided
12 that this copyright and permissions notice appear in all copies and
13 derivatives.
15 This software is supplied "as is" without express or implied warranty.
17 But that said, if there are any problems please get in touch.
19****************************************************************************/
21#ifndef DIRENT_H
22#define DIRENT_H
24// Allow custom memory allocators
25#ifndef DIRENT_MALLOC
26 #define DIRENT_MALLOC(sz) malloc(sz)
27#endif
28#ifndef DIRENT_FREE
29 #define DIRENT_FREE(p) free(p)
30#endif
32//----------------------------------------------------------------------------------
33// Types and Structures Definition
34//----------------------------------------------------------------------------------
36// Fordward declaration of DIR, implementation below
37typedef struct DIR DIR;
39struct dirent {
40 char *d_name;
41};
43#ifdef __cplusplus
44extern "C" {
45#endif
47//------------------------------------------------------------------------------------
48// Functions Declaration
49//------------------------------------------------------------------------------------
50DIR *opendir(const char *name);
51int closedir(DIR *dir);
52struct dirent *readdir(DIR *dir);
53void rewinddir(DIR *dir);
55#ifdef __cplusplus
56}
57#endif
59#endif // DIRENT_H
61/****************************************************************************
63 Implementation of POSIX directory browsing functions and types for Win32.
65 Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
66 History: Created March 1997. Updated June 2003.
67 Reviewed by Ramon Santamaria for raylib on January 2020.
69 Copyright Kevlin Henney, 1997, 2003. All rights reserved.
71 Permission to use, copy, modify, and distribute this software and its
72 documentation for any purpose is hereby granted without fee, provided
73 that this copyright and permissions notice appear in all copies and
74 derivatives.
76 This software is supplied "as is" without express or implied warranty.
78 But that said, if there are any problems please get in touch.
80****************************************************************************/
82#include <io.h> // _findfirst and _findnext set errno iff they return -1
83#include <stdlib.h>
84#include <string.h>
85#include <errno.h>
87//----------------------------------------------------------------------------------
88// Types and Structures Definition
89//----------------------------------------------------------------------------------
90typedef ptrdiff_t handle_type; // C99's intptr_t not sufficiently portable
92struct DIR {
93 handle_type handle; // -1 for failed rewind
94 struct _finddata_t info;
95 struct dirent result; // d_name null iff first time
96 char *name; // null-terminated char string
97};
99DIR *opendir(const char *name)
100{
101 DIR *dir = 0;
103 if (name && name[0])
104 {
105 size_t base_length = strlen(name);
107 // Search pattern must end with suitable wildcard
108 const char *all = strchr("/\\", name[base_length - 1]) ? "*" : "/*";
110 if ((dir = (DIR *)DIRENT_MALLOC(sizeof *dir)) != 0 &&
111 (dir->name = (char *)DIRENT_MALLOC(base_length + strlen(all) + 1)) != 0)
112 {
113 strcat(strcpy(dir->name, name), all);
115 if ((dir->handle = (handle_type) _findfirst(dir->name, &dir->info)) != -1)
116 {
117 dir->result.d_name = 0;
118 }
119 else // rollback
120 {
121 DIRENT_FREE(dir->name);
122 DIRENT_FREE(dir);
123 dir = 0;
124 }
125 }
126 else // rollback
127 {
128 DIRENT_FREE(dir);
129 dir = 0;
130 errno = ENOMEM;
131 }
132 }
133 else errno = EINVAL;
135 return dir;
136}
138int closedir(DIR *dir)
139{
140 int result = -1;
142 if (dir)
143 {
144 if (dir->handle != -1) result = _findclose(dir->handle);
146 DIRENT_FREE(dir->name);
147 DIRENT_FREE(dir);
148 }
150 // NOTE: All errors ampped to EBADF
151 if (result == -1) errno = EBADF;
153 return result;
154}
156struct dirent *readdir(DIR *dir)
157{
158 struct dirent *result = 0;
160 if (dir && dir->handle != -1)
161 {
162 if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
163 {
164 result = &dir->result;
165 result->d_name = dir->info.name;
166 }
167 }
168 else errno = EBADF;
170 return result;
171}
173void rewinddir(DIR *dir)
174{
175 if (dir && dir->handle != -1)
176 {
177 _findclose(dir->handle);
178 dir->handle = (handle_type) _findfirst(dir->name, &dir->info);
179 dir->result.d_name = 0;
180 }
181 else errno = EBADF;
182}
index : raylib-jai
Bindings from https://solarium.technology