[project @ 2001-01-11 14:09:50 by simonpj]
[ghc-hetmet.git] / ghc / lib / std / cbits / removeFile.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: removeFile.c,v 1.4 2000/04/06 10:33:07 rrt Exp $
5  *
6  * removeFile 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 removeFile(StgByteArray path)
22 {
23     struct stat sb;
24
25     /* Check for a non-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 = "file is a directory";
36         return -1;
37     }
38     while (unlink(path) != 0) {
39         if (errno != EINTR) {
40             cvtErrno();
41             stdErrno();
42             return -1;
43         }
44     }
45     return 0;
46 }