Don't make S_ISSOCK use conditional
[ghc-base.git] / include / HsBase.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The University of Glasgow 2001-2004
4  *
5  * Definitions for package `base' which are visible in Haskell land.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #ifndef __HSBASE_H__
10 #define __HSBASE_H__
11
12 #include "HsBaseConfig.h"
13
14 /* ultra-evil... */
15 #undef PACKAGE_BUGREPORT
16 #undef PACKAGE_NAME
17 #undef PACKAGE_STRING
18 #undef PACKAGE_TARNAME
19 #undef PACKAGE_VERSION
20
21 /* Needed to get the macro version of errno on some OSs (eg. Solaris).
22    We must do this, because these libs are only compiled once, but
23    must work in both single-threaded and multi-threaded programs. */
24 #define _REENTRANT 1
25
26 #include "HsFFI.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <math.h>
31
32 #if HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35 #if HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #if HAVE_SYS_STAT_H
39 #include <sys/stat.h>
40 #endif
41 #if HAVE_FCNTL_H
42 # include <fcntl.h>
43 #endif
44 #if HAVE_TERMIOS_H
45 #include <termios.h>
46 #endif
47 #if HAVE_SIGNAL_H
48 #include <signal.h>
49 /* Ultra-ugly: OpenBSD uses broken macros for sigemptyset and sigfillset (missing casts) */
50 #if __OpenBSD__
51 #undef sigemptyset
52 #undef sigfillset
53 #endif
54 #endif
55 #if HAVE_ERRNO_H
56 #include <errno.h>
57 #endif
58 #if HAVE_STRING_H
59 #include <string.h>
60 #endif
61 #if HAVE_DIRENT_H
62 #include <dirent.h>
63 #endif
64 #if HAVE_UTIME_H
65 #include <utime.h>
66 #endif
67 #if HAVE_SYS_UTSNAME_H
68 #include <sys/utsname.h>
69 #endif
70 #if HAVE_GETTIMEOFDAY
71 #  if HAVE_SYS_TIME_H
72 #   include <sys/time.h>
73 #  endif
74 #elif HAVE_GETCLOCK
75 # if HAVE_SYS_TIMERS_H
76 #  define POSIX_4D9 1
77 #  include <sys/timers.h>
78 # endif
79 #endif
80 #if HAVE_TIME_H
81 #include <time.h>
82 #endif
83 #if HAVE_SYS_TIMEB_H
84 #include <sys/timeb.h>
85 #endif
86 #if HAVE_WINDOWS_H
87 #include <windows.h>
88 #endif
89 #if HAVE_SYS_TIMES_H
90 #include <sys/times.h>
91 #endif
92 #if HAVE_WINSOCK_H && defined(__MINGW32__)
93 #include <winsock.h>
94 #endif
95 #if HAVE_LIMITS_H
96 #include <limits.h>
97 #endif
98 #if HAVE_WCTYPE_H
99 #include <wctype.h>
100 #endif
101 #if HAVE_INTTYPES_H
102 # include <inttypes.h>
103 #elif HAVE_STDINT_H
104 # include <stdint.h>
105 #endif
106
107 #if !defined(__MINGW32__) && !defined(irix_HOST_OS)
108 # if HAVE_SYS_RESOURCE_H
109 #  include <sys/resource.h>
110 # endif
111 #endif
112
113 #if !HAVE_GETRUSAGE && HAVE_SYS_SYSCALL_H
114 # include <sys/syscall.h>
115 # if defined(SYS_GETRUSAGE)     /* hpux_HOST_OS */
116 #  define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
117 #  define HAVE_GETRUSAGE 1
118 # endif
119 #endif
120
121 /* For System */
122 #if HAVE_SYS_WAIT_H
123 #include <sys/wait.h>
124 #endif
125 #if HAVE_VFORK_H
126 #include <vfork.h>
127 #endif
128 #include "dirUtils.h"
129 #include "WCsubst.h"
130
131 #if defined(__MINGW32__)
132 /* in Win32Utils.c */
133 extern void maperrno (void);
134 extern HsWord64 getUSecOfDay(void);
135 #endif
136
137 #if defined(__MINGW32__)
138 #include <io.h>
139 #include <fcntl.h>
140 #include <shlobj.h>
141 #include <share.h>
142 #endif
143
144 #if HAVE_SYS_SELECT_H
145 #include <sys/select.h>
146 #endif
147
148 /* in inputReady.c */
149 extern int fdReady(int fd, int write, int msecs, int isSock);
150
151 /* in Signals.c */
152 extern HsInt nocldstop;
153
154 /* -----------------------------------------------------------------------------
155    64-bit operations, defined in longlong.c
156    -------------------------------------------------------------------------- */
157
158 #ifdef SUPPORT_LONG_LONGS
159
160 HsBool hs_gtWord64 (HsWord64, HsWord64);
161 HsBool hs_geWord64 (HsWord64, HsWord64);
162 HsBool hs_eqWord64 (HsWord64, HsWord64);
163 HsBool hs_neWord64 (HsWord64, HsWord64);
164 HsBool hs_ltWord64 (HsWord64, HsWord64);
165 HsBool hs_leWord64 (HsWord64, HsWord64);
166
167 HsBool hs_gtInt64 (HsInt64, HsInt64);
168 HsBool hs_geInt64 (HsInt64, HsInt64);
169 HsBool hs_eqInt64 (HsInt64, HsInt64);
170 HsBool hs_neInt64 (HsInt64, HsInt64);
171 HsBool hs_ltInt64 (HsInt64, HsInt64);
172 HsBool hs_leInt64 (HsInt64, HsInt64);
173
174 HsWord64 hs_remWord64  (HsWord64, HsWord64);
175 HsWord64 hs_quotWord64 (HsWord64, HsWord64);
176
177 HsInt64 hs_remInt64    (HsInt64, HsInt64);
178 HsInt64 hs_quotInt64   (HsInt64, HsInt64);
179 HsInt64 hs_negateInt64 (HsInt64);
180 HsInt64 hs_plusInt64   (HsInt64, HsInt64);
181 HsInt64 hs_minusInt64  (HsInt64, HsInt64);
182 HsInt64 hs_timesInt64  (HsInt64, HsInt64);
183
184 HsWord64 hs_and64  (HsWord64, HsWord64);
185 HsWord64 hs_or64   (HsWord64, HsWord64);
186 HsWord64 hs_xor64  (HsWord64, HsWord64);
187 HsWord64 hs_not64  (HsWord64);
188
189 HsWord64 hs_uncheckedShiftL64   (HsWord64, HsInt);
190 HsWord64 hs_uncheckedShiftRL64  (HsWord64, HsInt);
191 HsInt64  hs_uncheckedIShiftL64  (HsInt64, HsInt);
192 HsInt64  hs_uncheckedIShiftRA64 (HsInt64, HsInt);
193 HsInt64  hs_uncheckedIShiftRL64 (HsInt64, HsInt);
194
195 HsInt64  hs_intToInt64    (HsInt);
196 HsInt    hs_int64ToInt    (HsInt64);
197 HsWord64 hs_int64ToWord64 (HsInt64);
198 HsWord64 hs_wordToWord64  (HsWord);
199 HsWord   hs_word64ToWord  (HsWord64);
200 HsInt64  hs_word64ToInt64 (HsWord64);
201
202 HsWord64 hs_integerToWord64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
203 HsInt64  hs_integerToInt64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
204
205 #endif /* SUPPORT_LONG_LONGS */
206
207 /* -----------------------------------------------------------------------------
208    INLINE functions.
209
210    These functions are given as inlines here for when compiling via C,
211    but we also generate static versions into the cbits library for
212    when compiling to native code.
213    -------------------------------------------------------------------------- */
214
215 #ifndef INLINE
216 # if defined(_MSC_VER)
217 #  define INLINE extern __inline
218 # else
219 #  define INLINE static inline
220 # endif
221 #endif
222
223 INLINE int __hscore_get_errno(void) { return errno; }
224 INLINE void __hscore_set_errno(int e) { errno = e; }
225
226 #if !defined(_MSC_VER)
227 INLINE int __hscore_s_isreg(mode_t m)  { return S_ISREG(m);  }
228 INLINE int __hscore_s_isdir(mode_t m)  { return S_ISDIR(m);  }
229 INLINE int __hscore_s_isfifo(mode_t m) { return S_ISFIFO(m); }
230 INLINE int __hscore_s_isblk(mode_t m)  { return S_ISBLK(m);  }
231 INLINE int __hscore_s_ischr(mode_t m)  { return S_ISCHR(m);  }
232 INLINE int __hscore_s_issock(mode_t m) { return S_ISSOCK(m); }
233 #endif
234
235 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
236 INLINE int
237 __hscore_sigemptyset( sigset_t *set )
238 { return sigemptyset(set); }
239
240 INLINE int
241 __hscore_sigfillset( sigset_t *set )
242 { return sigfillset(set); }
243
244 INLINE int
245 __hscore_sigaddset( sigset_t * set, int s )
246 { return sigaddset(set,s); }
247
248 INLINE int
249 __hscore_sigdelset( sigset_t * set, int s )
250 { return sigdelset(set,s); }
251
252 INLINE int
253 __hscore_sigismember( sigset_t * set, int s )
254 { return sigismember(set,s); }
255 #endif
256
257 INLINE void *
258 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
259 { return memcpy(dst+dst_off, src, sz); }
260
261 INLINE void *
262 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
263 { return memcpy(dst, src+src_off, sz); }
264
265 INLINE HsBool
266 __hscore_supportsTextMode()
267 {
268 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
269   return HS_BOOL_FALSE;
270 #else
271   return HS_BOOL_TRUE;
272 #endif
273 }
274
275 INLINE HsInt
276 __hscore_bufsiz()
277 {
278   return BUFSIZ;
279 }
280
281 INLINE int
282 __hscore_seek_cur()
283 {
284   return SEEK_CUR;
285 }
286
287 INLINE int
288 __hscore_o_binary()
289 {
290 #if defined(_MSC_VER)
291   return O_BINARY;
292 #else
293   return CONST_O_BINARY;
294 #endif
295 }
296
297 INLINE int
298 __hscore_o_rdonly()
299 {
300 #ifdef O_RDONLY
301   return O_RDONLY;
302 #else
303   return 0;
304 #endif
305 }
306
307 INLINE int
308 __hscore_o_wronly( void )
309 {
310 #ifdef O_WRONLY
311   return O_WRONLY;
312 #else
313   return 0;
314 #endif
315 }
316
317 INLINE int
318 __hscore_o_rdwr( void )
319 {
320 #ifdef O_RDWR
321   return O_RDWR;
322 #else
323   return 0;
324 #endif
325 }
326
327 INLINE int
328 __hscore_o_append( void )
329 {
330 #ifdef O_APPEND
331   return O_APPEND;
332 #else
333   return 0;
334 #endif
335 }
336
337 INLINE int
338 __hscore_o_creat( void )
339 {
340 #ifdef O_CREAT
341   return O_CREAT;
342 #else
343   return 0;
344 #endif
345 }
346
347 INLINE int
348 __hscore_o_excl( void )
349 {
350 #ifdef O_EXCL
351   return O_EXCL;
352 #else
353   return 0;
354 #endif
355 }
356
357 INLINE int
358 __hscore_o_trunc( void )
359 {
360 #ifdef O_TRUNC
361   return O_TRUNC;
362 #else
363   return 0;
364 #endif
365 }
366
367 INLINE int
368 __hscore_o_noctty( void )
369 {
370 #ifdef O_NOCTTY
371   return O_NOCTTY;
372 #else
373   return 0;
374 #endif
375 }
376
377 INLINE int
378 __hscore_o_nonblock( void )
379 {
380 #ifdef O_NONBLOCK
381   return O_NONBLOCK;
382 #else
383   return 0;
384 #endif
385 }
386
387 INLINE int
388 __hscore_seek_set( void )
389 {
390   return SEEK_SET;
391 }
392
393 INLINE int
394 __hscore_seek_end( void )
395 {
396   return SEEK_END;
397 }
398
399 INLINE int
400 __hscore_ftruncate( int fd, off_t where )
401 {
402 #if defined(HAVE_FTRUNCATE)
403   return ftruncate(fd,where);
404 #elif defined(HAVE__CHSIZE)
405   return _chsize(fd,where);
406 #else
407 // ToDo: we should use _chsize_s() on Windows which allows a 64-bit
408 // offset, but it doesn't seem to be available from mingw at this time 
409 // --SDM (01/2008)
410 #error at least ftruncate or _chsize functions are required to build
411 #endif
412 }
413
414 INLINE int
415 __hscore_setmode( int fd, HsBool toBin )
416 {
417 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
418   return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
419 #else
420   return 0;
421 #endif
422 }
423
424 #if __GLASGOW_HASKELL__
425
426 INLINE int
427 __hscore_PrelHandle_write( int fd, void *ptr, HsInt off, int sz )
428 {
429   return write(fd,(char *)ptr + off, sz);
430 }
431
432 INLINE int
433 __hscore_PrelHandle_read( int fd, void *ptr, HsInt off, int sz )
434 {
435   return read(fd,(char *)ptr + off, sz);
436
437 }
438
439 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
440 INLINE int
441 __hscore_PrelHandle_send( int fd, void *ptr, HsInt off, int sz )
442 {
443     return send(fd,(char *)ptr + off, sz, 0);
444 }
445
446 INLINE int
447 __hscore_PrelHandle_recv( int fd, void *ptr, HsInt off, int sz )
448 {
449     return recv(fd,(char *)ptr + off, sz, 0);
450 }
451 #endif
452
453 #endif /* __GLASGOW_HASKELL__ */
454
455 INLINE int
456 __hscore_mkdir( char *pathName, int mode )
457 {
458 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
459   return mkdir(pathName);
460 #else
461   return mkdir(pathName,mode);
462 #endif
463 }
464
465 INLINE int
466 __hscore_lstat( const char *fname, struct stat *st )
467 {
468 #if HAVE_LSTAT
469   return lstat(fname, st);
470 #else
471   return stat(fname, st);
472 #endif
473 }
474
475 INLINE char *
476 __hscore_d_name( struct dirent* d )
477 {
478   return (d->d_name);
479 }
480
481 INLINE int
482 __hscore_end_of_dir( void )
483 {
484   return READDIR_ERRNO_EOF;
485 }
486
487 INLINE void
488 __hscore_free_dirent(struct dirent *dEnt)
489 {
490 #if HAVE_READDIR_R
491   free(dEnt);
492 #endif
493 }
494
495 #if defined(__MINGW32__)
496 // We want the versions of stat/fstat/lseek that use 64-bit offsets,
497 // and you have to ask for those explicitly.  Unfortunately there
498 // doesn't seem to be a 64-bit version of truncate/ftruncate, so while
499 // hFileSize and hSeek will work with large files, hSetFileSize will not.
500 #define stat(file,buf)       _stati64(file,buf)
501 #define fstat(fd,buf)        _fstati64(fd,buf)
502 typedef struct _stati64 struct_stat;
503 typedef off64_t stsize_t;
504 #else
505 typedef struct stat struct_stat;
506 typedef off_t stsize_t;
507 #endif
508
509 INLINE HsInt
510 __hscore_sizeof_stat( void )
511 {
512   return sizeof(struct_stat);
513 }
514
515 INLINE time_t __hscore_st_mtime ( struct_stat* st ) { return st->st_mtime; }
516 INLINE stsize_t __hscore_st_size  ( struct_stat* st ) { return st->st_size; }
517 #if !defined(_MSC_VER)
518 INLINE mode_t __hscore_st_mode  ( struct_stat* st ) { return st->st_mode; }
519 INLINE dev_t  __hscore_st_dev  ( struct_stat* st ) { return st->st_dev; }
520 INLINE ino_t  __hscore_st_ino  ( struct_stat* st ) { return st->st_ino; }
521 #endif
522
523 #if HAVE_TERMIOS_H
524 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
525
526 INLINE void
527 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
528
529 INLINE unsigned char*
530 __hscore_ptr_c_cc( struct termios* ts )
531 { return (unsigned char*) &ts->c_cc; }
532
533 INLINE HsInt
534 __hscore_sizeof_termios( void )
535 {
536 #ifndef __MINGW32__
537   return sizeof(struct termios);
538 #else
539   return 0;
540 #endif
541 }
542 #endif
543
544 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
545 INLINE HsInt
546 __hscore_sizeof_sigset_t( void )
547 {
548   return sizeof(sigset_t);
549 }
550 #endif
551
552 INLINE int
553 __hscore_echo( void )
554 {
555 #ifdef ECHO
556   return ECHO;
557 #else
558   return 0;
559 #endif
560
561 }
562
563 INLINE int
564 __hscore_tcsanow( void )
565 {
566 #ifdef TCSANOW
567   return TCSANOW;
568 #else
569   return 0;
570 #endif
571
572 }
573
574 INLINE int
575 __hscore_icanon( void )
576 {
577 #ifdef ICANON
578   return ICANON;
579 #else
580   return 0;
581 #endif
582 }
583
584 INLINE int __hscore_vmin( void )
585 {
586 #ifdef VMIN
587   return VMIN;
588 #else
589   return 0;
590 #endif
591 }
592
593 INLINE int __hscore_vtime( void )
594 {
595 #ifdef VTIME
596   return VTIME;
597 #else
598   return 0;
599 #endif
600 }
601
602 INLINE int __hscore_sigttou( void )
603 {
604 #ifdef SIGTTOU
605   return SIGTTOU;
606 #else
607   return 0;
608 #endif
609 }
610
611 INLINE int __hscore_sig_block( void )
612 {
613 #ifdef SIG_BLOCK
614   return SIG_BLOCK;
615 #else
616   return 0;
617 #endif
618 }
619
620 INLINE int __hscore_sig_setmask( void )
621 {
622 #ifdef SIG_SETMASK
623   return SIG_SETMASK;
624 #else
625   return 0;
626 #endif
627 }
628
629 INLINE int
630 __hscore_f_getfl( void )
631 {
632 #ifdef F_GETFL
633   return F_GETFL;
634 #else
635   return 0;
636 #endif
637 }
638
639 INLINE int
640 __hscore_f_setfl( void )
641 {
642 #ifdef F_SETFL
643   return F_SETFL;
644 #else
645   return 0;
646 #endif
647 }
648
649 // defined in rts/RtsStartup.c.
650 extern void* __hscore_get_saved_termios(int fd);
651 extern void __hscore_set_saved_termios(int fd, void* ts);
652
653 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
654
655 INLINE int __hscore_open(char *file, int how, mode_t mode) {
656 #ifdef __MINGW32__
657         if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
658           return _sopen(file,how,_SH_DENYRW,mode);
659         else
660           return _sopen(file,how,_SH_DENYWR,mode);
661 #else
662         return open(file,how,mode);
663 #endif
664 }
665
666 // These are wrapped because on some OSs (eg. Linux) they are
667 // macros which redirect to the 64-bit-off_t versions when large file
668 // support is enabled.
669 //
670 #if defined(__MINGW32__)
671 INLINE off64_t __hscore_lseek(int fd, off64_t off, int whence) {
672         return (_lseeki64(fd,off,whence));
673 }
674 #else
675 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
676         return (lseek(fd,off,whence));
677 }
678 #endif
679
680 INLINE int __hscore_stat(char *file, struct_stat *buf) {
681         return (stat(file,buf));
682 }
683
684 INLINE int __hscore_fstat(int fd, struct_stat *buf) {
685         return (fstat(fd,buf));
686 }
687
688 // select-related stuff
689
690 #if !defined(__MINGW32__)
691 INLINE int  hsFD_SETSIZE(void) { return FD_SETSIZE; }
692 INLINE int  hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
693 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
694 INLINE HsInt sizeof_fd_set(void) { return sizeof(fd_set); }
695 extern void hsFD_ZERO(fd_set *fds);
696 #endif
697
698 // gettimeofday()-related
699
700 #if !defined(__MINGW32__)
701
702 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
703
704 INLINE HsWord64 getUSecOfDay(void)
705 {
706     struct timeval tv;
707     gettimeofday(&tv, (struct timezone *) NULL);
708     // Don't forget to cast *before* doing the arithmetic, otherwise
709     // the arithmetic happens at the type of tv_sec, which is probably
710     // only 'int'.
711     return ((HsWord64)tv.tv_sec * 1000000 + (HsWord64)tv.tv_usec);
712 }
713
714 INLINE void setTimevalTicks(struct timeval *p, HsWord64 usecs)
715 {
716     p->tv_sec  = usecs / 1000000;
717     p->tv_usec = usecs % 1000000;
718 }
719 #endif /* !defined(__MINGW32__) */
720
721 /* ToDo: write a feature test that doesn't assume 'environ' to
722  *    be in scope at link-time. */
723 extern char** environ;
724 INLINE char **__hscore_environ() { return environ; }
725
726 /* lossless conversions between pointers and integral types */
727 INLINE void *    __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
728 INLINE void *    __hscore_from_intptr (intptr_t n)  { return (void *)n; }
729 INLINE uintptr_t __hscore_to_uintptr  (void *p)     { return (uintptr_t)p; }
730 INLINE intptr_t  __hscore_to_intptr   (void *p)     { return (intptr_t)p; }
731
732 void errorBelch2(const char*s, char *t);
733 void debugBelch2(const char*s, char *t);
734
735 #endif /* __HSBASE_H__ */
736