Debian Codegraph @

libs/zziplib/zziplib-src/zzip/__dirent.h

raw
1 #ifndef ZZIP_INTERNAL_DIRENT_H
2 #define ZZIP_INTERNAL_DIRENT_H
3 #include <zzip/conf.h>
4
5 /*
6 * DO NOT USE THIS CODE.
7 *
8 * It is an internal header file for zziplib that carries some inline
9 * functions (or just static members) and a few defines, simply to be
10 * able to reuse these across - and have everything in a specific place.
11 *
12 * Copyright (c) Guido Draheim, use under copyleft (LGPL,MPL)
13 */
14
15 #ifdef ZZIP_HAVE_DIRENT_H
16 #define USE_DIRENT 1
17
18 #define _zzip_opendir opendir
19 #define _zzip_readdir readdir
20 #define _zzip_closedir closedir
21 #define _zzip_rewinddir rewinddir
22 #define _zzip_telldir telldir
23 #define _zzip_seekdir seekdir
24 #define _zzip_DIR DIR
25
26 #include <dirent.h>
27
28 #elif defined ZZIP_HAVE_WINBASE_H
29 #define USE_DIRENT 2
30
31 #define _zzip_opendir win32_opendir
32 #define _zzip_readdir win32_readdir
33 #define _zzip_closedir win32_closedir
34 #define _zzip_rewinddir win32_rewinddir
35 #define _zzip_telldir win32_telldir
36 #define _zzip_seekdir win32_seekdir
37 #define _zzip_DIR DIR
38
39 /*
40 * DIRENT.H (formerly DIRLIB.H)
41 *
42 * by M. J. Weinstein Released to public domain 1-Jan-89
43 *
44 * Because I have heard that this feature (opendir, readdir, closedir)
45 * it so useful for programmers coming from UNIX or attempting to port
46 * UNIX code, and because it is reasonably light weight, I have included
47 * it in the Mingw32 package. I have also added an implementation of
48 * rewinddir, seekdir and telldir.
49 * - Colin Peters <[email protected]>
50 *
51 * This code is distributed in the hope that is will be useful but
52 * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
53 * DISCLAMED. This includeds but is not limited to warranties of
54 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
55 *
56 */
57
58 #include <io.h>
59
60 struct dirent
61 {
62 long d_ino; /* Always zero. */
63 unsigned short d_reclen; /* Always zero. */
64 unsigned short d_namlen; /* Length of name in d_name. */
65 char* d_name; /* File name. */
66 /* NOTE: The name in the dirent structure points to the name in the
67 * finddata_t structure in the DIR. */
68 };
69
70 /*
71 * This is an internal data structure. Good programmers will not use it
72 * except as an argument to one of the functions below.
73 */
74 typedef struct
75 {
76 /* disk transfer area for this dir */
77 struct _finddata_t dd_dta;
78
79 /* dirent struct to return from dir (NOTE: this makes this thread
80 * safe as long as only one thread uses a particular DIR struct at
81 * a time) */
82 struct dirent dd_dir;
83
84 /* _findnext handle */
85 long dd_handle;
86
87 /*
88 * Status of search:
89 * 0 = not started yet (next entry to read is first entry)
90 * -1 = off the end
91 * positive = 0 based index of next entry
92 */
93 short dd_stat;
94
95 /* given path for dir with search pattern (struct is extended) */
96 char dd_name[1];
97 } DIR;
98
99 /*
100 * dirent.c
101 *
102 * Derived from DIRLIB.C by Matt J. Weinstein
103 * This note appears in the DIRLIB.H
104 * DIRLIB.H by M. J. Weinstein Released to public domain 1-Jan-89
105 *
106 * Updated by Jeremy Bettis <[email protected]>
107 * Significantly revised and rewinddir, seekdir and telldir added by Colin
108 * Peters <[email protected]>
109 */
110
111 #include <direct.h>
112 #include <errno.h>
113 #include <io.h>
114 #include <stdlib.h>
115 #include <string.h>
116 #include <sys/stat.h>
117
118 #define win32_SUFFIX "*"
119 #define win32_SLASH "\\"
120
121 #ifndef S_ISDIR
122 #define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR) /* is a directory */
123 #endif S_ISDIR
124
125
126 /*
127 opendir
128
129 Returns a pointer to a DIR structure appropriately filled in to begin
130 searching a directory.
131 */
132 static DIR*
133 win32_opendir (const char *szPath)
134 {
135 DIR *nd;
136 struct _stat statDir;
137
138 errno = 0;
139
140 if (!szPath) {
141 errno = EFAULT;
142 return (DIR *) 0;
143 }
144
145 if (szPath[0] == '\0') {
146 errno = ENOTDIR;
147 return (DIR *) 0;
148 }
149
150 /* Attempt to determine if the given path really is a directory. */
151 if (_stat (szPath, &statDir)) {
152 /* Error, stat should have set an error value. */
153 return (DIR *) 0;
154 }
155
156 if (!S_ISDIR (statDir.st_mode)) {
157 /* Error, stat reports not a directory. */
158 errno = ENOTDIR;
159 return (DIR *) 0;
160 }
161
162 /* Allocate enough space to store DIR structure and the complete *
163 directory path given. */
164 nd = (DIR *) calloc (1, sizeof (DIR) + strlen (szPath)
165 + strlen (win32_SLASH) + strlen (win32_SUFFIX));
166
167 if (!nd) {
168 /* Error, out of memory. */
169 errno = ENOMEM;
170 return (DIR *) 0;
171 }
172
173 /* Create the search expression. */
174 strcpy (nd->dd_name, szPath);
175
176 /* Add on a slash if the path does not end with one. */
177 if (nd->dd_name[0] != '\0' &&
178 nd->dd_name[strlen (nd->dd_name) - 1] != '/' &&
179 nd->dd_name[strlen (nd->dd_name) - 1] != '\\') {
180 strcat (nd->dd_name, win32_SLASH);
181 }
182
183 /* Add on the search pattern */
184 strcat (nd->dd_name, win32_SUFFIX);
185
186 /* Initialize handle to -1 so that a premature closedir doesn't try * to
187 call _findclose on it. */
188 nd->dd_handle = -1;
189
190 /* Initialize the status. */
191 nd->dd_stat = 0;
192
193 /* Initialize the dirent structure. ino and reclen are invalid under *
194 Win32, and name simply points at the appropriate part of the *
195 findfirst_t structure. */
196 nd->dd_dir.d_ino = 0;
197 nd->dd_dir.d_reclen = 0;
198 nd->dd_dir.d_namlen = 0;
199 nd->dd_dir.d_name = nd->dd_dta.name;
200
201 return nd;
202 }
203
204 /*
205 readdir
206
207 Return a pointer to a dirent structure filled with the information on the
208 next entry in the directory.
209 */
210 static struct dirent *
211 win32_readdir (DIR * dirp)
212 {
213 errno = 0;
214
215 /* Check for valid DIR struct. */
216 if (!dirp) {
217 errno = EFAULT;
218 return (struct dirent *) 0;
219 }
220
221 if (dirp->dd_dir.d_name != dirp->dd_dta.name) {
222 /* The structure does not seem to be set up correctly. */
223 errno = EINVAL;
224 return (struct dirent *) 0;
225 }
226
227 if (dirp->dd_stat < 0) {
228 /* We have already returned all files in the directory * (or the
229 structure has an invalid dd_stat). */
230 return (struct dirent *) 0;
231 } else if (dirp->dd_stat == 0) {
232 /* We haven't started the search yet. */
233 /* Start the search */
234 dirp->dd_handle = _findfirst (dirp->dd_name, &(dirp->dd_dta));
235
236 if (dirp->dd_handle == -1) {
237 /* Whoops! Seems there are no files in that * directory. */
238 dirp->dd_stat = -1;
239 } else {
240 dirp->dd_stat = 1;
241 }
242 } else {
243 /* Get the next search entry. */
244 if (_findnext (dirp->dd_handle, &(dirp->dd_dta))) {
245 /* We are off the end or otherwise error. */
246 _findclose (dirp->dd_handle);
247 dirp->dd_handle = -1;
248 dirp->dd_stat = -1;
249 } else {
250 /* Update the status to indicate the correct * number. */
251 dirp->dd_stat++;
252 }
253 }
254
255 if (dirp->dd_stat > 0) {
256 /* Successfully got an entry. Everything about the file is * already
257 appropriately filled in except the length of the * file name. */
258 dirp->dd_dir.d_namlen = (unsigned short) strlen (dirp->dd_dir.d_name);
259 return &dirp->dd_dir;
260 }
261
262 return (struct dirent *) 0;
263 }
264
265 /*
266 closedir
267
268 Frees up resources allocated by opendir.
269 */
270 static int
271 win32_closedir (DIR * dirp)
272 {
273 int rc;
274
275 errno = 0;
276 rc = 0;
277
278 if (!dirp) {
279 errno = EFAULT;
280 return -1;
281 }
282
283 if (dirp->dd_handle != -1) {
284 rc = _findclose (dirp->dd_handle);
285 }
286
287 /* Delete the dir structure. */
288 free (dirp);
289
290 return rc;
291 }
292
293 /*
294 rewinddir
295
296 Return to the beginning of the directory "stream". We simply call findclose
297 and then reset things like an opendir.
298 */
299 static void
300 win32_rewinddir (DIR * dirp)
301 {
302 errno = 0;
303
304 if (!dirp) {
305 errno = EFAULT;
306 return;
307 }
308
309 if (dirp->dd_handle != -1) {
310 _findclose (dirp->dd_handle);
311 }
312
313 dirp->dd_handle = -1;
314 dirp->dd_stat = 0;
315 }
316
317 /*
318 telldir
319
320 Returns the "position" in the "directory stream" which can be used with
321 seekdir to go back to an old entry. We simply return the value in stat.
322 */
323 static long
324 win32_telldir (DIR * dirp)
325 {
326 errno = 0;
327
328 if (!dirp) {
329 errno = EFAULT;
330 return -1;
331 }
332 return dirp->dd_stat;
333 }
334
335 /*
336 seekdir
337
338 Seek to an entry previously returned by telldir. We rewind the directory
339 and call readdir repeatedly until either dd_stat is the position number
340 or -1 (off the end). This is not perfect, in that the directory may
341 have changed while we weren't looking. But that is probably the case with
342 any such system.
343 */
344 static void
345 win32_seekdir (DIR * dirp, long lPos)
346 {
347 errno = 0;
348
349 if (!dirp) {
350 errno = EFAULT;
351 return;
352 }
353
354 if (lPos < -1) {
355 /* Seeking to an invalid position. */
356 errno = EINVAL;
357 return;
358 } else if (lPos == -1) {
359 /* Seek past end. */
360 if (dirp->dd_handle != -1) {
361 _findclose (dirp->dd_handle);
362 }
363 dirp->dd_handle = -1;
364 dirp->dd_stat = -1;
365 } else {
366 /* Rewind and read forward to the appropriate index. */
367 win32_rewinddir (dirp);
368
369 while ((dirp->dd_stat < lPos) && win32_readdir (dirp));
370 }
371 }
372
373 #else
374 #define USE_DIRENT 0
375
376 #define _zzip_opendir(_N_) 0
377 #define _zzip_readdir(_D_) 0
378 #define _zzip_closedir(_D_) /* omit return code */
379 #define _zzip_rewinddir(_D_)
380 #define _zzip_telldir(_D_) 0
381 #define _zzip_seekdir(_D_,_V_) /* omit return code */
382 #define _zzip_DIR void*
383
384 /* end of DIRENT implementation */
385 #endif
386
387 /* once */
388 #endif