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__ */
462 __hscore_mkdir( HsAddr pathName, HsInt mode )
464 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
465 return mkdir(pathName);
467 return mkdir(pathName,mode);
472 __hscore_lstat( HsAddr fname, HsAddr st )
475 return lstat((const char*)fname, (struct stat*)st);
477 return stat((const char*)fname, (struct stat*)st);
482 /* A size that will contain many path names, but not necessarily all
483 * (PATH_MAX is not defined on systems with unlimited path length,
486 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
488 INLINE HsInt __hscore_long_path_size() { return 4096; }
492 INLINE mode_t __hscore_R_OK() { return R_OK; }
495 INLINE mode_t __hscore_W_OK() { return W_OK; }
498 INLINE mode_t __hscore_X_OK() { return X_OK; }
502 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
505 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
508 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
512 __hscore_d_name( struct dirent* d )
514 return (HsAddr)(d->d_name);
518 __hscore_end_of_dir( void )
520 return READDIR_ERRNO_EOF;
524 __hscore_free_dirent(HsAddr dEnt)
532 __hscore_sizeof_stat( void )
534 return sizeof(struct stat);
537 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
538 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
539 #if !defined(_MSC_VER)
540 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
544 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
547 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
549 INLINE unsigned char*
550 __hscore_ptr_c_cc( struct termios* ts )
551 { return (unsigned char*) &ts->c_cc; }
554 __hscore_sizeof_termios( void )
556 #ifndef mingw32_HOST_OS
557 return sizeof(struct termios);
564 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
566 __hscore_sizeof_sigset_t( void )
568 return sizeof(sigset_t);
573 __hscore_echo( void )
584 __hscore_tcsanow( void )
595 __hscore_icanon( void )
604 INLINE int __hscore_vmin( void )
613 INLINE int __hscore_vtime( void )
622 INLINE int __hscore_sigttou( void )
631 INLINE int __hscore_sig_block( void )
640 INLINE int __hscore_sig_setmask( void )
650 __hscore_f_getfl( void )
660 __hscore_f_setfl( void )
669 // defined in rts/RtsStartup.c.
670 extern void* __hscore_get_saved_termios(int fd);
671 extern void __hscore_set_saved_termios(int fd, void* ts);
673 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
675 INLINE int __hscore_open(char *file, int how, mode_t mode) {
676 #ifdef mingw32_HOST_OS
677 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
678 return _sopen(file,how,_SH_DENYRW,mode);
680 return _sopen(file,how,_SH_DENYWR,mode);
682 return open(file,how,mode);
686 // These are wrapped because on some OSs (eg. Linux) they are
687 // macros which redirect to the 64-bit-off_t versions when large file
688 // support is enabled.
690 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
691 return (lseek(fd,off,whence));
694 INLINE int __hscore_stat(char *file, struct stat *buf) {
695 return (stat(file,buf));
698 INLINE int __hscore_fstat(int fd, struct stat *buf) {
699 return (fstat(fd,buf));
702 // select-related stuff
704 #if !defined(mingw32_HOST_OS)
705 INLINE int hsFD_SETSIZE(void) { return FD_SETSIZE; }
706 INLINE void hsFD_CLR(int fd, fd_set *fds) { FD_CLR(fd, fds); }
707 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
708 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
709 INLINE int sizeof_fd_set(void) { return sizeof(fd_set); }
710 extern void hsFD_ZERO(fd_set *fds);
713 // gettimeofday()-related
715 #if !defined(mingw32_HOST_OS)
718 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
720 INLINE HsInt getTicksOfDay(void)
723 gettimeofday(&tv, (struct timezone *) NULL);
724 return (tv.tv_sec * TICK_FREQ +
725 tv.tv_usec * TICK_FREQ / 1000000);
728 INLINE void setTimevalTicks(struct timeval *p, HsInt ticks)
730 p->tv_sec = ticks / TICK_FREQ;
731 p->tv_usec = (ticks % TICK_FREQ) * (1000000 / TICK_FREQ);
733 #endif /* !defined(mingw32_HOST_OS) */
737 #if defined(mingw32_HOST_OS)
739 /* Make sure we've got the reqd CSIDL_ constants in scope;
740 * w32api header files are lagging a bit in defining the full set.
742 #if !defined(CSIDL_APPDATA)
743 #define CSIDL_APPDATA 0x001a
745 #if !defined(CSIDL_PERSONAL)
746 #define CSIDL_PERSONAL 0x0005
748 #if !defined(CSIDL_PROFILE)
749 #define CSIDL_PROFILE 0x0028
751 #if !defined(CSIDL_WINDOWS)
752 #define CSIDL_WINDOWS 0x0024
755 INLINE int __hscore_CSIDL_PROFILE() { return CSIDL_PROFILE; }
756 INLINE int __hscore_CSIDL_APPDATA() { return CSIDL_APPDATA; }
757 INLINE int __hscore_CSIDL_WINDOWS() { return CSIDL_WINDOWS; }
758 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
761 #if defined(mingw32_HOST_OS)
762 INLINE unsigned int __hscore_get_osver(void) { return _osver; }
765 /* ToDo: write a feature test that doesn't assume 'environ' to
766 * be in scope at link-time. */
767 extern char** environ;
768 INLINE char **__hscore_environ() { return environ; }
770 /* lossless conversions between pointers and integral types */
771 INLINE void * __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
772 INLINE void * __hscore_from_intptr (intptr_t n) { return (void *)n; }
773 INLINE uintptr_t __hscore_to_uintptr (void *p) { return (uintptr_t)p; }
774 INLINE intptr_t __hscore_to_intptr (void *p) { return (intptr_t)p; }
776 #endif /* __HSBASE_H__ */