1 /* -----------------------------------------------------------------------------
3 * (c) The University of Glasgow 2001-2004
5 * Definitions for package `base' which are visible in Haskell land.
7 * ---------------------------------------------------------------------------*/
12 #include "ghcconfig.h"
14 #include "HsBaseConfig.h"
17 #undef PACKAGE_BUGREPORT
20 #undef PACKAGE_TARNAME
21 #undef PACKAGE_VERSION
30 #include <sys/types.h>
46 /* Ultra-ugly: OpenBSD uses broken macros for sigemptyset and sigfillset (missing casts) */
64 #if HAVE_SYS_UTSNAME_H
65 #include <sys/utsname.h>
69 # include <sys/time.h>
72 # if HAVE_SYS_TIMERS_H
74 # include <sys/timers.h>
81 #include <sys/timeb.h>
87 #include <sys/times.h>
89 #if HAVE_WINSOCK_H && defined(mingw32_TARGET_OS)
99 #if !defined(mingw32_TARGET_OS) && !defined(irix_TARGET_OS)
100 # if HAVE_SYS_RESOURCE_H
101 # include <sys/resource.h>
105 #ifdef hpux_TARGET_OS
106 #include <sys/syscall.h>
107 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
108 #define HAVE_GETRUSAGE
113 #include <sys/wait.h>
118 #include "lockFile.h"
119 #include "dirUtils.h"
121 #include "runProcess.h"
123 #if defined(mingw32_TARGET_OS)
126 #include "timeUtils.h"
131 #if HAVE_SYS_SELECT_H
132 #include <sys/select.h>
135 /* in inputReady.c */
136 int inputReady(int fd, int msecs, int isSock);
138 /* in writeError.c */
139 void writeErrString__(HsAddr msg, HsInt len);
142 extern HsInt nocldstop;
144 #if !defined(mingw32_TARGET_OS)
146 extern int execvpe(char *name, char *const argv[], char **envp);
147 extern void pPrPr_disableITimers (void);
150 /* -----------------------------------------------------------------------------
151 64-bit operations, defined in longlong.c
152 -------------------------------------------------------------------------- */
154 #ifdef SUPPORT_LONG_LONGS
156 StgInt stg_gtWord64 (StgWord64, StgWord64);
157 StgInt stg_geWord64 (StgWord64, StgWord64);
158 StgInt stg_eqWord64 (StgWord64, StgWord64);
159 StgInt stg_neWord64 (StgWord64, StgWord64);
160 StgInt stg_ltWord64 (StgWord64, StgWord64);
161 StgInt stg_leWord64 (StgWord64, StgWord64);
163 StgInt stg_gtInt64 (StgInt64, StgInt64);
164 StgInt stg_geInt64 (StgInt64, StgInt64);
165 StgInt stg_eqInt64 (StgInt64, StgInt64);
166 StgInt stg_neInt64 (StgInt64, StgInt64);
167 StgInt stg_ltInt64 (StgInt64, StgInt64);
168 StgInt stg_leInt64 (StgInt64, StgInt64);
170 StgWord64 stg_remWord64 (StgWord64, StgWord64);
171 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
173 StgInt64 stg_remInt64 (StgInt64, StgInt64);
174 StgInt64 stg_quotInt64 (StgInt64, StgInt64);
175 StgInt64 stg_negateInt64 (StgInt64);
176 StgInt64 stg_plusInt64 (StgInt64, StgInt64);
177 StgInt64 stg_minusInt64 (StgInt64, StgInt64);
178 StgInt64 stg_timesInt64 (StgInt64, StgInt64);
180 StgWord64 stg_and64 (StgWord64, StgWord64);
181 StgWord64 stg_or64 (StgWord64, StgWord64);
182 StgWord64 stg_xor64 (StgWord64, StgWord64);
183 StgWord64 stg_not64 (StgWord64);
185 StgWord64 stg_uncheckedShiftL64 (StgWord64, StgInt);
186 StgWord64 stg_uncheckedShiftRL64 (StgWord64, StgInt);
187 StgInt64 stg_uncheckedIShiftL64 (StgInt64, StgInt);
188 StgInt64 stg_uncheckedIShiftRL64 (StgInt64, StgInt);
189 StgInt64 stg_uncheckedIShiftRA64 (StgInt64, StgInt);
191 StgInt64 stg_intToInt64 (StgInt);
192 StgInt stg_int64ToInt (StgInt64);
193 StgWord64 stg_int64ToWord64 (StgInt64);
195 StgWord64 stg_wordToWord64 (StgWord);
196 StgWord stg_word64ToWord (StgWord64);
197 StgInt64 stg_word64ToInt64 (StgWord64);
199 StgInt64 stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
200 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
202 #endif /* SUPPORT_LONG_LONGS */
204 /* -----------------------------------------------------------------------------
207 These functions are given as inlines here for when compiling via C,
208 but we also generate static versions into the cbits library for
209 when compiling to native code.
210 -------------------------------------------------------------------------- */
213 # if defined(_MSC_VER)
214 # define INLINE extern __inline
215 # elif defined(__HUGS__)
216 # define INLINE INLINE_ONLY
218 # define INLINE extern inline
222 INLINE int __hscore_get_errno(void) { return errno; }
223 INLINE void __hscore_set_errno(int e) { errno = e; }
225 #if !defined(_MSC_VER)
226 INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
227 INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
228 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
229 INLINE int __hscore_s_isblk(m) { return S_ISBLK(m); }
230 INLINE int __hscore_s_ischr(m) { return S_ISCHR(m); }
232 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
236 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
238 __hscore_sigemptyset( sigset_t *set )
239 { return sigemptyset(set); }
242 __hscore_sigfillset( sigset_t *set )
243 { return sigfillset(set); }
246 __hscore_sigaddset( sigset_t * set, int s )
247 { return sigaddset(set,s); }
250 __hscore_sigdelset( sigset_t * set, int s )
251 { return sigdelset(set,s); }
254 __hscore_sigismember( sigset_t * set, int s )
255 { return sigismember(set,s); }
259 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
260 { return memcpy(dst+dst_off, src, sz); }
263 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
264 { return memcpy(dst, src+src_off, sz); }
267 __hscore_supportsTextMode()
269 #if defined(mingw32_TARGET_OS)
270 return HS_BOOL_FALSE;
291 #if defined(_MSC_VER)
294 return CONST_O_BINARY;
309 __hscore_o_wronly( void )
319 __hscore_o_rdwr( void )
329 __hscore_o_append( void )
339 __hscore_o_creat( void )
349 __hscore_o_excl( void )
359 __hscore_o_trunc( void )
369 __hscore_o_noctty( void )
379 __hscore_o_nonblock( void )
389 __hscore_seek_set( void )
395 __hscore_seek_end( void )
401 __hscore_ftruncate( int fd, off_t where )
403 #if defined(HAVE_FTRUNCATE)
404 return ftruncate(fd,where);
405 #elif defined(HAVE__CHSIZE)
406 return _chsize(fd,where);
408 #error at least ftruncate or _chsize functions are required to build
413 __hscore_setmode( HsInt fd, HsBool toBin )
415 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
416 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
423 __hscore_PrelHandle_write( HsInt fd, HsAddr ptr, HsInt off, int sz )
425 return write(fd,(char *)ptr + off, sz);
429 __hscore_PrelHandle_read( HsInt fd, HsAddr ptr, HsInt off, int sz )
431 return read(fd,(char *)ptr + off, sz);
435 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
437 __hscore_PrelHandle_send( HsInt fd, HsAddr ptr, HsInt off, int sz )
439 return send(fd,(char *)ptr + off, sz, 0);
443 __hscore_PrelHandle_recv( HsInt fd, HsAddr ptr, HsInt off, int sz )
445 return recv(fd,(char *)ptr + off, sz, 0);
449 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
451 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
454 __hscore_Time_ghcTzname( void ) { return _tzname; }
458 __hscore_mkdir( HsAddr pathName, HsInt mode )
460 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
461 return mkdir(pathName);
463 return mkdir(pathName,mode);
468 __hscore_lstat( HsAddr fname, HsAddr st )
471 return lstat((const char*)fname, (struct stat*)st);
473 return stat((const char*)fname, (struct stat*)st);
478 /* A size that will contain many path names, but not necessarily all
479 * (PATH_MAX is not defined on systems with unlimited path length,
482 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
484 INLINE HsInt __hscore_long_path_size() { return 4096; }
488 INLINE mode_t __hscore_R_OK() { return R_OK; }
491 INLINE mode_t __hscore_W_OK() { return W_OK; }
494 INLINE mode_t __hscore_X_OK() { return X_OK; }
498 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
501 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
504 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
508 __hscore_d_name( struct dirent* d )
510 return (HsAddr)(d->d_name);
514 __hscore_end_of_dir( void )
516 return READDIR_ERRNO_EOF;
520 __hscore_free_dirent(HsAddr dEnt)
528 __hscore_sizeof_stat( void )
530 return sizeof(struct stat);
533 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
534 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
535 #if !defined(_MSC_VER)
536 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
540 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
543 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
545 INLINE unsigned char*
546 __hscore_ptr_c_cc( struct termios* ts )
547 { return (unsigned char*) &ts->c_cc; }
550 __hscore_sizeof_termios( void )
552 #ifndef mingw32_TARGET_OS
553 return sizeof(struct termios);
560 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
562 __hscore_sizeof_sigset_t( void )
564 return sizeof(sigset_t);
569 __hscore_echo( void )
580 __hscore_tcsanow( void )
591 __hscore_icanon( void )
600 INLINE int __hscore_vmin( void )
609 INLINE int __hscore_vtime( void )
618 INLINE int __hscore_sigttou( void )
627 INLINE int __hscore_sig_block( void )
636 INLINE int __hscore_sig_setmask( void )
646 __hscore_f_getfl( void )
656 __hscore_f_setfl( void )
665 // defined in rts/RtsStartup.c.
666 extern void* __hscore_get_saved_termios(int fd);
667 extern void __hscore_set_saved_termios(int fd, void* ts);
669 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
671 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
672 INLINE int __hsposix_SIGABRT() { return SIGABRT; }
673 INLINE int __hsposix_SIGALRM() { return SIGALRM; }
674 INLINE int __hsposix_SIGBUS() { return SIGBUS; }
675 INLINE int __hsposix_SIGCHLD() { return SIGCHLD; }
676 INLINE int __hsposix_SIGCONT() { return SIGCONT; }
677 INLINE int __hsposix_SIGFPE() { return SIGFPE; }
678 INLINE int __hsposix_SIGHUP() { return SIGHUP; }
679 INLINE int __hsposix_SIGILL() { return SIGILL; }
680 INLINE int __hsposix_SIGINT() { return SIGINT; }
681 INLINE int __hsposix_SIGKILL() { return SIGKILL; }
682 INLINE int __hsposix_SIGPIPE() { return SIGPIPE; }
683 INLINE int __hsposix_SIGQUIT() { return SIGQUIT; }
684 INLINE int __hsposix_SIGSEGV() { return SIGSEGV; }
685 INLINE int __hsposix_SIGSTOP() { return SIGSTOP; }
686 INLINE int __hsposix_SIGTERM() { return SIGTERM; }
687 INLINE int __hsposix_SIGTSTP() { return SIGTSTP; }
688 INLINE int __hsposix_SIGTTIN() { return SIGTTIN; }
689 INLINE int __hsposix_SIGTTOU() { return SIGTTOU; }
690 INLINE int __hsposix_SIGUSR1() { return SIGUSR1; }
691 INLINE int __hsposix_SIGUSR2() { return SIGUSR2; }
693 INLINE int __hsposix_SIGPOLL() { return SIGPOLL; }
695 INLINE int __hsposix_SIGPROF() { return SIGPROF; }
696 INLINE int __hsposix_SIGSYS() { return SIGSYS; }
697 INLINE int __hsposix_SIGTRAP() { return SIGTRAP; }
698 INLINE int __hsposix_SIGURG() { return SIGURG; }
699 INLINE int __hsposix_SIGVTALRM() { return SIGVTALRM; }
700 INLINE int __hsposix_SIGXCPU() { return SIGXCPU; }
701 INLINE int __hsposix_SIGXFSZ() { return SIGXFSZ; }
703 INLINE int __hsposix_SIG_BLOCK() { return SIG_BLOCK; }
704 INLINE int __hsposix_SIG_UNBLOCK() { return SIG_UNBLOCK; }
705 INLINE int __hsposix_SIG_SETMASK() { return SIG_SETMASK; }
707 #endif /* mingw32_TARGET_OS */
709 INLINE int __hscore_open(char *file, int how, mode_t mode) {
710 #ifdef mingw32_TARGET_OS
711 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
712 return _sopen(file,how,_SH_DENYRW,mode);
714 return _sopen(file,how,_SH_DENYWR,mode);
716 return open(file,how,mode);
720 // These are wrapped because on some OSs (eg. Linux) they are
721 // macros which redirect to the 64-bit-off_t versions when large file
722 // support is enabled.
724 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
725 return (lseek(fd,off,whence));
728 INLINE int __hscore_stat(char *file, struct stat *buf) {
729 return (stat(file,buf));
732 INLINE int __hscore_fstat(int fd, struct stat *buf) {
733 return (fstat(fd,buf));
736 // select-related stuff
738 #if !defined(mingw32_TARGET_OS)
739 INLINE void hsFD_CLR(int fd, fd_set *fds) { FD_CLR(fd, fds); }
740 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
741 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
742 INLINE int sizeof_fd_set(void) { return sizeof(fd_set); }
743 extern void hsFD_ZERO(fd_set *fds);
746 // gettimeofday()-related
748 #if !defined(mingw32_TARGET_OS)
751 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
753 INLINE HsInt getTicksOfDay(void)
756 gettimeofday(&tv, (struct timezone *) NULL);
757 return (tv.tv_sec * TICK_FREQ +
758 tv.tv_usec * TICK_FREQ / 1000000);
761 INLINE void setTimevalTicks(struct timeval *p, HsInt ticks)
763 p->tv_sec = ticks / TICK_FREQ;
764 p->tv_usec = (ticks % TICK_FREQ) * (1000000 / TICK_FREQ);
766 #endif // !defined(mingw32_TARGET_OS)
770 #if defined(mingw32_TARGET_OS)
772 /* Make sure we've got the reqd CSIDL_ constants in scope;
773 * w32api header files are lagging a bit in defining the full set.
775 #if !defined(CSIDL_APPDATA)
776 #define CSIDL_APPDATA 0x001a
778 #if !defined(CSIDL_PERSONAL)
779 #define CSIDL_PERSONAL 0x0005
781 #if !defined(CSIDL_PROFILE)
782 #define CSIDL_PROFILE 0x0028
784 #if !defined(CSIDL_WINDOWS)
785 #define CSIDL_WINDOWS 0x0024
788 INLINE int __hscore_CSIDL_PROFILE() { return CSIDL_PROFILE; }
789 INLINE int __hscore_CSIDL_APPDATA() { return CSIDL_APPDATA; }
790 INLINE int __hscore_CSIDL_WINDOWS() { return CSIDL_WINDOWS; }
791 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
794 /* ToDo: write a feature test that doesn't assume 'environ' to
795 * be in scope at link-time. */
796 extern char** environ;
797 INLINE char **__hscore_environ() { return environ; }
799 #endif /* __HSBASE_H__ */