[project @ 1998-04-10 10:54:14 by simonm]
[ghc-hetmet.git] / ghc / lib / std / cbits / removeDirectory.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: removeDirectory.c,v 1.1 1998/04/10 10:54:44 simonm Exp $
5  *
6  * removeDirectory 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 removeDirectory(StgByteArray path)
22 {
23     struct stat sb;
24
25     /* Check for an actual directory */
26     while (stat(path, &sb) != 0) {
27         if (errno != EINTR) {
28             cvtErrno();
29             stdErrno();
30             return -1;
31         }
32     }
33     if (!S_ISDIR(sb.st_mode)) {
34         ghc_errtype = ERR_INAPPROPRIATETYPE;
35         ghc_errstr = "not a directory";
36         return -1;
37     }
38     while (rmdir(path) != 0) {
39         if (errno != EINTR) {
40             cvtErrno();
41             switch (ghc_errno) {
42             default:
43                 stdErrno();
44                 break;
45             case GHC_ENOTEMPTY:
46             case GHC_EEXIST:
47                 ghc_errtype = ERR_UNSATISFIEDCONSTRAINTS;
48                 ghc_errstr = "directory not empty";
49                 break;
50             }           
51             return -1;
52         }
53     }
54     return 0;
55 }