Unbreak the GHC build with older versions of gcc
[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 #include "ghcconfig.h"
9
10 // The following is required on Solaris to force the POSIX versions of
11 // the various _r functions instead of the Solaris versions.
12 #ifdef solaris2_HOST_OS
13 #define _POSIX_PTHREAD_SEMANTICS
14 #endif
15
16 #include "HsBase.h"
17
18 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
19 #include <windows.h>
20 #endif
21
22
23 /*
24  * read an entry from the directory stream; opt for the
25  * re-entrant friendly way of doing this, if available.
26  */
27 int
28 __hscore_readdir( DIR *dirPtr, struct dirent **pDirEnt )
29 {
30 #if HAVE_READDIR_R
31   struct dirent* p;
32   int res;
33   static unsigned int nm_max = (unsigned int)-1;
34   
35   if (pDirEnt == NULL) {
36     return -1;
37   }
38   if (nm_max == (unsigned int)-1) {
39 #ifdef NAME_MAX
40     nm_max = NAME_MAX + 1;
41 #else
42     nm_max = pathconf(".", _PC_NAME_MAX);
43     if (nm_max == -1) { nm_max = 255; }
44     nm_max++;
45 #endif
46   }
47   p = (struct dirent*)malloc(sizeof(struct dirent) + nm_max);
48   if (p == NULL) return -1;
49   res = readdir_r(dirPtr, p, pDirEnt);
50   if (res != 0) {
51       *pDirEnt = NULL;
52       free(p);
53   }
54   else if (*pDirEnt == NULL) {
55     // end of stream
56     free(p);
57   }
58   return res;
59 #else
60
61   if (pDirEnt == NULL) {
62     return -1;
63   }
64
65   *pDirEnt = readdir(dirPtr);
66   if (*pDirEnt == NULL) {
67     return -1;
68   } else {
69     return 0;
70   }  
71 #endif
72 }