HsByteArray doesn't exist
[packages/old-time.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 "lockFile.h"
129 #include "dirUtils.h"
130 #include "WCsubst.h"
131
132 #include "runProcess.h"
133
134 #if defined(__MINGW32__)
135 /* in Win32Utils.c */
136 extern void maperrno (void);
137 extern HsWord64 getUSecOfDay(void);
138 #endif
139
140 #if defined(__MINGW32__)
141 #include <io.h>
142 #include <fcntl.h>
143 #include "timeUtils.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 int inputReady(int fd, int msecs, int isSock);
154
155 /* in Signals.c */
156 extern HsInt nocldstop;
157
158 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
159 /* in execvpe.c */
160 extern int execvpe(char *name, char *const argv[], char **envp);
161 extern void pPrPr_disableITimers (void);
162 #endif
163
164 /* -----------------------------------------------------------------------------
165 64-bit operations, defined in longlong.c
166 -------------------------------------------------------------------------- */
167
168 #ifdef SUPPORT_LONG_LONGS
169
170 HsBool hs_gtWord64 (HsWord64, HsWord64);
171 HsBool hs_geWord64 (HsWord64, HsWord64);
172 HsBool hs_eqWord64 (HsWord64, HsWord64);
173 HsBool hs_neWord64 (HsWord64, HsWord64);
174 HsBool hs_ltWord64 (HsWord64, HsWord64);
175 HsBool hs_leWord64 (HsWord64, HsWord64);
176
177 HsBool hs_gtInt64 (HsInt64, HsInt64);
178 HsBool hs_geInt64 (HsInt64, HsInt64);
179 HsBool hs_eqInt64 (HsInt64, HsInt64);
180 HsBool hs_neInt64 (HsInt64, HsInt64);
181 HsBool hs_ltInt64 (HsInt64, HsInt64);
182 HsBool hs_leInt64 (HsInt64, HsInt64);
183
184 HsWord64 hs_remWord64 (HsWord64, HsWord64);
185 HsWord64 hs_quotWord64 (HsWord64, HsWord64);
186
187 HsInt64 hs_remInt64 (HsInt64, HsInt64);
188 HsInt64 hs_quotInt64 (HsInt64, HsInt64);
189 HsInt64 hs_negateInt64 (HsInt64);
190 HsInt64 hs_plusInt64 (HsInt64, HsInt64);
191 HsInt64 hs_minusInt64 (HsInt64, HsInt64);
192 HsInt64 hs_timesInt64 (HsInt64, HsInt64);
193
194 HsWord64 hs_and64 (HsWord64, HsWord64);
195 HsWord64 hs_or64 (HsWord64, HsWord64);
196 HsWord64 hs_xor64 (HsWord64, HsWord64);
197 HsWord64 hs_not64 (HsWord64);
198
199 HsWord64 hs_uncheckedShiftL64 (HsWord64, HsInt);
200 HsWord64 hs_uncheckedShiftRL64 (HsWord64, HsInt);
201 HsInt64 hs_uncheckedIShiftL64 (HsInt64, HsInt);
202 HsInt64 hs_uncheckedIShiftRA64 (HsInt64, HsInt);
203 HsInt64 hs_uncheckedIShiftRL64 (HsInt64, HsInt);
204
205 HsInt64 hs_intToInt64 (HsInt);
206 HsInt hs_int64ToInt (HsInt64);
207 HsWord64 hs_int64ToWord64 (HsInt64);
208 HsWord64 hs_wordToWord64 (HsWord);
209 HsWord hs_word64ToWord (HsWord64);
210 HsInt64 hs_word64ToInt64 (HsWord64);
211
212 HsWord64 hs_integerToWord64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
213 HsInt64 hs_integerToInt64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
214
215 #endif /* SUPPORT_LONG_LONGS */
216
217 /* -----------------------------------------------------------------------------
218 INLINE functions.
219
220 These functions are given as inlines here for when compiling via C,
221 but we also generate static versions into the cbits library for
222 when compiling to native code.
223 -------------------------------------------------------------------------- */
224
225 #ifndef INLINE
226 # if defined(_MSC_VER)
227 # define INLINE extern __inline
228 # else
229 # define INLINE static inline
230 # endif
231 #endif
232
233 INLINE int __hscore_get_errno(void) { return errno; }
234 INLINE void __hscore_set_errno(int e) { errno = e; }
235
236 #if !defined(_MSC_VER)
237 INLINE int __hscore_s_isreg(mode_t m) { return S_ISREG(m); }
238 INLINE int __hscore_s_isdir(mode_t m) { return S_ISDIR(m); }
239 INLINE int __hscore_s_isfifo(mode_t m) { return S_ISFIFO(m); }
240 INLINE int __hscore_s_isblk(mode_t m) { return S_ISBLK(m); }
241 INLINE int __hscore_s_ischr(mode_t m) { return S_ISCHR(m); }
242 #ifdef S_ISSOCK
243 INLINE int __hscore_s_issock(mode_t m) { return S_ISSOCK(m); }
244 #endif
245 #endif
246
247 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
248 INLINE int
249 __hscore_sigemptyset( sigset_t *set )
250 { return sigemptyset(set); }
251
252 INLINE int
253 __hscore_sigfillset( sigset_t *set )
254 { return sigfillset(set); }
255
256 INLINE int
257 __hscore_sigaddset( sigset_t * set, int s )
258 { return sigaddset(set,s); }
259
260 INLINE int
261 __hscore_sigdelset( sigset_t * set, int s )
262 { return sigdelset(set,s); }
263
264 INLINE int
265 __hscore_sigismember( sigset_t * set, int s )
266 { return sigismember(set,s); }
267 #endif
268
269 INLINE void *
270 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
271 { return memcpy(dst+dst_off, src, sz); }
272
273 INLINE void *
274 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
275 { return memcpy(dst, src+src_off, sz); }
276
277 INLINE HsBool
278 __hscore_supportsTextMode()
279 {
280 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
281 return HS_BOOL_FALSE;
282 #else
283 return HS_BOOL_TRUE;
284 #endif
285 }
286
287 INLINE HsInt
288 __hscore_bufsiz()
289 {
290 return BUFSIZ;
291 }
292
293 INLINE int
294 __hscore_seek_cur()
295 {
296 return SEEK_CUR;
297 }
298
299 INLINE int
300 __hscore_o_binary()
301 {
302 #if defined(_MSC_VER)
303 return O_BINARY;
304 #else
305 return CONST_O_BINARY;
306 #endif
307 }
308
309 INLINE int
310 __hscore_o_rdonly()
311 {
312 #ifdef O_RDONLY
313 return O_RDONLY;
314 #else
315 return 0;
316 #endif
317 }
318
319 INLINE int
320 __hscore_o_wronly( void )
321 {
322 #ifdef O_WRONLY
323 return O_WRONLY;
324 #else
325 return 0;
326 #endif
327 }
328
329 INLINE int
330 __hscore_o_rdwr( void )
331 {
332 #ifdef O_RDWR
333 return O_RDWR;
334 #else
335 return 0;
336 #endif
337 }
338
339 INLINE int
340 __hscore_o_append( void )
341 {
342 #ifdef O_APPEND
343 return O_APPEND;
344 #else
345 return 0;
346 #endif
347 }
348
349 INLINE int
350 __hscore_o_creat( void )
351 {
352 #ifdef O_CREAT
353 return O_CREAT;
354 #else
355 return 0;
356 #endif
357 }
358
359 INLINE int
360 __hscore_o_excl( void )
361 {
362 #ifdef O_EXCL
363 return O_EXCL;
364 #else
365 return 0;
366 #endif
367 }
368
369 INLINE int
370 __hscore_o_trunc( void )
371 {
372 #ifdef O_TRUNC
373 return O_TRUNC;
374 #else
375 return 0;
376 #endif
377 }
378
379 INLINE int
380 __hscore_o_noctty( void )
381 {
382 #ifdef O_NOCTTY
383 return O_NOCTTY;
384 #else
385 return 0;
386 #endif
387 }
388
389 INLINE int
390 __hscore_o_nonblock( void )
391 {
392 #ifdef O_NONBLOCK
393 return O_NONBLOCK;
394 #else
395 return 0;
396 #endif
397 }
398
399 INLINE int
400 __hscore_seek_set( void )
401 {
402 return SEEK_SET;
403 }
404
405 INLINE int
406 __hscore_seek_end( void )
407 {
408 return SEEK_END;
409 }
410
411 INLINE int
412 __hscore_ftruncate( int fd, off_t where )
413 {
414 #if defined(HAVE_FTRUNCATE)
415 return ftruncate(fd,where);
416 #elif defined(HAVE__CHSIZE)
417 return _chsize(fd,where);
418 #else
419 #error at least ftruncate or _chsize functions are required to build
420 #endif
421 }
422
423 INLINE int
424 __hscore_setmode( int fd, HsBool toBin )
425 {
426 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
427 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
428 #else
429 return 0;
430 #endif
431 }
432
433 #if __GLASGOW_HASKELL__
434
435 INLINE int
436 __hscore_PrelHandle_write( int fd, void *ptr, HsInt off, int sz )
437 {
438 return write(fd,(char *)ptr + off, sz);
439 }
440
441 INLINE int
442 __hscore_PrelHandle_read( int fd, void *ptr, HsInt off, int sz )
443 {
444 return read(fd,(char *)ptr + off, sz);
445
446 }
447
448 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
449 INLINE int
450 __hscore_PrelHandle_send( int fd, void *ptr, HsInt off, int sz )
451 {
452 return send(fd,(char *)ptr + off, sz, 0);
453 }
454
455 INLINE int
456 __hscore_PrelHandle_recv( int fd, void *ptr, HsInt off, int sz )
457 {
458 return recv(fd,(char *)ptr + off, sz, 0);
459 }
460 #endif
461
462 #endif /* __GLASGOW_HASKELL__ */
463
464 INLINE int
465 __hscore_mkdir( char *pathName, int mode )
466 {
467 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
468 return mkdir(pathName);
469 #else
470 return mkdir(pathName,mode);
471 #endif
472 }
473
474 INLINE int
475 __hscore_lstat( const char *fname, struct stat *st )
476 {
477 #if HAVE_LSTAT
478 return lstat(fname, st);
479 #else
480 return stat(fname, st);
481 #endif
482 }
483
484 #ifdef PATH_MAX
485 /* A size that will contain many path names, but not necessarily all
486 * (PATH_MAX is not defined on systems with unlimited path length,
487 * e.g. the Hurd).
488 */
489 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
490 #else
491 INLINE HsInt __hscore_long_path_size() { return 4096; }
492 #endif
493
494 #ifdef R_OK
495 INLINE int __hscore_R_OK() { return R_OK; }
496 #endif
497 #ifdef W_OK
498 INLINE int __hscore_W_OK() { return W_OK; }
499 #endif
500 #ifdef X_OK
501 INLINE int __hscore_X_OK() { return X_OK; }
502 #endif
503
504 #ifdef S_IRUSR
505 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
506 #endif
507 #ifdef S_IWUSR
508 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
509 #endif
510 #ifdef S_IXUSR
511 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
512 #endif
513
514 INLINE char *
515 __hscore_d_name( struct dirent* d )
516 {
517 return (d->d_name);
518 }
519
520 INLINE int
521 __hscore_end_of_dir( void )
522 {
523 return READDIR_ERRNO_EOF;
524 }
525
526 INLINE void
527 __hscore_free_dirent(struct dirent *dEnt)
528 {
529 #if HAVE_READDIR_R
530 free(dEnt);
531 #endif
532 }
533
534 INLINE HsInt
535 __hscore_sizeof_stat( void )
536 {
537 return sizeof(struct stat);
538 }
539
540 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
541 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
542 #if !defined(_MSC_VER)
543 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
544 #endif
545
546 #if HAVE_TERMIOS_H
547 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
548
549 INLINE void
550 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
551
552 INLINE unsigned char*
553 __hscore_ptr_c_cc( struct termios* ts )
554 { return (unsigned char*) &ts->c_cc; }
555
556 INLINE HsInt
557 __hscore_sizeof_termios( void )
558 {
559 #ifndef __MINGW32__
560 return sizeof(struct termios);
561 #else
562 return 0;
563 #endif
564 }
565 #endif
566
567 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
568 INLINE HsInt
569 __hscore_sizeof_sigset_t( void )
570 {
571 return sizeof(sigset_t);
572 }
573 #endif
574
575 INLINE int
576 __hscore_echo( void )
577 {
578 #ifdef ECHO
579 return ECHO;
580 #else
581 return 0;
582 #endif
583
584 }
585
586 INLINE int
587 __hscore_tcsanow( void )
588 {
589 #ifdef TCSANOW
590 return TCSANOW;
591 #else
592 return 0;
593 #endif
594
595 }
596
597 INLINE int
598 __hscore_icanon( void )
599 {
600 #ifdef ICANON
601 return ICANON;
602 #else
603 return 0;
604 #endif
605 }
606
607 INLINE int __hscore_vmin( void )
608 {
609 #ifdef VMIN
610 return VMIN;
611 #else
612 return 0;
613 #endif
614 }
615
616 INLINE int __hscore_vtime( void )
617 {
618 #ifdef VTIME
619 return VTIME;
620 #else
621 return 0;
622 #endif
623 }
624
625 INLINE int __hscore_sigttou( void )
626 {
627 #ifdef SIGTTOU
628 return SIGTTOU;
629 #else
630 return 0;
631 #endif
632 }
633
634 INLINE int __hscore_sig_block( void )
635 {
636 #ifdef SIG_BLOCK
637 return SIG_BLOCK;
638 #else
639 return 0;
640 #endif
641 }
642
643 INLINE int __hscore_sig_setmask( void )
644 {
645 #ifdef SIG_SETMASK
646 return SIG_SETMASK;
647 #else
648 return 0;
649 #endif
650 }
651
652 INLINE int
653 __hscore_f_getfl( void )
654 {
655 #ifdef F_GETFL
656 return F_GETFL;
657 #else
658 return 0;
659 #endif
660 }
661
662 INLINE int
663 __hscore_f_setfl( void )
664 {
665 #ifdef F_SETFL
666 return F_SETFL;
667 #else
668 return 0;
669 #endif
670 }
671
672 // defined in rts/RtsStartup.c.
673 extern void* __hscore_get_saved_termios(int fd);
674 extern void __hscore_set_saved_termios(int fd, void* ts);
675
676 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
677
678 INLINE int __hscore_open(char *file, int how, mode_t mode) {
679 #ifdef __MINGW32__
680 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
681 return _sopen(file,how,_SH_DENYRW,mode);
682 else
683 return _sopen(file,how,_SH_DENYWR,mode);
684 #else
685 return open(file,how,mode);
686 #endif
687 }
688
689 // These are wrapped because on some OSs (eg. Linux) they are
690 // macros which redirect to the 64-bit-off_t versions when large file
691 // support is enabled.
692 //
693 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
694 return (lseek(fd,off,whence));
695 }
696
697 INLINE int __hscore_stat(char *file, struct stat *buf) {
698 return (stat(file,buf));
699 }
700
701 INLINE int __hscore_fstat(int fd, struct stat *buf) {
702 return (fstat(fd,buf));
703 }
704
705 // select-related stuff
706
707 #if !defined(__MINGW32__)
708 INLINE int hsFD_SETSIZE(void) { return FD_SETSIZE; }
709 INLINE void hsFD_CLR(int fd, fd_set *fds) { FD_CLR(fd, fds); }
710 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
711 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
712 INLINE HsInt sizeof_fd_set(void) { return sizeof(fd_set); }
713 extern void hsFD_ZERO(fd_set *fds);
714 #endif
715
716 // gettimeofday()-related
717
718 #if !defined(__MINGW32__)
719
720 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
721
722 INLINE HsWord64 getUSecOfDay(void)
723 {
724 struct timeval tv;
725 gettimeofday(&tv, (struct timezone *) NULL);
726 // Don't forget to cast *before* doing the arithmetic, otherwise
727 // the arithmetic happens at the type of tv_sec, which is probably
728 // only 'int'.
729 return ((HsWord64)tv.tv_sec * 1000000 + (HsWord64)tv.tv_usec);
730 }
731
732 INLINE void setTimevalTicks(struct timeval *p, HsWord64 usecs)
733 {
734 p->tv_sec = usecs / 1000000;
735 p->tv_usec = usecs % 1000000;
736 }
737 #endif /* !defined(__MINGW32__) */
738
739 // Directory-related
740
741 #if defined(__MINGW32__)
742
743 /* Make sure we've got the reqd CSIDL_ constants in scope;
744 * w32api header files are lagging a bit in defining the full set.
745 */
746 #if !defined(CSIDL_APPDATA)
747 #define CSIDL_APPDATA 0x001a
748 #endif
749 #if !defined(CSIDL_PERSONAL)
750 #define CSIDL_PERSONAL 0x0005
751 #endif
752 #if !defined(CSIDL_PROFILE)
753 #define CSIDL_PROFILE 0x0028
754 #endif
755 #if !defined(CSIDL_WINDOWS)
756 #define CSIDL_WINDOWS 0x0024
757 #endif
758
759 INLINE int __hscore_CSIDL_PROFILE() { return CSIDL_PROFILE; }
760 INLINE int __hscore_CSIDL_APPDATA() { return CSIDL_APPDATA; }
761 INLINE int __hscore_CSIDL_WINDOWS() { return CSIDL_WINDOWS; }
762 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
763 #endif
764
765 #if defined(__MINGW32__)
766 INLINE unsigned int __hscore_get_osver(void) { return _osver; }
767 #endif
768
769 /* ToDo: write a feature test that doesn't assume 'environ' to
770 * be in scope at link-time. */
771 extern char** environ;
772 INLINE char **__hscore_environ() { return environ; }
773
774 /* lossless conversions between pointers and integral types */
775 INLINE void * __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
776 INLINE void * __hscore_from_intptr (intptr_t n) { return (void *)n; }
777 INLINE uintptr_t __hscore_to_uintptr (void *p) { return (uintptr_t)p; }
778 INLINE intptr_t __hscore_to_intptr (void *p) { return (intptr_t)p; }
779
780 #endif /* __HSBASE_H__ */
781