[project @ 2002-02-05 17:32:24 by simonmar]
[haskell-directory.git] / include / HsCore.h
1 /* -----------------------------------------------------------------------------
2  * $Id: HsCore.h,v 1.7 2002/02/05 17:32:27 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 int
179 __hscore_o_rdonly()
180 {
181 #ifdef O_RDONLY
182   return O_RDONLY;
183 #else
184   return 0;
185 #endif
186 }
187
188 INLINE int
189 __hscore_o_wronly( void )
190 {
191 #ifdef O_WRONLY
192   return O_WRONLY;
193 #else
194   return 0;
195 #endif
196 }
197
198 INLINE int
199 __hscore_o_rdwr( void )
200 {
201 #ifdef O_RDWR
202   return O_RDWR;
203 #else
204   return 0;
205 #endif
206 }
207
208 INLINE int
209 __hscore_o_append( void )
210 {
211 #ifdef O_APPEND
212   return O_APPEND;
213 #else
214   return 0;
215 #endif
216 }
217
218 INLINE int
219 __hscore_o_creat( void )
220 {
221 #ifdef O_CREAT
222   return O_CREAT;
223 #else
224   return 0;
225 #endif
226 }
227
228 INLINE int
229 __hscore_o_excl( void )
230 {
231 #ifdef O_EXCL
232   return O_EXCL;
233 #else
234   return 0;
235 #endif
236 }
237
238 INLINE int
239 __hscore_o_trunc( void )
240 {
241 #ifdef O_TRUNC
242   return O_TRUNC;
243 #else
244   return 0;
245 #endif
246 }
247
248 INLINE int
249 __hscore_o_noctty( void )
250 {
251 #ifdef O_NOCTTY
252   return O_NOCTTY;
253 #else
254   return 0;
255 #endif
256 }
257
258 INLINE int
259 __hscore_o_nonblock( void )
260 {
261 #ifdef O_NONBLOCK
262   return O_NONBLOCK;
263 #else
264   return 0;
265 #endif
266 }
267
268 INLINE HsInt
269 __hscore_seek_set( void )
270 {
271   return SEEK_SET;
272 }
273
274 INLINE HsInt
275 __hscore_seek_end( void )
276 {
277   return SEEK_END;
278 }
279
280 INLINE HsInt
281 __hscore_setmode( HsInt fd, HsBool toBin )
282 {
283 #ifdef _WIN32
284   return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
285 #else
286   return 0;
287 #endif  
288 }
289
290 INLINE HsInt
291 __hscore_PrelHandle_write( HsInt fd, HsBool isSock, HsAddr ptr, 
292                            HsInt off, int sz )
293 {
294 #ifdef _WIN32
295   if (isSock) {
296     return send(fd,ptr + off, sz, 0);
297   }
298 #endif
299   return write(fd,ptr + off, sz);
300 }
301
302 INLINE HsInt
303 __hscore_PrelHandle_read( HsInt fd, HsBool isSock, HsAddr ptr, 
304                           HsInt off, int sz )
305 {
306 #ifdef _WIN32
307   if (isSock) {
308     return recv(fd,ptr + off, sz, 0);
309   }
310 #endif
311   return read(fd,ptr + off, sz);
312
313 }
314
315 #ifdef mingw32_TARGET_OS
316 INLINE long *
317 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
318
319 INLINE char **
320 __hscore_Time_ghcTzname( void ) { return _tzname; }
321 #endif
322
323 INLINE HsInt
324 __hscore_mkdir( HsAddr pathName, HsInt mode )
325 {
326 #if defined(mingw32_TARGET_OS)
327   return mkdir(pathName);
328 #else
329   return mkdir(pathName,mode);
330 #endif
331 }
332
333 INLINE HsInt
334 __hscore_lstat( HsAddr fname, HsAddr st )
335 {
336 #ifdef HAVE_LSTAT
337   return lstat((const char*)fname, (struct stat*)st);
338 #else
339   return stat((const char*)fname, (struct stat*)st);
340 #endif
341 }
342
343 INLINE HsInt __hscore_path_max() { return PATH_MAX; }
344
345 INLINE mode_t __hscore_R_OK() { return R_OK; }
346 INLINE mode_t __hscore_W_OK() { return W_OK; }
347 INLINE mode_t __hscore_X_OK() { return X_OK; }
348
349 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
350 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
351 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
352
353 INLINE HsAddr
354 __hscore_d_name( struct dirent* d )
355
356 #ifndef mingw32_TARGET_OS
357   return (HsAddr)(&d->d_name);
358 #else
359   return (HsAddr)(d->d_name);
360 #endif
361 }
362
363 INLINE HsInt
364 __hscore_end_of_dir( void )
365 {
366 #ifndef mingw32_TARGET_OS
367   return 0;
368 #else
369   return ENOENT;
370 #endif  
371 }
372
373 INLINE void
374 __hscore_free_dirent(HsAddr dEnt)
375 {
376 #if HAVE_READDIR_R
377   free(dEnt);
378 #endif
379 }
380
381 INLINE HsInt
382 __hscore_sizeof_stat( void )
383 {
384   return sizeof(struct stat);
385 }
386
387 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
388 INLINE off_t  __hscore_st_size  ( struct stat* st ) { return st->st_size; }
389 INLINE mode_t __hscore_st_mode  ( struct stat* st ) { return st->st_mode; }
390
391 #if HAVE_TERMIOS_H
392 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
393
394 INLINE void
395 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
396
397 INLINE unsigned char*
398 __hscore_ptr_c_cc( struct termios* ts ) 
399 { return (unsigned char*) &ts->c_cc; }
400 #endif
401
402 INLINE HsInt
403 __hscore_sizeof_termios( void )
404 {
405 #ifndef mingw32_TARGET_OS
406   return sizeof(struct termios);
407 #else
408   return 0;
409 #endif
410 }
411
412 INLINE HsInt
413 __hscore_sizeof_sigset_t( void )
414 {
415 #ifndef mingw32_TARGET_OS
416   return sizeof(sigset_t);
417 #else
418   return 0;
419 #endif
420 }
421
422 INLINE int
423 __hscore_echo( void )
424 {
425 #ifdef ECHO
426   return ECHO;
427 #else
428   return 0;
429 #endif
430
431 }
432
433 INLINE int
434 __hscore_tcsanow( void )
435 {
436 #ifdef TCSANOW
437   return TCSANOW;
438 #else
439   return 0;
440 #endif
441
442 }
443
444 INLINE int
445 __hscore_icanon( void )
446 {
447 #ifdef ICANON
448   return ICANON;
449 #else
450   return 0;
451 #endif
452 }
453
454 INLINE int __hscore_vmin( void )
455 {
456 #ifdef VMIN
457   return VMIN;
458 #else
459   return 0;
460 #endif
461 }
462
463 INLINE int __hscore_vtime( void )
464 {
465 #ifdef VTIME
466   return VTIME;
467 #else
468   return 0;
469 #endif
470 }
471
472 INLINE int __hscore_sigttou( void )
473 {
474 #ifdef SIGTTOU
475   return SIGTTOU;
476 #else
477   return 0;
478 #endif
479 }
480
481 INLINE int __hscore_sig_block( void )
482 {
483 #ifdef SIG_BLOCK
484   return SIG_BLOCK;
485 #else
486   return 0;
487 #endif
488 }
489
490 INLINE int __hscore_sig_setmask( void )
491 {
492 #ifdef SIG_SETMASK
493   return SIG_SETMASK;
494 #else
495   return 0;
496 #endif
497 }
498
499 INLINE int
500 __hscore_f_getfl( void )
501 {
502 #ifdef F_GETFL
503   return F_GETFL;
504 #else
505   return 0;
506 #endif
507 }
508
509 INLINE int
510 __hscore_f_setfl( void )
511 {
512 #ifdef F_SETFL
513   return F_SETFL;
514 #else
515   return 0;
516 #endif
517 }
518
519 #endif
520