[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / lib / std / cbits / directoryAux.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
3 %
4 \subsection[directoryAux.lc]{Support functions for manipulating directories}
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 #ifdef HAVE_DIRENT_H
20 #include <dirent.h>
21 #endif
22
23 StgAddr
24 openDir__(path)
25 StgByteArray path;
26 {
27     struct stat sb;
28     DIR *dir;
29
30     /* Check for an actual directory */
31     while (stat(path, &sb) != 0) {
32         if (errno != EINTR) {
33             cvtErrno();
34             stdErrno();
35             return NULL;
36         }
37     }
38     if (!S_ISDIR(sb.st_mode)) {
39         ghc_errtype = ERR_INAPPROPRIATETYPE;
40         ghc_errstr = "not a directory";
41         return NULL;
42     }
43
44     while ((dir = opendir(path)) == NULL) {
45         if (errno != EINTR) {
46             cvtErrno();
47             stdErrno();
48             return NULL;
49         }
50     }
51     return dir;
52 }
53
54 StgAddr
55 readDir__(dir)
56 StgAddr dir;
57 {
58    struct dirent *d;
59    while ((d = readdir((DIR*)dir)) == NULL) {
60     if (errno == 0) {
61         (void) closedir((DIR*)dir);
62         return NULL;
63     } else if (errno != EINTR) {
64         cvtErrno();
65         stdErrno();
66         (void) closedir((DIR*)dir);
67         return NULL;
68     }
69     errno = 0;
70   }
71   return d;
72 }
73
74 \end{code}