[project @ 2002-04-24 16:01:51 by simonmar]
[packages/random.git] / cbits / dirUtils.c
1 /*
2 * (c) The University of Glasgow 2002
3 *
4 * Directory Runtime Support
5 */
6 #include "HsBase.h"
7
8 #if defined(mingw32_TARGET_OS)
9 #include <windows.h>
10 #endif
11
12 /*
13 * read an entry from the directory stream; opt for the
14 * re-entrant friendly way of doing this, if available.
15 */
16 HsInt
17 __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt )
18 {
19 struct dirent **pDirE = (struct dirent**)pDirEnt;
20 #if HAVE_READDIR_R
21 struct dirent* p;
22 int res;
23 static unsigned int nm_max = -1;
24
25 if (pDirE == NULL) {
26 return -1;
27 }
28 if (nm_max == -1) {
29 #ifdef NAME_MAX
30 nm_max = NAME_MAX + 1;
31 #else
32 nm_max = pathconf(".", _PC_NAME_MAX);
33 if (nm_max == -1) { nm_max = 255; }
34 nm_max++;
35 #endif
36 }
37 p = (struct dirent*)malloc(sizeof(struct dirent) + nm_max);
38 if (p == NULL) return -1;
39 res = readdir_r((DIR*)dirPtr, p, pDirE);
40 if (res != 0) {
41 *pDirE = NULL;
42 free(p);
43 }
44 return res;
45 #else
46
47 if (pDirE == NULL) {
48 return -1;
49 }
50
51 *pDirE = readdir((DIR*)dirPtr);
52 if (*pDirE == NULL) {
53 return -1;
54 } else {
55 return 0;
56 }
57 #endif
58 }