AGX Dynamics  2.35.0.1
dirent.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 /*
18 This source code has been taken and modified by Algoryx Simulation AB
19 from the source and under the license given below.
20 */
21 
22 /*
23  * uce-dirent.h - operating system independent dirent implementation
24  *
25  * Copyright (C) 1998-2002 Toni Ronkko
26  *
27  * Permission is hereby granted, free of charge, to any person obtaining
28  * a copy of this software and associated documentation files (the
29  * ``Software''), to deal in the Software without restriction, including
30  * without limitation the rights to use, copy, modify, merge, publish,
31  * distribute, sublicense, and/or sell copies of the Software, and to
32  * permit persons to whom the Software is furnished to do so, subject to
33  * the following conditions:
34  *
35  * The above copyright notice and this permission notice shall be included
36  * in all copies or substantial portions of the Software.
37  *
38  * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
39  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
41  * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
42  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
43  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
44  * OTHER DEALINGS IN THE SOFTWARE.
45  *
46  *
47  * May 28 1998, Toni Ronkko <tronkko@messi.uku.fi>
48  *
49  * $Id$
50  *
51  * $Log: dirent.h,v $
52  * Revision 1.1 2005/04/11 11:02:17 andersb
53  * New CVS repository for Colosseum3D
54  *
55  * Revision 1.1.2.3 2004/11/23 10:10:35 andersb
56  * *** empty log message ***
57  *
58  * Revision 1.7 2002/05/13 10:48:35 tr
59  * embedded some source code directly to the header so that no source
60  * modules need to be included in the MS Visual C project using the
61  * interface, removed all the dependencies to other headers of the `uce'
62  * library so that the header can be made public
63  *
64  * Revision 1.6 2002/04/12 16:22:04 tr
65  * Unified Compiling Environment (UCE) replaced `std' library
66  *
67  * Revision 1.5 2001/07/20 16:33:40 tr
68  * moved to `std' library and re-named defines accordingly
69  *
70  * Revision 1.4 2001/07/10 16:47:18 tronkko
71  * revised comments
72  *
73  * Revision 1.3 2001/01/11 13:16:43 tr
74  * using ``uce-machine.h'' for finding out defines such as `FREEBSD'
75  *
76  * Revision 1.2 2000/10/08 16:00:41 tr
77  * copy of FreeBSD man page
78  *
79  * Revision 1.1 2000/07/10 05:53:16 tr
80  * Initial revision
81  *
82  * Revision 1.2 1998/07/19 18:29:14 tr
83  * Added error reporting capabilities and some asserts.
84  *
85  * Revision 1.1 1998/07/04 16:27:51 tr
86  * Initial revision
87  *
88  *
89  * MSVC 1.0 scans automatic dependencies incorrectly when your project
90  * contains this very header. The problem is that MSVC cannot handle
91  * include directives inside #if..#endif block those are never entered.
92  * Since this header ought to compile in many different operating systems,
93  * there had to be several conditional blocks that are compiled only in
94  * operating systems for what they were designed for. MSVC 1.0 cannot
95  * handle inclusion of sys/dir.h in a part that is compiled only in Apollo
96  * operating system. To fix the problem you need to insert DIR.H into
97  * SYSINCL.DAT located in MSVC\BIN directory and restart visual C++.
98  * Consult manuals for more informaton about the problem.
99  *
100  * Since many UNIX systems have dirent.h we assume to have one also.
101  * However, if your UNIX system does not have dirent.h you can download one
102  * for example at: http://ftp.uni-mannheim.de/ftp/GNU/dirent/dirent.tar.gz.
103  * You can also see if you have one of dirent.h, direct.h, dir.h, ndir.h,
104  * sys/dir.h and sys/ndir.h somewhere. Try defining HAVE_DIRENT_H,
105  * HAVE_DIRECT_H, HAVE_DIR_H, HAVE_NDIR_H, HAVE_SYS_DIR_H and
106  * HAVE_SYS_NDIR_H according to the files found.
107  */
108 #ifndef DIRENT_H
109 #define DIRENT_H
110 #define DIRENT_H_INCLUDED
111 
112 /* find out platform */
113 #if defined(MSDOS) /* MS-DOS */
114 #elif defined(__MSDOS__) /* Turbo C/Borland */
115 # define MSDOS
116 #elif defined(__DOS__) /* Watcom */
117 # define MSDOS
118 #endif
119 
120 #if defined(WIN32) /* MS-Windows */
121 #elif defined(__NT__) /* Watcom */
122 # define WIN32
123 #elif defined(_WIN32) /* Microsoft */
124 # define WIN32
125 #elif defined(__WIN32__) /* Borland */
126 # define WIN32
127 #endif
128 
129 /*
130  * See what kind of dirent interface we have unless autoconf has already
131  * determinated that.
132  */
133 #if !defined(HAVE_DIRENT_H) && !defined(HAVE_DIRECT_H) && !defined(HAVE_SYS_DIR_H) && !defined(HAVE_NDIR_H) && !defined(HAVE_SYS_NDIR_H) && !defined(HAVE_DIR_H)
134 # if defined(_MSC_VER) /* Microsoft C/C++ */
135  /* no dirent.h */
136 # elif defined(__BORLANDC__) /* Borland C/C++ */
137 # define HAVE_DIRENT_H
138 # define VOID_CLOSEDIR
139 # elif defined(__TURBOC__) /* Borland Turbo C */
140  /* no dirent.h */
141 # elif defined(__WATCOMC__) /* Watcom C/C++ */
142 # define HAVE_DIRECT_H
143 # elif defined(__apollo) /* Apollo */
144 # define HAVE_SYS_DIR_H
145 # elif defined(__hpux) /* HP-UX */
146 # define HAVE_DIRENT_H
147 # elif defined(__alpha) || defined(__alpha__) /* Alpha OSF1 */
148 # error "not implemented"
149 # elif defined(__sgi) /* Silicon Graphics */
150 # define HAVE_DIRENT_H
151 # elif defined(sun) || defined(_sun) /* Sun Solaris */
152 # define HAVE_DIRENT_H
153 # elif defined(__FreeBSD__) /* FreeBSD */
154 # define HAVE_DIRENT_H
155 # elif defined(__linux__) /* Linux */
156 # define HAVE_DIRENT_H
157 # elif defined(__GNUC__) /* GNU C/C++ */
158 # define HAVE_DIRENT_H
159 # else
160 # error "not implemented"
161 # endif
162 #endif
163 
164 /* include proper interface headers */
165 #if defined(HAVE_DIRENT_H)
166 # include <dirent.h>
167 # ifdef FREEBSD
168 # define NAMLEN(dp) ((int)((dp)->d_namlen))
169 # else
170 # define NAMLEN(dp) ((int)(strlen((dp)->d_name)))
171 # endif
172 
173 #elif defined(HAVE_NDIR_H)
174 # include <ndir.h>
175 # define NAMLEN(dp) ((int)((dp)->d_namlen))
176 
177 #elif defined(HAVE_SYS_NDIR_H)
178 # include <sys/ndir.h>
179 # define NAMLEN(dp) ((int)((dp)->d_namlen))
180 
181 #elif defined(HAVE_DIRECT_H)
182 # include <direct.h>
183 # define NAMLEN(dp) ((int)((dp)->d_namlen))
184 
185 #elif defined(HAVE_DIR_H)
186 # include <dir.h>
187 # define NAMLEN(dp) ((int)((dp)->d_namlen))
188 
189 #elif defined(HAVE_SYS_DIR_H)
190 # include <sys/types.h>
191 # include <sys/dir.h>
192 # ifndef dirent
193 # define dirent direct
194 # endif
195 # define NAMLEN(dp) ((int)((dp)->d_namlen))
196 
197 #elif defined(MSDOS) || defined(WIN32)
198 
199  /* figure out type of underlaying directory interface to be used */
200 # if defined(WIN32)
201 # define DIRENT_WIN32_INTERFACE
202 # elif defined(MSDOS)
203 # define DIRENT_MSDOS_INTERFACE
204 # else
205 # error "missing native dirent interface"
206 # endif
207 
208  /*** WIN32 specifics ***/
209 # if defined(DIRENT_WIN32_INTERFACE)
210 # include <agx/Windows.h>
211 # if !defined(DIRENT_MAXNAMLEN)
212 # define DIRENT_MAXNAMLEN (MAX_PATH)
213 # endif
214 
215 
216  /*** MS-DOS specifics ***/
217 # elif defined(DIRENT_MSDOS_INTERFACE)
218 # include <dos.h>
219 
220  /* Borland defines file length macros in dir.h */
221 # if defined(__BORLANDC__)
222 # include <dir.h>
223 # if !defined(DIRENT_MAXNAMLEN)
224 # define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))
225 # endif
226 # if !defined(_find_t)
227 # define _find_t find_t
228 # endif
229 
230  /* Turbo C defines ffblk structure in dir.h */
231 # elif defined(__TURBOC__)
232 # include <dir.h>
233 # if !defined(DIRENT_MAXNAMLEN)
234 # define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))
235 # endif
236 # define DIRENT_USE_FFBLK
237 
238  /* MSVC */
239 # elif defined(_MSC_VER)
240 # if !defined(DIRENT_MAXNAMLEN)
241 # define DIRENT_MAXNAMLEN (12)
242 # endif
243 
244  /* Watcom */
245 # elif defined(__WATCOMC__)
246 # if !defined(DIRENT_MAXNAMLEN)
247 # if defined(__OS2__) || defined(__NT__)
248 # define DIRENT_MAXNAMLEN (255)
249 # else
250 # define DIRENT_MAXNAMLEN (12)
251 # endif
252 # endif
253 
254 # endif
255 # endif
256 
257  /*** generic MS-DOS and MS-Windows stuff ***/
258 # if !defined(NAME_MAX) && defined(DIRENT_MAXNAMLEN)
259 # define NAME_MAX DIRENT_MAXNAMLEN
260 # endif
261 # if NAME_MAX < DIRENT_MAXNAMLEN
262 # error "assertion failed: NAME_MAX >= DIRENT_MAXNAMLEN"
263 # endif
264 
265 
266  /*
267  * Substitute for real dirent structure. Note that `d_name' field is a
268  * true character array although we have it copied in the implementation
269  * dependent data. We could save some memory if we had declared `d_name'
270  * as a pointer refering the name within implementation dependent data.
271  * We have not done that since some code may rely on sizeof(d_name) to be
272  * something other than four. Besides, directory entries are typically so
273  * small that it takes virtually no time to copy them from place to place.
274  */
275  typedef struct dirent {
276  char d_name[NAME_MAX + 1];
277 
278  /*** Operating system specific part ***/
279 # if defined(DIRENT_WIN32_INTERFACE) /*WIN32*/
280  WIN32_FIND_DATA data;
281 # elif defined(DIRENT_MSDOS_INTERFACE) /*MSDOS*/
282 # if defined(DIRENT_USE_FFBLK)
283  struct ffblk data;
284 # else
285  struct _find_t data;
286 # endif
287 # endif
288  } dirent;
289 
290  /* DIR substitute structure containing directory name. The name is
291  * essential for the operation of ``rewinndir'' function. */
292  typedef struct DIR {
293  char *dirname; /* directory being scanned */
294  dirent current; /* current entry */
295  int dirent_filled; /* is current un-processed? */
296 
297  /*** Operating system specific part ***/
298 # if defined(DIRENT_WIN32_INTERFACE)
299  HANDLE search_handle;
300 # elif defined(DIRENT_MSDOS_INTERFACE)
301 # endif
302  } DIR;
303 
304 # ifdef __cplusplus
305 extern "C" {
306 # endif
307 
308 
309 /*
310  * Implement dirent interface as static functions so that the user does not
311  * need to change his project in any way to use dirent function. With this
312  * it is sufficient to include this very header from source modules using
313  * dirent functions and the functions will be pulled in automatically.
314  */
315 #include <stdio.h>
316 #include <stdlib.h>
317 #include <string.h>
318 #include <assert.h>
319 #include <errno.h>
320 
321 /* use ffblk instead of _find_t if requested */
322 #if defined(DIRENT_USE_FFBLK)
323 # define _A_ARCH (FA_ARCH)
324 # define _A_HIDDEN (FA_HIDDEN)
325 # define _A_NORMAL (0)
326 # define _A_RDONLY (FA_RDONLY)
327 # define _A_SUBDIR (FA_DIREC)
328 # define _A_SYSTEM (FA_SYSTEM)
329 # define _A_VOLID (FA_LABEL)
330 # define _dos_findnext(dest) findnext(dest)
331 # define _dos_findfirst(name,flags,dest) findfirst(name,dest,flags)
332 #endif
333 
334 #include <agxLua/export.h>
335 /*
336  * <function name="opendir">
337  * <intro>open directory stream for reading
338  * <syntax>DIR *opendir (const char *dirname);
339  *
340  * <desc>Open named directory stream for read and return pointer to the
341  * internal working area that is used for retrieving individual directory
342  * entries. The internal working area has no fields of your interest.
343  *
344  * <ret>Returns a pointer to the internal working area or nullptr in case the
345  * directory stream could not be opened. Global `errno' variable will set
346  * in case of error as follows:
347  *
348  * <table>
349  * [EACESS |Permission denied.
350  * [EMFILE |Too many open files used by the process.
351  * [ENFILE |Too many open files in system.
352  * [ENOENT |Directory does not exist.
353  * [ENOMEM |Insufficient memory.
354  * [ENOTDIR |dirname does not refer to directory. This value is not
355  * reliable on MS-DOS and MS-Windows platforms. Many
356  * implementations return ENOENT even when the name refers to a
357  * file.]
358  * </table>
359  * </function>
360  */
361 AGXLUA_EXPORT DIR *
362 opendir(const char *dirname);
363 
364 
365 /*
366  * <function name="readdir">
367  * <intro>read a directory entry
368  * <syntax>struct dirent *readdir (DIR *dirp);
369  *
370  * <desc>Read individual directory entry and return pointer to a structure
371  * containing the name of the entry. Individual directory entries returned
372  * include normal files, sub-directories, pseudo-directories "." and ".."
373  * and also volume labels, hidden files and system files in MS-DOS and
374  * MS-Windows. You might want to use stat(2) function to determinate which
375  * one are you dealing with. Many dirent implementations already contain
376  * equivalent information in dirent structure but you cannot depend on
377  * this.
378  *
379  * The dirent structure contains several system dependent fields that
380  * generally have no interest to you. The only interesting one is char
381  * d_name[] that is also portable across different systems. The d_name
382  * field contains the name of the directory entry without leading path.
383  * While d_name is portable across different systems the actual storage
384  * capacity of d_name varies from system to system and there is no portable
385  * way to find out it at compile time as different systems define the
386  * capacity of d_name with different macros and some systems do not define
387  * capacity at all (besides actual declaration of the field). If you really
388  * need to find out storage capacity of d_name then you might want to try
389  * NAME_MAX macro. The NAME_MAX is defined in POSIX standard although
390  * there are many MS-DOS and MS-Windows implementations those do not define
391  * it. There are also systems that declare d_name as "char d_name[1]" and
392  * then allocate suitable amount of memory at run-time. Thanks to Alain
393  * Decamps (Alain.Decamps@advalvas.be) for pointing it out to me.
394  *
395  * This all leads to the fact that it is difficult to allocate space
396  * for the directory names when the very same program is being compiled on
397  * number of operating systems. Therefore I suggest that you always
398  * allocate space for directory names dynamically.
399  *
400  * <ret>
401  * Returns a pointer to a structure containing name of the directory entry
402  * in `d_name' field or nullptr if there was an error. In case of an error the
403  * global `errno' variable will set as follows:
404  *
405  * <table>
406  * [EBADF |dir parameter refers to an invalid directory stream. This value
407  * is not set reliably on all implementations.]
408  * </table>
409  * </function>
410  */
411 AGXLUA_EXPORT struct dirent *
412  readdir (DIR *dirp);
413 
414 /*
415  * <function name="closedir">
416  * <intro>close directory stream.
417  * <syntax>int closedir (DIR *dirp);
418  *
419  * <desc>Close directory stream opened by the `opendir' function. Close of
420  * directory stream invalidates the DIR structure as well as previously read
421  * dirent entry.
422  *
423  * <ret>The function typically returns 0 on success and -1 on failure but
424  * the function may be declared to return void on same systems. At least
425  * Borland C/C++ and some UNIX implementations use void as a return type.
426  * The dirent wrapper tries to define VOID_CLOSEDIR whenever closedir is
427  * known to return nothing. The very same definition is made by the GNU
428  * autoconf if you happen to use it.
429  *
430  * The global `errno' variable will set to EBADF in case of error.
431  * </function>
432  */
433 AGXLUA_EXPORT int
434 closedir (DIR *dirp);
435 
436 /*
437  * <function name="rewinddir">
438  * <intro>rewind directory stream to the beginning
439  * <syntax>void rewinddir (DIR *dirp);
440  *
441  * <desc>Rewind directory stream to the beginning so that the next call of
442  * readdir() returns the very first directory entry again. However, note
443  * that next call of readdir() may not return the same directory entry as it
444  * did in first time. The directory stream may have been affected by newly
445  * created files.
446  *
447  * Almost every dirent implementation ensure that rewinddir will update
448  * the directory stream to reflect any changes made to the directory entries
449  * since the previous ``opendir'' or ``rewinddir'' call. Keep an eye on
450  * this if your program depends on the feature. I know at least one dirent
451  * implementation where you are required to close and re-open the stream to
452  * see the changes.
453  *
454  * <ret>Returns nothing. If something went wrong while rewinding, you will
455  * notice it later when you try to retrieve the first directory entry.
456  */
457 AGXLUA_EXPORT void
458 rewinddir (DIR *dirp);
459 
460 
461 # ifdef __cplusplus
462 }
463 # endif
464 # define NAMLEN(dp) ((int)(strlen((dp)->d_name)))
465 
466 #else
467 # error "missing dirent interface"
468 #endif
469 
470 
471 #endif /*DIRENT_H*/
void * HANDLE
#define AGXLUA_EXPORT
Definition: agxLua/export.h:42