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 #if !defined(mingw32_HOST_OS) && !defined(irix_HOST_OS)
100 # if HAVE_SYS_RESOURCE_H
101 # include <sys/resource.h>
106 #include <sys/syscall.h>
107 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
108 #define HAVE_GETRUSAGE
113 #include <sys/wait.h>
118 #include "lockFile.h"
119 #include "dirUtils.h"
122 #include "runProcess.h"
124 #if defined(mingw32_HOST_OS)
127 #include "timeUtils.h"
132 #if HAVE_SYS_SELECT_H
133 #include <sys/select.h>
136 /* in inputReady.c */
137 int inputReady(int fd, int msecs, int isSock);
140 extern HsInt nocldstop;
142 #if !defined(mingw32_HOST_OS)
144 extern int execvpe(char *name, char *const argv[], char **envp);
145 extern void pPrPr_disableITimers (void);
148 /* -----------------------------------------------------------------------------
149 64-bit operations, defined in longlong.c
150 -------------------------------------------------------------------------- */
152 #ifdef SUPPORT_LONG_LONGS
154 StgInt stg_gtWord64 (StgWord64, StgWord64);
155 StgInt stg_geWord64 (StgWord64, StgWord64);
156 StgInt stg_eqWord64 (StgWord64, StgWord64);
157 StgInt stg_neWord64 (StgWord64, StgWord64);
158 StgInt stg_ltWord64 (StgWord64, StgWord64);
159 StgInt stg_leWord64 (StgWord64, StgWord64);
161 StgInt stg_gtInt64 (StgInt64, StgInt64);
162 StgInt stg_geInt64 (StgInt64, StgInt64);
163 StgInt stg_eqInt64 (StgInt64, StgInt64);
164 StgInt stg_neInt64 (StgInt64, StgInt64);
165 StgInt stg_ltInt64 (StgInt64, StgInt64);
166 StgInt stg_leInt64 (StgInt64, StgInt64);
168 StgWord64 stg_remWord64 (StgWord64, StgWord64);
169 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
171 StgInt64 stg_remInt64 (StgInt64, StgInt64);
172 StgInt64 stg_quotInt64 (StgInt64, StgInt64);
173 StgInt64 stg_negateInt64 (StgInt64);
174 StgInt64 stg_plusInt64 (StgInt64, StgInt64);
175 StgInt64 stg_minusInt64 (StgInt64, StgInt64);
176 StgInt64 stg_timesInt64 (StgInt64, StgInt64);
178 StgWord64 stg_and64 (StgWord64, StgWord64);
179 StgWord64 stg_or64 (StgWord64, StgWord64);
180 StgWord64 stg_xor64 (StgWord64, StgWord64);
181 StgWord64 stg_not64 (StgWord64);
183 StgWord64 stg_uncheckedShiftL64 (StgWord64, StgInt);
184 StgWord64 stg_uncheckedShiftRL64 (StgWord64, StgInt);
185 StgInt64 stg_uncheckedIShiftL64 (StgInt64, StgInt);
186 StgInt64 stg_uncheckedIShiftRL64 (StgInt64, StgInt);
187 StgInt64 stg_uncheckedIShiftRA64 (StgInt64, StgInt);
189 StgInt64 stg_intToInt64 (StgInt);
190 StgInt stg_int64ToInt (StgInt64);
191 StgWord64 stg_int64ToWord64 (StgInt64);
193 StgWord64 stg_wordToWord64 (StgWord);
194 StgWord stg_word64ToWord (StgWord64);
195 StgInt64 stg_word64ToInt64 (StgWord64);
197 StgInt64 stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
198 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
200 #endif /* SUPPORT_LONG_LONGS */
202 /* -----------------------------------------------------------------------------
205 These functions are given as inlines here for when compiling via C,
206 but we also generate static versions into the cbits library for
207 when compiling to native code.
208 -------------------------------------------------------------------------- */
211 # if defined(_MSC_VER)
212 # define INLINE extern __inline
213 # elif defined(__HUGS__)
214 # define INLINE INLINE_ONLY
216 # define INLINE extern inline
220 INLINE int __hscore_get_errno(void) { return errno; }
221 INLINE void __hscore_set_errno(int e) { errno = e; }
223 #if !defined(_MSC_VER)
224 INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
225 INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
226 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
227 INLINE int __hscore_s_isblk(m) { return S_ISBLK(m); }
228 INLINE int __hscore_s_ischr(m) { return S_ISCHR(m); }
230 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
234 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
236 __hscore_sigemptyset( sigset_t *set )
237 { return sigemptyset(set); }
240 __hscore_sigfillset( sigset_t *set )
241 { return sigfillset(set); }
244 __hscore_sigaddset( sigset_t * set, int s )
245 { return sigaddset(set,s); }
248 __hscore_sigdelset( sigset_t * set, int s )
249 { return sigdelset(set,s); }
252 __hscore_sigismember( sigset_t * set, int s )
253 { return sigismember(set,s); }
257 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
258 { return memcpy(dst+dst_off, src, sz); }
261 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
262 { return memcpy(dst, src+src_off, sz); }
265 __hscore_supportsTextMode()
267 #if defined(mingw32_HOST_OS)
268 return HS_BOOL_FALSE;
289 #if defined(_MSC_VER)
292 return CONST_O_BINARY;
307 __hscore_o_wronly( void )
317 __hscore_o_rdwr( void )
327 __hscore_o_append( void )
337 __hscore_o_creat( void )
347 __hscore_o_excl( void )
357 __hscore_o_trunc( void )
367 __hscore_o_noctty( void )
377 __hscore_o_nonblock( void )
387 __hscore_seek_set( void )
393 __hscore_seek_end( void )
399 __hscore_ftruncate( int fd, off_t where )
401 #if defined(HAVE_FTRUNCATE)
402 return ftruncate(fd,where);
403 #elif defined(HAVE__CHSIZE)
404 return _chsize(fd,where);
406 #error at least ftruncate or _chsize functions are required to build
411 __hscore_setmode( HsInt fd, HsBool toBin )
413 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
414 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
421 __hscore_PrelHandle_write( HsInt fd, HsAddr ptr, HsInt off, int sz )
423 return write(fd,(char *)ptr + off, sz);
427 __hscore_PrelHandle_read( HsInt fd, HsAddr ptr, HsInt off, int sz )
429 return read(fd,(char *)ptr + off, sz);
433 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
435 __hscore_PrelHandle_send( HsInt fd, HsAddr ptr, HsInt off, int sz )
437 return send(fd,(char *)ptr + off, sz, 0);
441 __hscore_PrelHandle_recv( HsInt fd, HsAddr ptr, HsInt off, int sz )
443 return recv(fd,(char *)ptr + off, sz, 0);
447 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
449 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
452 __hscore_Time_ghcTzname( void ) { return _tzname; }
456 __hscore_mkdir( HsAddr pathName, HsInt mode )
458 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
459 return mkdir(pathName);
461 return mkdir(pathName,mode);
466 __hscore_lstat( HsAddr fname, HsAddr st )
469 return lstat((const char*)fname, (struct stat*)st);
471 return stat((const char*)fname, (struct stat*)st);
476 /* A size that will contain many path names, but not necessarily all
477 * (PATH_MAX is not defined on systems with unlimited path length,
480 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
482 INLINE HsInt __hscore_long_path_size() { return 4096; }
486 INLINE mode_t __hscore_R_OK() { return R_OK; }
489 INLINE mode_t __hscore_W_OK() { return W_OK; }
492 INLINE mode_t __hscore_X_OK() { return X_OK; }
496 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
499 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
502 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
506 __hscore_d_name( struct dirent* d )
508 return (HsAddr)(d->d_name);
512 __hscore_end_of_dir( void )
514 return READDIR_ERRNO_EOF;
518 __hscore_free_dirent(HsAddr dEnt)
526 __hscore_sizeof_stat( void )
528 return sizeof(struct stat);
531 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
532 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
533 #if !defined(_MSC_VER)
534 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
538 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
541 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
543 INLINE unsigned char*
544 __hscore_ptr_c_cc( struct termios* ts )
545 { return (unsigned char*) &ts->c_cc; }
548 __hscore_sizeof_termios( void )
550 #ifndef mingw32_HOST_OS
551 return sizeof(struct termios);
558 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
560 __hscore_sizeof_sigset_t( void )
562 return sizeof(sigset_t);
567 __hscore_echo( void )
578 __hscore_tcsanow( void )
589 __hscore_icanon( void )
598 INLINE int __hscore_vmin( void )
607 INLINE int __hscore_vtime( void )
616 INLINE int __hscore_sigttou( void )
625 INLINE int __hscore_sig_block( void )
634 INLINE int __hscore_sig_setmask( void )
644 __hscore_f_getfl( void )
654 __hscore_f_setfl( void )
663 // defined in rts/RtsStartup.c.
664 extern void* __hscore_get_saved_termios(int fd);
665 extern void __hscore_set_saved_termios(int fd, void* ts);
667 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
669 INLINE int __hscore_open(char *file, int how, mode_t mode) {
670 #ifdef mingw32_HOST_OS
671 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
672 return _sopen(file,how,_SH_DENYRW,mode);
674 return _sopen(file,how,_SH_DENYWR,mode);
676 return open(file,how,mode);
680 // These are wrapped because on some OSs (eg. Linux) they are
681 // macros which redirect to the 64-bit-off_t versions when large file
682 // support is enabled.
684 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
685 return (lseek(fd,off,whence));
688 INLINE int __hscore_stat(char *file, struct stat *buf) {
689 return (stat(file,buf));
692 INLINE int __hscore_fstat(int fd, struct stat *buf) {
693 return (fstat(fd,buf));
696 // select-related stuff
698 #if !defined(mingw32_HOST_OS)
699 INLINE void hsFD_CLR(int fd, fd_set *fds) { FD_CLR(fd, fds); }
700 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
701 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
702 INLINE int sizeof_fd_set(void) { return sizeof(fd_set); }
703 extern void hsFD_ZERO(fd_set *fds);
706 // gettimeofday()-related
708 #if !defined(mingw32_HOST_OS)
711 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
713 INLINE HsInt getTicksOfDay(void)
716 gettimeofday(&tv, (struct timezone *) NULL);
717 return (tv.tv_sec * TICK_FREQ +
718 tv.tv_usec * TICK_FREQ / 1000000);
721 INLINE void setTimevalTicks(struct timeval *p, HsInt ticks)
723 p->tv_sec = ticks / TICK_FREQ;
724 p->tv_usec = (ticks % TICK_FREQ) * (1000000 / TICK_FREQ);
726 #endif // !defined(mingw32_HOST_OS)
730 #if defined(mingw32_HOST_OS)
732 /* Make sure we've got the reqd CSIDL_ constants in scope;
733 * w32api header files are lagging a bit in defining the full set.
735 #if !defined(CSIDL_APPDATA)
736 #define CSIDL_APPDATA 0x001a
738 #if !defined(CSIDL_PERSONAL)
739 #define CSIDL_PERSONAL 0x0005
741 #if !defined(CSIDL_PROFILE)
742 #define CSIDL_PROFILE 0x0028
744 #if !defined(CSIDL_WINDOWS)
745 #define CSIDL_WINDOWS 0x0024
748 INLINE int __hscore_CSIDL_PROFILE() { return CSIDL_PROFILE; }
749 INLINE int __hscore_CSIDL_APPDATA() { return CSIDL_APPDATA; }
750 INLINE int __hscore_CSIDL_WINDOWS() { return CSIDL_WINDOWS; }
751 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
754 /* ToDo: write a feature test that doesn't assume 'environ' to
755 * be in scope at link-time. */
756 extern char** environ;
757 INLINE char **__hscore_environ() { return environ; }
759 #endif /* __HSBASE_H__ */