[project @ 1998-12-02 13:17:09 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.3 1998/12/02 13:27: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(path)
22 StgByteArray path;
23 {
24     int rc;
25     struct stat sb;
26
27     while((rc = mkdir(path, 0777)) != 0) {
28         if (errno != EINTR) {
29             cvtErrno();
30             switch (ghc_errno) {
31             default:
32                 stdErrno();
33                 break;
34             case GHC_ENOENT:
35             case GHC_ENOTDIR:
36                 ghc_errtype = ERR_NOSUCHTHING;
37                 ghc_errstr = "no path to directory";
38                 break;
39             case GHC_EEXIST:
40                 if (stat(path, &sb) != 0) {
41                     ghc_errtype = ERR_OTHERERROR;
42                     ghc_errstr = "cannot stat existing file";
43                 } 
44                 if (S_ISDIR(sb.st_mode)) {
45                     ghc_errtype = ERR_ALREADYEXISTS;
46                     ghc_errstr = "directory already exists";
47                 } else {
48                     ghc_errtype = ERR_INAPPROPRIATETYPE;
49                     ghc_errstr = "file already exists";
50                 }
51                 break;
52             }
53             return -1;
54         }
55     }
56     return 0;
57 }