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
23 /* Needed to get the macro version of errno on some OSs (eg. Solaris).
24 We must do this, because these libs are only compiled once, but
25 must work in both single-threaded and multi-threaded programs. */
35 #include <sys/types.h>
51 /* Ultra-ugly: OpenBSD uses broken macros for sigemptyset and sigfillset (missing casts) */
69 #if HAVE_SYS_UTSNAME_H
70 #include <sys/utsname.h>
74 # include <sys/time.h>
77 # if HAVE_SYS_TIMERS_H
79 # include <sys/timers.h>
86 #include <sys/timeb.h>
92 #include <sys/times.h>
94 #if HAVE_WINSOCK_H && defined(mingw32_HOST_OS)
104 # include <inttypes.h>
109 #if !defined(mingw32_HOST_OS) && !defined(irix_HOST_OS)
110 # if HAVE_SYS_RESOURCE_H
111 # include <sys/resource.h>
116 #include <sys/syscall.h>
117 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
118 #define HAVE_GETRUSAGE
123 #include <sys/wait.h>
128 #include "lockFile.h"
129 #include "dirUtils.h"
132 #include "runProcess.h"
134 #if defined(mingw32_HOST_OS)
137 #include "timeUtils.h"
142 #if HAVE_SYS_SELECT_H
143 #include <sys/select.h>
146 /* in inputReady.c */
147 int inputReady(int fd, int msecs, int isSock);
150 extern HsInt nocldstop;
152 #if !defined(mingw32_HOST_OS)
154 extern int execvpe(char *name, char *const argv[], char **envp);
155 extern void pPrPr_disableITimers (void);
158 /* -----------------------------------------------------------------------------
159 64-bit operations, defined in longlong.c
160 -------------------------------------------------------------------------- */
162 #ifdef SUPPORT_LONG_LONGS
164 StgInt stg_gtWord64 (StgWord64, StgWord64);
165 StgInt stg_geWord64 (StgWord64, StgWord64);
166 StgInt stg_eqWord64 (StgWord64, StgWord64);
167 StgInt stg_neWord64 (StgWord64, StgWord64);
168 StgInt stg_ltWord64 (StgWord64, StgWord64);
169 StgInt stg_leWord64 (StgWord64, StgWord64);
171 StgInt stg_gtInt64 (StgInt64, StgInt64);
172 StgInt stg_geInt64 (StgInt64, StgInt64);
173 StgInt stg_eqInt64 (StgInt64, StgInt64);
174 StgInt stg_neInt64 (StgInt64, StgInt64);
175 StgInt stg_ltInt64 (StgInt64, StgInt64);
176 StgInt stg_leInt64 (StgInt64, StgInt64);
178 StgWord64 stg_remWord64 (StgWord64, StgWord64);
179 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
181 StgInt64 stg_remInt64 (StgInt64, StgInt64);
182 StgInt64 stg_quotInt64 (StgInt64, StgInt64);
183 StgInt64 stg_negateInt64 (StgInt64);
184 StgInt64 stg_plusInt64 (StgInt64, StgInt64);
185 StgInt64 stg_minusInt64 (StgInt64, StgInt64);
186 StgInt64 stg_timesInt64 (StgInt64, StgInt64);
188 StgWord64 stg_and64 (StgWord64, StgWord64);
189 StgWord64 stg_or64 (StgWord64, StgWord64);
190 StgWord64 stg_xor64 (StgWord64, StgWord64);
191 StgWord64 stg_not64 (StgWord64);
193 StgWord64 stg_uncheckedShiftL64 (StgWord64, StgInt);
194 StgWord64 stg_uncheckedShiftRL64 (StgWord64, StgInt);
195 StgInt64 stg_uncheckedIShiftL64 (StgInt64, StgInt);
196 StgInt64 stg_uncheckedIShiftRL64 (StgInt64, StgInt);
197 StgInt64 stg_uncheckedIShiftRA64 (StgInt64, StgInt);
199 StgInt64 stg_intToInt64 (StgInt);
200 StgInt stg_int64ToInt (StgInt64);
201 StgWord64 stg_int64ToWord64 (StgInt64);
203 StgWord64 stg_wordToWord64 (StgWord);
204 StgWord stg_word64ToWord (StgWord64);
205 StgInt64 stg_word64ToInt64 (StgWord64);
207 StgInt64 stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
208 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
210 #endif /* SUPPORT_LONG_LONGS */
212 /* -----------------------------------------------------------------------------
215 These functions are given as inlines here for when compiling via C,
216 but we also generate static versions into the cbits library for
217 when compiling to native code.
218 -------------------------------------------------------------------------- */
221 # if defined(_MSC_VER)
222 # define INLINE extern __inline
223 # elif defined(__GNUC__)
224 # define INLINE extern inline
226 # define INLINE inline
230 INLINE int __hscore_get_errno(void) { return errno; }
231 INLINE void __hscore_set_errno(int e) { errno = e; }
233 #if !defined(_MSC_VER)
234 INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
235 INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
236 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
237 INLINE int __hscore_s_isblk(m) { return S_ISBLK(m); }
238 INLINE int __hscore_s_ischr(m) { return S_ISCHR(m); }
240 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
244 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
246 __hscore_sigemptyset( sigset_t *set )
247 { return sigemptyset(set); }
250 __hscore_sigfillset( sigset_t *set )
251 { return sigfillset(set); }
254 __hscore_sigaddset( sigset_t * set, int s )
255 { return sigaddset(set,s); }
258 __hscore_sigdelset( sigset_t * set, int s )
259 { return sigdelset(set,s); }
262 __hscore_sigismember( sigset_t * set, int s )
263 { return sigismember(set,s); }
267 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
268 { return memcpy(dst+dst_off, src, sz); }
271 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
272 { return memcpy(dst, src+src_off, sz); }
275 __hscore_supportsTextMode()
277 #if defined(mingw32_HOST_OS)
278 return HS_BOOL_FALSE;
299 #if defined(_MSC_VER)
302 return CONST_O_BINARY;
317 __hscore_o_wronly( void )
327 __hscore_o_rdwr( void )
337 __hscore_o_append( void )
347 __hscore_o_creat( void )
357 __hscore_o_excl( void )
367 __hscore_o_trunc( void )
377 __hscore_o_noctty( void )
387 __hscore_o_nonblock( void )
397 __hscore_seek_set( void )
403 __hscore_seek_end( void )
409 __hscore_ftruncate( int fd, off_t where )
411 #if defined(HAVE_FTRUNCATE)
412 return ftruncate(fd,where);
413 #elif defined(HAVE__CHSIZE)
414 return _chsize(fd,where);
416 #error at least ftruncate or _chsize functions are required to build
421 __hscore_setmode( HsInt fd, HsBool toBin )
423 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
424 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
430 #if __GLASGOW_HASKELL__
433 __hscore_PrelHandle_write( HsInt fd, HsAddr ptr, HsInt off, int sz )
435 return write(fd,(char *)ptr + off, sz);
439 __hscore_PrelHandle_read( HsInt fd, HsAddr ptr, HsInt off, int sz )
441 return read(fd,(char *)ptr + off, sz);
445 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
447 __hscore_PrelHandle_send( HsInt fd, HsAddr ptr, HsInt off, int sz )
449 return send(fd,(char *)ptr + off, sz, 0);
453 __hscore_PrelHandle_recv( HsInt fd, HsAddr ptr, HsInt off, int sz )
455 return recv(fd,(char *)ptr + off, sz, 0);
459 #endif /* __GLASGOW_HASKELL__ */
461 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
463 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
466 __hscore_Time_ghcTzname( void ) { return _tzname; }
470 __hscore_mkdir( HsAddr pathName, HsInt mode )
472 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
473 return mkdir(pathName);
475 return mkdir(pathName,mode);
480 __hscore_lstat( HsAddr fname, HsAddr st )
483 return lstat((const char*)fname, (struct stat*)st);
485 return stat((const char*)fname, (struct stat*)st);
490 /* A size that will contain many path names, but not necessarily all
491 * (PATH_MAX is not defined on systems with unlimited path length,
494 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
496 INLINE HsInt __hscore_long_path_size() { return 4096; }
500 INLINE mode_t __hscore_R_OK() { return R_OK; }
503 INLINE mode_t __hscore_W_OK() { return W_OK; }
506 INLINE mode_t __hscore_X_OK() { return X_OK; }
510 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
513 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
516 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
520 __hscore_d_name( struct dirent* d )
522 return (HsAddr)(d->d_name);
526 __hscore_end_of_dir( void )
528 return READDIR_ERRNO_EOF;
532 __hscore_free_dirent(HsAddr dEnt)
540 __hscore_sizeof_stat( void )
542 return sizeof(struct stat);
545 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
546 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
547 #if !defined(_MSC_VER)
548 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
552 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
555 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
557 INLINE unsigned char*
558 __hscore_ptr_c_cc( struct termios* ts )
559 { return (unsigned char*) &ts->c_cc; }
562 __hscore_sizeof_termios( void )
564 #ifndef mingw32_HOST_OS
565 return sizeof(struct termios);
572 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
574 __hscore_sizeof_sigset_t( void )
576 return sizeof(sigset_t);
581 __hscore_echo( void )
592 __hscore_tcsanow( void )
603 __hscore_icanon( void )
612 INLINE int __hscore_vmin( void )
621 INLINE int __hscore_vtime( void )
630 INLINE int __hscore_sigttou( void )
639 INLINE int __hscore_sig_block( void )
648 INLINE int __hscore_sig_setmask( void )
658 __hscore_f_getfl( void )
668 __hscore_f_setfl( void )
677 // defined in rts/RtsStartup.c.
678 extern void* __hscore_get_saved_termios(int fd);
679 extern void __hscore_set_saved_termios(int fd, void* ts);
681 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
683 INLINE int __hscore_open(char *file, int how, mode_t mode) {
684 #ifdef mingw32_HOST_OS
685 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
686 return _sopen(file,how,_SH_DENYRW,mode);
688 return _sopen(file,how,_SH_DENYWR,mode);
690 return open(file,how,mode);
694 // These are wrapped because on some OSs (eg. Linux) they are
695 // macros which redirect to the 64-bit-off_t versions when large file
696 // support is enabled.
698 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
699 return (lseek(fd,off,whence));
702 INLINE int __hscore_stat(char *file, struct stat *buf) {
703 return (stat(file,buf));
706 INLINE int __hscore_fstat(int fd, struct stat *buf) {
707 return (fstat(fd,buf));
710 // select-related stuff
712 #if !defined(mingw32_HOST_OS)
713 INLINE int hsFD_SETSIZE(void) { return FD_SETSIZE; }
714 INLINE void hsFD_CLR(int fd, fd_set *fds) { FD_CLR(fd, fds); }
715 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
716 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
717 INLINE int sizeof_fd_set(void) { return sizeof(fd_set); }
718 extern void hsFD_ZERO(fd_set *fds);
721 // gettimeofday()-related
723 #if !defined(mingw32_HOST_OS)
726 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
728 INLINE HsInt getTicksOfDay(void)
731 gettimeofday(&tv, (struct timezone *) NULL);
732 return (tv.tv_sec * TICK_FREQ +
733 tv.tv_usec * TICK_FREQ / 1000000);
736 INLINE void setTimevalTicks(struct timeval *p, HsInt ticks)
738 p->tv_sec = ticks / TICK_FREQ;
739 p->tv_usec = (ticks % TICK_FREQ) * (1000000 / TICK_FREQ);
741 #endif /* !defined(mingw32_HOST_OS) */
745 #if defined(mingw32_HOST_OS)
747 /* Make sure we've got the reqd CSIDL_ constants in scope;
748 * w32api header files are lagging a bit in defining the full set.
750 #if !defined(CSIDL_APPDATA)
751 #define CSIDL_APPDATA 0x001a
753 #if !defined(CSIDL_PERSONAL)
754 #define CSIDL_PERSONAL 0x0005
756 #if !defined(CSIDL_PROFILE)
757 #define CSIDL_PROFILE 0x0028
759 #if !defined(CSIDL_WINDOWS)
760 #define CSIDL_WINDOWS 0x0024
763 INLINE int __hscore_CSIDL_PROFILE() { return CSIDL_PROFILE; }
764 INLINE int __hscore_CSIDL_APPDATA() { return CSIDL_APPDATA; }
765 INLINE int __hscore_CSIDL_WINDOWS() { return CSIDL_WINDOWS; }
766 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
769 #if defined(mingw32_HOST_OS)
770 INLINE unsigned int __hscore_get_osver(void) { return _osver; }
773 /* ToDo: write a feature test that doesn't assume 'environ' to
774 * be in scope at link-time. */
775 extern char** environ;
776 INLINE char **__hscore_environ() { return environ; }
778 /* lossless conversions between pointers and integral types */
779 INLINE void * __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
780 INLINE void * __hscore_from_intptr (intptr_t n) { return (void *)n; }
781 INLINE uintptr_t __hscore_to_uintptr (void *p) { return (uintptr_t)p; }
782 INLINE intptr_t __hscore_to_intptr (void *p) { return (intptr_t)p; }
784 #endif /* __HSBASE_H__ */