[project @ 2000-08-24 10:27:01 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / cbits / directoryAux.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1998
3  *
4  * $Id: directoryAux.c,v 1.3 2000/08/24 10:27:01 simonmar Exp $
5  *
6  * Support functions for manipulating directories
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 #ifdef HAVE_DIRENT_H
21 #include <dirent.h>
22 #endif
23
24 StgAddr
25 openDir__(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__(StgAddr dir)
56
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 StgAddr 
75 get_dirent_d_name(StgAddr d)
76 {
77     return ((struct dirent*)d)->d_name;
78 }
79
80 StgInt sizeof_stat( void ) { return sizeof(struct stat); }
81
82 StgInt  prim_stat(StgAddr x, StgAddr y)
83 {
84     return stat((char*)x, (struct stat*)y);
85 }
86
87
88 StgWord 
89 get_stat_st_mode  (StgAddr x)
90 {
91     return ((struct stat *)x)->st_mode;
92 }
93
94
95 StgInt64
96 get_stat_st_mtime(StgAddr x)
97 {
98   return ((struct stat *)x)->st_mtime;
99 }
100
101 void
102 set_stat_st_mtime(StgByteArray p, StgByteArray x)
103 {
104   ((unsigned long *)p)[0] = ((struct stat *)x)->st_mtime;
105   return;
106 }
107
108 StgWord const_S_IRUSR( void ) { return S_IRUSR; }
109 StgWord const_S_IWUSR( void ) { return S_IWUSR; }
110 StgWord const_S_IXUSR( void ) { return S_IXUSR; }
111
112 StgInt  
113 prim_S_ISDIR( StgWord x )
114
115     return S_ISDIR(x);
116 }
117
118 StgInt  
119 prim_S_ISREG( StgWord x )
120
121     return S_ISREG(x);
122 }
123
124
125 StgWord const_R_OK( void ) { return R_OK; }
126 StgWord const_W_OK( void ) { return W_OK; }
127 StgWord const_X_OK( void ) { return X_OK; }
128 StgWord const_F_OK( void ) { return F_OK; }