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