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_HOST_OS)
99 # include <inttypes.h>
104 #if !defined(mingw32_HOST_OS) && !defined(irix_HOST_OS)
105 # if HAVE_SYS_RESOURCE_H
106 # include <sys/resource.h>
111 #include <sys/syscall.h>
112 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
113 #define HAVE_GETRUSAGE
118 #include <sys/wait.h>
123 #include "lockFile.h"
124 #include "dirUtils.h"
127 #include "runProcess.h"
129 #if defined(mingw32_HOST_OS)
132 #include "timeUtils.h"
137 #if HAVE_SYS_SELECT_H
138 #include <sys/select.h>
141 /* in inputReady.c */
142 int inputReady(int fd, int msecs, int isSock);
145 extern HsInt nocldstop;
147 #if !defined(mingw32_HOST_OS)
149 extern int execvpe(char *name, char *const argv[], char **envp);
150 extern void pPrPr_disableITimers (void);
153 /* -----------------------------------------------------------------------------
154 64-bit operations, defined in longlong.c
155 -------------------------------------------------------------------------- */
157 #ifdef SUPPORT_LONG_LONGS
159 StgInt stg_gtWord64 (StgWord64, StgWord64);
160 StgInt stg_geWord64 (StgWord64, StgWord64);
161 StgInt stg_eqWord64 (StgWord64, StgWord64);
162 StgInt stg_neWord64 (StgWord64, StgWord64);
163 StgInt stg_ltWord64 (StgWord64, StgWord64);
164 StgInt stg_leWord64 (StgWord64, StgWord64);
166 StgInt stg_gtInt64 (StgInt64, StgInt64);
167 StgInt stg_geInt64 (StgInt64, StgInt64);
168 StgInt stg_eqInt64 (StgInt64, StgInt64);
169 StgInt stg_neInt64 (StgInt64, StgInt64);
170 StgInt stg_ltInt64 (StgInt64, StgInt64);
171 StgInt stg_leInt64 (StgInt64, StgInt64);
173 StgWord64 stg_remWord64 (StgWord64, StgWord64);
174 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
176 StgInt64 stg_remInt64 (StgInt64, StgInt64);
177 StgInt64 stg_quotInt64 (StgInt64, StgInt64);
178 StgInt64 stg_negateInt64 (StgInt64);
179 StgInt64 stg_plusInt64 (StgInt64, StgInt64);
180 StgInt64 stg_minusInt64 (StgInt64, StgInt64);
181 StgInt64 stg_timesInt64 (StgInt64, StgInt64);
183 StgWord64 stg_and64 (StgWord64, StgWord64);
184 StgWord64 stg_or64 (StgWord64, StgWord64);
185 StgWord64 stg_xor64 (StgWord64, StgWord64);
186 StgWord64 stg_not64 (StgWord64);
188 StgWord64 stg_uncheckedShiftL64 (StgWord64, StgInt);
189 StgWord64 stg_uncheckedShiftRL64 (StgWord64, StgInt);
190 StgInt64 stg_uncheckedIShiftL64 (StgInt64, StgInt);
191 StgInt64 stg_uncheckedIShiftRL64 (StgInt64, StgInt);
192 StgInt64 stg_uncheckedIShiftRA64 (StgInt64, StgInt);
194 StgInt64 stg_intToInt64 (StgInt);
195 StgInt stg_int64ToInt (StgInt64);
196 StgWord64 stg_int64ToWord64 (StgInt64);
198 StgWord64 stg_wordToWord64 (StgWord);
199 StgWord stg_word64ToWord (StgWord64);
200 StgInt64 stg_word64ToInt64 (StgWord64);
202 StgInt64 stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
203 StgWord64 stg_integerToWord64 (StgInt 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
218 # elif defined(__GNUC__)
219 # define INLINE extern inline
221 # define INLINE inline
225 INLINE int __hscore_get_errno(void) { return errno; }
226 INLINE void __hscore_set_errno(int e) { errno = e; }
228 #if !defined(_MSC_VER)
229 INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
230 INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
231 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
232 INLINE int __hscore_s_isblk(m) { return S_ISBLK(m); }
233 INLINE int __hscore_s_ischr(m) { return S_ISCHR(m); }
235 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
239 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
241 __hscore_sigemptyset( sigset_t *set )
242 { return sigemptyset(set); }
245 __hscore_sigfillset( sigset_t *set )
246 { return sigfillset(set); }
249 __hscore_sigaddset( sigset_t * set, int s )
250 { return sigaddset(set,s); }
253 __hscore_sigdelset( sigset_t * set, int s )
254 { return sigdelset(set,s); }
257 __hscore_sigismember( sigset_t * set, int s )
258 { return sigismember(set,s); }
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); }
270 __hscore_supportsTextMode()
272 #if defined(mingw32_HOST_OS)
273 return HS_BOOL_FALSE;
294 #if defined(_MSC_VER)
297 return CONST_O_BINARY;
312 __hscore_o_wronly( void )
322 __hscore_o_rdwr( void )
332 __hscore_o_append( void )
342 __hscore_o_creat( void )
352 __hscore_o_excl( void )
362 __hscore_o_trunc( void )
372 __hscore_o_noctty( void )
382 __hscore_o_nonblock( void )
392 __hscore_seek_set( void )
398 __hscore_seek_end( void )
404 __hscore_ftruncate( int fd, off_t where )
406 #if defined(HAVE_FTRUNCATE)
407 return ftruncate(fd,where);
408 #elif defined(HAVE__CHSIZE)
409 return _chsize(fd,where);
411 #error at least ftruncate or _chsize functions are required to build
416 __hscore_setmode( HsInt fd, HsBool toBin )
418 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
419 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
425 #if __GLASGOW_HASKELL__
428 __hscore_PrelHandle_write( HsInt fd, HsAddr ptr, HsInt off, int sz )
430 return write(fd,(char *)ptr + off, sz);
434 __hscore_PrelHandle_read( HsInt fd, HsAddr ptr, HsInt off, int sz )
436 return read(fd,(char *)ptr + off, sz);
440 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
442 __hscore_PrelHandle_send( HsInt fd, HsAddr ptr, HsInt off, int sz )
444 return send(fd,(char *)ptr + off, sz, 0);
448 __hscore_PrelHandle_recv( HsInt fd, HsAddr ptr, HsInt off, int sz )
450 return recv(fd,(char *)ptr + off, sz, 0);
454 #endif /* __GLASGOW_HASKELL__ */
456 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
458 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
461 __hscore_Time_ghcTzname( void ) { return _tzname; }
465 __hscore_mkdir( HsAddr pathName, HsInt mode )
467 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
468 return mkdir(pathName);
470 return mkdir(pathName,mode);
475 __hscore_lstat( HsAddr fname, HsAddr st )
478 return lstat((const char*)fname, (struct stat*)st);
480 return stat((const char*)fname, (struct stat*)st);
485 /* A size that will contain many path names, but not necessarily all
486 * (PATH_MAX is not defined on systems with unlimited path length,
489 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
491 INLINE HsInt __hscore_long_path_size() { return 4096; }
495 INLINE mode_t __hscore_R_OK() { return R_OK; }
498 INLINE mode_t __hscore_W_OK() { return W_OK; }
501 INLINE mode_t __hscore_X_OK() { return X_OK; }
505 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
508 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
511 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
515 __hscore_d_name( struct dirent* d )
517 return (HsAddr)(d->d_name);
521 __hscore_end_of_dir( void )
523 return READDIR_ERRNO_EOF;
527 __hscore_free_dirent(HsAddr dEnt)
535 __hscore_sizeof_stat( void )
537 return sizeof(struct stat);
540 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
541 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
542 #if !defined(_MSC_VER)
543 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
547 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
550 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
552 INLINE unsigned char*
553 __hscore_ptr_c_cc( struct termios* ts )
554 { return (unsigned char*) &ts->c_cc; }
557 __hscore_sizeof_termios( void )
559 #ifndef mingw32_HOST_OS
560 return sizeof(struct termios);
567 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
569 __hscore_sizeof_sigset_t( void )
571 return sizeof(sigset_t);
576 __hscore_echo( void )
587 __hscore_tcsanow( void )
598 __hscore_icanon( void )
607 INLINE int __hscore_vmin( void )
616 INLINE int __hscore_vtime( void )
625 INLINE int __hscore_sigttou( void )
634 INLINE int __hscore_sig_block( void )
643 INLINE int __hscore_sig_setmask( void )
653 __hscore_f_getfl( void )
663 __hscore_f_setfl( void )
672 // defined in rts/RtsStartup.c.
673 extern void* __hscore_get_saved_termios(int fd);
674 extern void __hscore_set_saved_termios(int fd, void* ts);
676 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
678 INLINE int __hscore_open(char *file, int how, mode_t mode) {
679 #ifdef mingw32_HOST_OS
680 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
681 return _sopen(file,how,_SH_DENYRW,mode);
683 return _sopen(file,how,_SH_DENYWR,mode);
685 return open(file,how,mode);
689 // These are wrapped because on some OSs (eg. Linux) they are
690 // macros which redirect to the 64-bit-off_t versions when large file
691 // support is enabled.
693 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
694 return (lseek(fd,off,whence));
697 INLINE int __hscore_stat(char *file, struct stat *buf) {
698 return (stat(file,buf));
701 INLINE int __hscore_fstat(int fd, struct stat *buf) {
702 return (fstat(fd,buf));
705 // select-related stuff
707 #if !defined(mingw32_HOST_OS)
708 INLINE int hsFD_SETSIZE(void) { return FD_SETSIZE; }
709 INLINE void hsFD_CLR(int fd, fd_set *fds) { FD_CLR(fd, fds); }
710 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
711 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
712 INLINE int sizeof_fd_set(void) { return sizeof(fd_set); }
713 extern void hsFD_ZERO(fd_set *fds);
716 // gettimeofday()-related
718 #if !defined(mingw32_HOST_OS)
721 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
723 INLINE HsInt getTicksOfDay(void)
726 gettimeofday(&tv, (struct timezone *) NULL);
727 return (tv.tv_sec * TICK_FREQ +
728 tv.tv_usec * TICK_FREQ / 1000000);
731 INLINE void setTimevalTicks(struct timeval *p, HsInt ticks)
733 p->tv_sec = ticks / TICK_FREQ;
734 p->tv_usec = (ticks % TICK_FREQ) * (1000000 / TICK_FREQ);
736 #endif /* !defined(mingw32_HOST_OS) */
740 #if defined(mingw32_HOST_OS)
742 /* Make sure we've got the reqd CSIDL_ constants in scope;
743 * w32api header files are lagging a bit in defining the full set.
745 #if !defined(CSIDL_APPDATA)
746 #define CSIDL_APPDATA 0x001a
748 #if !defined(CSIDL_PERSONAL)
749 #define CSIDL_PERSONAL 0x0005
751 #if !defined(CSIDL_PROFILE)
752 #define CSIDL_PROFILE 0x0028
754 #if !defined(CSIDL_WINDOWS)
755 #define CSIDL_WINDOWS 0x0024
758 INLINE int __hscore_CSIDL_PROFILE() { return CSIDL_PROFILE; }
759 INLINE int __hscore_CSIDL_APPDATA() { return CSIDL_APPDATA; }
760 INLINE int __hscore_CSIDL_WINDOWS() { return CSIDL_WINDOWS; }
761 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
764 #if defined(mingw32_HOST_OS)
765 INLINE unsigned int __hscore_get_osver(void) { return _osver; }
768 /* ToDo: write a feature test that doesn't assume 'environ' to
769 * be in scope at link-time. */
770 extern char** environ;
771 INLINE char **__hscore_environ() { return environ; }
773 /* lossless conversions between pointers and integral types */
774 INLINE void * __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
775 INLINE void * __hscore_from_intptr (intptr_t n) { return (void *)n; }
776 INLINE uintptr_t __hscore_to_uintptr (void *p) { return (uintptr_t)p; }
777 INLINE intptr_t __hscore_to_intptr (void *p) { return (intptr_t)p; }
779 #endif /* __HSBASE_H__ */