createDirectory.c 1.17 KB
Newer Older
1
2
3
/* 
 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
 *
sof's avatar
sof committed
4
 * $Id: createDirectory.c,v 1.4 1999/03/01 09:03:37 sof Exp $
5
6
7
 *
 * createDirectory Runtime Support}
 */
8

9
#include "Rts.h"
10
11
12
13
14
15
16
17
18
19
#include "stgio.h"

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

sof's avatar
sof committed
20
21
22
23
24
25
#if defined(mingw32_TARGET_OS)
#define mkDir(nm,p) mkdir(nm)
#else
#define mkDir(nm,p) mkdir(nm,p)
#endif

26
StgInt 
27
28
createDirectory(path)
StgByteArray path;
29
30
31
32
{
    int rc;
    struct stat sb;

sof's avatar
sof committed
33
    while((rc = mkDir(path, 0777)) != 0) {
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	if (errno != EINTR) {
	    cvtErrno();
	    switch (ghc_errno) {
	    default:
		stdErrno();
		break;
	    case GHC_ENOENT:
	    case GHC_ENOTDIR:
		ghc_errtype = ERR_NOSUCHTHING;
		ghc_errstr = "no path to directory";
		break;
	    case GHC_EEXIST:
		if (stat(path, &sb) != 0) {
		    ghc_errtype = ERR_OTHERERROR;
		    ghc_errstr = "cannot stat existing file";
		} 
		if (S_ISDIR(sb.st_mode)) {
		    ghc_errtype = ERR_ALREADYEXISTS;
		    ghc_errstr = "directory already exists";
		} else {
		    ghc_errtype = ERR_INAPPROPRIATETYPE;
		    ghc_errstr = "file already exists";
		}
		break;
	    }
	    return -1;
	}
    }
    return 0;
}