[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / lib / std / cbits / getCurrentDirectory.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \subsection[getCurrentDirectory.lc]{getCurrentDirectory Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 #ifndef PATH_MAX
12 #ifdef  MAXPATHLEN
13 #define PATH_MAX MAXPATHLEN
14 #else
15 #define PATH_MAX 1024
16 #endif
17 #endif
18
19 StgAddr
20 getCurrentDirectory(STG_NO_ARGS)
21 {
22     char *pwd;
23     int alloc;
24
25     alloc = PATH_MAX;
26     if ((pwd = malloc(alloc)) == NULL) {
27         ghc_errtype = ERR_RESOURCEEXHAUSTED;
28         ghc_errstr = "not enough virtual memory";
29         return NULL;
30     }
31     while (getcwd(pwd, alloc) == NULL) {
32         if (errno == ERANGE) {
33             alloc += PATH_MAX;
34             if ((pwd = realloc(pwd, alloc)) == NULL) {
35                 ghc_errtype = ERR_RESOURCEEXHAUSTED;
36                 ghc_errstr = "not enough virtual memory";
37                 return NULL;
38             }
39         } else if (errno != EINTR) {
40             cvtErrno();
41             stdErrno();
42             return NULL;
43         }
44     }
45     return (StgAddr) pwd;
46 }
47
48 \end{code}