1 /* -----------------------------------------------------------------------------
3 * (c) The University of Glasgow 2001-2004
5 * Definitions for package `base' which are visible in Haskell land.
7 * ---------------------------------------------------------------------------*/
13 # include "Nhc98BaseConfig.h"
15 #include "HsBaseConfig.h"
19 #undef PACKAGE_BUGREPORT
22 #undef PACKAGE_TARNAME
23 #undef PACKAGE_VERSION
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. */
37 #include <sys/types.h>
53 /* Ultra-ugly: OpenBSD uses broken macros for sigemptyset and sigfillset (missing casts) */
71 #if HAVE_SYS_UTSNAME_H
72 #include <sys/utsname.h>
76 # include <sys/time.h>
79 # if HAVE_SYS_TIMERS_H
81 # include <sys/timers.h>
88 #include <sys/timeb.h>
94 #include <sys/times.h>
96 #if HAVE_WINSOCK_H && defined(__MINGW32__)
106 # include <inttypes.h>
111 #if !defined(__MINGW32__) && !defined(irix_HOST_OS)
112 # if HAVE_SYS_RESOURCE_H
113 # include <sys/resource.h>
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
127 #include <sys/wait.h>
132 #include "dirUtils.h"
135 #if defined(__MINGW32__)
136 /* in Win32Utils.c */
137 extern void maperrno (void);
138 extern HsWord64 getUSecOfDay(void);
141 #if defined(__MINGW32__)
148 #if HAVE_SYS_SELECT_H
149 #include <sys/select.h>
152 /* in inputReady.c */
153 extern int fdReady(int fd, int write, int msecs, int isSock);
156 extern HsInt nocldstop;
158 /* -----------------------------------------------------------------------------
159 64-bit operations, defined in longlong.c
160 -------------------------------------------------------------------------- */
162 #ifdef SUPPORT_LONG_LONGS
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);
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);
178 HsWord64 hs_remWord64 (HsWord64, HsWord64);
179 HsWord64 hs_quotWord64 (HsWord64, HsWord64);
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);
188 HsWord64 hs_and64 (HsWord64, HsWord64);
189 HsWord64 hs_or64 (HsWord64, HsWord64);
190 HsWord64 hs_xor64 (HsWord64, HsWord64);
191 HsWord64 hs_not64 (HsWord64);
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);
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);
206 HsWord64 hs_integerToWord64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
207 HsInt64 hs_integerToInt64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
209 #endif /* SUPPORT_LONG_LONGS */
211 /* -----------------------------------------------------------------------------
214 These functions are given as inlines here for when compiling via C,
215 but we also generate static versions into the cbits library for
216 when compiling to native code.
217 -------------------------------------------------------------------------- */
220 # if defined(_MSC_VER)
221 # define INLINE extern __inline
223 # define INLINE static inline
227 INLINE int __hscore_get_errno(void) { return errno; }
228 INLINE void __hscore_set_errno(int e) { errno = e; }
230 #if !defined(_MSC_VER)
231 INLINE int __hscore_s_isreg(mode_t m) { return S_ISREG(m); }
232 INLINE int __hscore_s_isdir(mode_t m) { return S_ISDIR(m); }
233 INLINE int __hscore_s_isfifo(mode_t m) { return S_ISFIFO(m); }
234 INLINE int __hscore_s_isblk(mode_t m) { return S_ISBLK(m); }
235 INLINE int __hscore_s_ischr(mode_t m) { return S_ISCHR(m); }
236 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
237 INLINE int __hscore_s_issock(mode_t m) { return S_ISSOCK(m); }
241 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
243 __hscore_sigemptyset( sigset_t *set )
244 { return sigemptyset(set); }
247 __hscore_sigfillset( sigset_t *set )
248 { return sigfillset(set); }
251 __hscore_sigaddset( sigset_t * set, int s )
252 { return sigaddset(set,s); }
255 __hscore_sigdelset( sigset_t * set, int s )
256 { return sigdelset(set,s); }
259 __hscore_sigismember( sigset_t * set, int s )
260 { return sigismember(set,s); }
264 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
265 { return memcpy(dst+dst_off, src, sz); }
268 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
269 { return memcpy(dst, src+src_off, sz); }
286 #if defined(_MSC_VER)
289 return CONST_O_BINARY;
304 __hscore_o_wronly( void )
314 __hscore_o_rdwr( void )
324 __hscore_o_append( void )
334 __hscore_o_creat( void )
344 __hscore_o_excl( void )
354 __hscore_o_trunc( void )
364 __hscore_o_noctty( void )
374 __hscore_o_nonblock( void )
384 __hscore_seek_set( void )
390 __hscore_seek_end( void )
396 __hscore_ftruncate( int fd, off_t where )
398 #if defined(HAVE_FTRUNCATE)
399 return ftruncate(fd,where);
400 #elif defined(HAVE__CHSIZE)
401 return _chsize(fd,where);
403 // ToDo: we should use _chsize_s() on Windows which allows a 64-bit
404 // offset, but it doesn't seem to be available from mingw at this time
406 #error at least ftruncate or _chsize functions are required to build
411 __hscore_setmode( int fd, HsBool toBin )
413 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
414 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
420 #if __GLASGOW_HASKELL__
423 __hscore_PrelHandle_write( int fd, void *ptr, HsInt off, int sz )
425 return write(fd,(char *)ptr + off, sz);
429 __hscore_PrelHandle_read( int fd, void *ptr, HsInt off, int sz )
431 return read(fd,(char *)ptr + off, sz);
435 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
437 __hscore_PrelHandle_send( int fd, void *ptr, HsInt off, int sz )
439 return send(fd,(char *)ptr + off, sz, 0);
443 __hscore_PrelHandle_recv( int fd, void *ptr, HsInt off, int sz )
445 return recv(fd,(char *)ptr + off, sz, 0);
449 #endif /* __GLASGOW_HASKELL__ */
452 __hscore_mkdir( char *pathName, int mode )
454 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
455 return mkdir(pathName);
457 return mkdir(pathName,mode);
462 __hscore_lstat( const char *fname, struct stat *st )
465 return lstat(fname, st);
467 return stat(fname, st);
472 __hscore_d_name( struct dirent* d )
478 __hscore_end_of_dir( void )
480 return READDIR_ERRNO_EOF;
484 __hscore_free_dirent(struct dirent *dEnt)
491 #if defined(__MINGW32__)
492 // We want the versions of stat/fstat/lseek that use 64-bit offsets,
493 // and you have to ask for those explicitly. Unfortunately there
494 // doesn't seem to be a 64-bit version of truncate/ftruncate, so while
495 // hFileSize and hSeek will work with large files, hSetFileSize will not.
496 #define stat(file,buf) _stati64(file,buf)
497 #define fstat(fd,buf) _fstati64(fd,buf)
498 typedef struct _stati64 struct_stat;
499 typedef off64_t stsize_t;
501 typedef struct stat struct_stat;
502 typedef off_t stsize_t;
506 __hscore_sizeof_stat( void )
508 return sizeof(struct_stat);
511 INLINE time_t __hscore_st_mtime ( struct_stat* st ) { return st->st_mtime; }
512 INLINE stsize_t __hscore_st_size ( struct_stat* st ) { return st->st_size; }
513 #if !defined(_MSC_VER)
514 INLINE mode_t __hscore_st_mode ( struct_stat* st ) { return st->st_mode; }
515 INLINE dev_t __hscore_st_dev ( struct_stat* st ) { return st->st_dev; }
516 INLINE ino_t __hscore_st_ino ( struct_stat* st ) { return st->st_ino; }
520 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
523 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
525 INLINE unsigned char*
526 __hscore_ptr_c_cc( struct termios* ts )
527 { return (unsigned char*) &ts->c_cc; }
530 __hscore_sizeof_termios( void )
533 return sizeof(struct termios);
540 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
542 __hscore_sizeof_sigset_t( void )
544 return sizeof(sigset_t);
549 __hscore_echo( void )
560 __hscore_tcsanow( void )
571 __hscore_icanon( void )
580 INLINE int __hscore_vmin( void )
589 INLINE int __hscore_vtime( void )
598 INLINE int __hscore_sigttou( void )
607 INLINE int __hscore_sig_block( void )
616 INLINE int __hscore_sig_setmask( void )
626 INLINE size_t __hscore_sizeof_siginfo_t (void)
628 return sizeof(siginfo_t);
633 __hscore_f_getfl( void )
643 __hscore_f_setfl( void )
653 __hscore_f_setfd( void )
663 __hscore_fd_cloexec( void )
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);
676 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
678 INLINE int __hscore_open(char *file, int how, mode_t mode) {
680 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
681 return _sopen(file,how | _O_NOINHERIT,_SH_DENYRW,mode);
682 // _O_NOINHERIT: see #2650
684 return _sopen(file,how | _O_NOINHERIT,_SH_DENYWR,mode);
685 // _O_NOINHERIT: see #2650
687 return open(file,how,mode);
691 // These are wrapped because on some OSs (eg. Linux) they are
692 // macros which redirect to the 64-bit-off_t versions when large file
693 // support is enabled.
695 #if defined(__MINGW32__)
696 INLINE off64_t __hscore_lseek(int fd, off64_t off, int whence) {
697 return (_lseeki64(fd,off,whence));
700 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
701 return (lseek(fd,off,whence));
705 INLINE int __hscore_stat(char *file, struct_stat *buf) {
706 return (stat(file,buf));
709 INLINE int __hscore_fstat(int fd, struct_stat *buf) {
710 return (fstat(fd,buf));
713 // select-related stuff
715 #if !defined(__MINGW32__)
716 INLINE int hsFD_SETSIZE(void) { return FD_SETSIZE; }
717 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
718 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
719 INLINE HsInt sizeof_fd_set(void) { return sizeof(fd_set); }
720 extern void hsFD_ZERO(fd_set *fds);
723 // gettimeofday()-related
725 #if !defined(__MINGW32__)
727 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
729 INLINE HsWord64 getUSecOfDay(void)
732 gettimeofday(&tv, (struct timezone *) NULL);
733 // Don't forget to cast *before* doing the arithmetic, otherwise
734 // the arithmetic happens at the type of tv_sec, which is probably
736 return ((HsWord64)tv.tv_sec * 1000000 + (HsWord64)tv.tv_usec);
739 INLINE void setTimevalTicks(struct timeval *p, HsWord64 usecs)
741 p->tv_sec = usecs / 1000000;
742 p->tv_usec = usecs % 1000000;
744 #endif /* !defined(__MINGW32__) */
746 /* ToDo: write a feature test that doesn't assume 'environ' to
747 * be in scope at link-time. */
748 extern char** environ;
749 INLINE char **__hscore_environ() { return environ; }
751 /* lossless conversions between pointers and integral types */
752 INLINE void * __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
753 INLINE void * __hscore_from_intptr (intptr_t n) { return (void *)n; }
754 INLINE uintptr_t __hscore_to_uintptr (void *p) { return (uintptr_t)p; }
755 INLINE intptr_t __hscore_to_intptr (void *p) { return (intptr_t)p; }
757 void errorBelch2(const char*s, char *t);
758 void debugBelch2(const char*s, char *t);
760 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
762 INLINE int fcntl_read(int fd, int cmd) {
763 return fcntl(fd, cmd);
765 INLINE int fcntl_write(int fd, int cmd, long arg) {
766 return fcntl(fd, cmd, arg);
768 INLINE int fcntl_lock(int fd, int cmd, struct flock *lock) {
769 return fcntl(fd, cmd, lock);
774 #endif /* __HSBASE_H__ */