1 /* -----------------------------------------------------------------------------
3 * (c) The University of Glasgow 2001-2004
5 * Definitions for package `base' which are visible in Haskell land.
7 * ---------------------------------------------------------------------------*/
12 #include "HsBaseConfig.h"
15 #undef PACKAGE_BUGREPORT
18 #undef PACKAGE_TARNAME
19 #undef PACKAGE_VERSION
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. */
33 #include <sys/types.h>
49 /* Ultra-ugly: OpenBSD uses broken macros for sigemptyset and sigfillset (missing casts) */
67 #if HAVE_SYS_UTSNAME_H
68 #include <sys/utsname.h>
72 # include <sys/time.h>
75 # if HAVE_SYS_TIMERS_H
77 # include <sys/timers.h>
84 #include <sys/timeb.h>
90 #include <sys/times.h>
92 #if HAVE_WINSOCK_H && defined(__MINGW32__)
102 # include <inttypes.h>
107 #if !defined(__MINGW32__) && !defined(irix_HOST_OS)
108 # if HAVE_SYS_RESOURCE_H
109 # include <sys/resource.h>
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
123 #include <sys/wait.h>
128 #include "dirUtils.h"
131 #if defined(__MINGW32__)
132 /* in Win32Utils.c */
133 extern void maperrno (void);
134 extern HsWord64 getUSecOfDay(void);
137 #if defined(__MINGW32__)
144 #if HAVE_SYS_SELECT_H
145 #include <sys/select.h>
148 /* in inputReady.c */
149 extern int fdReady(int fd, int write, int msecs, int isSock);
152 extern HsInt nocldstop;
154 /* -----------------------------------------------------------------------------
155 64-bit operations, defined in longlong.c
156 -------------------------------------------------------------------------- */
158 #ifdef SUPPORT_LONG_LONGS
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);
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);
174 HsWord64 hs_remWord64 (HsWord64, HsWord64);
175 HsWord64 hs_quotWord64 (HsWord64, HsWord64);
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);
184 HsWord64 hs_and64 (HsWord64, HsWord64);
185 HsWord64 hs_or64 (HsWord64, HsWord64);
186 HsWord64 hs_xor64 (HsWord64, HsWord64);
187 HsWord64 hs_not64 (HsWord64);
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);
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);
202 HsWord64 hs_integerToWord64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
203 HsInt64 hs_integerToInt64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
205 #endif /* SUPPORT_LONG_LONGS */
207 /* -----------------------------------------------------------------------------
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 -------------------------------------------------------------------------- */
216 # if defined(_MSC_VER)
217 # define INLINE extern __inline
219 # define INLINE static inline
223 INLINE int __hscore_get_errno(void) { return errno; }
224 INLINE void __hscore_set_errno(int e) { errno = e; }
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); }
233 INLINE int __hscore_s_issock(mode_t m) { return S_ISSOCK(m); }
237 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
239 __hscore_sigemptyset( sigset_t *set )
240 { return sigemptyset(set); }
243 __hscore_sigfillset( sigset_t *set )
244 { return sigfillset(set); }
247 __hscore_sigaddset( sigset_t * set, int s )
248 { return sigaddset(set,s); }
251 __hscore_sigdelset( sigset_t * set, int s )
252 { return sigdelset(set,s); }
255 __hscore_sigismember( sigset_t * set, int s )
256 { return sigismember(set,s); }
260 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
261 { return memcpy(dst+dst_off, src, sz); }
264 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
265 { return memcpy(dst, src+src_off, sz); }
268 __hscore_supportsTextMode()
270 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
271 return HS_BOOL_FALSE;
292 #if defined(_MSC_VER)
295 return CONST_O_BINARY;
310 __hscore_o_wronly( void )
320 __hscore_o_rdwr( void )
330 __hscore_o_append( void )
340 __hscore_o_creat( void )
350 __hscore_o_excl( void )
360 __hscore_o_trunc( void )
370 __hscore_o_noctty( void )
380 __hscore_o_nonblock( void )
390 __hscore_seek_set( void )
396 __hscore_seek_end( void )
402 __hscore_ftruncate( int fd, off_t where )
404 #if defined(HAVE_FTRUNCATE)
405 return ftruncate(fd,where);
406 #elif defined(HAVE__CHSIZE)
407 return _chsize(fd,where);
409 #error at least ftruncate or _chsize functions are required to build
414 __hscore_setmode( int fd, HsBool toBin )
416 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
417 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
423 #if __GLASGOW_HASKELL__
426 __hscore_PrelHandle_write( int fd, void *ptr, HsInt off, int sz )
428 return write(fd,(char *)ptr + off, sz);
432 __hscore_PrelHandle_read( int fd, void *ptr, HsInt off, int sz )
434 return read(fd,(char *)ptr + off, sz);
438 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
440 __hscore_PrelHandle_send( int fd, void *ptr, HsInt off, int sz )
442 return send(fd,(char *)ptr + off, sz, 0);
446 __hscore_PrelHandle_recv( int fd, void *ptr, HsInt off, int sz )
448 return recv(fd,(char *)ptr + off, sz, 0);
452 #endif /* __GLASGOW_HASKELL__ */
455 __hscore_mkdir( char *pathName, int mode )
457 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
458 return mkdir(pathName);
460 return mkdir(pathName,mode);
465 __hscore_lstat( const char *fname, struct stat *st )
468 return lstat(fname, st);
470 return stat(fname, st);
475 __hscore_d_name( struct dirent* d )
481 __hscore_end_of_dir( void )
483 return READDIR_ERRNO_EOF;
487 __hscore_free_dirent(struct dirent *dEnt)
494 #if defined(__MINGW32__)
495 // We want the versions of stat/fstat/lseek that use 64-bit offsets,
496 // and you have to ask for those explicitly. Unfortunately there
497 // doesn't seem to be a 64-bit version of truncate/ftruncate, so while
498 // hFileSize and hSeek will work with large files, hSetFileSize will not.
499 #define stat(file,buf) _stati64(file,buf)
500 #define fstat(fd,buf) _fstati64(fd,buf)
501 typedef struct _stati64 struct_stat;
502 typedef off64_t stsize_t;
504 typedef struct stat struct_stat;
505 typedef off_t stsize_t;
509 __hscore_sizeof_stat( void )
511 return sizeof(struct_stat);
514 INLINE time_t __hscore_st_mtime ( struct_stat* st ) { return st->st_mtime; }
515 INLINE stsize_t __hscore_st_size ( struct_stat* st ) { return st->st_size; }
516 #if !defined(_MSC_VER)
517 INLINE mode_t __hscore_st_mode ( struct_stat* st ) { return st->st_mode; }
518 INLINE mode_t __hscore_st_dev ( struct_stat* st ) { return st->st_dev; }
519 INLINE mode_t __hscore_st_ino ( struct_stat* st ) { return st->st_ino; }
523 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
526 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
528 INLINE unsigned char*
529 __hscore_ptr_c_cc( struct termios* ts )
530 { return (unsigned char*) &ts->c_cc; }
533 __hscore_sizeof_termios( void )
536 return sizeof(struct termios);
543 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
545 __hscore_sizeof_sigset_t( void )
547 return sizeof(sigset_t);
552 __hscore_echo( void )
563 __hscore_tcsanow( void )
574 __hscore_icanon( void )
583 INLINE int __hscore_vmin( void )
592 INLINE int __hscore_vtime( void )
601 INLINE int __hscore_sigttou( void )
610 INLINE int __hscore_sig_block( void )
619 INLINE int __hscore_sig_setmask( void )
629 __hscore_f_getfl( void )
639 __hscore_f_setfl( void )
648 // defined in rts/RtsStartup.c.
649 extern void* __hscore_get_saved_termios(int fd);
650 extern void __hscore_set_saved_termios(int fd, void* ts);
652 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
654 INLINE int __hscore_open(char *file, int how, mode_t mode) {
656 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
657 return _sopen(file,how,_SH_DENYRW,mode);
659 return _sopen(file,how,_SH_DENYWR,mode);
661 return open(file,how,mode);
665 // These are wrapped because on some OSs (eg. Linux) they are
666 // macros which redirect to the 64-bit-off_t versions when large file
667 // support is enabled.
669 #if defined(__MINGW32__)
670 INLINE off64_t __hscore_lseek(int fd, off64_t off, int whence) {
671 return (_lseeki64(fd,off,whence));
674 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
675 return (lseek(fd,off,whence));
679 INLINE int __hscore_stat(char *file, struct_stat *buf) {
680 return (stat(file,buf));
683 INLINE int __hscore_fstat(int fd, struct_stat *buf) {
684 return (fstat(fd,buf));
687 // select-related stuff
689 #if !defined(__MINGW32__)
690 INLINE int hsFD_SETSIZE(void) { return FD_SETSIZE; }
691 INLINE void hsFD_CLR(int fd, fd_set *fds) { FD_CLR(fd, fds); }
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);
698 // gettimeofday()-related
700 #if !defined(__MINGW32__)
702 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
704 INLINE HsWord64 getUSecOfDay(void)
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
711 return ((HsWord64)tv.tv_sec * 1000000 + (HsWord64)tv.tv_usec);
714 INLINE void setTimevalTicks(struct timeval *p, HsWord64 usecs)
716 p->tv_sec = usecs / 1000000;
717 p->tv_usec = usecs % 1000000;
719 #endif /* !defined(__MINGW32__) */
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; }
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; }
732 #endif /* __HSBASE_H__ */