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) */
68 #if HAVE_SYS_UTSNAME_H
69 #include <sys/utsname.h>
73 # include <sys/time.h>
76 # if HAVE_SYS_TIMERS_H
78 # include <sys/timers.h>
85 #include <sys/timeb.h>
91 #include <sys/times.h>
93 #if HAVE_WINSOCK_H && defined(__MINGW32__)
103 # include <inttypes.h>
108 #if !defined(__MINGW32__) && !defined(irix_HOST_OS)
109 # if HAVE_SYS_RESOURCE_H
110 # include <sys/resource.h>
114 #if !HAVE_GETRUSAGE && HAVE_SYS_SYSCALL_H
115 # include <sys/syscall.h>
116 # if defined(SYS_GETRUSAGE) /* hpux_HOST_OS */
117 # define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
118 # define HAVE_GETRUSAGE 1
124 #include <sys/wait.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 /* -----------------------------------------------------------------------------
157 These functions are given as inlines here for when compiling via C,
158 but we also generate static versions into the cbits library for
159 when compiling to native code.
160 -------------------------------------------------------------------------- */
163 # if defined(_MSC_VER)
164 # define INLINE extern __inline
166 # define INLINE static inline
170 INLINE int __hscore_get_errno(void) { return errno; }
171 INLINE void __hscore_set_errno(int e) { errno = e; }
173 #if !defined(_MSC_VER)
174 INLINE int __hscore_s_isreg(mode_t m) { return S_ISREG(m); }
175 INLINE int __hscore_s_isdir(mode_t m) { return S_ISDIR(m); }
176 INLINE int __hscore_s_isfifo(mode_t m) { return S_ISFIFO(m); }
177 INLINE int __hscore_s_isblk(mode_t m) { return S_ISBLK(m); }
178 INLINE int __hscore_s_ischr(mode_t m) { return S_ISCHR(m); }
179 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
180 INLINE int __hscore_s_issock(mode_t m) { return S_ISSOCK(m); }
184 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
186 __hscore_sigemptyset( sigset_t *set )
187 { return sigemptyset(set); }
190 __hscore_sigfillset( sigset_t *set )
191 { return sigfillset(set); }
194 __hscore_sigaddset( sigset_t * set, int s )
195 { return sigaddset(set,s); }
198 __hscore_sigdelset( sigset_t * set, int s )
199 { return sigdelset(set,s); }
202 __hscore_sigismember( sigset_t * set, int s )
203 { return sigismember(set,s); }
206 __hscore_utime( const char *file, const struct utimbuf *timep )
207 { return utime(file,timep); }
210 // This is used by dph:Data.Array.Parallel.Arr.BUArr, and shouldn't be
212 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
213 { return memcpy(dst+dst_off, src, sz); }
216 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
217 { return memcpy(dst, src+src_off, sz); }
234 #if defined(_MSC_VER)
237 return CONST_O_BINARY;
252 __hscore_o_wronly( void )
262 __hscore_o_rdwr( void )
272 __hscore_o_append( void )
282 __hscore_o_creat( void )
292 __hscore_o_excl( void )
302 __hscore_o_trunc( void )
312 __hscore_o_noctty( void )
322 __hscore_o_nonblock( void )
332 __hscore_seek_set( void )
338 __hscore_seek_end( void )
344 __hscore_ftruncate( int fd, off_t where )
346 #if defined(HAVE_FTRUNCATE)
347 return ftruncate(fd,where);
348 #elif defined(HAVE__CHSIZE)
349 return _chsize(fd,where);
351 // ToDo: we should use _chsize_s() on Windows which allows a 64-bit
352 // offset, but it doesn't seem to be available from mingw at this time
354 #error at least ftruncate or _chsize functions are required to build
359 __hscore_setmode( int fd, HsBool toBin )
361 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
362 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
368 #if __GLASGOW_HASKELL__
370 #endif /* __GLASGOW_HASKELL__ */
372 #if defined(__MINGW32__)
373 // We want the versions of stat/fstat/lseek that use 64-bit offsets,
374 // and you have to ask for those explicitly. Unfortunately there
375 // doesn't seem to be a 64-bit version of truncate/ftruncate, so while
376 // hFileSize and hSeek will work with large files, hSetFileSize will not.
377 typedef struct _stati64 struct_stat;
378 typedef off64_t stsize_t;
380 typedef struct stat struct_stat;
381 typedef off_t stsize_t;
385 __hscore_sizeof_stat( void )
387 return sizeof(struct_stat);
390 INLINE time_t __hscore_st_mtime ( struct_stat* st ) { return st->st_mtime; }
391 INLINE stsize_t __hscore_st_size ( struct_stat* st ) { return st->st_size; }
392 #if !defined(_MSC_VER)
393 INLINE mode_t __hscore_st_mode ( struct_stat* st ) { return st->st_mode; }
394 INLINE dev_t __hscore_st_dev ( struct_stat* st ) { return st->st_dev; }
395 INLINE ino_t __hscore_st_ino ( struct_stat* st ) { return st->st_ino; }
398 #if defined(__MINGW32__)
399 INLINE int __hscore_stat(wchar_t *file, struct_stat *buf) {
400 return _wstati64(file,buf);
403 INLINE int __hscore_fstat(int fd, struct_stat *buf) {
404 return _fstati64(fd,buf);
406 INLINE int __hscore_lstat(wchar_t *fname, struct_stat *buf )
408 return _wstati64(fname,buf);
411 INLINE int __hscore_stat(char *file, struct_stat *buf) {
412 return stat(file,buf);
415 INLINE int __hscore_fstat(int fd, struct_stat *buf) {
416 return fstat(fd,buf);
419 INLINE int __hscore_lstat( const char *fname, struct stat *buf )
422 return lstat(fname, buf);
424 return stat(fname, buf);
430 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
433 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
435 INLINE unsigned char*
436 __hscore_ptr_c_cc( struct termios* ts )
437 { return (unsigned char*) &ts->c_cc; }
440 __hscore_sizeof_termios( void )
443 return sizeof(struct termios);
450 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
452 __hscore_sizeof_sigset_t( void )
454 return sizeof(sigset_t);
459 __hscore_echo( void )
470 __hscore_tcsanow( void )
481 __hscore_icanon( void )
490 INLINE int __hscore_vmin( void )
499 INLINE int __hscore_vtime( void )
508 INLINE int __hscore_sigttou( void )
517 INLINE int __hscore_sig_block( void )
526 INLINE int __hscore_sig_setmask( void )
536 INLINE size_t __hscore_sizeof_siginfo_t (void)
538 return sizeof(siginfo_t);
543 __hscore_f_getfl( void )
553 __hscore_f_setfl( void )
563 __hscore_f_setfd( void )
573 __hscore_fd_cloexec( void )
582 // defined in rts/RtsStartup.c.
583 extern void* __hscore_get_saved_termios(int fd);
584 extern void __hscore_set_saved_termios(int fd, void* ts);
586 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
589 INLINE int __hscore_open(wchar_t *file, int how, mode_t mode) {
590 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
591 return _wsopen(file,how | _O_NOINHERIT,_SH_DENYRW,mode);
592 // _O_NOINHERIT: see #2650
594 return _wsopen(file,how | _O_NOINHERIT,_SH_DENYWR,mode);
595 // _O_NOINHERIT: see #2650
598 INLINE int __hscore_open(char *file, int how, mode_t mode) {
599 return open(file,how,mode);
603 // These are wrapped because on some OSs (eg. Linux) they are
604 // macros which redirect to the 64-bit-off_t versions when large file
605 // support is enabled.
607 #if defined(__MINGW32__)
608 INLINE off64_t __hscore_lseek(int fd, off64_t off, int whence) {
609 return (_lseeki64(fd,off,whence));
612 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
613 return (lseek(fd,off,whence));
617 // select-related stuff
619 #if !defined(__MINGW32__)
620 INLINE int hsFD_SETSIZE(void) { return FD_SETSIZE; }
621 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
622 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
623 INLINE HsInt sizeof_fd_set(void) { return sizeof(fd_set); }
624 extern void hsFD_ZERO(fd_set *fds);
627 INLINE int __hscore_select(int nfds, fd_set *readfds, fd_set *writefds,
628 fd_set *exceptfds, struct timeval *timeout) {
629 return (select(nfds,readfds,writefds,exceptfds,timeout));
632 // gettimeofday()-related
634 #if !defined(__MINGW32__)
636 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
638 INLINE HsWord64 getUSecOfDay(void)
641 gettimeofday(&tv, (struct timezone *) NULL);
642 // Don't forget to cast *before* doing the arithmetic, otherwise
643 // the arithmetic happens at the type of tv_sec, which is probably
645 return ((HsWord64)tv.tv_sec * 1000000 + (HsWord64)tv.tv_usec);
648 INLINE void setTimevalTicks(struct timeval *p, HsWord64 usecs)
650 p->tv_sec = usecs / 1000000;
651 p->tv_usec = usecs % 1000000;
653 #endif /* !defined(__MINGW32__) */
656 // You should not access _environ directly on Darwin in a bundle/shared library.
657 // See #2458 and http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man7/environ.7.html
658 #include <crt_externs.h>
659 INLINE char **__hscore_environ() { return *(_NSGetEnviron()); }
661 /* ToDo: write a feature test that doesn't assume 'environ' to
662 * be in scope at link-time. */
663 extern char** environ;
664 INLINE char **__hscore_environ() { return environ; }
667 /* lossless conversions between pointers and integral types */
668 INLINE void * __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
669 INLINE void * __hscore_from_intptr (intptr_t n) { return (void *)n; }
670 INLINE uintptr_t __hscore_to_uintptr (void *p) { return (uintptr_t)p; }
671 INLINE intptr_t __hscore_to_intptr (void *p) { return (intptr_t)p; }
673 void errorBelch2(const char*s, char *t);
674 void debugBelch2(const char*s, char *t);
676 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
678 INLINE int fcntl_read(int fd, int cmd) {
679 return fcntl(fd, cmd);
681 INLINE int fcntl_write(int fd, int cmd, long arg) {
682 return fcntl(fd, cmd, arg);
684 INLINE int fcntl_lock(int fd, int cmd, struct flock *lock) {
685 return fcntl(fd, cmd, lock);
690 #endif /* __HSBASE_H__ */