[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / lib / std / cbits / createDirectory.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \subsection[createDirectory.lc]{createDirectory Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 #ifdef HAVE_SYS_TYPES_H
12 #include <sys/types.h>
13 #endif
14
15 #ifdef HAVE_SYS_STAT_H
16 #include <sys/stat.h>
17 #endif
18
19 StgInt 
20 createDirectory(path)
21 StgByteArray path;
22 {
23     int rc;
24     struct stat sb;
25
26     while((rc = mkdir(path, 0777)) != 0) {
27         if (errno != EINTR) {
28             cvtErrno();
29             switch (ghc_errno) {
30             default:
31                 stdErrno();
32                 break;
33             case GHC_ENOENT:
34             case GHC_ENOTDIR:
35                 ghc_errtype = ERR_NOSUCHTHING;
36                 ghc_errstr = "no path to directory";
37                 break;
38             case GHC_EEXIST:
39                 if (stat(path, &sb) != 0) {
40                     ghc_errtype = ERR_OTHERERROR;
41                     ghc_errstr = "cannot stat existing file";
42                 } 
43                 if (S_ISDIR(sb.st_mode)) {
44                     ghc_errtype = ERR_ALREADYEXISTS;
45                     ghc_errstr = "directory already exists";
46                 } else {
47                     ghc_errtype = ERR_INAPPROPRIATETYPE;
48                     ghc_errstr = "file already exists";
49                 }
50                 break;
51             }
52             return -1;
53         }
54     }
55     return 0;
56 }
57
58 \end{code}