summaryrefslogtreecommitdiff
path: root/ghc/lib/cbits/getCurrentDirectory.lc
blob: 4da895aaccf0b6b4ec55c3b03ce659feee27618a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
%
% (c) The GRASP/AQUA Project, Glasgow University, 1995
%
\subsection[getCurrentDirectory.lc]{getCurrentDirectory Runtime Support}

\begin{code}

#include "rtsdefs.h"
#include "stgio.h"

#ifndef PATH_MAX
#ifdef  MAXPATHLEN
#define PATH_MAX MAXPATHLEN
#else
#define PATH_MAX 1024
#endif
#endif

StgAddr
getCurrentDirectory(STG_NO_ARGS)
{
    char *pwd;
    int alloc;

    alloc = PATH_MAX;
    if ((pwd = malloc(alloc)) == NULL) {
	ghc_errtype = ERR_RESOURCEEXHAUSTED;
	ghc_errstr = "not enough virtual memory";
	return NULL;
    }
    while (getcwd(pwd, alloc) == NULL) {
	if (errno == ERANGE) {
	    alloc += PATH_MAX;
	    if ((pwd = realloc(pwd, alloc)) == NULL) {
		ghc_errtype = ERR_RESOURCEEXHAUSTED;
		ghc_errstr = "not enough virtual memory";
		return NULL;
	    }
	} else if (errno != EINTR) {
	    cvtErrno();
	    stdErrno();
	    return NULL;
	}
    }
    return (StgAddr) pwd;
}

\end{code}