1 /* -----------------------------------------------------------------------------
3 * (c) The University of Glasgow 2001-2004
5 * Definitions for package `base' which are visible in Haskell land.
7 * ---------------------------------------------------------------------------*/
12 #include "HsBaseConfig.h"
15 #undef PACKAGE_BUGREPORT
18 #undef PACKAGE_TARNAME
19 #undef PACKAGE_VERSION
21 /* Needed to get the macro version of errno on some OSs (eg. Solaris).
22 We must do this, because these libs are only compiled once, but
23 must work in both single-threaded and multi-threaded programs. */
33 #include <sys/types.h>
49 /* Ultra-ugly: OpenBSD uses broken macros for sigemptyset and sigfillset (missing casts) */
67 #if HAVE_SYS_UTSNAME_H
68 #include <sys/utsname.h>
72 # include <sys/time.h>
75 # if HAVE_SYS_TIMERS_H
77 # include <sys/timers.h>
84 #include <sys/timeb.h>
90 #include <sys/times.h>
92 #if HAVE_WINSOCK_H && defined(__MINGW32__)
102 # include <inttypes.h>
107 #if !defined(__MINGW32__) && !defined(irix_HOST_OS)
108 # if HAVE_SYS_RESOURCE_H
109 # include <sys/resource.h>
113 #if !HAVE_GETRUSAGE && HAVE_SYS_SYSCALL_H
114 # include <sys/syscall.h>
115 # if defined(SYS_GETRUSAGE) /* hpux_HOST_OS */
116 # define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
117 # define HAVE_GETRUSAGE 1
123 #include <sys/wait.h>
128 #include "lockFile.h"
129 #include "dirUtils.h"
132 #include "runProcess.h"
134 #if defined(__MINGW32__)
135 /* in Win32Utils.c */
136 extern void maperrno (void);
137 extern HsInt getTicksOfDay(void);
140 #if defined(__MINGW32__)
143 #include "timeUtils.h"
148 #if HAVE_SYS_SELECT_H
149 #include <sys/select.h>
152 /* in inputReady.c */
153 int inputReady(int fd, int msecs, int isSock);
156 extern HsInt nocldstop;
158 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
160 extern int execvpe(char *name, char *const argv[], char **envp);
161 extern void pPrPr_disableITimers (void);
164 /* -----------------------------------------------------------------------------
165 64-bit operations, defined in longlong.c
166 -------------------------------------------------------------------------- */
168 #ifdef SUPPORT_LONG_LONGS
170 StgInt stg_gtWord64 (StgWord64, StgWord64);
171 StgInt stg_geWord64 (StgWord64, StgWord64);
172 StgInt stg_eqWord64 (StgWord64, StgWord64);
173 StgInt stg_neWord64 (StgWord64, StgWord64);
174 StgInt stg_ltWord64 (StgWord64, StgWord64);
175 StgInt stg_leWord64 (StgWord64, StgWord64);
177 StgInt stg_gtInt64 (StgInt64, StgInt64);
178 StgInt stg_geInt64 (StgInt64, StgInt64);
179 StgInt stg_eqInt64 (StgInt64, StgInt64);
180 StgInt stg_neInt64 (StgInt64, StgInt64);
181 StgInt stg_ltInt64 (StgInt64, StgInt64);
182 StgInt stg_leInt64 (StgInt64, StgInt64);
184 StgWord64 stg_remWord64 (StgWord64, StgWord64);
185 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
187 StgInt64 stg_remInt64 (StgInt64, StgInt64);
188 StgInt64 stg_quotInt64 (StgInt64, StgInt64);
189 StgInt64 stg_negateInt64 (StgInt64);
190 StgInt64 stg_plusInt64 (StgInt64, StgInt64);
191 StgInt64 stg_minusInt64 (StgInt64, StgInt64);
192 StgInt64 stg_timesInt64 (StgInt64, StgInt64);
194 StgWord64 stg_and64 (StgWord64, StgWord64);
195 StgWord64 stg_or64 (StgWord64, StgWord64);
196 StgWord64 stg_xor64 (StgWord64, StgWord64);
197 StgWord64 stg_not64 (StgWord64);
199 StgWord64 stg_uncheckedShiftL64 (StgWord64, StgInt);
200 StgWord64 stg_uncheckedShiftRL64 (StgWord64, StgInt);
201 StgInt64 stg_uncheckedIShiftL64 (StgInt64, StgInt);
202 StgInt64 stg_uncheckedIShiftRL64 (StgInt64, StgInt);
203 StgInt64 stg_uncheckedIShiftRA64 (StgInt64, StgInt);
205 StgInt64 stg_intToInt64 (StgInt);
206 StgInt stg_int64ToInt (StgInt64);
207 StgWord64 stg_int64ToWord64 (StgInt64);
209 StgWord64 stg_wordToWord64 (StgWord);
210 StgWord stg_word64ToWord (StgWord64);
211 StgInt64 stg_word64ToInt64 (StgWord64);
213 StgInt64 stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
214 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
216 #endif /* SUPPORT_LONG_LONGS */
218 /* -----------------------------------------------------------------------------
221 These functions are given as inlines here for when compiling via C,
222 but we also generate static versions into the cbits library for
223 when compiling to native code.
224 -------------------------------------------------------------------------- */
227 # if defined(_MSC_VER)
228 # define INLINE extern __inline
229 # elif defined(__GNUC__)
230 # define INLINE extern inline
232 # define INLINE inline
236 INLINE int __hscore_get_errno(void) { return errno; }
237 INLINE void __hscore_set_errno(int e) { errno = e; }
239 #if !defined(_MSC_VER)
240 INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
241 INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
242 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
243 INLINE int __hscore_s_isblk(m) { return S_ISBLK(m); }
244 INLINE int __hscore_s_ischr(m) { return S_ISCHR(m); }
246 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
250 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
252 __hscore_sigemptyset( sigset_t *set )
253 { return sigemptyset(set); }
256 __hscore_sigfillset( sigset_t *set )
257 { return sigfillset(set); }
260 __hscore_sigaddset( sigset_t * set, int s )
261 { return sigaddset(set,s); }
264 __hscore_sigdelset( sigset_t * set, int s )
265 { return sigdelset(set,s); }
268 __hscore_sigismember( sigset_t * set, int s )
269 { return sigismember(set,s); }
273 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
274 { return memcpy(dst+dst_off, src, sz); }
277 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
278 { return memcpy(dst, src+src_off, sz); }
281 __hscore_supportsTextMode()
283 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
284 return HS_BOOL_FALSE;
305 #if defined(_MSC_VER)
308 return CONST_O_BINARY;
323 __hscore_o_wronly( void )
333 __hscore_o_rdwr( void )
343 __hscore_o_append( void )
353 __hscore_o_creat( void )
363 __hscore_o_excl( void )
373 __hscore_o_trunc( void )
383 __hscore_o_noctty( void )
393 __hscore_o_nonblock( void )
403 __hscore_seek_set( void )
409 __hscore_seek_end( void )
415 __hscore_ftruncate( int fd, off_t where )
417 #if defined(HAVE_FTRUNCATE)
418 return ftruncate(fd,where);
419 #elif defined(HAVE__CHSIZE)
420 return _chsize(fd,where);
422 #error at least ftruncate or _chsize functions are required to build
427 __hscore_setmode( HsInt fd, HsBool toBin )
429 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
430 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
436 #if __GLASGOW_HASKELL__
439 __hscore_PrelHandle_write( HsInt fd, HsAddr ptr, HsInt off, int sz )
441 return write(fd,(char *)ptr + off, sz);
445 __hscore_PrelHandle_read( HsInt fd, HsAddr ptr, HsInt off, int sz )
447 return read(fd,(char *)ptr + off, sz);
451 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
453 __hscore_PrelHandle_send( HsInt fd, HsAddr ptr, HsInt off, int sz )
455 return send(fd,(char *)ptr + off, sz, 0);
459 __hscore_PrelHandle_recv( HsInt fd, HsAddr ptr, HsInt off, int sz )
461 return recv(fd,(char *)ptr + off, sz, 0);
465 #endif /* __GLASGOW_HASKELL__ */
468 __hscore_mkdir( HsAddr pathName, HsInt mode )
470 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
471 return mkdir(pathName);
473 return mkdir(pathName,mode);
478 __hscore_lstat( HsAddr fname, HsAddr st )
481 return lstat((const char*)fname, (struct stat*)st);
483 return stat((const char*)fname, (struct stat*)st);
488 /* A size that will contain many path names, but not necessarily all
489 * (PATH_MAX is not defined on systems with unlimited path length,
492 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
494 INLINE HsInt __hscore_long_path_size() { return 4096; }
498 INLINE mode_t __hscore_R_OK() { return R_OK; }
501 INLINE mode_t __hscore_W_OK() { return W_OK; }
504 INLINE mode_t __hscore_X_OK() { return X_OK; }
508 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
511 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
514 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
518 __hscore_d_name( struct dirent* d )
520 return (HsAddr)(d->d_name);
524 __hscore_end_of_dir( void )
526 return READDIR_ERRNO_EOF;
530 __hscore_free_dirent(HsAddr dEnt)
538 __hscore_sizeof_stat( void )
540 return sizeof(struct stat);
543 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
544 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
545 #if !defined(_MSC_VER)
546 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
550 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
553 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
555 INLINE unsigned char*
556 __hscore_ptr_c_cc( struct termios* ts )
557 { return (unsigned char*) &ts->c_cc; }
560 __hscore_sizeof_termios( void )
563 return sizeof(struct termios);
570 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
572 __hscore_sizeof_sigset_t( void )
574 return sizeof(sigset_t);
579 __hscore_echo( void )
590 __hscore_tcsanow( void )
601 __hscore_icanon( void )
610 INLINE int __hscore_vmin( void )
619 INLINE int __hscore_vtime( void )
628 INLINE int __hscore_sigttou( void )
637 INLINE int __hscore_sig_block( void )
646 INLINE int __hscore_sig_setmask( void )
656 __hscore_f_getfl( void )
666 __hscore_f_setfl( void )
675 // defined in rts/RtsStartup.c.
676 extern void* __hscore_get_saved_termios(int fd);
677 extern void __hscore_set_saved_termios(int fd, void* ts);
679 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
681 INLINE int __hscore_open(char *file, int how, mode_t mode) {
683 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
684 return _sopen(file,how,_SH_DENYRW,mode);
686 return _sopen(file,how,_SH_DENYWR,mode);
688 return open(file,how,mode);
692 // These are wrapped because on some OSs (eg. Linux) they are
693 // macros which redirect to the 64-bit-off_t versions when large file
694 // support is enabled.
696 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
697 return (lseek(fd,off,whence));
700 INLINE int __hscore_stat(char *file, struct stat *buf) {
701 return (stat(file,buf));
704 INLINE int __hscore_fstat(int fd, struct stat *buf) {
705 return (fstat(fd,buf));
708 // select-related stuff
710 #if !defined(__MINGW32__)
711 INLINE int hsFD_SETSIZE(void) { return FD_SETSIZE; }
712 INLINE void hsFD_CLR(int fd, fd_set *fds) { FD_CLR(fd, fds); }
713 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
714 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
715 INLINE int sizeof_fd_set(void) { return sizeof(fd_set); }
716 extern void hsFD_ZERO(fd_set *fds);
719 // gettimeofday()-related
721 #if !defined(__MINGW32__)
724 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
726 INLINE HsInt getTicksOfDay(void)
729 gettimeofday(&tv, (struct timezone *) NULL);
730 return (tv.tv_sec * TICK_FREQ +
731 tv.tv_usec * TICK_FREQ / 1000000);
734 INLINE void setTimevalTicks(struct timeval *p, HsInt ticks)
736 p->tv_sec = ticks / TICK_FREQ;
737 p->tv_usec = (ticks % TICK_FREQ) * (1000000 / TICK_FREQ);
739 #endif /* !defined(__MINGW32__) */
743 #if defined(__MINGW32__)
745 /* Make sure we've got the reqd CSIDL_ constants in scope;
746 * w32api header files are lagging a bit in defining the full set.
748 #if !defined(CSIDL_APPDATA)
749 #define CSIDL_APPDATA 0x001a
751 #if !defined(CSIDL_PERSONAL)
752 #define CSIDL_PERSONAL 0x0005
754 #if !defined(CSIDL_PROFILE)
755 #define CSIDL_PROFILE 0x0028
757 #if !defined(CSIDL_WINDOWS)
758 #define CSIDL_WINDOWS 0x0024
761 INLINE int __hscore_CSIDL_PROFILE() { return CSIDL_PROFILE; }
762 INLINE int __hscore_CSIDL_APPDATA() { return CSIDL_APPDATA; }
763 INLINE int __hscore_CSIDL_WINDOWS() { return CSIDL_WINDOWS; }
764 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
767 #if defined(__MINGW32__)
768 INLINE unsigned int __hscore_get_osver(void) { return _osver; }
771 /* ToDo: write a feature test that doesn't assume 'environ' to
772 * be in scope at link-time. */
773 extern char** environ;
774 INLINE char **__hscore_environ() { return environ; }
776 /* lossless conversions between pointers and integral types */
777 INLINE void * __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
778 INLINE void * __hscore_from_intptr (intptr_t n) { return (void *)n; }
779 INLINE uintptr_t __hscore_to_uintptr (void *p) { return (uintptr_t)p; }
780 INLINE intptr_t __hscore_to_intptr (void *p) { return (intptr_t)p; }
782 #endif /* __HSBASE_H__ */