[project @ 1996-07-25 20:43:49 by partain]
[ghc-hetmet.git] / ghc / lib / cbits / removeDirectory.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \subsection[removeDirectory.lc]{removeDirectory Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 #ifdef HAVE_SYS_TYPES_H
12 #include <sys/types.h>
13 #endif
14
15 #ifdef HAVE_SYS_STAT_H
16 #include <sys/stat.h>
17 #endif
18
19 StgInt
20 removeDirectory(path)
21 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 }
56
57 \end{code}