summaryrefslogtreecommitdiff
path: root/ghc/runtime/io/renameDirectory.lc
blob: 2a41186bfe175b2125ade2b7f8d92aa1d9e76490 (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[renameDirectory.lc]{renameDirectory Runtime Support}

\begin{code}

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

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

StgInt
renameDirectory(opath, npath)
StgByteArray opath;
StgByteArray npath;
{
    struct stat sb;

    /* Check for an actual directory */
    while (stat(opath, &sb) != 0) {
	if (errno != EINTR) {
	    cvtErrno();
	    stdErrno();
	    return -1;
	}
    }
    if (!S_ISDIR(sb.st_mode)) {
	ghc_errtype = ERR_INAPPROPRIATETYPE;
	ghc_errstr = "not a directory";
	return -1;
    }
    while(rename(opath, npath) != 0) {
	if (errno != EINTR) {
	    cvtErrno();
	    stdErrno();
	    return -1;
	}
    }
    return 0;
}
\end{code}