1 /* -----------------------------------------------------------------------------
3 * (c) The University of Glasgow 2001-2002
5 * Definitions for package `base' which are visible in Haskell land.
7 * ---------------------------------------------------------------------------*/
12 #include "ghcconfig.h"
20 #include <sys/types.h>
36 /* Ultra-ugly: OpenBSD uses broken macros for sigemptyset and sigfillset (missing casts) */
54 #if HAVE_SYS_UTSNAME_H
55 #include <sys/utsname.h>
59 # include <sys/time.h>
62 # if HAVE_SYS_TIMERS_H
64 # include <sys/timers.h>
71 #include <sys/timeb.h>
77 #include <sys/times.h>
79 #if HAVE_WINSOCK_H && defined(mingw32_TARGET_OS)
89 #if !defined(mingw32_TARGET_OS) && !defined(irix_TARGET_OS)
90 # if HAVE_SYS_RESOURCE_H
91 # include <sys/resource.h>
96 #include <sys/syscall.h>
97 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
98 #define HAVE_GETRUSAGE
103 #include <sys/wait.h>
108 #include "lockFile.h"
109 #include "dirUtils.h"
111 #if defined(mingw32_TARGET_OS)
114 #include "timeUtils.h"
119 HsInt systemCmd(HsAddr cmd);
122 #if defined(mingw32_TARGET_OS)
123 HsInt rawSystem(HsAddr cmd);
125 HsInt rawSystem(HsAddr cmd, HsAddr args);
128 /* in inputReady.c */
129 int inputReady(int fd, int msecs, int isSock);
131 /* in writeError.c */
132 void writeErrString__(HsAddr msg, HsInt len);
135 extern HsInt nocldstop;
137 /* -----------------------------------------------------------------------------
138 64-bit operations, defined in longlong.c
139 -------------------------------------------------------------------------- */
141 #ifdef SUPPORT_LONG_LONGS
143 StgInt stg_gtWord64 (StgWord64, StgWord64);
144 StgInt stg_geWord64 (StgWord64, StgWord64);
145 StgInt stg_eqWord64 (StgWord64, StgWord64);
146 StgInt stg_neWord64 (StgWord64, StgWord64);
147 StgInt stg_ltWord64 (StgWord64, StgWord64);
148 StgInt stg_leWord64 (StgWord64, StgWord64);
150 StgInt stg_gtInt64 (StgInt64, StgInt64);
151 StgInt stg_geInt64 (StgInt64, StgInt64);
152 StgInt stg_eqInt64 (StgInt64, StgInt64);
153 StgInt stg_neInt64 (StgInt64, StgInt64);
154 StgInt stg_ltInt64 (StgInt64, StgInt64);
155 StgInt stg_leInt64 (StgInt64, StgInt64);
157 StgWord64 stg_remWord64 (StgWord64, StgWord64);
158 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
160 StgInt64 stg_remInt64 (StgInt64, StgInt64);
161 StgInt64 stg_quotInt64 (StgInt64, StgInt64);
162 StgInt64 stg_negateInt64 (StgInt64);
163 StgInt64 stg_plusInt64 (StgInt64, StgInt64);
164 StgInt64 stg_minusInt64 (StgInt64, StgInt64);
165 StgInt64 stg_timesInt64 (StgInt64, StgInt64);
167 StgWord64 stg_and64 (StgWord64, StgWord64);
168 StgWord64 stg_or64 (StgWord64, StgWord64);
169 StgWord64 stg_xor64 (StgWord64, StgWord64);
170 StgWord64 stg_not64 (StgWord64);
172 StgWord64 stg_uncheckedShiftL64 (StgWord64, StgInt);
173 StgWord64 stg_uncheckedShiftRL64 (StgWord64, StgInt);
174 StgInt64 stg_uncheckedIShiftL64 (StgInt64, StgInt);
175 StgInt64 stg_uncheckedIShiftRL64 (StgInt64, StgInt);
176 StgInt64 stg_uncheckedIShiftRA64 (StgInt64, StgInt);
178 StgInt64 stg_intToInt64 (StgInt);
179 StgInt stg_int64ToInt (StgInt64);
180 StgWord64 stg_int64ToWord64 (StgInt64);
182 StgWord64 stg_wordToWord64 (StgWord);
183 StgWord stg_word64ToWord (StgWord64);
184 StgInt64 stg_word64ToInt64 (StgWord64);
186 StgInt64 stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
187 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
189 #endif /* SUPPORT_LONG_LONGS */
191 /* -----------------------------------------------------------------------------
194 These functions are given as inlines here for when compiling via C,
195 but we also generate static versions into the cbits library for
196 when compiling to native code.
197 -------------------------------------------------------------------------- */
200 # if defined(_MSC_VER)
201 # define INLINE extern __inline
202 # elif defined(__HUGS__)
203 # define INLINE INLINE_ONLY
205 # define INLINE extern inline
209 INLINE int __hscore_get_errno(void) { return errno; }
210 INLINE void __hscore_set_errno(int e) { errno = e; }
212 #if !defined(_MSC_VER)
213 INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
214 INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
215 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
216 INLINE int __hscore_s_isblk(m) { return S_ISBLK(m); }
217 INLINE int __hscore_s_ischr(m) { return S_ISCHR(m); }
219 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
223 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
225 __hscore_sigemptyset( sigset_t *set )
226 { return sigemptyset(set); }
229 __hscore_sigfillset( sigset_t *set )
230 { return sigfillset(set); }
233 __hscore_sigaddset( sigset_t * set, int s )
234 { return sigaddset(set,s); }
237 __hscore_sigdelset( sigset_t * set, int s )
238 { return sigdelset(set,s); }
241 __hscore_sigismember( sigset_t * set, int s )
242 { return sigismember(set,s); }
246 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
247 { return memcpy(dst+dst_off, src, sz); }
250 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
251 { return memcpy(dst, src+src_off, sz); }
254 __hscore_supportsTextMode()
256 #if defined(mingw32_TARGET_OS)
257 return HS_BOOL_FALSE;
278 #if defined(_MSC_VER)
281 return CONST_O_BINARY;
296 __hscore_o_wronly( void )
306 __hscore_o_rdwr( void )
316 __hscore_o_append( void )
326 __hscore_o_creat( void )
336 __hscore_o_excl( void )
346 __hscore_o_trunc( void )
356 __hscore_o_noctty( void )
366 __hscore_o_nonblock( void )
376 __hscore_seek_set( void )
382 __hscore_seek_end( void )
388 __hscore_setmode( HsInt fd, HsBool toBin )
390 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
391 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
398 __hscore_PrelHandle_write( HsInt fd, HsAddr ptr, HsInt off, int sz )
400 return write(fd,(char *)ptr + off, sz);
404 __hscore_PrelHandle_read( HsInt fd, HsAddr ptr, HsInt off, int sz )
406 return read(fd,(char *)ptr + off, sz);
410 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
412 __hscore_PrelHandle_send( HsInt fd, HsAddr ptr, HsInt off, int sz )
414 return send(fd,(char *)ptr + off, sz, 0);
418 __hscore_PrelHandle_recv( HsInt fd, HsAddr ptr, HsInt off, int sz )
420 return recv(fd,(char *)ptr + off, sz, 0);
424 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
426 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
429 __hscore_Time_ghcTzname( void ) { return _tzname; }
433 __hscore_mkdir( HsAddr pathName, HsInt mode )
435 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
436 return mkdir(pathName);
438 return mkdir(pathName,mode);
443 __hscore_lstat( HsAddr fname, HsAddr st )
446 return lstat((const char*)fname, (struct stat*)st);
448 return stat((const char*)fname, (struct stat*)st);
453 /* A size that will contain many path names, but not necessarily all
454 * (PATH_MAX is not defined on systems with unlimited path length,
457 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
459 INLINE HsInt __hscore_long_path_size() { return 4096; }
463 INLINE mode_t __hscore_R_OK() { return R_OK; }
466 INLINE mode_t __hscore_W_OK() { return W_OK; }
469 INLINE mode_t __hscore_X_OK() { return X_OK; }
473 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
476 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
479 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
482 #if !defined(_MSC_VER)
484 __hscore_d_name( struct dirent* d )
486 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
487 return (HsAddr)(&d->d_name);
489 return (HsAddr)(d->d_name);
495 __hscore_end_of_dir( void )
497 #ifndef mingw32_TARGET_OS
505 __hscore_free_dirent(HsAddr dEnt)
513 __hscore_sizeof_stat( void )
515 return sizeof(struct stat);
518 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
519 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
520 #if !defined(_MSC_VER)
521 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
525 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
528 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
530 INLINE unsigned char*
531 __hscore_ptr_c_cc( struct termios* ts )
532 { return (unsigned char*) &ts->c_cc; }
535 __hscore_sizeof_termios( void )
537 #ifndef mingw32_TARGET_OS
538 return sizeof(struct termios);
545 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
547 __hscore_sizeof_sigset_t( void )
549 return sizeof(sigset_t);
554 __hscore_echo( void )
565 __hscore_tcsanow( void )
576 __hscore_icanon( void )
585 INLINE int __hscore_vmin( void )
594 INLINE int __hscore_vtime( void )
603 INLINE int __hscore_sigttou( void )
612 INLINE int __hscore_sig_block( void )
621 INLINE int __hscore_sig_setmask( void )
631 __hscore_f_getfl( void )
641 __hscore_f_setfl( void )
650 // defined in rts/RtsStartup.c.
651 extern void* __hscore_get_saved_termios(int fd);
652 extern void __hscore_set_saved_termios(int fd, void* ts);
654 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
656 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
657 INLINE int __hsposix_SIGABRT() { return SIGABRT; }
658 INLINE int __hsposix_SIGALRM() { return SIGALRM; }
659 INLINE int __hsposix_SIGBUS() { return SIGBUS; }
660 INLINE int __hsposix_SIGCHLD() { return SIGCHLD; }
661 INLINE int __hsposix_SIGCONT() { return SIGCONT; }
662 INLINE int __hsposix_SIGFPE() { return SIGFPE; }
663 INLINE int __hsposix_SIGHUP() { return SIGHUP; }
664 INLINE int __hsposix_SIGILL() { return SIGILL; }
665 INLINE int __hsposix_SIGINT() { return SIGINT; }
666 INLINE int __hsposix_SIGKILL() { return SIGKILL; }
667 INLINE int __hsposix_SIGPIPE() { return SIGPIPE; }
668 INLINE int __hsposix_SIGQUIT() { return SIGQUIT; }
669 INLINE int __hsposix_SIGSEGV() { return SIGSEGV; }
670 INLINE int __hsposix_SIGSTOP() { return SIGSTOP; }
671 INLINE int __hsposix_SIGTERM() { return SIGTERM; }
672 INLINE int __hsposix_SIGTSTP() { return SIGTSTP; }
673 INLINE int __hsposix_SIGTTIN() { return SIGTTIN; }
674 INLINE int __hsposix_SIGTTOU() { return SIGTTOU; }
675 INLINE int __hsposix_SIGUSR1() { return SIGUSR1; }
676 INLINE int __hsposix_SIGUSR2() { return SIGUSR2; }
678 INLINE int __hsposix_SIGPOLL() { return SIGPOLL; }
680 INLINE int __hsposix_SIGPROF() { return SIGPROF; }
681 INLINE int __hsposix_SIGSYS() { return SIGSYS; }
682 INLINE int __hsposix_SIGTRAP() { return SIGTRAP; }
683 INLINE int __hsposix_SIGURG() { return SIGURG; }
684 INLINE int __hsposix_SIGVTALRM() { return SIGVTALRM; }
685 INLINE int __hsposix_SIGXCPU() { return SIGXCPU; }
686 INLINE int __hsposix_SIGXFSZ() { return SIGXFSZ; }
688 INLINE int __hsposix_SIG_BLOCK() { return SIG_BLOCK; }
689 INLINE int __hsposix_SIG_UNBLOCK() { return SIG_UNBLOCK; }
690 INLINE int __hsposix_SIG_SETMASK() { return SIG_SETMASK; }
691 #endif /* mingw32_TARGET_OS */
693 // These are wrapped because on some OSs (eg. Linux) they are
694 // macros which redirect to the 64-bit-off_t versions when large file
695 // support is enabled.
697 INLINE int __hscore_open(char *file, int how, mode_t mode) {
698 return (open(file,how,mode));
701 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
702 return (lseek(fd,off,whence));
705 INLINE int __hscore_stat(char *file, struct stat *buf) {
706 return (stat(file,buf));
709 INLINE int __hscore_fstat(int fd, struct stat *buf) {
710 return (fstat(fd,buf));
713 #if defined(mingw32_TARGET_OS)
714 INLINE int __hscore_CSIDL_PROFILE() { return CSIDL_PROFILE; }
715 INLINE int __hscore_CSIDL_APPDATA() { return CSIDL_APPDATA; }
716 INLINE int __hscore_CSIDL_WINDOWS() { return CSIDL_WINDOWS; }
719 #endif /* __HSBASE_H__ */