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 HsWord64 getUSecOfDay(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 HsBool hs_gtWord64 (HsWord64, HsWord64);
171 HsBool hs_geWord64 (HsWord64, HsWord64);
172 HsBool hs_eqWord64 (HsWord64, HsWord64);
173 HsBool hs_neWord64 (HsWord64, HsWord64);
174 HsBool hs_ltWord64 (HsWord64, HsWord64);
175 HsBool hs_leWord64 (HsWord64, HsWord64);
177 HsBool hs_gtInt64 (HsInt64, HsInt64);
178 HsBool hs_geInt64 (HsInt64, HsInt64);
179 HsBool hs_eqInt64 (HsInt64, HsInt64);
180 HsBool hs_neInt64 (HsInt64, HsInt64);
181 HsBool hs_ltInt64 (HsInt64, HsInt64);
182 HsBool hs_leInt64 (HsInt64, HsInt64);
184 HsWord64 hs_remWord64 (HsWord64, HsWord64);
185 HsWord64 hs_quotWord64 (HsWord64, HsWord64);
187 HsInt64 hs_remInt64 (HsInt64, HsInt64);
188 HsInt64 hs_quotInt64 (HsInt64, HsInt64);
189 HsInt64 hs_negateInt64 (HsInt64);
190 HsInt64 hs_plusInt64 (HsInt64, HsInt64);
191 HsInt64 hs_minusInt64 (HsInt64, HsInt64);
192 HsInt64 hs_timesInt64 (HsInt64, HsInt64);
194 HsWord64 hs_and64 (HsWord64, HsWord64);
195 HsWord64 hs_or64 (HsWord64, HsWord64);
196 HsWord64 hs_xor64 (HsWord64, HsWord64);
197 HsWord64 hs_not64 (HsWord64);
199 HsWord64 hs_uncheckedShiftL64 (HsWord64, HsInt);
200 HsWord64 hs_uncheckedShiftRL64 (HsWord64, HsInt);
201 HsInt64 hs_uncheckedIShiftL64 (HsInt64, HsInt);
202 HsInt64 hs_uncheckedIShiftRA64 (HsInt64, HsInt);
203 HsInt64 hs_uncheckedIShiftRL64 (HsInt64, HsInt);
205 HsInt64 hs_intToInt64 (HsInt);
206 HsInt hs_int64ToInt (HsInt64);
207 HsWord64 hs_int64ToWord64 (HsInt64);
208 HsWord64 hs_wordToWord64 (HsWord);
209 HsWord hs_word64ToWord (HsWord64);
210 HsInt64 hs_word64ToInt64 (HsWord64);
212 HsWord64 hs_integerToWord64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
213 HsInt64 hs_integerToInt64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
215 #endif /* SUPPORT_LONG_LONGS */
217 /* -----------------------------------------------------------------------------
220 These functions are given as inlines here for when compiling via C,
221 but we also generate static versions into the cbits library for
222 when compiling to native code.
223 -------------------------------------------------------------------------- */
226 # if defined(_MSC_VER)
227 # define INLINE extern __inline
229 # define INLINE static inline
233 INLINE int __hscore_get_errno(void) { return errno; }
234 INLINE void __hscore_set_errno(int e) { errno = e; }
236 #if !defined(_MSC_VER)
237 INLINE int __hscore_s_isreg(mode_t m) { return S_ISREG(m); }
238 INLINE int __hscore_s_isdir(mode_t m) { return S_ISDIR(m); }
239 INLINE int __hscore_s_isfifo(mode_t m) { return S_ISFIFO(m); }
240 INLINE int __hscore_s_isblk(mode_t m) { return S_ISBLK(m); }
241 INLINE int __hscore_s_ischr(mode_t m) { return S_ISCHR(m); }
243 INLINE int __hscore_s_issock(mode_t m) { return S_ISSOCK(m); }
247 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
249 __hscore_sigemptyset( sigset_t *set )
250 { return sigemptyset(set); }
253 __hscore_sigfillset( sigset_t *set )
254 { return sigfillset(set); }
257 __hscore_sigaddset( sigset_t * set, int s )
258 { return sigaddset(set,s); }
261 __hscore_sigdelset( sigset_t * set, int s )
262 { return sigdelset(set,s); }
265 __hscore_sigismember( sigset_t * set, int s )
266 { return sigismember(set,s); }
270 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
271 { return memcpy(dst+dst_off, src, sz); }
274 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
275 { return memcpy(dst, src+src_off, sz); }
278 __hscore_supportsTextMode()
280 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
281 return HS_BOOL_FALSE;
302 #if defined(_MSC_VER)
305 return CONST_O_BINARY;
320 __hscore_o_wronly( void )
330 __hscore_o_rdwr( void )
340 __hscore_o_append( void )
350 __hscore_o_creat( void )
360 __hscore_o_excl( void )
370 __hscore_o_trunc( void )
380 __hscore_o_noctty( void )
390 __hscore_o_nonblock( void )
400 __hscore_seek_set( void )
406 __hscore_seek_end( void )
412 __hscore_ftruncate( int fd, off_t where )
414 #if defined(HAVE_FTRUNCATE)
415 return ftruncate(fd,where);
416 #elif defined(HAVE__CHSIZE)
417 return _chsize(fd,where);
419 #error at least ftruncate or _chsize functions are required to build
424 __hscore_setmode( int fd, HsBool toBin )
426 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
427 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
433 #if __GLASGOW_HASKELL__
436 __hscore_PrelHandle_write( int fd, void *ptr, HsInt off, int sz )
438 return write(fd,(char *)ptr + off, sz);
442 __hscore_PrelHandle_read( int fd, void *ptr, HsInt off, int sz )
444 return read(fd,(char *)ptr + off, sz);
448 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
450 __hscore_PrelHandle_send( int fd, void *ptr, HsInt off, int sz )
452 return send(fd,(char *)ptr + off, sz, 0);
456 __hscore_PrelHandle_recv( int fd, void *ptr, HsInt off, int sz )
458 return recv(fd,(char *)ptr + off, sz, 0);
462 #endif /* __GLASGOW_HASKELL__ */
465 __hscore_mkdir( char *pathName, int mode )
467 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
468 return mkdir(pathName);
470 return mkdir(pathName,mode);
475 __hscore_lstat( const char *fname, struct stat *st )
478 return lstat(fname, st);
480 return stat(fname, 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 int __hscore_R_OK() { return R_OK; }
498 INLINE int __hscore_W_OK() { return W_OK; }
501 INLINE int __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 )
521 __hscore_end_of_dir( void )
523 return READDIR_ERRNO_EOF;
527 __hscore_free_dirent(struct dirent *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 )
560 return sizeof(struct termios);
567 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
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) {
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__)
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 HsInt sizeof_fd_set(void) { return sizeof(fd_set); }
713 extern void hsFD_ZERO(fd_set *fds);
716 // gettimeofday()-related
718 #if !defined(__MINGW32__)
720 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
722 INLINE HsWord64 getUSecOfDay(void)
725 gettimeofday(&tv, (struct timezone *) NULL);
726 // Don't forget to cast *before* doing the arithmetic, otherwise
727 // the arithmetic happens at the type of tv_sec, which is probably
729 return ((HsWord64)tv.tv_sec * 1000000 + (HsWord64)tv.tv_usec);
732 INLINE void setTimevalTicks(struct timeval *p, HsWord64 usecs)
734 p->tv_sec = usecs / 1000000;
735 p->tv_usec = usecs % 1000000;
737 #endif /* !defined(__MINGW32__) */
741 #if defined(__MINGW32__)
743 /* Make sure we've got the reqd CSIDL_ constants in scope;
744 * w32api header files are lagging a bit in defining the full set.
746 #if !defined(CSIDL_APPDATA)
747 #define CSIDL_APPDATA 0x001a
749 #if !defined(CSIDL_PERSONAL)
750 #define CSIDL_PERSONAL 0x0005
752 #if !defined(CSIDL_PROFILE)
753 #define CSIDL_PROFILE 0x0028
755 #if !defined(CSIDL_WINDOWS)
756 #define CSIDL_WINDOWS 0x0024
759 INLINE int __hscore_CSIDL_PROFILE() { return CSIDL_PROFILE; }
760 INLINE int __hscore_CSIDL_APPDATA() { return CSIDL_APPDATA; }
761 INLINE int __hscore_CSIDL_WINDOWS() { return CSIDL_WINDOWS; }
762 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
765 #if defined(__MINGW32__)
766 INLINE unsigned int __hscore_get_osver(void) { return _osver; }
769 /* ToDo: write a feature test that doesn't assume 'environ' to
770 * be in scope at link-time. */
771 extern char** environ;
772 INLINE char **__hscore_environ() { return environ; }
774 /* lossless conversions between pointers and integral types */
775 INLINE void * __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
776 INLINE void * __hscore_from_intptr (intptr_t n) { return (void *)n; }
777 INLINE uintptr_t __hscore_to_uintptr (void *p) { return (uintptr_t)p; }
778 INLINE intptr_t __hscore_to_intptr (void *p) { return (intptr_t)p; }
780 #endif /* __HSBASE_H__ */