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