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