Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

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