[project @ 2001-01-11 14:09:50 by simonpj]
[ghc-hetmet.git] / ghc / lib / std / cbits / createDirectory.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: createDirectory.c,v 1.4 1999/03/01 09:03:37 sof Exp $
5  *
6  * createDirectory 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 #if defined(mingw32_TARGET_OS)
21 #define mkDir(nm,p) mkdir(nm)
22 #else
23 #define mkDir(nm,p) mkdir(nm,p)
24 #endif
25
26 StgInt 
27 createDirectory(path)
28 StgByteArray path;
29 {
30     int rc;
31     struct stat sb;
32
33     while((rc = mkDir(path, 0777)) != 0) {
34         if (errno != EINTR) {
35             cvtErrno();
36             switch (ghc_errno) {
37             default:
38                 stdErrno();
39                 break;
40             case GHC_ENOENT:
41             case GHC_ENOTDIR:
42                 ghc_errtype = ERR_NOSUCHTHING;
43                 ghc_errstr = "no path to directory";
44                 break;
45             case GHC_EEXIST:
46                 if (stat(path, &sb) != 0) {
47                     ghc_errtype = ERR_OTHERERROR;
48                     ghc_errstr = "cannot stat existing file";
49                 } 
50                 if (S_ISDIR(sb.st_mode)) {
51                     ghc_errtype = ERR_ALREADYEXISTS;
52                     ghc_errstr = "directory already exists";
53                 } else {
54                     ghc_errtype = ERR_INAPPROPRIATETYPE;
55                     ghc_errstr = "file already exists";
56                 }
57                 break;
58             }
59             return -1;
60         }
61     }
62     return 0;
63 }