[project @ 1998-04-10 10:54:14 by simonm]
[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.1 1998/04/10 10:54:16 simonm 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 StgInt 
21 createDirectory(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 }