moved "openjpeg3d" directory from the trunk to the branches directory.
[openjpeg.git] / codec / windirent.h
1 \r
2 /*\r
3  * uce-dirent.h - operating system independent dirent implementation\r
4  * \r
5  * Copyright (C) 1998-2002  Toni Ronkko\r
6  * \r
7  * Permission is hereby granted, free of charge, to any person obtaining\r
8  * a copy of this software and associated documentation files (the\r
9  * ``Software''), to deal in the Software without restriction, including\r
10  * without limitation the rights to use, copy, modify, merge, publish,\r
11  * distribute, sublicense, and/or sell copies of the Software, and to\r
12  * permit persons to whom the Software is furnished to do so, subject to\r
13  * the following conditions:\r
14  * \r
15  * The above copyright notice and this permission notice shall be included\r
16  * in all copies or substantial portions of the Software.\r
17  * \r
18  * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r
21  * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR\r
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\r
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\r
24  * OTHER DEALINGS IN THE SOFTWARE.\r
25  * \r
26  * \r
27  * May 28 1998, Toni Ronkko <tronkko@messi.uku.fi>\r
28  *\r
29  * $Id: uce-dirent.h,v 1.7 2002/05/13 10:48:35 tr Exp $\r
30  *\r
31  * $Log: uce-dirent.h,v $\r
32  * Revision 1.7  2002/05/13 10:48:35  tr\r
33  * embedded some source code directly to the header so that no source\r
34  * modules need to be included in the MS Visual C project using the\r
35  * interface, removed all the dependencies to other headers of the `uce'\r
36  * library so that the header can be made public\r
37  *\r
38  * Revision 1.6  2002/04/12 16:22:04  tr\r
39  * Unified Compiling Environment (UCE) replaced `std' library\r
40  *\r
41  * Revision 1.5  2001/07/20 16:33:40  tr\r
42  * moved to `std' library and re-named defines accordingly\r
43  *\r
44  * Revision 1.4  2001/07/10 16:47:18  tronkko\r
45  * revised comments\r
46  *\r
47  * Revision 1.3  2001/01/11 13:16:43  tr\r
48  * using ``uce-machine.h'' for finding out defines such as `FREEBSD'\r
49  *\r
50  * Revision 1.2  2000/10/08 16:00:41  tr\r
51  * copy of FreeBSD man page\r
52  *\r
53  * Revision 1.1  2000/07/10 05:53:16  tr\r
54  * Initial revision\r
55  *\r
56  * Revision 1.2  1998/07/19 18:29:14  tr\r
57  * Added error reporting capabilities and some asserts.\r
58  *\r
59  * Revision 1.1  1998/07/04 16:27:51  tr\r
60  * Initial revision\r
61  *\r
62  * \r
63  * MSVC 1.0 scans automatic dependencies incorrectly when your project\r
64  * contains this very header.  The problem is that MSVC cannot handle\r
65  * include directives inside #if..#endif block those are never entered.\r
66  * Since this header ought to compile in many different operating systems,\r
67  * there had to be several conditional blocks that are compiled only in\r
68  * operating systems for what they were designed for.  MSVC 1.0 cannot\r
69  * handle inclusion of sys/dir.h in a part that is compiled only in Apollo\r
70  * operating system.  To fix the problem you need to insert DIR.H into\r
71  * SYSINCL.DAT located in MSVC\BIN directory and restart visual C++.\r
72  * Consult manuals for more informaton about the problem.\r
73  *\r
74  * Since many UNIX systems have dirent.h we assume to have one also.\r
75  * However, if your UNIX system does not have dirent.h you can download one\r
76  * for example at: http://ftp.uni-mannheim.de/ftp/GNU/dirent/dirent.tar.gz.\r
77  * You can also see if you have one of dirent.h, direct.h, dir.h, ndir.h,\r
78  * sys/dir.h and sys/ndir.h somewhere.  Try defining HAVE_DIRENT_H,\r
79  * HAVE_DIRECT_H, HAVE_DIR_H, HAVE_NDIR_H, HAVE_SYS_DIR_H and\r
80  * HAVE_SYS_NDIR_H according to the files found.\r
81  */\r
82 #ifndef DIRENT_H\r
83 #define DIRENT_H\r
84 #define DIRENT_H_INCLUDED\r
85 \r
86 /* find out platform */\r
87 #if defined(MSDOS)                             /* MS-DOS */\r
88 #elif defined(__MSDOS__)                       /* Turbo C/Borland */\r
89 # define MSDOS\r
90 #elif defined(__DOS__)                         /* Watcom */\r
91 # define MSDOS\r
92 #endif\r
93 \r
94 #if defined(WIN32)                             /* MS-Windows */\r
95 #elif defined(__NT__)                          /* Watcom */\r
96 # define WIN32\r
97 #elif defined(_WIN32)                          /* Microsoft */\r
98 # define WIN32\r
99 #elif defined(__WIN32__)                       /* Borland */\r
100 # define WIN32\r
101 #endif\r
102 \r
103 /*\r
104  * See what kind of dirent interface we have unless autoconf has already\r
105  * determinated that.\r
106  */\r
107 #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)\r
108 # if defined(_MSC_VER)                         /* Microsoft C/C++ */\r
109     /* no dirent.h */\r
110 # elif defined(__MINGW32__)                    /* MinGW */\r
111     /* no dirent.h */\r
112 # elif defined(__BORLANDC__)                   /* Borland C/C++ */\r
113 #   define HAVE_DIRENT_H\r
114 #   define VOID_CLOSEDIR\r
115 # elif defined(__TURBOC__)                     /* Borland Turbo C */\r
116     /* no dirent.h */\r
117 # elif defined(__WATCOMC__)                    /* Watcom C/C++ */\r
118 #   define HAVE_DIRECT_H\r
119 # elif defined(__apollo)                       /* Apollo */\r
120 #   define HAVE_SYS_DIR_H\r
121 # elif defined(__hpux)                         /* HP-UX */\r
122 #   define HAVE_DIRENT_H\r
123 # elif (defined(__alpha) || defined(__alpha__)) && !defined(__linux__)  /* Alpha OSF1 */\r
124 #   error "not implemented"\r
125 # elif defined(__sgi)                          /* Silicon Graphics */\r
126 #   define HAVE_DIRENT_H\r
127 # elif defined(sun) || defined(_sun)           /* Sun Solaris */\r
128 #   define HAVE_DIRENT_H\r
129 # elif defined(__FreeBSD__)                    /* FreeBSD */\r
130 #   define HAVE_DIRENT_H\r
131 # elif defined(__linux__)                      /* Linux */\r
132 #   define HAVE_DIRENT_H\r
133 # elif defined(__GNUC__)                       /* GNU C/C++ */\r
134 #   define HAVE_DIRENT_H\r
135 # else\r
136 #   error "not implemented"\r
137 # endif\r
138 #endif\r
139 \r
140 /* include proper interface headers */\r
141 #if defined(HAVE_DIRENT_H)\r
142 # include <dirent.h>\r
143 # ifdef FREEBSD\r
144 #   define NAMLEN(dp) ((int)((dp)->d_namlen))\r
145 # else\r
146 #   define NAMLEN(dp) ((int)(strlen((dp)->d_name)))\r
147 # endif\r
148 \r
149 #elif defined(HAVE_NDIR_H)\r
150 # include <ndir.h>\r
151 # define NAMLEN(dp) ((int)((dp)->d_namlen))\r
152 \r
153 #elif defined(HAVE_SYS_NDIR_H)\r
154 # include <sys/ndir.h>\r
155 # define NAMLEN(dp) ((int)((dp)->d_namlen))\r
156 \r
157 #elif defined(HAVE_DIRECT_H)\r
158 # include <direct.h>\r
159 # define NAMLEN(dp) ((int)((dp)->d_namlen))\r
160 \r
161 #elif defined(HAVE_DIR_H)\r
162 # include <dir.h>\r
163 # define NAMLEN(dp) ((int)((dp)->d_namlen))\r
164 \r
165 #elif defined(HAVE_SYS_DIR_H)\r
166 # include <sys/types.h>\r
167 # include <sys/dir.h>\r
168 # ifndef dirent\r
169 #   define dirent direct\r
170 # endif\r
171 # define NAMLEN(dp) ((int)((dp)->d_namlen))\r
172 \r
173 #elif defined(MSDOS) || defined(WIN32)\r
174 \r
175   /* figure out type of underlaying directory interface to be used */\r
176 # if defined(WIN32)\r
177 #   define DIRENT_WIN32_INTERFACE\r
178 # elif defined(MSDOS)\r
179 #   define DIRENT_MSDOS_INTERFACE\r
180 # else\r
181 #   error "missing native dirent interface"\r
182 # endif\r
183 \r
184   /*** WIN32 specifics ***/\r
185 # if defined(DIRENT_WIN32_INTERFACE)\r
186 #   include <windows.h>\r
187 #   if !defined(DIRENT_MAXNAMLEN)\r
188 #     define DIRENT_MAXNAMLEN (MAX_PATH)\r
189 #   endif\r
190 \r
191 \r
192   /*** MS-DOS specifics ***/\r
193 # elif defined(DIRENT_MSDOS_INTERFACE)\r
194 #   include <dos.h>\r
195 \r
196     /* Borland defines file length macros in dir.h */\r
197 #   if defined(__BORLANDC__)\r
198 #     include <dir.h>\r
199 #     if !defined(DIRENT_MAXNAMLEN)\r
200 #       define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))\r
201 #     endif\r
202 #     if !defined(_find_t)\r
203 #       define _find_t find_t\r
204 #     endif\r
205 \r
206     /* Turbo C defines ffblk structure in dir.h */\r
207 #   elif defined(__TURBOC__)\r
208 #     include <dir.h>\r
209 #     if !defined(DIRENT_MAXNAMLEN)\r
210 #       define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))\r
211 #     endif\r
212 #     define DIRENT_USE_FFBLK\r
213 \r
214     /* MSVC */\r
215 #   elif defined(_MSC_VER)\r
216 #     if !defined(DIRENT_MAXNAMLEN)\r
217 #       define DIRENT_MAXNAMLEN (12)\r
218 #     endif\r
219 \r
220     /* Watcom */\r
221 #   elif defined(__WATCOMC__)\r
222 #     if !defined(DIRENT_MAXNAMLEN)\r
223 #       if defined(__OS2__) || defined(__NT__)\r
224 #         define DIRENT_MAXNAMLEN (255)\r
225 #       else\r
226 #         define DIRENT_MAXNAMLEN (12)\r
227 #       endif\r
228 #     endif\r
229 \r
230 #   endif\r
231 # endif\r
232 \r
233   /*** generic MS-DOS and MS-Windows stuff ***/\r
234 # if !defined(NAME_MAX) && defined(DIRENT_MAXNAMLEN)\r
235 #   define NAME_MAX DIRENT_MAXNAMLEN\r
236 # endif\r
237 # if NAME_MAX < DIRENT_MAXNAMLEN\r
238 #   error "assertion failed: NAME_MAX >= DIRENT_MAXNAMLEN"\r
239 # endif\r
240 \r
241 \r
242   /*\r
243    * Substitute for real dirent structure.  Note that `d_name' field is a\r
244    * true character array although we have it copied in the implementation\r
245    * dependent data.  We could save some memory if we had declared `d_name'\r
246    * as a pointer refering the name within implementation dependent data.\r
247    * We have not done that since some code may rely on sizeof(d_name) to be\r
248    * something other than four.  Besides, directory entries are typically so\r
249    * small that it takes virtually no time to copy them from place to place.\r
250    */\r
251   typedef struct dirent {\r
252     char d_name[NAME_MAX + 1];\r
253 \r
254     /*** Operating system specific part ***/\r
255 # if defined(DIRENT_WIN32_INTERFACE)       /*WIN32*/\r
256     WIN32_FIND_DATA data;\r
257 # elif defined(DIRENT_MSDOS_INTERFACE)     /*MSDOS*/\r
258 #   if defined(DIRENT_USE_FFBLK)\r
259     struct ffblk data;\r
260 #   else\r
261     struct _find_t data;\r
262 #   endif\r
263 # endif\r
264   } dirent;\r
265 \r
266   /* DIR substitute structure containing directory name.  The name is\r
267    * essential for the operation of ``rewinndir'' function. */\r
268   typedef struct DIR {\r
269     char          *dirname;                    /* directory being scanned */\r
270     dirent        current;                     /* current entry */\r
271     int           dirent_filled;               /* is current un-processed? */\r
272 \r
273   /*** Operating system specific part ***/\r
274 #  if defined(DIRENT_WIN32_INTERFACE)\r
275     HANDLE        search_handle;\r
276 #  elif defined(DIRENT_MSDOS_INTERFACE)\r
277 #  endif\r
278   } DIR;\r
279 \r
280 # ifdef __cplusplus\r
281 extern "C" {\r
282 # endif\r
283 \r
284 /* supply prototypes for dirent functions */\r
285 static DIR *opendir (const char *dirname);\r
286 static struct dirent *readdir (DIR *dirp);\r
287 static int closedir (DIR *dirp);\r
288 static void rewinddir (DIR *dirp);\r
289 \r
290 /*\r
291  * Implement dirent interface as static functions so that the user does not\r
292  * need to change his project in any way to use dirent function.  With this\r
293  * it is sufficient to include this very header from source modules using\r
294  * dirent functions and the functions will be pulled in automatically.\r
295  */\r
296 #include <stdio.h>\r
297 #include <stdlib.h>\r
298 #include <string.h>\r
299 #include <assert.h>\r
300 #include <errno.h>\r
301 \r
302 /* use ffblk instead of _find_t if requested */\r
303 #if defined(DIRENT_USE_FFBLK)\r
304 # define _A_ARCH   (FA_ARCH)\r
305 # define _A_HIDDEN (FA_HIDDEN)\r
306 # define _A_NORMAL (0)\r
307 # define _A_RDONLY (FA_RDONLY)\r
308 # define _A_SUBDIR (FA_DIREC)\r
309 # define _A_SYSTEM (FA_SYSTEM)\r
310 # define _A_VOLID  (FA_LABEL)\r
311 # define _dos_findnext(dest) findnext(dest)\r
312 # define _dos_findfirst(name,flags,dest) findfirst(name,dest,flags)\r
313 #endif\r
314 \r
315 static int _initdir (DIR *p);\r
316 static const char *_getdirname (const struct dirent *dp);\r
317 static void _setdirname (struct DIR *dirp);\r
318 \r
319 /*\r
320  * <function name="opendir">\r
321  * <intro>open directory stream for reading\r
322  * <syntax>DIR *opendir (const char *dirname);\r
323  *\r
324  * <desc>Open named directory stream for read and return pointer to the\r
325  * internal working area that is used for retrieving individual directory\r
326  * entries.  The internal working area has no fields of your interest.\r
327  *\r
328  * <ret>Returns a pointer to the internal working area or NULL in case the \r
329  * directory stream could not be opened.  Global `errno' variable will set\r
330  * in case of error as follows:\r
331  *\r
332  * <table>\r
333  * [EACESS  |Permission denied.\r
334  * [EMFILE  |Too many open files used by the process.\r
335  * [ENFILE  |Too many open files in system.\r
336  * [ENOENT  |Directory does not exist.\r
337  * [ENOMEM  |Insufficient memory.\r
338  * [ENOTDIR |dirname does not refer to directory.  This value is not\r
339  *           reliable on MS-DOS and MS-Windows platforms.  Many\r
340  *           implementations return ENOENT even when the name refers to a\r
341  *           file.]\r
342  * </table>\r
343  * </function>\r
344  */\r
345 static DIR *opendir(const char *dirname)\r
346 {\r
347   DIR *dirp;\r
348   assert (dirname != NULL);\r
349   \r
350   dirp = (DIR*)malloc (sizeof (struct DIR));\r
351   if (dirp != NULL) {\r
352     char *p;\r
353     \r
354     /* allocate room for directory name */\r
355     dirp->dirname = (char*) malloc (strlen (dirname) + 1 + strlen ("\\*.*"));\r
356     if (dirp->dirname == NULL) {\r
357       /* failed to duplicate directory name.  errno set by malloc() */\r
358       free (dirp);\r
359       return NULL;\r
360     }\r
361     /* Copy directory name while appending directory separator and "*.*".\r
362      * Directory separator is not appended if the name already ends with\r
363      * drive or directory separator.  Directory separator is assumed to be\r
364      * '/' or '\' and drive separator is assumed to be ':'. */\r
365     strcpy (dirp->dirname, dirname);\r
366     p = strchr (dirp->dirname, '\0');\r
367     if (dirp->dirname < p  &&\r
368         *(p - 1) != '\\'  &&  *(p - 1) != '/'  &&  *(p - 1) != ':')\r
369     {\r
370       strcpy (p++, "\\");\r
371     }\r
372 # ifdef DIRENT_WIN32_INTERFACE\r
373     strcpy (p, "*"); /*scan files with and without extension in win32*/\r
374 # else\r
375     strcpy (p, "*.*"); /*scan files with and without extension in DOS*/\r
376 # endif\r
377 \r
378     /* open stream */\r
379     if (_initdir (dirp) == 0) {\r
380       /* initialization failed */\r
381       free (dirp->dirname);\r
382       free (dirp);\r
383       return NULL;\r
384     }\r
385   }\r
386   return dirp;\r
387 }\r
388 \r
389 \r
390 /*\r
391  * <function name="readdir">\r
392  * <intro>read a directory entry\r
393  * <syntax>struct dirent *readdir (DIR *dirp);\r
394  *\r
395  * <desc>Read individual directory entry and return pointer to a structure\r
396  * containing the name of the entry.  Individual directory entries returned\r
397  * include normal files, sub-directories, pseudo-directories "." and ".."\r
398  * and also volume labels, hidden files and system files in MS-DOS and\r
399  * MS-Windows.   You might want to use stat(2) function to determinate which\r
400  * one are you dealing with.  Many dirent implementations already contain\r
401  * equivalent information in dirent structure but you cannot depend on\r
402  * this.\r
403  *\r
404  * The dirent structure contains several system dependent fields that\r
405  * generally have no interest to you.  The only interesting one is char\r
406  * d_name[] that is also portable across different systems.  The d_name\r
407  * field contains the name of the directory entry without leading path.\r
408  * While d_name is portable across different systems the actual storage\r
409  * capacity of d_name varies from system to system and there is no portable\r
410  * way to find out it at compile time as different systems define the\r
411  * capacity of d_name with different macros and some systems do not define\r
412  * capacity at all (besides actual declaration of the field). If you really\r
413  * need to find out storage capacity of d_name then you might want to try\r
414  * NAME_MAX macro. The NAME_MAX is defined in POSIX standard althought\r
415  * there are many MS-DOS and MS-Windows implementations those do not define\r
416  * it.  There are also systems that declare d_name as "char d_name[1]" and\r
417  * then allocate suitable amount of memory at run-time.  Thanks to Alain\r
418  * Decamps (Alain.Decamps@advalvas.be) for pointing it out to me.\r
419  *\r
420  * This all leads to the fact that it is difficult to allocate space\r
421  * for the directory names when the very same program is being compiled on\r
422  * number of operating systems.  Therefore I suggest that you always\r
423  * allocate space for directory names dynamically.\r
424  *\r
425  * <ret>\r
426  * Returns a pointer to a structure containing name of the directory entry\r
427  * in `d_name' field or NULL if there was an error.  In case of an error the\r
428  * global `errno' variable will set as follows:\r
429  *\r
430  * <table>\r
431  * [EBADF  |dir parameter refers to an invalid directory stream.  This value\r
432  *          is not set reliably on all implementations.]\r
433  * </table>\r
434  * </function>\r
435  */\r
436 static struct dirent *\r
437 readdir (DIR *dirp)\r
438 {\r
439   assert(dirp != NULL);\r
440   if (dirp == NULL) {\r
441     errno = EBADF;\r
442     return NULL;\r
443   }\r
444 \r
445 #if defined(DIRENT_WIN32_INTERFACE)\r
446   if (dirp->search_handle == INVALID_HANDLE_VALUE) {\r
447     /* directory stream was opened/rewound incorrectly or it ended normally */\r
448     errno = EBADF;\r
449     return NULL;\r
450   }\r
451 #endif\r
452 \r
453   if (dirp->dirent_filled != 0) {\r
454     /*\r
455      * Directory entry has already been retrieved and there is no need to\r
456      * retrieve a new one.  Directory entry will be retrieved in advance\r
457      * when the user calls readdir function for the first time.  This is so\r
458      * because real dirent has separate functions for opening and reading\r
459      * the stream whereas Win32 and DOS dirents open the stream\r
460      * automatically when we retrieve the first file.  Therefore, we have to\r
461      * save the first file when opening the stream and later we have to\r
462      * return the saved entry when the user tries to read the first entry.\r
463      */\r
464     dirp->dirent_filled = 0;\r
465   } else {\r
466     /* fill in entry and return that */\r
467 #if defined(DIRENT_WIN32_INTERFACE)\r
468     if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) {\r
469       /* Last file has been processed or an error occured */\r
470       FindClose (dirp->search_handle);\r
471       dirp->search_handle = INVALID_HANDLE_VALUE;\r
472       errno = ENOENT;\r
473       return NULL;\r
474     }\r
475 \r
476 # elif defined(DIRENT_MSDOS_INTERFACE)\r
477     if (_dos_findnext (&dirp->current.data) != 0) {\r
478       /* _dos_findnext and findnext will set errno to ENOENT when no\r
479        * more entries could be retrieved. */\r
480       return NULL;\r
481     }\r
482 # endif\r
483 \r
484     _setdirname (dirp);\r
485     assert (dirp->dirent_filled == 0);\r
486   }\r
487   return &dirp->current;\r
488 }\r
489 \r
490 \r
491 /*\r
492  * <function name="closedir">\r
493  * <intro>close directory stream.\r
494  * <syntax>int closedir (DIR *dirp);\r
495  *\r
496  * <desc>Close directory stream opened by the `opendir' function.  Close of\r
497  * directory stream invalidates the DIR structure as well as previously read\r
498  * dirent entry.\r
499  *\r
500  * <ret>The function typically returns 0 on success and -1 on failure but\r
501  * the function may be declared to return void on same systems.  At least\r
502  * Borland C/C++ and some UNIX implementations use void as a return type.\r
503  * The dirent wrapper tries to define VOID_CLOSEDIR whenever closedir is\r
504  * known to return nothing.  The very same definition is made by the GNU\r
505  * autoconf if you happen to use it.\r
506  *\r
507  * The global `errno' variable will set to EBADF in case of error.\r
508  * </function>\r
509  */\r
510 static int\r
511 closedir (DIR *dirp)\r
512 {   \r
513   int retcode = 0;\r
514 \r
515   /* make sure that dirp points to legal structure */\r
516   assert (dirp != NULL);\r
517   if (dirp == NULL) {\r
518     errno = EBADF;\r
519     return -1;\r
520   }\r
521  \r
522   /* free directory name and search handles */\r
523   if (dirp->dirname != NULL) free (dirp->dirname);\r
524 \r
525 #if defined(DIRENT_WIN32_INTERFACE)\r
526   if (dirp->search_handle != INVALID_HANDLE_VALUE) {\r
527     if (FindClose (dirp->search_handle) == FALSE) {\r
528       /* Unknown error */\r
529       retcode = -1;\r
530       errno = EBADF;\r
531     }\r
532   }\r
533 #endif                     \r
534 \r
535   /* clear dirp structure to make sure that it cannot be used anymore*/\r
536   memset (dirp, 0, sizeof (*dirp));\r
537 # if defined(DIRENT_WIN32_INTERFACE)\r
538   dirp->search_handle = INVALID_HANDLE_VALUE;\r
539 # endif\r
540 \r
541   free (dirp);\r
542   return retcode;\r
543 }\r
544 \r
545 \r
546 /*\r
547  * <function name="rewinddir">\r
548  * <intro>rewind directory stream to the beginning\r
549  * <syntax>void rewinddir (DIR *dirp);\r
550  *\r
551  * <desc>Rewind directory stream to the beginning so that the next call of\r
552  * readdir() returns the very first directory entry again.  However, note\r
553  * that next call of readdir() may not return the same directory entry as it\r
554  * did in first time.  The directory stream may have been affected by newly\r
555  * created files.\r
556  *\r
557  * Almost every dirent implementation ensure that rewinddir will update\r
558  * the directory stream to reflect any changes made to the directory entries\r
559  * since the previous ``opendir'' or ``rewinddir'' call.  Keep an eye on\r
560  * this if your program depends on the feature.  I know at least one dirent\r
561  * implementation where you are required to close and re-open the stream to\r
562  * see the changes.\r
563  *\r
564  * <ret>Returns nothing.  If something went wrong while rewinding, you will\r
565  * notice it later when you try to retrieve the first directory entry.\r
566  */\r
567 static void\r
568 rewinddir (DIR *dirp)\r
569 {   \r
570   /* make sure that dirp is legal */\r
571   assert (dirp != NULL);\r
572   if (dirp == NULL) {\r
573     errno = EBADF;\r
574     return;\r
575   }\r
576   assert (dirp->dirname != NULL);\r
577   \r
578   /* close previous stream */\r
579 #if defined(DIRENT_WIN32_INTERFACE)\r
580   if (dirp->search_handle != INVALID_HANDLE_VALUE) {\r
581     if (FindClose (dirp->search_handle) == FALSE) {\r
582       /* Unknown error */\r
583       errno = EBADF;\r
584     }\r
585   }\r
586 #endif\r
587 \r
588   /* re-open previous stream */\r
589   if (_initdir (dirp) == 0) {\r
590     /* initialization failed but we cannot deal with error.  User will notice\r
591      * error later when she tries to retrieve first directory enty. */\r
592     /*EMPTY*/;\r
593   }\r
594 }\r
595 \r
596 \r
597 /*\r
598  * Open native directory stream object and retrieve first file.\r
599  * Be sure to close previous stream before opening new one.\r
600  */\r
601 static int\r
602 _initdir (DIR *dirp)\r
603\r
604   assert (dirp != NULL);\r
605   assert (dirp->dirname != NULL);\r
606   dirp->dirent_filled = 0;\r
607 \r
608 # if defined(DIRENT_WIN32_INTERFACE)\r
609   /* Open stream and retrieve first file */\r
610   dirp->search_handle = FindFirstFile (dirp->dirname, &dirp->current.data);\r
611   if (dirp->search_handle == INVALID_HANDLE_VALUE) {\r
612     /* something went wrong but we don't know what.  GetLastError() could\r
613      * give us more information about the error, but then we should map\r
614      * the error code into errno. */\r
615     errno = ENOENT;\r
616     return 0;\r
617   }\r
618 \r
619 # elif defined(DIRENT_MSDOS_INTERFACE)\r
620   if (_dos_findfirst (dirp->dirname,\r
621           _A_SUBDIR | _A_RDONLY | _A_ARCH | _A_SYSTEM | _A_HIDDEN,\r
622           &dirp->current.data) != 0)\r
623   {\r
624     /* _dos_findfirst and findfirst will set errno to ENOENT when no \r
625      * more entries could be retrieved. */\r
626     return 0;\r
627   }\r
628 # endif\r
629 \r
630   /* initialize DIR and it's first entry */\r
631   _setdirname (dirp);\r
632   dirp->dirent_filled = 1;\r
633   return 1;\r
634 }\r
635 \r
636 \r
637 /*\r
638  * Return implementation dependent name of the current directory entry.\r
639  */\r
640 static const char *\r
641 _getdirname (const struct dirent *dp)\r
642 {\r
643 #if defined(DIRENT_WIN32_INTERFACE)\r
644   return dp->data.cFileName;\r
645   \r
646 #elif defined(DIRENT_USE_FFBLK)\r
647   return dp->data.ff_name;\r
648   \r
649 #else\r
650   return dp->data.name;\r
651 #endif  \r
652 }\r
653 \r
654 \r
655 /*\r
656  * Copy name of implementation dependent directory entry to the d_name field.\r
657  */\r
658 static void\r
659 _setdirname (struct DIR *dirp) {\r
660   /* make sure that d_name is long enough */\r
661   assert (strlen (_getdirname (&dirp->current)) <= NAME_MAX);\r
662   \r
663   strncpy (dirp->current.d_name,\r
664       _getdirname (&dirp->current),\r
665       NAME_MAX);\r
666   dirp->current.d_name[NAME_MAX] = '\0'; /*char d_name[NAME_MAX+1]*/\r
667 }\r
668   \r
669 # ifdef __cplusplus\r
670 }\r
671 # endif\r
672 # define NAMLEN(dp) ((int)(strlen((dp)->d_name)))\r
673 \r
674 #else\r
675 # error "missing dirent interface"\r
676 #endif\r
677 \r
678 \r
679 #endif /*DIRENT_H*/\r