[project @ 1998-04-10 11:04:49 by simonm]
[ghc-hetmet.git] / ghc / lib / std / cbits / renameDirectory.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: renameDirectory.c,v 1.1 1998/04/10 10:54:47 simonm Exp $
5  *
6  * renameDirectory Runtime Support
7  */
8
9 #include "Rts.h"
10 #include "stgio.h"
11
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
14 #endif
15
16 #ifdef HAVE_SYS_STAT_H
17 #include <sys/stat.h>
18 #endif
19
20 StgInt
21 renameDirectory(StgByteArray opath, StgByteArray npath)
22 {
23     struct stat sb;
24
25     /* Check for an actual directory */
26     while (stat(opath, &sb) != 0) {
27         if (errno != EINTR) {
28             cvtErrno();
29             stdErrno();
30             return -1;
31         }
32     }
33     if (!S_ISDIR(sb.st_mode)) {
34         ghc_errtype = ERR_INAPPROPRIATETYPE;
35         ghc_errstr = "not a directory";
36         return -1;
37     }
38     while(rename(opath, npath) != 0) {
39         if (errno != EINTR) {
40             cvtErrno();
41             stdErrno();
42             return -1;
43         }
44     }
45     return 0;
46 }