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