Call nl_langinfo(CODESET) to get the name of the locale encoding on Unix
[ghc-base.git] / cbits / dirUtils.c
1 /* 
2  * (c) The University of Glasgow 2002
3  *
4  * Directory Runtime Support
5  */
6
7 /* needed only for solaris2_HOST_OS */
8 #ifdef __GLASGOW_HASKELL__
9 #include "ghcconfig.h"
10 #endif
11
12 // The following is required on Solaris to force the POSIX versions of
13 // the various _r functions instead of the Solaris versions.
14 #ifdef solaris2_HOST_OS
15 #define _POSIX_PTHREAD_SEMANTICS
16 #endif
17
18 #include "HsBase.h"
19
20 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
21 #include <windows.h>
22 #endif
23
24
25 /*
26  * read an entry from the directory stream; opt for the
27  * re-entrant friendly way of doing this, if available.
28  */
29 int
30 __hscore_readdir( DIR *dirPtr, struct dirent **pDirEnt )
31 {
32 #if HAVE_READDIR_R
33   struct dirent* p;
34   int res;
35   static unsigned int nm_max = (unsigned int)-1;
36   
37   if (pDirEnt == NULL) {
38     return -1;
39   }
40   if (nm_max == (unsigned int)-1) {
41 #ifdef NAME_MAX
42     nm_max = NAME_MAX + 1;
43 #else
44     nm_max = pathconf(".", _PC_NAME_MAX);
45     if (nm_max == -1) { nm_max = 255; }
46     nm_max++;
47 #endif
48   }
49   p = (struct dirent*)malloc(sizeof(struct dirent) + nm_max);
50   if (p == NULL) return -1;
51   res = readdir_r(dirPtr, p, pDirEnt);
52   if (res != 0) {
53       *pDirEnt = NULL;
54       free(p);
55   }
56   else if (*pDirEnt == NULL) {
57     // end of stream
58     free(p);
59   }
60   return res;
61 #else
62
63   if (pDirEnt == NULL) {
64     return -1;
65   }
66
67   *pDirEnt = readdir(dirPtr);
68   if (*pDirEnt == NULL) {
69     return -1;
70   } else {
71     return 0;
72   }  
73 #endif
74 }