Added the dirent.h file used for opening and reading Directory holding image files...
authorParvatha Elangovan <p.elangovan@intopix.com>
Tue, 27 Feb 2007 08:54:56 +0000 (08:54 +0000)
committerParvatha Elangovan <p.elangovan@intopix.com>
Tue, 27 Feb 2007 08:54:56 +0000 (08:54 +0000)
codec/compat/getopt.c
codec/dirent.h [new file with mode: 0644]

index 426095b56fa689c3582ddf3ad054caf3d53b6153..29404b60a89d7b41772f443198fa0d3323d585a7 100644 (file)
@@ -108,10 +108,11 @@ int getopt(int nargc, char *const *nargv, const char *ostr) {
       return (-1);
     if (!*place)
       ++optind;
-    if (opterr && *ostr != ':')
-      (void) fprintf(stderr,
+               if (opterr && *ostr != ':') {
+      fprintf(stderr,
                     "%s: illegal option -- %c\n", __progname, optopt);
-    return (BADCH);
+                       return (BADCH);
+               }
   }
   if (*++oli != ':') {         /* don't need argument */
     optarg = NULL;
@@ -124,11 +125,12 @@ int getopt(int nargc, char *const *nargv, const char *ostr) {
       place = EMSG;
       if (*ostr == ':')
        return (BADARG);
-      if (opterr)
-       (void) fprintf(stderr,
+                       if (opterr) {
+                               fprintf(stderr,
                       "%s: option requires an argument -- %c\n",
                       __progname, optopt);
-      return (BADCH);
+                               return (BADCH);
+                       }
     } else                     /* white space */
       optarg = nargv[optind];
     place = EMSG;
@@ -171,17 +173,19 @@ again:
                                                if(optarg){
                                                        if (strchr(optarg,'-')){ /* No argument */
                                                                if (*optstring==':') return ':';
-                                                               if (opterr)
-                                                               (void) fprintf(stderr,"%s: option requires an argument %c\n",arg, optopt);
-                                                               return (BADCH);
+                                                               if (opterr) {
+                                                                       fprintf(stderr,"%s: option requires an argument %c\n",arg, optopt);
+                                                                       return (BADCH);
+                                                               }
                                                                ++optind;
                                                        }
                                                }
                                                if (!optarg && o->has_arg==1) { /* no argument there */
                                                        if (*optstring==':') return ':';
-                                                       if (opterr)
-                                                       (void) fprintf(stderr,"%s: option requires an argument %c\n",arg, optopt);
-                                                       return (BADCH);
+                                                       if (opterr) {
+                                                               fprintf(stderr,"%s: option requires an argument %c\n",arg, optopt);
+                                                               return (BADCH);                                                 
+                                                       }
                                                        ++optind;
                                                }
                                                ++optind;
@@ -242,4 +246,5 @@ found:
                ++optind;
                return '?';
        }// end of long option
+       return (BADCH);
 }
diff --git a/codec/dirent.h b/codec/dirent.h
new file mode 100644 (file)
index 0000000..a2b9176
--- /dev/null
@@ -0,0 +1,677 @@
+\r
+/*\r
+ * uce-dirent.h - operating system independent dirent implementation\r
+ * \r
+ * Copyright (C) 1998-2002  Toni Ronkko\r
+ * \r
+ * Permission is hereby granted, free of charge, to any person obtaining\r
+ * a copy of this software and associated documentation files (the\r
+ * ``Software''), to deal in the Software without restriction, including\r
+ * without limitation the rights to use, copy, modify, merge, publish,\r
+ * distribute, sublicense, and/or sell copies of the Software, and to\r
+ * permit persons to whom the Software is furnished to do so, subject to\r
+ * the following conditions:\r
+ * \r
+ * The above copyright notice and this permission notice shall be included\r
+ * in all copies or substantial portions of the Software.\r
+ * \r
+ * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r
+ * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR\r
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\r
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\r
+ * OTHER DEALINGS IN THE SOFTWARE.\r
+ * \r
+ * \r
+ * May 28 1998, Toni Ronkko <tronkko@messi.uku.fi>\r
+ *\r
+ * $Id: uce-dirent.h,v 1.7 2002/05/13 10:48:35 tr Exp $\r
+ *\r
+ * $Log: uce-dirent.h,v $\r
+ * Revision 1.7  2002/05/13 10:48:35  tr\r
+ * embedded some source code directly to the header so that no source\r
+ * modules need to be included in the MS Visual C project using the\r
+ * interface, removed all the dependencies to other headers of the `uce'\r
+ * library so that the header can be made public\r
+ *\r
+ * Revision 1.6  2002/04/12 16:22:04  tr\r
+ * Unified Compiling Environment (UCE) replaced `std' library\r
+ *\r
+ * Revision 1.5  2001/07/20 16:33:40  tr\r
+ * moved to `std' library and re-named defines accordingly\r
+ *\r
+ * Revision 1.4  2001/07/10 16:47:18  tronkko\r
+ * revised comments\r
+ *\r
+ * Revision 1.3  2001/01/11 13:16:43  tr\r
+ * using ``uce-machine.h'' for finding out defines such as `FREEBSD'\r
+ *\r
+ * Revision 1.2  2000/10/08 16:00:41  tr\r
+ * copy of FreeBSD man page\r
+ *\r
+ * Revision 1.1  2000/07/10 05:53:16  tr\r
+ * Initial revision\r
+ *\r
+ * Revision 1.2  1998/07/19 18:29:14  tr\r
+ * Added error reporting capabilities and some asserts.\r
+ *\r
+ * Revision 1.1  1998/07/04 16:27:51  tr\r
+ * Initial revision\r
+ *\r
+ * \r
+ * MSVC 1.0 scans automatic dependencies incorrectly when your project\r
+ * contains this very header.  The problem is that MSVC cannot handle\r
+ * include directives inside #if..#endif block those are never entered.\r
+ * Since this header ought to compile in many different operating systems,\r
+ * there had to be several conditional blocks that are compiled only in\r
+ * operating systems for what they were designed for.  MSVC 1.0 cannot\r
+ * handle inclusion of sys/dir.h in a part that is compiled only in Apollo\r
+ * operating system.  To fix the problem you need to insert DIR.H into\r
+ * SYSINCL.DAT located in MSVC\BIN directory and restart visual C++.\r
+ * Consult manuals for more informaton about the problem.\r
+ *\r
+ * Since many UNIX systems have dirent.h we assume to have one also.\r
+ * However, if your UNIX system does not have dirent.h you can download one\r
+ * for example at: http://ftp.uni-mannheim.de/ftp/GNU/dirent/dirent.tar.gz.\r
+ * You can also see if you have one of dirent.h, direct.h, dir.h, ndir.h,\r
+ * sys/dir.h and sys/ndir.h somewhere.  Try defining HAVE_DIRENT_H,\r
+ * HAVE_DIRECT_H, HAVE_DIR_H, HAVE_NDIR_H, HAVE_SYS_DIR_H and\r
+ * HAVE_SYS_NDIR_H according to the files found.\r
+ */\r
+#ifndef DIRENT_H\r
+#define DIRENT_H\r
+#define DIRENT_H_INCLUDED\r
+\r
+/* find out platform */\r
+#if defined(MSDOS)                             /* MS-DOS */\r
+#elif defined(__MSDOS__)                       /* Turbo C/Borland */\r
+# define MSDOS\r
+#elif defined(__DOS__)                         /* Watcom */\r
+# define MSDOS\r
+#endif\r
+\r
+#if defined(WIN32)                             /* MS-Windows */\r
+#elif defined(__NT__)                          /* Watcom */\r
+# define WIN32\r
+#elif defined(_WIN32)                          /* Microsoft */\r
+# define WIN32\r
+#elif defined(__WIN32__)                       /* Borland */\r
+# define WIN32\r
+#endif\r
+\r
+/*\r
+ * See what kind of dirent interface we have unless autoconf has already\r
+ * determinated that.\r
+ */\r
+#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
+# if defined(_MSC_VER)                         /* Microsoft C/C++ */\r
+    /* no dirent.h */\r
+# elif defined(__BORLANDC__)                   /* Borland C/C++ */\r
+#   define HAVE_DIRENT_H\r
+#   define VOID_CLOSEDIR\r
+# elif defined(__TURBOC__)                     /* Borland Turbo C */\r
+    /* no dirent.h */\r
+# elif defined(__WATCOMC__)                    /* Watcom C/C++ */\r
+#   define HAVE_DIRECT_H\r
+# elif defined(__apollo)                       /* Apollo */\r
+#   define HAVE_SYS_DIR_H\r
+# elif defined(__hpux)                         /* HP-UX */\r
+#   define HAVE_DIRENT_H\r
+# elif defined(__alpha) || defined(__alpha__)  /* Alpha OSF1 */\r
+#   error "not implemented"\r
+# elif defined(__sgi)                          /* Silicon Graphics */\r
+#   define HAVE_DIRENT_H\r
+# elif defined(sun) || defined(_sun)           /* Sun Solaris */\r
+#   define HAVE_DIRENT_H\r
+# elif defined(__FreeBSD__)                    /* FreeBSD */\r
+#   define HAVE_DIRENT_H\r
+# elif defined(__linux__)                      /* Linux */\r
+#   define HAVE_DIRENT_H\r
+# elif defined(__GNUC__)                       /* GNU C/C++ */\r
+#   define HAVE_DIRENT_H\r
+# else\r
+#   error "not implemented"\r
+# endif\r
+#endif\r
+\r
+/* include proper interface headers */\r
+#if defined(HAVE_DIRENT_H)\r
+# include <dirent.h>\r
+# ifdef FREEBSD\r
+#   define NAMLEN(dp) ((int)((dp)->d_namlen))\r
+# else\r
+#   define NAMLEN(dp) ((int)(strlen((dp)->d_name)))\r
+# endif\r
+\r
+#elif defined(HAVE_NDIR_H)\r
+# include <ndir.h>\r
+# define NAMLEN(dp) ((int)((dp)->d_namlen))\r
+\r
+#elif defined(HAVE_SYS_NDIR_H)\r
+# include <sys/ndir.h>\r
+# define NAMLEN(dp) ((int)((dp)->d_namlen))\r
+\r
+#elif defined(HAVE_DIRECT_H)\r
+# include <direct.h>\r
+# define NAMLEN(dp) ((int)((dp)->d_namlen))\r
+\r
+#elif defined(HAVE_DIR_H)\r
+# include <dir.h>\r
+# define NAMLEN(dp) ((int)((dp)->d_namlen))\r
+\r
+#elif defined(HAVE_SYS_DIR_H)\r
+# include <sys/types.h>\r
+# include <sys/dir.h>\r
+# ifndef dirent\r
+#   define dirent direct\r
+# endif\r
+# define NAMLEN(dp) ((int)((dp)->d_namlen))\r
+\r
+#elif defined(MSDOS) || defined(WIN32)\r
+\r
+  /* figure out type of underlaying directory interface to be used */\r
+# if defined(WIN32)\r
+#   define DIRENT_WIN32_INTERFACE\r
+# elif defined(MSDOS)\r
+#   define DIRENT_MSDOS_INTERFACE\r
+# else\r
+#   error "missing native dirent interface"\r
+# endif\r
+\r
+  /*** WIN32 specifics ***/\r
+# if defined(DIRENT_WIN32_INTERFACE)\r
+#   include <windows.h>\r
+#   if !defined(DIRENT_MAXNAMLEN)\r
+#     define DIRENT_MAXNAMLEN (MAX_PATH)\r
+#   endif\r
+\r
+\r
+  /*** MS-DOS specifics ***/\r
+# elif defined(DIRENT_MSDOS_INTERFACE)\r
+#   include <dos.h>\r
+\r
+    /* Borland defines file length macros in dir.h */\r
+#   if defined(__BORLANDC__)\r
+#     include <dir.h>\r
+#     if !defined(DIRENT_MAXNAMLEN)\r
+#       define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))\r
+#     endif\r
+#     if !defined(_find_t)\r
+#       define _find_t find_t\r
+#     endif\r
+\r
+    /* Turbo C defines ffblk structure in dir.h */\r
+#   elif defined(__TURBOC__)\r
+#     include <dir.h>\r
+#     if !defined(DIRENT_MAXNAMLEN)\r
+#       define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))\r
+#     endif\r
+#     define DIRENT_USE_FFBLK\r
+\r
+    /* MSVC */\r
+#   elif defined(_MSC_VER)\r
+#     if !defined(DIRENT_MAXNAMLEN)\r
+#       define DIRENT_MAXNAMLEN (12)\r
+#     endif\r
+\r
+    /* Watcom */\r
+#   elif defined(__WATCOMC__)\r
+#     if !defined(DIRENT_MAXNAMLEN)\r
+#       if defined(__OS2__) || defined(__NT__)\r
+#         define DIRENT_MAXNAMLEN (255)\r
+#       else\r
+#         define DIRENT_MAXNAMLEN (12)\r
+#       endif\r
+#     endif\r
+\r
+#   endif\r
+# endif\r
+\r
+  /*** generic MS-DOS and MS-Windows stuff ***/\r
+# if !defined(NAME_MAX) && defined(DIRENT_MAXNAMLEN)\r
+#   define NAME_MAX DIRENT_MAXNAMLEN\r
+# endif\r
+# if NAME_MAX < DIRENT_MAXNAMLEN\r
+#   error "assertion failed: NAME_MAX >= DIRENT_MAXNAMLEN"\r
+# endif\r
+\r
+\r
+  /*\r
+   * Substitute for real dirent structure.  Note that `d_name' field is a\r
+   * true character array although we have it copied in the implementation\r
+   * dependent data.  We could save some memory if we had declared `d_name'\r
+   * as a pointer refering the name within implementation dependent data.\r
+   * We have not done that since some code may rely on sizeof(d_name) to be\r
+   * something other than four.  Besides, directory entries are typically so\r
+   * small that it takes virtually no time to copy them from place to place.\r
+   */\r
+  typedef struct dirent {\r
+    char d_name[NAME_MAX + 1];\r
+\r
+    /*** Operating system specific part ***/\r
+# if defined(DIRENT_WIN32_INTERFACE)       /*WIN32*/\r
+    WIN32_FIND_DATA data;\r
+# elif defined(DIRENT_MSDOS_INTERFACE)     /*MSDOS*/\r
+#   if defined(DIRENT_USE_FFBLK)\r
+    struct ffblk data;\r
+#   else\r
+    struct _find_t data;\r
+#   endif\r
+# endif\r
+  } dirent;\r
+\r
+  /* DIR substitute structure containing directory name.  The name is\r
+   * essential for the operation of ``rewinndir'' function. */\r
+  typedef struct DIR {\r
+    char          *dirname;                    /* directory being scanned */\r
+    dirent        current;                     /* current entry */\r
+    int           dirent_filled;               /* is current un-processed? */\r
+\r
+  /*** Operating system specific part ***/\r
+#  if defined(DIRENT_WIN32_INTERFACE)\r
+    HANDLE        search_handle;\r
+#  elif defined(DIRENT_MSDOS_INTERFACE)\r
+#  endif\r
+  } DIR;\r
+\r
+# ifdef __cplusplus\r
+extern "C" {\r
+# endif\r
+\r
+/* supply prototypes for dirent functions */\r
+static DIR *opendir (const char *dirname);\r
+static struct dirent *readdir (DIR *dirp);\r
+static int closedir (DIR *dirp);\r
+static void rewinddir (DIR *dirp);\r
+\r
+/*\r
+ * Implement dirent interface as static functions so that the user does not\r
+ * need to change his project in any way to use dirent function.  With this\r
+ * it is sufficient to include this very header from source modules using\r
+ * dirent functions and the functions will be pulled in automatically.\r
+ */\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <assert.h>\r
+#include <errno.h>\r
+\r
+/* use ffblk instead of _find_t if requested */\r
+#if defined(DIRENT_USE_FFBLK)\r
+# define _A_ARCH   (FA_ARCH)\r
+# define _A_HIDDEN (FA_HIDDEN)\r
+# define _A_NORMAL (0)\r
+# define _A_RDONLY (FA_RDONLY)\r
+# define _A_SUBDIR (FA_DIREC)\r
+# define _A_SYSTEM (FA_SYSTEM)\r
+# define _A_VOLID  (FA_LABEL)\r
+# define _dos_findnext(dest) findnext(dest)\r
+# define _dos_findfirst(name,flags,dest) findfirst(name,dest,flags)\r
+#endif\r
+\r
+static int _initdir (DIR *p);\r
+static const char *_getdirname (const struct dirent *dp);\r
+static void _setdirname (struct DIR *dirp);\r
+\r
+/*\r
+ * <function name="opendir">\r
+ * <intro>open directory stream for reading\r
+ * <syntax>DIR *opendir (const char *dirname);\r
+ *\r
+ * <desc>Open named directory stream for read and return pointer to the\r
+ * internal working area that is used for retrieving individual directory\r
+ * entries.  The internal working area has no fields of your interest.\r
+ *\r
+ * <ret>Returns a pointer to the internal working area or NULL in case the \r
+ * directory stream could not be opened.  Global `errno' variable will set\r
+ * in case of error as follows:\r
+ *\r
+ * <table>\r
+ * [EACESS  |Permission denied.\r
+ * [EMFILE  |Too many open files used by the process.\r
+ * [ENFILE  |Too many open files in system.\r
+ * [ENOENT  |Directory does not exist.\r
+ * [ENOMEM  |Insufficient memory.\r
+ * [ENOTDIR |dirname does not refer to directory.  This value is not\r
+ *           reliable on MS-DOS and MS-Windows platforms.  Many\r
+ *           implementations return ENOENT even when the name refers to a\r
+ *           file.]\r
+ * </table>\r
+ * </function>\r
+ */\r
+static DIR *opendir(const char *dirname)\r
+{\r
+  DIR *dirp;\r
+  assert (dirname != NULL);\r
+  \r
+  dirp = (DIR*)malloc (sizeof (struct DIR));\r
+  if (dirp != NULL) {\r
+    char *p;\r
+    \r
+    /* allocate room for directory name */\r
+    dirp->dirname = (char*) malloc (strlen (dirname) + 1 + strlen ("\\*.*"));\r
+    if (dirp->dirname == NULL) {\r
+      /* failed to duplicate directory name.  errno set by malloc() */\r
+      free (dirp);\r
+      return NULL;\r
+    }\r
+    /* Copy directory name while appending directory separator and "*.*".\r
+     * Directory separator is not appended if the name already ends with\r
+     * drive or directory separator.  Directory separator is assumed to be\r
+     * '/' or '\' and drive separator is assumed to be ':'. */\r
+    strcpy (dirp->dirname, dirname);\r
+    p = strchr (dirp->dirname, '\0');\r
+    if (dirp->dirname < p  &&\r
+        *(p - 1) != '\\'  &&  *(p - 1) != '/'  &&  *(p - 1) != ':')\r
+    {\r
+      strcpy (p++, "\\");\r
+    }\r
+# ifdef DIRENT_WIN32_INTERFACE\r
+    strcpy (p, "*"); /*scan files with and without extension in win32*/\r
+# else\r
+    strcpy (p, "*.*"); /*scan files with and without extension in DOS*/\r
+# endif\r
+\r
+    /* open stream */\r
+    if (_initdir (dirp) == 0) {\r
+      /* initialization failed */\r
+      free (dirp->dirname);\r
+      free (dirp);\r
+      return NULL;\r
+    }\r
+  }\r
+  return dirp;\r
+}\r
+\r
+\r
+/*\r
+ * <function name="readdir">\r
+ * <intro>read a directory entry\r
+ * <syntax>struct dirent *readdir (DIR *dirp);\r
+ *\r
+ * <desc>Read individual directory entry and return pointer to a structure\r
+ * containing the name of the entry.  Individual directory entries returned\r
+ * include normal files, sub-directories, pseudo-directories "." and ".."\r
+ * and also volume labels, hidden files and system files in MS-DOS and\r
+ * MS-Windows.   You might want to use stat(2) function to determinate which\r
+ * one are you dealing with.  Many dirent implementations already contain\r
+ * equivalent information in dirent structure but you cannot depend on\r
+ * this.\r
+ *\r
+ * The dirent structure contains several system dependent fields that\r
+ * generally have no interest to you.  The only interesting one is char\r
+ * d_name[] that is also portable across different systems.  The d_name\r
+ * field contains the name of the directory entry without leading path.\r
+ * While d_name is portable across different systems the actual storage\r
+ * capacity of d_name varies from system to system and there is no portable\r
+ * way to find out it at compile time as different systems define the\r
+ * capacity of d_name with different macros and some systems do not define\r
+ * capacity at all (besides actual declaration of the field). If you really\r
+ * need to find out storage capacity of d_name then you might want to try\r
+ * NAME_MAX macro. The NAME_MAX is defined in POSIX standard althought\r
+ * there are many MS-DOS and MS-Windows implementations those do not define\r
+ * it.  There are also systems that declare d_name as "char d_name[1]" and\r
+ * then allocate suitable amount of memory at run-time.  Thanks to Alain\r
+ * Decamps (Alain.Decamps@advalvas.be) for pointing it out to me.\r
+ *\r
+ * This all leads to the fact that it is difficult to allocate space\r
+ * for the directory names when the very same program is being compiled on\r
+ * number of operating systems.  Therefore I suggest that you always\r
+ * allocate space for directory names dynamically.\r
+ *\r
+ * <ret>\r
+ * Returns a pointer to a structure containing name of the directory entry\r
+ * in `d_name' field or NULL if there was an error.  In case of an error the\r
+ * global `errno' variable will set as follows:\r
+ *\r
+ * <table>\r
+ * [EBADF  |dir parameter refers to an invalid directory stream.  This value\r
+ *          is not set reliably on all implementations.]\r
+ * </table>\r
+ * </function>\r
+ */\r
+static struct dirent *\r
+readdir (DIR *dirp)\r
+{\r
+  assert(dirp != NULL);\r
+  if (dirp == NULL) {\r
+    errno = EBADF;\r
+    return NULL;\r
+  }\r
+\r
+#if defined(DIRENT_WIN32_INTERFACE)\r
+  if (dirp->search_handle == INVALID_HANDLE_VALUE) {\r
+    /* directory stream was opened/rewound incorrectly or it ended normally */\r
+    errno = EBADF;\r
+    return NULL;\r
+  }\r
+#endif\r
+\r
+  if (dirp->dirent_filled != 0) {\r
+    /*\r
+     * Directory entry has already been retrieved and there is no need to\r
+     * retrieve a new one.  Directory entry will be retrieved in advance\r
+     * when the user calls readdir function for the first time.  This is so\r
+     * because real dirent has separate functions for opening and reading\r
+     * the stream whereas Win32 and DOS dirents open the stream\r
+     * automatically when we retrieve the first file.  Therefore, we have to\r
+     * save the first file when opening the stream and later we have to\r
+     * return the saved entry when the user tries to read the first entry.\r
+     */\r
+    dirp->dirent_filled = 0;\r
+  } else {\r
+    /* fill in entry and return that */\r
+#if defined(DIRENT_WIN32_INTERFACE)\r
+    if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) {\r
+      /* Last file has been processed or an error occured */\r
+      FindClose (dirp->search_handle);\r
+      dirp->search_handle = INVALID_HANDLE_VALUE;\r
+      errno = ENOENT;\r
+      return NULL;\r
+    }\r
+\r
+# elif defined(DIRENT_MSDOS_INTERFACE)\r
+    if (_dos_findnext (&dirp->current.data) != 0) {\r
+      /* _dos_findnext and findnext will set errno to ENOENT when no\r
+       * more entries could be retrieved. */\r
+      return NULL;\r
+    }\r
+# endif\r
+\r
+    _setdirname (dirp);\r
+    assert (dirp->dirent_filled == 0);\r
+  }\r
+  return &dirp->current;\r
+}\r
+\r
+\r
+/*\r
+ * <function name="closedir">\r
+ * <intro>close directory stream.\r
+ * <syntax>int closedir (DIR *dirp);\r
+ *\r
+ * <desc>Close directory stream opened by the `opendir' function.  Close of\r
+ * directory stream invalidates the DIR structure as well as previously read\r
+ * dirent entry.\r
+ *\r
+ * <ret>The function typically returns 0 on success and -1 on failure but\r
+ * the function may be declared to return void on same systems.  At least\r
+ * Borland C/C++ and some UNIX implementations use void as a return type.\r
+ * The dirent wrapper tries to define VOID_CLOSEDIR whenever closedir is\r
+ * known to return nothing.  The very same definition is made by the GNU\r
+ * autoconf if you happen to use it.\r
+ *\r
+ * The global `errno' variable will set to EBADF in case of error.\r
+ * </function>\r
+ */\r
+static int\r
+closedir (DIR *dirp)\r
+{   \r
+  int retcode = 0;\r
+\r
+  /* make sure that dirp points to legal structure */\r
+  assert (dirp != NULL);\r
+  if (dirp == NULL) {\r
+    errno = EBADF;\r
+    return -1;\r
+  }\r
\r
+  /* free directory name and search handles */\r
+  if (dirp->dirname != NULL) free (dirp->dirname);\r
+\r
+#if defined(DIRENT_WIN32_INTERFACE)\r
+  if (dirp->search_handle != INVALID_HANDLE_VALUE) {\r
+    if (FindClose (dirp->search_handle) == FALSE) {\r
+      /* Unknown error */\r
+      retcode = -1;\r
+      errno = EBADF;\r
+    }\r
+  }\r
+#endif                     \r
+\r
+  /* clear dirp structure to make sure that it cannot be used anymore*/\r
+  memset (dirp, 0, sizeof (*dirp));\r
+# if defined(DIRENT_WIN32_INTERFACE)\r
+  dirp->search_handle = INVALID_HANDLE_VALUE;\r
+# endif\r
+\r
+  free (dirp);\r
+  return retcode;\r
+}\r
+\r
+\r
+/*\r
+ * <function name="rewinddir">\r
+ * <intro>rewind directory stream to the beginning\r
+ * <syntax>void rewinddir (DIR *dirp);\r
+ *\r
+ * <desc>Rewind directory stream to the beginning so that the next call of\r
+ * readdir() returns the very first directory entry again.  However, note\r
+ * that next call of readdir() may not return the same directory entry as it\r
+ * did in first time.  The directory stream may have been affected by newly\r
+ * created files.\r
+ *\r
+ * Almost every dirent implementation ensure that rewinddir will update\r
+ * the directory stream to reflect any changes made to the directory entries\r
+ * since the previous ``opendir'' or ``rewinddir'' call.  Keep an eye on\r
+ * this if your program depends on the feature.  I know at least one dirent\r
+ * implementation where you are required to close and re-open the stream to\r
+ * see the changes.\r
+ *\r
+ * <ret>Returns nothing.  If something went wrong while rewinding, you will\r
+ * notice it later when you try to retrieve the first directory entry.\r
+ */\r
+static void\r
+rewinddir (DIR *dirp)\r
+{   \r
+  /* make sure that dirp is legal */\r
+  assert (dirp != NULL);\r
+  if (dirp == NULL) {\r
+    errno = EBADF;\r
+    return;\r
+  }\r
+  assert (dirp->dirname != NULL);\r
+  \r
+  /* close previous stream */\r
+#if defined(DIRENT_WIN32_INTERFACE)\r
+  if (dirp->search_handle != INVALID_HANDLE_VALUE) {\r
+    if (FindClose (dirp->search_handle) == FALSE) {\r
+      /* Unknown error */\r
+      errno = EBADF;\r
+    }\r
+  }\r
+#endif\r
+\r
+  /* re-open previous stream */\r
+  if (_initdir (dirp) == 0) {\r
+    /* initialization failed but we cannot deal with error.  User will notice\r
+     * error later when she tries to retrieve first directory enty. */\r
+    /*EMPTY*/;\r
+  }\r
+}\r
+\r
+\r
+/*\r
+ * Open native directory stream object and retrieve first file.\r
+ * Be sure to close previous stream before opening new one.\r
+ */\r
+static int\r
+_initdir (DIR *dirp)\r
+{ \r
+  assert (dirp != NULL);\r
+  assert (dirp->dirname != NULL);\r
+  dirp->dirent_filled = 0;\r
+\r
+# if defined(DIRENT_WIN32_INTERFACE)\r
+  /* Open stream and retrieve first file */\r
+  dirp->search_handle = FindFirstFile (dirp->dirname, &dirp->current.data);\r
+  if (dirp->search_handle == INVALID_HANDLE_VALUE) {\r
+    /* something went wrong but we don't know what.  GetLastError() could\r
+     * give us more information about the error, but then we should map\r
+     * the error code into errno. */\r
+    errno = ENOENT;\r
+    return 0;\r
+  }\r
+\r
+# elif defined(DIRENT_MSDOS_INTERFACE)\r
+  if (_dos_findfirst (dirp->dirname,\r
+          _A_SUBDIR | _A_RDONLY | _A_ARCH | _A_SYSTEM | _A_HIDDEN,\r
+          &dirp->current.data) != 0)\r
+  {\r
+    /* _dos_findfirst and findfirst will set errno to ENOENT when no \r
+     * more entries could be retrieved. */\r
+    return 0;\r
+  }\r
+# endif\r
+\r
+  /* initialize DIR and it's first entry */\r
+  _setdirname (dirp);\r
+  dirp->dirent_filled = 1;\r
+  return 1;\r
+}\r
+\r
+\r
+/*\r
+ * Return implementation dependent name of the current directory entry.\r
+ */\r
+static const char *\r
+_getdirname (const struct dirent *dp)\r
+{\r
+#if defined(DIRENT_WIN32_INTERFACE)\r
+  return dp->data.cFileName;\r
+  \r
+#elif defined(DIRENT_USE_FFBLK)\r
+  return dp->data.ff_name;\r
+  \r
+#else\r
+  return dp->data.name;\r
+#endif  \r
+}\r
+\r
+\r
+/*\r
+ * Copy name of implementation dependent directory entry to the d_name field.\r
+ */\r
+static void\r
+_setdirname (struct DIR *dirp) {\r
+  /* make sure that d_name is long enough */\r
+  assert (strlen (_getdirname (&dirp->current)) <= NAME_MAX);\r
+  \r
+  strncpy (dirp->current.d_name,\r
+      _getdirname (&dirp->current),\r
+      NAME_MAX);\r
+  dirp->current.d_name[NAME_MAX] = '\0'; /*char d_name[NAME_MAX+1]*/\r
+}\r
+  \r
+# ifdef __cplusplus\r
+}\r
+# endif\r
+# define NAMLEN(dp) ((int)(strlen((dp)->d_name)))\r
+\r
+#else\r
+# error "missing dirent interface"\r
+#endif\r
+\r
+\r
+#endif /*DIRENT_H*/\r