[project @ 1998-12-02 13:17:09 by simonm]
[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.2 1998/12/02 13:27:17 simonm 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 const_F_OK( void ) { return F_OK; }
81
82 StgInt sizeof_stat( void ) { return sizeof(struct stat); }
83
84 StgInt  prim_stat(StgAddr x, StgAddr y)
85 {
86     return stat((char*)x, (struct stat*)y);
87 }
88
89
90 StgWord 
91 get_stat_st_mode  (StgAddr x)
92 {
93     return ((struct stat *)x)->st_mode;
94 }
95
96
97 StgInt64
98 get_stat_st_mtime(StgAddr x)
99 {
100   return ((struct stat *)x)->st_mtime;
101 }
102
103 void
104 set_stat_st_mtime(StgByteArray p, StgByteArray x)
105 {
106   ((unsigned long *)p)[0] = ((struct stat *)x)->st_mtime;
107   return;
108 }
109
110 StgWord const_S_IRUSR( void ) { return S_IRUSR; }
111 StgWord const_S_IWUSR( void ) { return S_IWUSR; }
112 StgWord const_S_IXUSR( void ) { return S_IXUSR; }
113
114 StgInt  
115 prim_S_ISDIR( StgWord x )
116
117     return S_ISDIR(x);
118 }
119
120 StgInt  
121 prim_S_ISREG( StgWord x )
122
123     return S_ISREG(x);
124 }
125