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__)
147 #if HAVE_SYS_SELECT_H
148 #include <sys/select.h>
151 /* in inputReady.c */
152 int inputReady(int fd, int msecs, int isSock);
155 extern HsInt nocldstop;
157 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
159 extern int execvpe(char *name, char *const argv[], char **envp);
160 extern void pPrPr_disableITimers (void);
163 /* -----------------------------------------------------------------------------
164 64-bit operations, defined in longlong.c
165 -------------------------------------------------------------------------- */
167 #ifdef SUPPORT_LONG_LONGS
169 HsBool hs_gtWord64 (HsWord64, HsWord64);
170 HsBool hs_geWord64 (HsWord64, HsWord64);
171 HsBool hs_eqWord64 (HsWord64, HsWord64);
172 HsBool hs_neWord64 (HsWord64, HsWord64);
173 HsBool hs_ltWord64 (HsWord64, HsWord64);
174 HsBool hs_leWord64 (HsWord64, HsWord64);
176 HsBool hs_gtInt64 (HsInt64, HsInt64);
177 HsBool hs_geInt64 (HsInt64, HsInt64);
178 HsBool hs_eqInt64 (HsInt64, HsInt64);
179 HsBool hs_neInt64 (HsInt64, HsInt64);
180 HsBool hs_ltInt64 (HsInt64, HsInt64);
181 HsBool hs_leInt64 (HsInt64, HsInt64);
183 HsWord64 hs_remWord64 (HsWord64, HsWord64);
184 HsWord64 hs_quotWord64 (HsWord64, HsWord64);
186 HsInt64 hs_remInt64 (HsInt64, HsInt64);
187 HsInt64 hs_quotInt64 (HsInt64, HsInt64);
188 HsInt64 hs_negateInt64 (HsInt64);
189 HsInt64 hs_plusInt64 (HsInt64, HsInt64);
190 HsInt64 hs_minusInt64 (HsInt64, HsInt64);
191 HsInt64 hs_timesInt64 (HsInt64, HsInt64);
193 HsWord64 hs_and64 (HsWord64, HsWord64);
194 HsWord64 hs_or64 (HsWord64, HsWord64);
195 HsWord64 hs_xor64 (HsWord64, HsWord64);
196 HsWord64 hs_not64 (HsWord64);
198 HsWord64 hs_uncheckedShiftL64 (HsWord64, HsInt);
199 HsWord64 hs_uncheckedShiftRL64 (HsWord64, HsInt);
200 HsInt64 hs_uncheckedIShiftL64 (HsInt64, HsInt);
201 HsInt64 hs_uncheckedIShiftRA64 (HsInt64, HsInt);
202 HsInt64 hs_uncheckedIShiftRL64 (HsInt64, HsInt);
204 HsInt64 hs_intToInt64 (HsInt);
205 HsInt hs_int64ToInt (HsInt64);
206 HsWord64 hs_int64ToWord64 (HsInt64);
207 HsWord64 hs_wordToWord64 (HsWord);
208 HsWord hs_word64ToWord (HsWord64);
209 HsInt64 hs_word64ToInt64 (HsWord64);
211 HsWord64 hs_integerToWord64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
212 HsInt64 hs_integerToInt64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
214 #endif /* SUPPORT_LONG_LONGS */
216 /* -----------------------------------------------------------------------------
219 These functions are given as inlines here for when compiling via C,
220 but we also generate static versions into the cbits library for
221 when compiling to native code.
222 -------------------------------------------------------------------------- */
225 # if defined(_MSC_VER)
226 # define INLINE extern __inline
228 # define INLINE static inline
232 INLINE int __hscore_get_errno(void) { return errno; }
233 INLINE void __hscore_set_errno(int e) { errno = e; }
235 #if !defined(_MSC_VER)
236 INLINE int __hscore_s_isreg(mode_t m) { return S_ISREG(m); }
237 INLINE int __hscore_s_isdir(mode_t m) { return S_ISDIR(m); }
238 INLINE int __hscore_s_isfifo(mode_t m) { return S_ISFIFO(m); }
239 INLINE int __hscore_s_isblk(mode_t m) { return S_ISBLK(m); }
240 INLINE int __hscore_s_ischr(mode_t m) { return S_ISCHR(m); }
242 INLINE int __hscore_s_issock(mode_t m) { return S_ISSOCK(m); }
246 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
248 __hscore_sigemptyset( sigset_t *set )
249 { return sigemptyset(set); }
252 __hscore_sigfillset( sigset_t *set )
253 { return sigfillset(set); }
256 __hscore_sigaddset( sigset_t * set, int s )
257 { return sigaddset(set,s); }
260 __hscore_sigdelset( sigset_t * set, int s )
261 { return sigdelset(set,s); }
264 __hscore_sigismember( sigset_t * set, int s )
265 { return sigismember(set,s); }
269 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
270 { return memcpy(dst+dst_off, src, sz); }
273 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
274 { return memcpy(dst, src+src_off, sz); }
277 __hscore_supportsTextMode()
279 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
280 return HS_BOOL_FALSE;
301 #if defined(_MSC_VER)
304 return CONST_O_BINARY;
319 __hscore_o_wronly( void )
329 __hscore_o_rdwr( void )
339 __hscore_o_append( void )
349 __hscore_o_creat( void )
359 __hscore_o_excl( void )
369 __hscore_o_trunc( void )
379 __hscore_o_noctty( void )
389 __hscore_o_nonblock( void )
399 __hscore_seek_set( void )
405 __hscore_seek_end( void )
411 __hscore_ftruncate( int fd, off_t where )
413 #if defined(HAVE_FTRUNCATE)
414 return ftruncate(fd,where);
415 #elif defined(HAVE__CHSIZE)
416 return _chsize(fd,where);
418 #error at least ftruncate or _chsize functions are required to build
423 __hscore_setmode( int fd, HsBool toBin )
425 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
426 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
432 #if __GLASGOW_HASKELL__
435 __hscore_PrelHandle_write( int fd, void *ptr, HsInt off, int sz )
437 return write(fd,(char *)ptr + off, sz);
441 __hscore_PrelHandle_read( int fd, void *ptr, HsInt off, int sz )
443 return read(fd,(char *)ptr + off, sz);
447 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
449 __hscore_PrelHandle_send( int fd, void *ptr, HsInt off, int sz )
451 return send(fd,(char *)ptr + off, sz, 0);
455 __hscore_PrelHandle_recv( int fd, void *ptr, HsInt off, int sz )
457 return recv(fd,(char *)ptr + off, sz, 0);
461 #endif /* __GLASGOW_HASKELL__ */
464 __hscore_mkdir( char *pathName, int mode )
466 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
467 return mkdir(pathName);
469 return mkdir(pathName,mode);
474 __hscore_lstat( const char *fname, struct stat *st )
477 return lstat(fname, st);
479 return stat(fname, st);
484 __hscore_d_name( struct dirent* d )
490 __hscore_end_of_dir( void )
492 return READDIR_ERRNO_EOF;
496 __hscore_free_dirent(struct dirent *dEnt)
504 __hscore_sizeof_stat( void )
506 return sizeof(struct stat);
509 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
510 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
511 #if !defined(_MSC_VER)
512 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
516 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
519 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
521 INLINE unsigned char*
522 __hscore_ptr_c_cc( struct termios* ts )
523 { return (unsigned char*) &ts->c_cc; }
526 __hscore_sizeof_termios( void )
529 return sizeof(struct termios);
536 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
538 __hscore_sizeof_sigset_t( void )
540 return sizeof(sigset_t);
545 __hscore_echo( void )
556 __hscore_tcsanow( void )
567 __hscore_icanon( void )
576 INLINE int __hscore_vmin( void )
585 INLINE int __hscore_vtime( void )
594 INLINE int __hscore_sigttou( void )
603 INLINE int __hscore_sig_block( void )
612 INLINE int __hscore_sig_setmask( void )
622 __hscore_f_getfl( void )
632 __hscore_f_setfl( void )
641 // defined in rts/RtsStartup.c.
642 extern void* __hscore_get_saved_termios(int fd);
643 extern void __hscore_set_saved_termios(int fd, void* ts);
645 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
647 INLINE int __hscore_open(char *file, int how, mode_t mode) {
649 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
650 return _sopen(file,how,_SH_DENYRW,mode);
652 return _sopen(file,how,_SH_DENYWR,mode);
654 return open(file,how,mode);
658 // These are wrapped because on some OSs (eg. Linux) they are
659 // macros which redirect to the 64-bit-off_t versions when large file
660 // support is enabled.
662 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
663 return (lseek(fd,off,whence));
666 INLINE int __hscore_stat(char *file, struct stat *buf) {
667 return (stat(file,buf));
670 INLINE int __hscore_fstat(int fd, struct stat *buf) {
671 return (fstat(fd,buf));
674 // select-related stuff
676 #if !defined(__MINGW32__)
677 INLINE int hsFD_SETSIZE(void) { return FD_SETSIZE; }
678 INLINE void hsFD_CLR(int fd, fd_set *fds) { FD_CLR(fd, fds); }
679 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
680 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
681 INLINE HsInt sizeof_fd_set(void) { return sizeof(fd_set); }
682 extern void hsFD_ZERO(fd_set *fds);
685 // gettimeofday()-related
687 #if !defined(__MINGW32__)
689 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
691 INLINE HsWord64 getUSecOfDay(void)
694 gettimeofday(&tv, (struct timezone *) NULL);
695 // Don't forget to cast *before* doing the arithmetic, otherwise
696 // the arithmetic happens at the type of tv_sec, which is probably
698 return ((HsWord64)tv.tv_sec * 1000000 + (HsWord64)tv.tv_usec);
701 INLINE void setTimevalTicks(struct timeval *p, HsWord64 usecs)
703 p->tv_sec = usecs / 1000000;
704 p->tv_usec = usecs % 1000000;
706 #endif /* !defined(__MINGW32__) */
708 #if defined(__MINGW32__)
709 INLINE unsigned int __hscore_get_osver(void) { return _osver; }
712 /* ToDo: write a feature test that doesn't assume 'environ' to
713 * be in scope at link-time. */
714 extern char** environ;
715 INLINE char **__hscore_environ() { return environ; }
717 /* lossless conversions between pointers and integral types */
718 INLINE void * __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
719 INLINE void * __hscore_from_intptr (intptr_t n) { return (void *)n; }
720 INLINE uintptr_t __hscore_to_uintptr (void *p) { return (uintptr_t)p; }
721 INLINE intptr_t __hscore_to_intptr (void *p) { return (intptr_t)p; }
723 #endif /* __HSBASE_H__ */