Avoided ABI breakage
[openjpeg.git] / codec / dirent.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(__BORLANDC__)                   /* Borland C/C++ */\r
111 #   define HAVE_DIRENT_H\r
112 #   define VOID_CLOSEDIR\r
113 # elif defined(__TURBOC__)                     /* Borland Turbo C */\r
114     /* no dirent.h */\r
115 # elif defined(__WATCOMC__)                    /* Watcom C/C++ */\r
116 #   define HAVE_DIRECT_H\r
117 # elif defined(__apollo)                       /* Apollo */\r
118 #   define HAVE_SYS_DIR_H\r
119 # elif defined(__hpux)                         /* HP-UX */\r
120 #   define HAVE_DIRENT_H\r
121 # elif defined(__alpha) || defined(__alpha__)  /* Alpha OSF1 */\r
122 #   error "not implemented"\r
123 # elif defined(__sgi)                          /* Silicon Graphics */\r
124 #   define HAVE_DIRENT_H\r
125 # elif defined(sun) || defined(_sun)           /* Sun Solaris */\r
126 #   define HAVE_DIRENT_H\r
127 # elif defined(__FreeBSD__)                    /* FreeBSD */\r
128 #   define HAVE_DIRENT_H\r
129 # elif defined(__linux__)                      /* Linux */\r
130 #   define HAVE_DIRENT_H\r
131 # elif defined(__GNUC__)                       /* GNU C/C++ */\r
132 #   define HAVE_DIRENT_H\r
133 # else\r
134 #   error "not implemented"\r
135 # endif\r
136 #endif\r
137 \r
138 /* include proper interface headers */\r
139 #if defined(HAVE_DIRENT_H)\r
140 # include <dirent.h>\r
141 # ifdef FREEBSD\r
142 #   define NAMLEN(dp) ((int)((dp)->d_namlen))\r
143 # else\r
144 #   define NAMLEN(dp) ((int)(strlen((dp)->d_name)))\r
145 # endif\r
146 \r
147 #elif defined(HAVE_NDIR_H)\r
148 # include <ndir.h>\r
149 # define NAMLEN(dp) ((int)((dp)->d_namlen))\r
150 \r
151 #elif defined(HAVE_SYS_NDIR_H)\r
152 # include <sys/ndir.h>\r
153 # define NAMLEN(dp) ((int)((dp)->d_namlen))\r
154 \r
155 #elif defined(HAVE_DIRECT_H)\r
156 # include <direct.h>\r
157 # define NAMLEN(dp) ((int)((dp)->d_namlen))\r
158 \r
159 #elif defined(HAVE_DIR_H)\r
160 # include <dir.h>\r
161 # define NAMLEN(dp) ((int)((dp)->d_namlen))\r
162 \r
163 #elif defined(HAVE_SYS_DIR_H)\r
164 # include <sys/types.h>\r
165 # include <sys/dir.h>\r
166 # ifndef dirent\r
167 #   define dirent direct\r
168 # endif\r
169 # define NAMLEN(dp) ((int)((dp)->d_namlen))\r
170 \r
171 #elif defined(MSDOS) || defined(WIN32)\r
172 \r
173   /* figure out type of underlaying directory interface to be used */\r
174 # if defined(WIN32)\r
175 #   define DIRENT_WIN32_INTERFACE\r
176 # elif defined(MSDOS)\r
177 #   define DIRENT_MSDOS_INTERFACE\r
178 # else\r
179 #   error "missing native dirent interface"\r
180 # endif\r
181 \r
182   /*** WIN32 specifics ***/\r
183 # if defined(DIRENT_WIN32_INTERFACE)\r
184 #   include <windows.h>\r
185 #   if !defined(DIRENT_MAXNAMLEN)\r
186 #     define DIRENT_MAXNAMLEN (MAX_PATH)\r
187 #   endif\r
188 \r
189 \r
190   /*** MS-DOS specifics ***/\r
191 # elif defined(DIRENT_MSDOS_INTERFACE)\r
192 #   include <dos.h>\r
193 \r
194     /* Borland defines file length macros in dir.h */\r
195 #   if defined(__BORLANDC__)\r
196 #     include <dir.h>\r
197 #     if !defined(DIRENT_MAXNAMLEN)\r
198 #       define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))\r
199 #     endif\r
200 #     if !defined(_find_t)\r
201 #       define _find_t find_t\r
202 #     endif\r
203 \r
204     /* Turbo C defines ffblk structure in dir.h */\r
205 #   elif defined(__TURBOC__)\r
206 #     include <dir.h>\r
207 #     if !defined(DIRENT_MAXNAMLEN)\r
208 #       define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))\r
209 #     endif\r
210 #     define DIRENT_USE_FFBLK\r
211 \r
212     /* MSVC */\r
213 #   elif defined(_MSC_VER)\r
214 #     if !defined(DIRENT_MAXNAMLEN)\r
215 #       define DIRENT_MAXNAMLEN (12)\r
216 #     endif\r
217 \r
218     /* Watcom */\r
219 #   elif defined(__WATCOMC__)\r
220 #     if !defined(DIRENT_MAXNAMLEN)\r
221 #       if defined(__OS2__) || defined(__NT__)\r
222 #         define DIRENT_MAXNAMLEN (255)\r
223 #       else\r
224 #         define DIRENT_MAXNAMLEN (12)\r
225 #       endif\r
226 #     endif\r
227 \r
228 #   endif\r
229 # endif\r
230 \r
231   /*** generic MS-DOS and MS-Windows stuff ***/\r
232 # if !defined(NAME_MAX) && defined(DIRENT_MAXNAMLEN)\r
233 #   define NAME_MAX DIRENT_MAXNAMLEN\r
234 # endif\r
235 # if NAME_MAX < DIRENT_MAXNAMLEN\r
236 #   error "assertion failed: NAME_MAX >= DIRENT_MAXNAMLEN"\r
237 # endif\r
238 \r
239 \r
240   /*\r
241    * Substitute for real dirent structure.  Note that `d_name' field is a\r
242    * true character array although we have it copied in the implementation\r
243    * dependent data.  We could save some memory if we had declared `d_name'\r
244    * as a pointer refering the name within implementation dependent data.\r
245    * We have not done that since some code may rely on sizeof(d_name) to be\r
246    * something other than four.  Besides, directory entries are typically so\r
247    * small that it takes virtually no time to copy them from place to place.\r
248    */\r
249   typedef struct dirent {\r
250     char d_name[NAME_MAX + 1];\r
251 \r
252     /*** Operating system specific part ***/\r
253 # if defined(DIRENT_WIN32_INTERFACE)       /*WIN32*/\r
254     WIN32_FIND_DATA data;\r
255 # elif defined(DIRENT_MSDOS_INTERFACE)     /*MSDOS*/\r
256 #   if defined(DIRENT_USE_FFBLK)\r
257     struct ffblk data;\r
258 #   else\r
259     struct _find_t data;\r
260 #   endif\r
261 # endif\r
262   } dirent;\r
263 \r
264   /* DIR substitute structure containing directory name.  The name is\r
265    * essential for the operation of ``rewinndir'' function. */\r
266   typedef struct DIR {\r
267     char          *dirname;                    /* directory being scanned */\r
268     dirent        current;                     /* current entry */\r
269     int           dirent_filled;               /* is current un-processed? */\r
270 \r
271   /*** Operating system specific part ***/\r
272 #  if defined(DIRENT_WIN32_INTERFACE)\r
273     HANDLE        search_handle;\r
274 #  elif defined(DIRENT_MSDOS_INTERFACE)\r
275 #  endif\r
276   } DIR;\r
277 \r
278 # ifdef __cplusplus\r
279 extern "C" {\r
280 # endif\r
281 \r
282 /* supply prototypes for dirent functions */\r
283 static DIR *opendir (const char *dirname);\r
284 static struct dirent *readdir (DIR *dirp);\r
285 static int closedir (DIR *dirp);\r
286 static void rewinddir (DIR *dirp);\r
287 \r
288 /*\r
289  * Implement dirent interface as static functions so that the user does not\r
290  * need to change his project in any way to use dirent function.  With this\r
291  * it is sufficient to include this very header from source modules using\r
292  * dirent functions and the functions will be pulled in automatically.\r
293  */\r
294 #include <stdio.h>\r
295 #include <stdlib.h>\r
296 #include <string.h>\r
297 #include <assert.h>\r
298 #include <errno.h>\r
299 \r
300 /* use ffblk instead of _find_t if requested */\r
301 #if defined(DIRENT_USE_FFBLK)\r
302 # define _A_ARCH   (FA_ARCH)\r
303 # define _A_HIDDEN (FA_HIDDEN)\r
304 # define _A_NORMAL (0)\r
305 # define _A_RDONLY (FA_RDONLY)\r
306 # define _A_SUBDIR (FA_DIREC)\r
307 # define _A_SYSTEM (FA_SYSTEM)\r
308 # define _A_VOLID  (FA_LABEL)\r
309 # define _dos_findnext(dest) findnext(dest)\r
310 # define _dos_findfirst(name,flags,dest) findfirst(name,dest,flags)\r
311 #endif\r
312 \r
313 static int _initdir (DIR *p);\r
314 static const char *_getdirname (const struct dirent *dp);\r
315 static void _setdirname (struct DIR *dirp);\r
316 \r
317 /*\r
318  * <function name="opendir">\r
319  * <intro>open directory stream for reading\r
320  * <syntax>DIR *opendir (const char *dirname);\r
321  *\r
322  * <desc>Open named directory stream for read and return pointer to the\r
323  * internal working area that is used for retrieving individual directory\r
324  * entries.  The internal working area has no fields of your interest.\r
325  *\r
326  * <ret>Returns a pointer to the internal working area or NULL in case the \r
327  * directory stream could not be opened.  Global `errno' variable will set\r
328  * in case of error as follows:\r
329  *\r
330  * <table>\r
331  * [EACESS  |Permission denied.\r
332  * [EMFILE  |Too many open files used by the process.\r
333  * [ENFILE  |Too many open files in system.\r
334  * [ENOENT  |Directory does not exist.\r
335  * [ENOMEM  |Insufficient memory.\r
336  * [ENOTDIR |dirname does not refer to directory.  This value is not\r
337  *           reliable on MS-DOS and MS-Windows platforms.  Many\r
338  *           implementations return ENOENT even when the name refers to a\r
339  *           file.]\r
340  * </table>\r
341  * </function>\r
342  */\r
343 static DIR *opendir(const char *dirname)\r
344 {\r
345   DIR *dirp;\r
346   assert (dirname != NULL);\r
347   \r
348   dirp = (DIR*)malloc (sizeof (struct DIR));\r
349   if (dirp != NULL) {\r
350     char *p;\r
351     \r
352     /* allocate room for directory name */\r
353     dirp->dirname = (char*) malloc (strlen (dirname) + 1 + strlen ("\\*.*"));\r
354     if (dirp->dirname == NULL) {\r
355       /* failed to duplicate directory name.  errno set by malloc() */\r
356       free (dirp);\r
357       return NULL;\r
358     }\r
359     /* Copy directory name while appending directory separator and "*.*".\r
360      * Directory separator is not appended if the name already ends with\r
361      * drive or directory separator.  Directory separator is assumed to be\r
362      * '/' or '\' and drive separator is assumed to be ':'. */\r
363     strcpy (dirp->dirname, dirname);\r
364     p = strchr (dirp->dirname, '\0');\r
365     if (dirp->dirname < p  &&\r
366         *(p - 1) != '\\'  &&  *(p - 1) != '/'  &&  *(p - 1) != ':')\r
367     {\r
368       strcpy (p++, "\\");\r
369     }\r
370 # ifdef DIRENT_WIN32_INTERFACE\r
371     strcpy (p, "*"); /*scan files with and without extension in win32*/\r
372 # else\r
373     strcpy (p, "*.*"); /*scan files with and without extension in DOS*/\r
374 # endif\r
375 \r
376     /* open stream */\r
377     if (_initdir (dirp) == 0) {\r
378       /* initialization failed */\r
379       free (dirp->dirname);\r
380       free (dirp);\r
381       return NULL;\r
382     }\r
383   }\r
384   return dirp;\r
385 }\r
386 \r
387 \r
388 /*\r
389  * <function name="readdir">\r
390  * <intro>read a directory entry\r
391  * <syntax>struct dirent *readdir (DIR *dirp);\r
392  *\r
393  * <desc>Read individual directory entry and return pointer to a structure\r
394  * containing the name of the entry.  Individual directory entries returned\r
395  * include normal files, sub-directories, pseudo-directories "." and ".."\r
396  * and also volume labels, hidden files and system files in MS-DOS and\r
397  * MS-Windows.   You might want to use stat(2) function to determinate which\r
398  * one are you dealing with.  Many dirent implementations already contain\r
399  * equivalent information in dirent structure but you cannot depend on\r
400  * this.\r
401  *\r
402  * The dirent structure contains several system dependent fields that\r
403  * generally have no interest to you.  The only interesting one is char\r
404  * d_name[] that is also portable across different systems.  The d_name\r
405  * field contains the name of the directory entry without leading path.\r
406  * While d_name is portable across different systems the actual storage\r
407  * capacity of d_name varies from system to system and there is no portable\r
408  * way to find out it at compile time as different systems define the\r
409  * capacity of d_name with different macros and some systems do not define\r
410  * capacity at all (besides actual declaration of the field). If you really\r
411  * need to find out storage capacity of d_name then you might want to try\r
412  * NAME_MAX macro. The NAME_MAX is defined in POSIX standard althought\r
413  * there are many MS-DOS and MS-Windows implementations those do not define\r
414  * it.  There are also systems that declare d_name as "char d_name[1]" and\r
415  * then allocate suitable amount of memory at run-time.  Thanks to Alain\r
416  * Decamps (Alain.Decamps@advalvas.be) for pointing it out to me.\r
417  *\r
418  * This all leads to the fact that it is difficult to allocate space\r
419  * for the directory names when the very same program is being compiled on\r
420  * number of operating systems.  Therefore I suggest that you always\r
421  * allocate space for directory names dynamically.\r
422  *\r
423  * <ret>\r
424  * Returns a pointer to a structure containing name of the directory entry\r
425  * in `d_name' field or NULL if there was an error.  In case of an error the\r
426  * global `errno' variable will set as follows:\r
427  *\r
428  * <table>\r
429  * [EBADF  |dir parameter refers to an invalid directory stream.  This value\r
430  *          is not set reliably on all implementations.]\r
431  * </table>\r
432  * </function>\r
433  */\r
434 static struct dirent *\r
435 readdir (DIR *dirp)\r
436 {\r
437   assert(dirp != NULL);\r
438   if (dirp == NULL) {\r
439     errno = EBADF;\r
440     return NULL;\r
441   }\r
442 \r
443 #if defined(DIRENT_WIN32_INTERFACE)\r
444   if (dirp->search_handle == INVALID_HANDLE_VALUE) {\r
445     /* directory stream was opened/rewound incorrectly or it ended normally */\r
446     errno = EBADF;\r
447     return NULL;\r
448   }\r
449 #endif\r
450 \r
451   if (dirp->dirent_filled != 0) {\r
452     /*\r
453      * Directory entry has already been retrieved and there is no need to\r
454      * retrieve a new one.  Directory entry will be retrieved in advance\r
455      * when the user calls readdir function for the first time.  This is so\r
456      * because real dirent has separate functions for opening and reading\r
457      * the stream whereas Win32 and DOS dirents open the stream\r
458      * automatically when we retrieve the first file.  Therefore, we have to\r
459      * save the first file when opening the stream and later we have to\r
460      * return the saved entry when the user tries to read the first entry.\r
461      */\r
462     dirp->dirent_filled = 0;\r
463   } else {\r
464     /* fill in entry and return that */\r
465 #if defined(DIRENT_WIN32_INTERFACE)\r
466     if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) {\r
467       /* Last file has been processed or an error occured */\r
468       FindClose (dirp->search_handle);\r
469       dirp->search_handle = INVALID_HANDLE_VALUE;\r
470       errno = ENOENT;\r
471       return NULL;\r
472     }\r
473 \r
474 # elif defined(DIRENT_MSDOS_INTERFACE)\r
475     if (_dos_findnext (&dirp->current.data) != 0) {\r
476       /* _dos_findnext and findnext will set errno to ENOENT when no\r
477        * more entries could be retrieved. */\r
478       return NULL;\r
479     }\r
480 # endif\r
481 \r
482     _setdirname (dirp);\r
483     assert (dirp->dirent_filled == 0);\r
484   }\r
485   return &dirp->current;\r
486 }\r
487 \r
488 \r
489 /*\r
490  * <function name="closedir">\r
491  * <intro>close directory stream.\r
492  * <syntax>int closedir (DIR *dirp);\r
493  *\r
494  * <desc>Close directory stream opened by the `opendir' function.  Close of\r
495  * directory stream invalidates the DIR structure as well as previously read\r
496  * dirent entry.\r
497  *\r
498  * <ret>The function typically returns 0 on success and -1 on failure but\r
499  * the function may be declared to return void on same systems.  At least\r
500  * Borland C/C++ and some UNIX implementations use void as a return type.\r
501  * The dirent wrapper tries to define VOID_CLOSEDIR whenever closedir is\r
502  * known to return nothing.  The very same definition is made by the GNU\r
503  * autoconf if you happen to use it.\r
504  *\r
505  * The global `errno' variable will set to EBADF in case of error.\r
506  * </function>\r
507  */\r
508 static int\r
509 closedir (DIR *dirp)\r
510 {   \r
511   int retcode = 0;\r
512 \r
513   /* make sure that dirp points to legal structure */\r
514   assert (dirp != NULL);\r
515   if (dirp == NULL) {\r
516     errno = EBADF;\r
517     return -1;\r
518   }\r
519  \r
520   /* free directory name and search handles */\r
521   if (dirp->dirname != NULL) free (dirp->dirname);\r
522 \r
523 #if defined(DIRENT_WIN32_INTERFACE)\r
524   if (dirp->search_handle != INVALID_HANDLE_VALUE) {\r
525     if (FindClose (dirp->search_handle) == FALSE) {\r
526       /* Unknown error */\r
527       retcode = -1;\r
528       errno = EBADF;\r
529     }\r
530   }\r
531 #endif                     \r
532 \r
533   /* clear dirp structure to make sure that it cannot be used anymore*/\r
534   memset (dirp, 0, sizeof (*dirp));\r
535 # if defined(DIRENT_WIN32_INTERFACE)\r
536   dirp->search_handle = INVALID_HANDLE_VALUE;\r
537 # endif\r
538 \r
539   free (dirp);\r
540   return retcode;\r
541 }\r
542 \r
543 \r
544 /*\r
545  * <function name="rewinddir">\r
546  * <intro>rewind directory stream to the beginning\r
547  * <syntax>void rewinddir (DIR *dirp);\r
548  *\r
549  * <desc>Rewind directory stream to the beginning so that the next call of\r
550  * readdir() returns the very first directory entry again.  However, note\r
551  * that next call of readdir() may not return the same directory entry as it\r
552  * did in first time.  The directory stream may have been affected by newly\r
553  * created files.\r
554  *\r
555  * Almost every dirent implementation ensure that rewinddir will update\r
556  * the directory stream to reflect any changes made to the directory entries\r
557  * since the previous ``opendir'' or ``rewinddir'' call.  Keep an eye on\r
558  * this if your program depends on the feature.  I know at least one dirent\r
559  * implementation where you are required to close and re-open the stream to\r
560  * see the changes.\r
561  *\r
562  * <ret>Returns nothing.  If something went wrong while rewinding, you will\r
563  * notice it later when you try to retrieve the first directory entry.\r
564  */\r
565 static void\r
566 rewinddir (DIR *dirp)\r
567 {   \r
568   /* make sure that dirp is legal */\r
569   assert (dirp != NULL);\r
570   if (dirp == NULL) {\r
571     errno = EBADF;\r
572     return;\r
573   }\r
574   assert (dirp->dirname != NULL);\r
575   \r
576   /* close previous stream */\r
577 #if defined(DIRENT_WIN32_INTERFACE)\r
578   if (dirp->search_handle != INVALID_HANDLE_VALUE) {\r
579     if (FindClose (dirp->search_handle) == FALSE) {\r
580       /* Unknown error */\r
581       errno = EBADF;\r
582     }\r
583   }\r
584 #endif\r
585 \r
586   /* re-open previous stream */\r
587   if (_initdir (dirp) == 0) {\r
588     /* initialization failed but we cannot deal with error.  User will notice\r
589      * error later when she tries to retrieve first directory enty. */\r
590     /*EMPTY*/;\r
591   }\r
592 }\r
593 \r
594 \r
595 /*\r
596  * Open native directory stream object and retrieve first file.\r
597  * Be sure to close previous stream before opening new one.\r
598  */\r
599 static int\r
600 _initdir (DIR *dirp)\r
601\r
602   assert (dirp != NULL);\r
603   assert (dirp->dirname != NULL);\r
604   dirp->dirent_filled = 0;\r
605 \r
606 # if defined(DIRENT_WIN32_INTERFACE)\r
607   /* Open stream and retrieve first file */\r
608   dirp->search_handle = FindFirstFile (dirp->dirname, &dirp->current.data);\r
609   if (dirp->search_handle == INVALID_HANDLE_VALUE) {\r
610     /* something went wrong but we don't know what.  GetLastError() could\r
611      * give us more information about the error, but then we should map\r
612      * the error code into errno. */\r
613     errno = ENOENT;\r
614     return 0;\r
615   }\r
616 \r
617 # elif defined(DIRENT_MSDOS_INTERFACE)\r
618   if (_dos_findfirst (dirp->dirname,\r
619           _A_SUBDIR | _A_RDONLY | _A_ARCH | _A_SYSTEM | _A_HIDDEN,\r
620           &dirp->current.data) != 0)\r
621   {\r
622     /* _dos_findfirst and findfirst will set errno to ENOENT when no \r
623      * more entries could be retrieved. */\r
624     return 0;\r
625   }\r
626 # endif\r
627 \r
628   /* initialize DIR and it's first entry */\r
629   _setdirname (dirp);\r
630   dirp->dirent_filled = 1;\r
631   return 1;\r
632 }\r
633 \r
634 \r
635 /*\r
636  * Return implementation dependent name of the current directory entry.\r
637  */\r
638 static const char *\r
639 _getdirname (const struct dirent *dp)\r
640 {\r
641 #if defined(DIRENT_WIN32_INTERFACE)\r
642   return dp->data.cFileName;\r
643   \r
644 #elif defined(DIRENT_USE_FFBLK)\r
645   return dp->data.ff_name;\r
646   \r
647 #else\r
648   return dp->data.name;\r
649 #endif  \r
650 }\r
651 \r
652 \r
653 /*\r
654  * Copy name of implementation dependent directory entry to the d_name field.\r
655  */\r
656 static void\r
657 _setdirname (struct DIR *dirp) {\r
658   /* make sure that d_name is long enough */\r
659   assert (strlen (_getdirname (&dirp->current)) <= NAME_MAX);\r
660   \r
661   strncpy (dirp->current.d_name,\r
662       _getdirname (&dirp->current),\r
663       NAME_MAX);\r
664   dirp->current.d_name[NAME_MAX] = '\0'; /*char d_name[NAME_MAX+1]*/\r
665 }\r
666   \r
667 # ifdef __cplusplus\r
668 }\r
669 # endif\r
670 # define NAMLEN(dp) ((int)(strlen((dp)->d_name)))\r
671 \r
672 #else\r
673 # error "missing dirent interface"\r
674 #endif\r
675 \r
676 \r
677 #endif /*DIRENT_H*/\r