diff options
Diffstat (limited to 'cbits')
-rw-r--r-- | cbits/dirUtils.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/cbits/dirUtils.c b/cbits/dirUtils.c new file mode 100644 index 0000000..6fc0830 --- /dev/null +++ b/cbits/dirUtils.c @@ -0,0 +1,83 @@ +/* + * (c) The University of Glasgow 2002 + * + * Directory Runtime Support + */ + +/* needed only for solaris2_HOST_OS */ +#ifdef __GLASGOW_HASKELL__ +#include "ghcconfig.h" +#endif + +// The following is required on Solaris to force the POSIX versions of +// the various _r functions instead of the Solaris versions. +#ifdef solaris2_HOST_OS +#define _POSIX_PTHREAD_SEMANTICS +#endif + +#include "HsUnix.h" + +/* + * read an entry from the directory stream; opt for the + * re-entrant friendly way of doing this, if available. + */ +int +__hscore_readdir( DIR *dirPtr, struct dirent **pDirEnt ) +{ +#if HAVE_READDIR_R + struct dirent* p; + int res; + static unsigned int nm_max = (unsigned int)-1; + + if (pDirEnt == NULL) { + return -1; + } + if (nm_max == (unsigned int)-1) { +#ifdef NAME_MAX + nm_max = NAME_MAX + 1; +#else + nm_max = pathconf(".", _PC_NAME_MAX); + if (nm_max == -1) { nm_max = 255; } + nm_max++; +#endif + } + p = (struct dirent*)malloc(sizeof(struct dirent) + nm_max); + if (p == NULL) return -1; + res = readdir_r(dirPtr, p, pDirEnt); + if (res != 0) { + *pDirEnt = NULL; + free(p); + } + else if (*pDirEnt == NULL) { + // end of stream + free(p); + } + return res; +#else + + if (pDirEnt == NULL) { + return -1; + } + + *pDirEnt = readdir(dirPtr); + if (*pDirEnt == NULL) { + return -1; + } else { + return 0; + } +#endif +} + +char * +__hscore_d_name( struct dirent* d ) +{ + return (d->d_name); +} + +void +__hscore_free_dirent(struct dirent *dEnt) +{ +#if HAVE_READDIR_R + free(dEnt); +#endif +} |