[project @ 2001-12-21 15:07:20 by simonmar]
[haskell-directory.git] / include / HsCore.h
1 /* -----------------------------------------------------------------------------
2  * $Id: HsCore.h,v 1.5 2001/12/21 15:07:26 simonmar Exp $
3  *
4  * (c) The University of Glasgow 2001-2002
5  *
6  * Definitions for package `core' which are visible in Haskell land.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #ifndef HSCORE_H
11 #define HSCORE_H
12
13 #include "config.h"
14 #include "HsFFI.h"
15
16 #ifdef HAVE_SYS_TYPES_H
17 #include <sys/types.h>
18 #endif
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 #ifdef HAVE_SYS_STAT_H
23 #include <sys/stat.h>
24 #endif
25 #ifdef HAVE_FCNTL_H
26 # include <fcntl.h>
27 #endif
28 #ifdef HAVE_TERMIOS_H
29 #include <termios.h>
30 #endif
31 #ifdef HAVE_SIGNAL_H
32 #include <signal.h>
33 #endif
34 #ifdef HAVE_ERRNO_H
35 #include <errno.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 #ifdef HAVE_DIRENT_H
41 #include <dirent.h>
42 #endif
43 #ifdef HAVE_UTIME_H
44 #include <utime.h>
45 #endif
46 #if defined(HAVE_GETTIMEOFDAY)
47 #  ifdef HAVE_SYS_TIME_H
48 #   include <sys/time.h>
49 #  endif
50 #elif defined(HAVE_GETCLOCK)
51 # ifdef HAVE_SYS_TIMERS_H
52 #  define POSIX_4D9 1
53 #  include <sys/timers.h>
54 # endif
55 #endif
56 #if defined(HAVE_TIME_H)
57 # include <time.h>
58 #endif
59 #ifdef HAVE_SYS_TIMEB_H
60 #include <sys/timeb.h>
61 #endif
62 #ifdef HAVE_WINDOWS_H
63 #include <windows.h>
64 #endif
65 #ifdef HAVE_SYS_TIMES_H
66 #include <sys/times.h>
67 #endif
68 #ifdef HAVE_WINSOCK_H
69 #include <winsock.h>
70 #endif
71
72 #if !defined(mingw32_TARGET_OS) && !defined(irix_TARGET_OS)
73 # if defined(HAVE_SYS_RESOURCE_H)
74 #  include <sys/resource.h>
75 # endif
76 #endif
77
78 #ifdef hpux_TARGET_OS
79 #include <sys/syscall.h>
80 #define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
81 #define HAVE_GETRUSAGE
82 #endif
83
84 /* For System */
85 #ifdef HAVE_SYS_WAIT_H
86 #include <sys/wait.h>
87 #endif
88 #ifdef HAVE_VFORK_H
89 #include <vfork.h>
90 #endif
91 #include "lockFile.h"
92 #include "dirUtils.h"
93 #include "errUtils.h"
94 #include "PrelIOUtils.h"
95
96 #ifdef _WIN32
97 #include <io.h>
98 #include <fcntl.h>
99 #endif
100
101 /* in ghc_errno.c */
102 int *ghcErrno(void);
103
104 /* in system.c */
105 HsInt systemCmd(HsAddr cmd);
106
107 /* in inputReady.c */
108 int inputReady(int fd, int msecs, int isSock);
109
110 /* -----------------------------------------------------------------------------
111    INLINE functions.
112
113    These functions are given as inlines here for when compiling via C,
114    but we also generate static versions into the cbits library for
115    when compiling to native code.
116    -------------------------------------------------------------------------- */
117
118 #ifndef INLINE
119 #define INLINE extern inline
120 #endif
121
122 INLINE int __hscore_s_isreg(m)  { return S_ISREG(m);  }
123 INLINE int __hscore_s_isdir(m)  { return S_ISDIR(m);  }
124 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
125 INLINE int __hscore_s_isblk(m)  { return S_ISBLK(m);  }
126 INLINE int __hscore_s_ischr(m)  { return S_ISCHR(m);  }
127 #ifdef S_ISSOCK
128 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
129 #endif
130
131 INLINE void
132 __hscore_sigemptyset( sigset_t *set )
133 { sigemptyset(set); }
134
135 INLINE void *
136 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
137 { return memcpy(dst+dst_off, src, sz); }
138
139 INLINE void *
140 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
141 { return memcpy(dst, src+src_off, sz); }
142
143 INLINE HsBool
144 __hscore_supportsTextMode()
145 {
146 #if defined(mingw32_TARGET_OS)
147   return HS_BOOL_FALSE;
148 #else
149   return HS_BOOL_TRUE;
150 #endif
151 }
152
153 INLINE HsInt
154 __hscore_bufsiz()
155 {
156   return BUFSIZ;
157 }
158
159 INLINE HsInt
160 __hscore_seek_cur()
161 {
162   return SEEK_CUR;
163 }
164
165 INLINE HsInt
166 __hscore_o_binary()
167 {
168 #ifdef HAVE_O_BINARY
169   return O_BINARY;
170 #else
171   return 0;
172 #endif
173 }
174
175 INLINE HsInt
176 __hscore_seek_set()
177 {
178   return SEEK_SET;
179 }
180
181 INLINE HsInt
182 __hscore_seek_end()
183 {
184   return SEEK_END;
185 }
186
187 INLINE HsInt
188 __hscore_setmode( HsInt fd, HsBool toBin )
189 {
190 #ifdef _WIN32
191   return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
192 #else
193   return 0;
194 #endif  
195 }
196
197 INLINE HsInt
198 __hscore_PrelHandle_write( HsInt fd, HsBool isSock, HsAddr ptr, 
199                            HsInt off, int sz )
200 {
201 #ifdef _WIN32
202   if (isSock) {
203     return send(fd,ptr + off, sz, 0);
204   }
205 #endif
206   return write(fd,ptr + off, sz);
207 }
208
209 INLINE HsInt
210 __hscore_PrelHandle_read( HsInt fd, HsBool isSock, HsAddr ptr, 
211                           HsInt off, int sz )
212 {
213 #ifdef _WIN32
214   if (isSock) {
215     return recv(fd,ptr + off, sz, 0);
216   }
217 #endif
218   return read(fd,ptr + off, sz);
219
220 }
221
222 #endif
223