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 #endif /* SUPPORT_LONG_LONGS */
208 /* -----------------------------------------------------------------------------
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 -------------------------------------------------------------------------- */
217 # if defined(_MSC_VER)
218 # define INLINE extern __inline
220 # define INLINE static inline
224 INLINE int __hscore_get_errno(void) { return errno; }
225 INLINE void __hscore_set_errno(int e) { errno = e; }
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); }
238 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
240 __hscore_sigemptyset( sigset_t *set )
241 { return sigemptyset(set); }
244 __hscore_sigfillset( sigset_t *set )
245 { return sigfillset(set); }
248 __hscore_sigaddset( sigset_t * set, int s )
249 { return sigaddset(set,s); }
252 __hscore_sigdelset( sigset_t * set, int s )
253 { return sigdelset(set,s); }
256 __hscore_sigismember( sigset_t * set, int s )
257 { return sigismember(set,s); }
260 // This is used by dph:Data.Array.Parallel.Arr.BUArr, and shouldn't be
262 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
263 { return memcpy(dst+dst_off, src, sz); }
266 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
267 { return memcpy(dst, src+src_off, sz); }
284 #if defined(_MSC_VER)
287 return CONST_O_BINARY;
302 __hscore_o_wronly( void )
312 __hscore_o_rdwr( void )
322 __hscore_o_append( void )
332 __hscore_o_creat( void )
342 __hscore_o_excl( void )
352 __hscore_o_trunc( void )
362 __hscore_o_noctty( void )
372 __hscore_o_nonblock( void )
382 __hscore_seek_set( void )
388 __hscore_seek_end( void )
394 __hscore_ftruncate( int fd, off_t where )
396 #if defined(HAVE_FTRUNCATE)
397 return ftruncate(fd,where);
398 #elif defined(HAVE__CHSIZE)
399 return _chsize(fd,where);
401 // ToDo: we should use _chsize_s() on Windows which allows a 64-bit
402 // offset, but it doesn't seem to be available from mingw at this time
404 #error at least ftruncate or _chsize functions are required to build
409 __hscore_setmode( int fd, HsBool toBin )
411 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
412 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
418 #if __GLASGOW_HASKELL__
421 __hscore_PrelHandle_write( int fd, void *ptr, HsInt off, int sz )
423 return write(fd,(char *)ptr + off, sz);
427 __hscore_PrelHandle_read( int fd, void *ptr, HsInt off, int sz )
429 return read(fd,(char *)ptr + off, sz);
433 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
435 __hscore_PrelHandle_send( int fd, void *ptr, HsInt off, int sz )
437 return send(fd,(char *)ptr + off, sz, 0);
441 __hscore_PrelHandle_recv( int fd, void *ptr, HsInt off, int sz )
443 return recv(fd,(char *)ptr + off, sz, 0);
447 #endif /* __GLASGOW_HASKELL__ */
450 __hscore_mkdir( char *pathName, int mode )
452 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
453 return mkdir(pathName);
455 return mkdir(pathName,mode);
460 __hscore_d_name( struct dirent* d )
466 __hscore_end_of_dir( void )
468 return READDIR_ERRNO_EOF;
472 __hscore_free_dirent(struct dirent *dEnt)
479 #if defined(__MINGW32__)
480 // We want the versions of stat/fstat/lseek that use 64-bit offsets,
481 // and you have to ask for those explicitly. Unfortunately there
482 // doesn't seem to be a 64-bit version of truncate/ftruncate, so while
483 // hFileSize and hSeek will work with large files, hSetFileSize will not.
484 typedef struct _stati64 struct_stat;
485 typedef off64_t stsize_t;
487 typedef struct stat struct_stat;
488 typedef off_t stsize_t;
492 __hscore_sizeof_stat( void )
494 return sizeof(struct_stat);
497 INLINE time_t __hscore_st_mtime ( struct_stat* st ) { return st->st_mtime; }
498 INLINE stsize_t __hscore_st_size ( struct_stat* st ) { return st->st_size; }
499 #if !defined(_MSC_VER)
500 INLINE mode_t __hscore_st_mode ( struct_stat* st ) { return st->st_mode; }
501 INLINE dev_t __hscore_st_dev ( struct_stat* st ) { return st->st_dev; }
502 INLINE ino_t __hscore_st_ino ( struct_stat* st ) { return st->st_ino; }
505 #if defined(__MINGW32__)
506 INLINE int __hscore_stat(wchar_t *file, struct_stat *buf) {
507 return _wstati64(file,buf);
510 INLINE int __hscore_fstat(int fd, struct_stat *buf) {
511 return _fstati64(fd,buf);
513 INLINE int __hscore_lstat(wchar_t *fname, struct_stat *buf )
515 return _wstati64(fname,buf);
518 INLINE int __hscore_stat(char *file, struct_stat *buf) {
519 return stat(file,buf);
522 INLINE int __hscore_fstat(int fd, struct_stat *buf) {
523 return fstat(fd,buf);
526 INLINE int __hscore_lstat( const char *fname, struct stat *buf )
529 return lstat(fname, buf);
531 return stat(fname, buf);
537 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
540 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
542 INLINE unsigned char*
543 __hscore_ptr_c_cc( struct termios* ts )
544 { return (unsigned char*) &ts->c_cc; }
547 __hscore_sizeof_termios( void )
550 return sizeof(struct termios);
557 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
559 __hscore_sizeof_sigset_t( void )
561 return sizeof(sigset_t);
566 __hscore_echo( void )
577 __hscore_tcsanow( void )
588 __hscore_icanon( void )
597 INLINE int __hscore_vmin( void )
606 INLINE int __hscore_vtime( void )
615 INLINE int __hscore_sigttou( void )
624 INLINE int __hscore_sig_block( void )
633 INLINE int __hscore_sig_setmask( void )
643 INLINE size_t __hscore_sizeof_siginfo_t (void)
645 return sizeof(siginfo_t);
650 __hscore_f_getfl( void )
660 __hscore_f_setfl( void )
670 __hscore_f_setfd( void )
680 __hscore_fd_cloexec( void )
689 // defined in rts/RtsStartup.c.
690 extern void* __hscore_get_saved_termios(int fd);
691 extern void __hscore_set_saved_termios(int fd, void* ts);
693 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
696 INLINE int __hscore_open(wchar_t *file, int how, mode_t mode) {
697 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
698 return _wsopen(file,how | _O_NOINHERIT,_SH_DENYRW,mode);
699 // _O_NOINHERIT: see #2650
701 return _wsopen(file,how | _O_NOINHERIT,_SH_DENYWR,mode);
702 // _O_NOINHERIT: see #2650
705 INLINE int __hscore_open(char *file, int how, mode_t mode) {
706 return open(file,how,mode);
710 // These are wrapped because on some OSs (eg. Linux) they are
711 // macros which redirect to the 64-bit-off_t versions when large file
712 // support is enabled.
714 #if defined(__MINGW32__)
715 INLINE off64_t __hscore_lseek(int fd, off64_t off, int whence) {
716 return (_lseeki64(fd,off,whence));
719 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
720 return (lseek(fd,off,whence));
724 // select-related stuff
726 #if !defined(__MINGW32__)
727 INLINE int hsFD_SETSIZE(void) { return FD_SETSIZE; }
728 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
729 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
730 INLINE HsInt sizeof_fd_set(void) { return sizeof(fd_set); }
731 extern void hsFD_ZERO(fd_set *fds);
734 // gettimeofday()-related
736 #if !defined(__MINGW32__)
738 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
740 INLINE HsWord64 getUSecOfDay(void)
743 gettimeofday(&tv, (struct timezone *) NULL);
744 // Don't forget to cast *before* doing the arithmetic, otherwise
745 // the arithmetic happens at the type of tv_sec, which is probably
747 return ((HsWord64)tv.tv_sec * 1000000 + (HsWord64)tv.tv_usec);
750 INLINE void setTimevalTicks(struct timeval *p, HsWord64 usecs)
752 p->tv_sec = usecs / 1000000;
753 p->tv_usec = usecs % 1000000;
755 #endif /* !defined(__MINGW32__) */
757 /* ToDo: write a feature test that doesn't assume 'environ' to
758 * be in scope at link-time. */
759 extern char** environ;
760 INLINE char **__hscore_environ() { return environ; }
762 /* lossless conversions between pointers and integral types */
763 INLINE void * __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
764 INLINE void * __hscore_from_intptr (intptr_t n) { return (void *)n; }
765 INLINE uintptr_t __hscore_to_uintptr (void *p) { return (uintptr_t)p; }
766 INLINE intptr_t __hscore_to_intptr (void *p) { return (intptr_t)p; }
768 void errorBelch2(const char*s, char *t);
769 void debugBelch2(const char*s, char *t);
771 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
773 INLINE int fcntl_read(int fd, int cmd) {
774 return fcntl(fd, cmd);
776 INLINE int fcntl_write(int fd, int cmd, long arg) {
777 return fcntl(fd, cmd, arg);
779 INLINE int fcntl_lock(int fd, int cmd, struct flock *lock) {
780 return fcntl(fd, cmd, lock);
785 #endif /* __HSBASE_H__ */