/*
* uce-dirent.h - operating system independent dirent implementation
- *
+ *
* Copyright (C) 1998-2002 Toni Ronkko
- *
+ *
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* ``Software''), to deal in the Software without restriction, including
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
- *
+ *
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
- *
+ *
* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
- *
- *
+ *
+ *
* May 28 1998, Toni Ronkko <tronkko@messi.uku.fi>
*
* $Id: uce-dirent.h,v 1.7 2002/05/13 10:48:35 tr Exp $
* Revision 1.1 1998/07/04 16:27:51 tr
* Initial revision
*
- *
+ *
* MSVC 1.0 scans automatic dependencies incorrectly when your project
* contains this very header. The problem is that MSVC cannot handle
* include directives inside #if..#endif block those are never entered.
*/
#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)
# if defined(_MSC_VER) /* Microsoft C/C++ */
- /* no dirent.h */
+/* no dirent.h */
# elif defined(__MINGW32__) /* MinGW */
- /* no dirent.h */
+/* no dirent.h */
# elif defined(__BORLANDC__) /* Borland C/C++ */
# define HAVE_DIRENT_H
# define VOID_CLOSEDIR
# elif defined(__TURBOC__) /* Borland Turbo C */
- /* no dirent.h */
+/* no dirent.h */
# elif defined(__WATCOMC__) /* Watcom C/C++ */
# define HAVE_DIRECT_H
# elif defined(__apollo) /* Apollo */
#elif defined(MSDOS) || defined(WIN32)
- /* figure out type of underlaying directory interface to be used */
+/* figure out type of underlaying directory interface to be used */
# if defined(WIN32)
# define DIRENT_WIN32_INTERFACE
# elif defined(MSDOS)
# error "missing native dirent interface"
# endif
- /*** WIN32 specifics ***/
+/*** WIN32 specifics ***/
# if defined(DIRENT_WIN32_INTERFACE)
# include <windows.h>
# if !defined(DIRENT_MAXNAMLEN)
# endif
- /*** MS-DOS specifics ***/
+/*** MS-DOS specifics ***/
# elif defined(DIRENT_MSDOS_INTERFACE)
# include <dos.h>
- /* Borland defines file length macros in dir.h */
+/* Borland defines file length macros in dir.h */
# if defined(__BORLANDC__)
# include <dir.h>
# if !defined(DIRENT_MAXNAMLEN)
# define _find_t find_t
# endif
- /* Turbo C defines ffblk structure in dir.h */
+/* Turbo C defines ffblk structure in dir.h */
# elif defined(__TURBOC__)
# include <dir.h>
# if !defined(DIRENT_MAXNAMLEN)
# endif
# define DIRENT_USE_FFBLK
- /* MSVC */
+/* MSVC */
# elif defined(_MSC_VER)
# if !defined(DIRENT_MAXNAMLEN)
# define DIRENT_MAXNAMLEN (12)
# endif
- /* Watcom */
+/* Watcom */
# elif defined(__WATCOMC__)
# if !defined(DIRENT_MAXNAMLEN)
# if defined(__OS2__) || defined(__NT__)
# endif
# endif
- /*** generic MS-DOS and MS-Windows stuff ***/
+/*** generic MS-DOS and MS-Windows stuff ***/
# if !defined(NAME_MAX) && defined(DIRENT_MAXNAMLEN)
# define NAME_MAX DIRENT_MAXNAMLEN
# endif
# endif
- /*
- * Substitute for real dirent structure. Note that `d_name' field is a
- * true character array although we have it copied in the implementation
- * dependent data. We could save some memory if we had declared `d_name'
- * as a pointer referring the name within implementation dependent data.
- * We have not done that since some code may rely on sizeof(d_name) to be
- * something other than four. Besides, directory entries are typically so
- * small that it takes virtually no time to copy them from place to place.
- */
- typedef struct dirent {
+/*
+ * Substitute for real dirent structure. Note that `d_name' field is a
+ * true character array although we have it copied in the implementation
+ * dependent data. We could save some memory if we had declared `d_name'
+ * as a pointer referring the name within implementation dependent data.
+ * We have not done that since some code may rely on sizeof(d_name) to be
+ * something other than four. Besides, directory entries are typically so
+ * small that it takes virtually no time to copy them from place to place.
+ */
+typedef struct dirent {
char d_name[NAME_MAX + 1];
/*** Operating system specific part ***/
struct _find_t data;
# endif
# endif
- } dirent;
+} dirent;
- /* DIR substitute structure containing directory name. The name is
- * essential for the operation of ``rewinndir'' function. */
- typedef struct DIR {
+/* DIR substitute structure containing directory name. The name is
+ * essential for the operation of ``rewinndir'' function. */
+typedef struct DIR {
char *dirname; /* directory being scanned */
dirent current; /* current entry */
int dirent_filled; /* is current un-processed? */
- /*** Operating system specific part ***/
+ /*** Operating system specific part ***/
# if defined(DIRENT_WIN32_INTERFACE)
HANDLE search_handle;
# elif defined(DIRENT_MSDOS_INTERFACE)
# endif
- } DIR;
+} DIR;
# ifdef __cplusplus
extern "C" {
# endif
/* supply prototypes for dirent functions */
-static DIR *opendir (const char *dirname);
-static struct dirent *readdir (DIR *dirp);
-static int closedir (DIR *dirp);
-static void rewinddir (DIR *dirp);
+static DIR *opendir(const char *dirname);
+static struct dirent *readdir(DIR *dirp);
+static int closedir(DIR *dirp);
+#ifdef unused
+static void rewinddir(DIR *dirp);
+#endif
/*
* Implement dirent interface as static functions so that the user does not
# define _dos_findfirst(name,flags,dest) findfirst(name,dest,flags)
#endif
-static int _initdir (DIR *p);
-static const char *_getdirname (const struct dirent *dp);
-static void _setdirname (struct DIR *dirp);
+static int _initdir(DIR *p);
+static const char *_getdirname(const struct dirent *dp);
+static void _setdirname(struct DIR *dirp);
/*
* <function name="opendir">
* internal working area that is used for retrieving individual directory
* entries. The internal working area has no fields of your interest.
*
- * <ret>Returns a pointer to the internal working area or NULL in case the
+ * <ret>Returns a pointer to the internal working area or NULL in case the
* directory stream could not be opened. Global `errno' variable will set
* in case of error as follows:
*
*/
static DIR *opendir(const char *dirname)
{
- DIR *dirp;
- assert (dirname != NULL);
-
- dirp = (DIR*)malloc (sizeof (struct DIR));
- if (dirp != NULL) {
- char *p;
-
- /* allocate room for directory name */
- dirp->dirname = (char*) malloc (strlen (dirname) + 1 + strlen ("\\*.*"));
- if (dirp->dirname == NULL) {
- /* failed to duplicate directory name. errno set by malloc() */
- free (dirp);
- return NULL;
- }
- /* Copy directory name while appending directory separator and "*.*".
- * Directory separator is not appended if the name already ends with
- * drive or directory separator. Directory separator is assumed to be
- * '/' or '\' and drive separator is assumed to be ':'. */
- strcpy (dirp->dirname, dirname);
- p = strchr (dirp->dirname, '\0');
- if (dirp->dirname < p &&
- *(p - 1) != '\\' && *(p - 1) != '/' && *(p - 1) != ':')
- {
- strcpy (p++, "\\");
- }
+ DIR *dirp;
+ assert(dirname != NULL);
+
+ dirp = (DIR*)malloc(sizeof(struct DIR));
+ if (dirp != NULL) {
+ char *p;
+
+ /* allocate room for directory name */
+ dirp->dirname = (char*) malloc(strlen(dirname) + 1 + strlen("\\*.*"));
+ if (dirp->dirname == NULL) {
+ /* failed to duplicate directory name. errno set by malloc() */
+ free(dirp);
+ return NULL;
+ }
+ /* Copy directory name while appending directory separator and "*.*".
+ * Directory separator is not appended if the name already ends with
+ * drive or directory separator. Directory separator is assumed to be
+ * '/' or '\' and drive separator is assumed to be ':'. */
+ strcpy(dirp->dirname, dirname);
+ p = strchr(dirp->dirname, '\0');
+ if (dirp->dirname < p &&
+ *(p - 1) != '\\' && *(p - 1) != '/' && *(p - 1) != ':') {
+ strcpy(p++, "\\");
+ }
# ifdef DIRENT_WIN32_INTERFACE
- strcpy (p, "*"); /*scan files with and without extension in win32*/
+ strcpy(p, "*"); /*scan files with and without extension in win32*/
# else
- strcpy (p, "*.*"); /*scan files with and without extension in DOS*/
+ strcpy(p, "*.*"); /*scan files with and without extension in DOS*/
# endif
- /* open stream */
- if (_initdir (dirp) == 0) {
- /* initialization failed */
- free (dirp->dirname);
- free (dirp);
- return NULL;
+ /* open stream */
+ if (_initdir(dirp) == 0) {
+ /* initialization failed */
+ free(dirp->dirname);
+ free(dirp);
+ return NULL;
+ }
}
- }
- return dirp;
+ return dirp;
}
* </function>
*/
static struct dirent *
-readdir (DIR *dirp)
+readdir(DIR *dirp)
{
- assert(dirp != NULL);
- if (dirp == NULL) {
- errno = EBADF;
- return NULL;
- }
+ assert(dirp != NULL);
+ if (dirp == NULL) {
+ errno = EBADF;
+ return NULL;
+ }
#if defined(DIRENT_WIN32_INTERFACE)
- if (dirp->search_handle == INVALID_HANDLE_VALUE) {
- /* directory stream was opened/rewound incorrectly or it ended normally */
- errno = EBADF;
- return NULL;
- }
+ if (dirp->search_handle == INVALID_HANDLE_VALUE) {
+ /* directory stream was opened/rewound incorrectly or it ended normally */
+ errno = EBADF;
+ return NULL;
+ }
#endif
- if (dirp->dirent_filled != 0) {
- /*
- * Directory entry has already been retrieved and there is no need to
- * retrieve a new one. Directory entry will be retrieved in advance
- * when the user calls readdir function for the first time. This is so
- * because real dirent has separate functions for opening and reading
- * the stream whereas Win32 and DOS dirents open the stream
- * automatically when we retrieve the first file. Therefore, we have to
- * save the first file when opening the stream and later we have to
- * return the saved entry when the user tries to read the first entry.
- */
- dirp->dirent_filled = 0;
- } else {
- /* fill in entry and return that */
+ if (dirp->dirent_filled != 0) {
+ /*
+ * Directory entry has already been retrieved and there is no need to
+ * retrieve a new one. Directory entry will be retrieved in advance
+ * when the user calls readdir function for the first time. This is so
+ * because real dirent has separate functions for opening and reading
+ * the stream whereas Win32 and DOS dirents open the stream
+ * automatically when we retrieve the first file. Therefore, we have to
+ * save the first file when opening the stream and later we have to
+ * return the saved entry when the user tries to read the first entry.
+ */
+ dirp->dirent_filled = 0;
+ } else {
+ /* fill in entry and return that */
#if defined(DIRENT_WIN32_INTERFACE)
- if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) {
- /* Last file has been processed or an error occurred */
- FindClose (dirp->search_handle);
- dirp->search_handle = INVALID_HANDLE_VALUE;
- errno = ENOENT;
- return NULL;
- }
+ if (FindNextFile(dirp->search_handle, &dirp->current.data) == FALSE) {
+ /* Last file has been processed or an error occurred */
+ FindClose(dirp->search_handle);
+ dirp->search_handle = INVALID_HANDLE_VALUE;
+ errno = ENOENT;
+ return NULL;
+ }
# elif defined(DIRENT_MSDOS_INTERFACE)
- if (_dos_findnext (&dirp->current.data) != 0) {
- /* _dos_findnext and findnext will set errno to ENOENT when no
- * more entries could be retrieved. */
- return NULL;
- }
+ if (_dos_findnext(&dirp->current.data) != 0) {
+ /* _dos_findnext and findnext will set errno to ENOENT when no
+ * more entries could be retrieved. */
+ return NULL;
+ }
# endif
- _setdirname (dirp);
- assert (dirp->dirent_filled == 0);
- }
- return &dirp->current;
+ _setdirname(dirp);
+ assert(dirp->dirent_filled == 0);
+ }
+ return &dirp->current;
}
* </function>
*/
static int
-closedir (DIR *dirp)
-{
- int retcode = 0;
-
- /* make sure that dirp points to legal structure */
- assert (dirp != NULL);
- if (dirp == NULL) {
- errno = EBADF;
- return -1;
- }
-
- /* free directory name and search handles */
- if (dirp->dirname != NULL) free (dirp->dirname);
+closedir(DIR *dirp)
+{
+ int retcode = 0;
+
+ /* make sure that dirp points to legal structure */
+ assert(dirp != NULL);
+ if (dirp == NULL) {
+ errno = EBADF;
+ return -1;
+ }
+
+ /* free directory name and search handles */
+ if (dirp->dirname != NULL) {
+ free(dirp->dirname);
+ }
#if defined(DIRENT_WIN32_INTERFACE)
- if (dirp->search_handle != INVALID_HANDLE_VALUE) {
- if (FindClose (dirp->search_handle) == FALSE) {
- /* Unknown error */
- retcode = -1;
- errno = EBADF;
+ if (dirp->search_handle != INVALID_HANDLE_VALUE) {
+ if (FindClose(dirp->search_handle) == FALSE) {
+ /* Unknown error */
+ retcode = -1;
+ errno = EBADF;
+ }
}
- }
-#endif
+#endif
- /* clear dirp structure to make sure that it cannot be used anymore*/
- memset (dirp, 0, sizeof (*dirp));
+ /* clear dirp structure to make sure that it cannot be used anymore*/
+ memset(dirp, 0, sizeof(*dirp));
# if defined(DIRENT_WIN32_INTERFACE)
- dirp->search_handle = INVALID_HANDLE_VALUE;
+ dirp->search_handle = INVALID_HANDLE_VALUE;
# endif
- free (dirp);
- return retcode;
+ free(dirp);
+ return retcode;
}
+#ifdef unused
/*
* <function name="rewinddir">
* <intro>rewind directory stream to the beginning
* notice it later when you try to retrieve the first directory entry.
*/
static void
-rewinddir (DIR *dirp)
-{
- /* make sure that dirp is legal */
- assert (dirp != NULL);
- if (dirp == NULL) {
- errno = EBADF;
- return;
- }
- assert (dirp->dirname != NULL);
-
- /* close previous stream */
+rewinddir(DIR *dirp)
+{
+ /* make sure that dirp is legal */
+ assert(dirp != NULL);
+ if (dirp == NULL) {
+ errno = EBADF;
+ return;
+ }
+ assert(dirp->dirname != NULL);
+
+ /* close previous stream */
#if defined(DIRENT_WIN32_INTERFACE)
- if (dirp->search_handle != INVALID_HANDLE_VALUE) {
- if (FindClose (dirp->search_handle) == FALSE) {
- /* Unknown error */
- errno = EBADF;
+ if (dirp->search_handle != INVALID_HANDLE_VALUE) {
+ if (FindClose(dirp->search_handle) == FALSE) {
+ /* Unknown error */
+ errno = EBADF;
+ }
}
- }
#endif
- /* re-open previous stream */
- if (_initdir (dirp) == 0) {
- /* initialization failed but we cannot deal with error. User will notice
- * error later when she tries to retrieve first directory enty. */
- /*EMPTY*/;
- }
+ /* re-open previous stream */
+ if (_initdir(dirp) == 0) {
+ /* initialization failed but we cannot deal with error. User will notice
+ * error later when she tries to retrieve first directory enty. */
+ /*EMPTY*/;
+ }
}
-
+#endif
/*
* Open native directory stream object and retrieve first file.
* Be sure to close previous stream before opening new one.
*/
static int
-_initdir (DIR *dirp)
-{
- assert (dirp != NULL);
- assert (dirp->dirname != NULL);
- dirp->dirent_filled = 0;
+_initdir(DIR *dirp)
+{
+ assert(dirp != NULL);
+ assert(dirp->dirname != NULL);
+ dirp->dirent_filled = 0;
# if defined(DIRENT_WIN32_INTERFACE)
- /* Open stream and retrieve first file */
- dirp->search_handle = FindFirstFile (dirp->dirname, &dirp->current.data);
- if (dirp->search_handle == INVALID_HANDLE_VALUE) {
- /* something went wrong but we don't know what. GetLastError() could
- * give us more information about the error, but then we should map
- * the error code into errno. */
- errno = ENOENT;
- return 0;
- }
+ /* Open stream and retrieve first file */
+ dirp->search_handle = FindFirstFile(dirp->dirname, &dirp->current.data);
+ if (dirp->search_handle == INVALID_HANDLE_VALUE) {
+ /* something went wrong but we don't know what. GetLastError() could
+ * give us more information about the error, but then we should map
+ * the error code into errno. */
+ errno = ENOENT;
+ return 0;
+ }
# elif defined(DIRENT_MSDOS_INTERFACE)
- if (_dos_findfirst (dirp->dirname,
- _A_SUBDIR | _A_RDONLY | _A_ARCH | _A_SYSTEM | _A_HIDDEN,
- &dirp->current.data) != 0)
- {
- /* _dos_findfirst and findfirst will set errno to ENOENT when no
- * more entries could be retrieved. */
- return 0;
- }
+ if (_dos_findfirst(dirp->dirname,
+ _A_SUBDIR | _A_RDONLY | _A_ARCH | _A_SYSTEM | _A_HIDDEN,
+ &dirp->current.data) != 0) {
+ /* _dos_findfirst and findfirst will set errno to ENOENT when no
+ * more entries could be retrieved. */
+ return 0;
+ }
# endif
- /* initialize DIR and it's first entry */
- _setdirname (dirp);
- dirp->dirent_filled = 1;
- return 1;
+ /* initialize DIR and it's first entry */
+ _setdirname(dirp);
+ dirp->dirent_filled = 1;
+ return 1;
}
* Return implementation dependent name of the current directory entry.
*/
static const char *
-_getdirname (const struct dirent *dp)
+_getdirname(const struct dirent *dp)
{
#if defined(DIRENT_WIN32_INTERFACE)
- return dp->data.cFileName;
-
+ return dp->data.cFileName;
+
#elif defined(DIRENT_USE_FFBLK)
- return dp->data.ff_name;
-
+ return dp->data.ff_name;
+
#else
- return dp->data.name;
-#endif
+ return dp->data.name;
+#endif
}
* Copy name of implementation dependent directory entry to the d_name field.
*/
static void
-_setdirname (struct DIR *dirp) {
- /* make sure that d_name is long enough */
- assert (strlen (_getdirname (&dirp->current)) <= NAME_MAX);
-
- strncpy (dirp->current.d_name,
- _getdirname (&dirp->current),
- NAME_MAX);
- dirp->current.d_name[NAME_MAX] = '\0'; /*char d_name[NAME_MAX+1]*/
+_setdirname(struct DIR *dirp)
+{
+ /* make sure that d_name is long enough */
+ assert(strlen(_getdirname(&dirp->current)) <= NAME_MAX);
+
+ strncpy(dirp->current.d_name,
+ _getdirname(&dirp->current),
+ NAME_MAX);
+ dirp->current.d_name[NAME_MAX] = '\0'; /*char d_name[NAME_MAX+1]*/
}
-
+
# ifdef __cplusplus
}
# endif