1 /* -----------------------------------------------------------------------------
3 * (c) The University of Glasgow 2001-2002
5 * Definitions for package `base' which are visible in Haskell land.
7 * ---------------------------------------------------------------------------*/
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"
118 HsInt systemCmd(HsAddr cmd);
121 #if defined(mingw32_TARGET_OS)
122 HsInt rawSystem(HsAddr cmd);
124 HsInt rawSystem(HsAddr cmd, HsAddr args);
127 /* in inputReady.c */
128 int inputReady(int fd, int msecs, int isSock);
130 /* in writeError.c */
131 void writeErrString__(HsAddr msg, HsInt len);
134 extern HsInt nocldstop;
136 /* -----------------------------------------------------------------------------
137 64-bit operations, defined in longlong.c
138 -------------------------------------------------------------------------- */
140 #ifdef SUPPORT_LONG_LONGS
142 StgInt stg_gtWord64 (StgWord64, StgWord64);
143 StgInt stg_geWord64 (StgWord64, StgWord64);
144 StgInt stg_eqWord64 (StgWord64, StgWord64);
145 StgInt stg_neWord64 (StgWord64, StgWord64);
146 StgInt stg_ltWord64 (StgWord64, StgWord64);
147 StgInt stg_leWord64 (StgWord64, StgWord64);
149 StgInt stg_gtInt64 (StgInt64, StgInt64);
150 StgInt stg_geInt64 (StgInt64, StgInt64);
151 StgInt stg_eqInt64 (StgInt64, StgInt64);
152 StgInt stg_neInt64 (StgInt64, StgInt64);
153 StgInt stg_ltInt64 (StgInt64, StgInt64);
154 StgInt stg_leInt64 (StgInt64, StgInt64);
156 StgWord64 stg_remWord64 (StgWord64, StgWord64);
157 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
159 StgInt64 stg_remInt64 (StgInt64, StgInt64);
160 StgInt64 stg_quotInt64 (StgInt64, StgInt64);
161 StgInt64 stg_negateInt64 (StgInt64);
162 StgInt64 stg_plusInt64 (StgInt64, StgInt64);
163 StgInt64 stg_minusInt64 (StgInt64, StgInt64);
164 StgInt64 stg_timesInt64 (StgInt64, StgInt64);
166 StgWord64 stg_and64 (StgWord64, StgWord64);
167 StgWord64 stg_or64 (StgWord64, StgWord64);
168 StgWord64 stg_xor64 (StgWord64, StgWord64);
169 StgWord64 stg_not64 (StgWord64);
171 StgWord64 stg_uncheckedShiftL64 (StgWord64, StgInt);
172 StgWord64 stg_uncheckedShiftRL64 (StgWord64, StgInt);
173 StgInt64 stg_uncheckedIShiftL64 (StgInt64, StgInt);
174 StgInt64 stg_uncheckedIShiftRL64 (StgInt64, StgInt);
175 StgInt64 stg_uncheckedIShiftRA64 (StgInt64, StgInt);
177 StgInt64 stg_intToInt64 (StgInt);
178 StgInt stg_int64ToInt (StgInt64);
179 StgWord64 stg_int64ToWord64 (StgInt64);
181 StgWord64 stg_wordToWord64 (StgWord);
182 StgWord stg_word64ToWord (StgWord64);
183 StgInt64 stg_word64ToInt64 (StgWord64);
185 StgInt64 stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
186 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
188 #endif /* SUPPORT_LONG_LONGS */
190 /* -----------------------------------------------------------------------------
193 These functions are given as inlines here for when compiling via C,
194 but we also generate static versions into the cbits library for
195 when compiling to native code.
196 -------------------------------------------------------------------------- */
199 # if defined(_MSC_VER)
200 # define INLINE extern __inline
201 # elif defined(__HUGS__)
202 # define INLINE INLINE_ONLY
204 # define INLINE extern inline
208 INLINE int __hscore_get_errno(void) { return errno; }
209 INLINE void __hscore_set_errno(int e) { errno = e; }
211 #if !defined(_MSC_VER)
212 INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
213 INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
214 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
215 INLINE int __hscore_s_isblk(m) { return S_ISBLK(m); }
216 INLINE int __hscore_s_ischr(m) { return S_ISCHR(m); }
218 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
222 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
224 __hscore_sigemptyset( sigset_t *set )
225 { return sigemptyset(set); }
228 __hscore_sigfillset( sigset_t *set )
229 { return sigfillset(set); }
232 __hscore_sigaddset( sigset_t * set, int s )
233 { return sigaddset(set,s); }
236 __hscore_sigdelset( sigset_t * set, int s )
237 { return sigdelset(set,s); }
240 __hscore_sigismember( sigset_t * set, int s )
241 { return sigismember(set,s); }
245 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
246 { return memcpy(dst+dst_off, src, sz); }
249 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
250 { return memcpy(dst, src+src_off, sz); }
253 __hscore_supportsTextMode()
255 #if defined(mingw32_TARGET_OS)
256 return HS_BOOL_FALSE;
277 #if defined(_MSC_VER)
280 return CONST_O_BINARY;
295 __hscore_o_wronly( void )
305 __hscore_o_rdwr( void )
315 __hscore_o_append( void )
325 __hscore_o_creat( void )
335 __hscore_o_excl( void )
345 __hscore_o_trunc( void )
355 __hscore_o_noctty( void )
365 __hscore_o_nonblock( void )
375 __hscore_seek_set( void )
381 __hscore_seek_end( void )
387 __hscore_setmode( HsInt fd, HsBool toBin )
389 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
390 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
397 __hscore_PrelHandle_write( HsInt fd, HsAddr ptr, HsInt off, int sz )
399 return write(fd,(char *)ptr + off, sz);
403 __hscore_PrelHandle_read( HsInt fd, HsAddr ptr, HsInt off, int sz )
405 return read(fd,(char *)ptr + off, sz);
409 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
411 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
414 __hscore_Time_ghcTzname( void ) { return _tzname; }
418 __hscore_mkdir( HsAddr pathName, HsInt mode )
420 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
421 return mkdir(pathName);
423 return mkdir(pathName,mode);
428 __hscore_lstat( HsAddr fname, HsAddr st )
431 return lstat((const char*)fname, (struct stat*)st);
433 return stat((const char*)fname, (struct stat*)st);
438 /* A size that will contain many path names, but not necessarily all
439 * (PATH_MAX is not defined on systems with unlimited path length,
442 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
444 INLINE HsInt __hscore_long_path_size() { return 4096; }
448 INLINE mode_t __hscore_R_OK() { return R_OK; }
451 INLINE mode_t __hscore_W_OK() { return W_OK; }
454 INLINE mode_t __hscore_X_OK() { return X_OK; }
458 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
461 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
464 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
467 #if !defined(_MSC_VER)
469 __hscore_d_name( struct dirent* d )
471 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
472 return (HsAddr)(&d->d_name);
474 return (HsAddr)(d->d_name);
480 __hscore_end_of_dir( void )
482 #ifndef mingw32_TARGET_OS
490 __hscore_free_dirent(HsAddr dEnt)
498 __hscore_sizeof_stat( void )
500 return sizeof(struct stat);
503 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
504 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
505 #if !defined(_MSC_VER)
506 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
510 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
513 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
515 INLINE unsigned char*
516 __hscore_ptr_c_cc( struct termios* ts )
517 { return (unsigned char*) &ts->c_cc; }
520 __hscore_sizeof_termios( void )
522 #ifndef mingw32_TARGET_OS
523 return sizeof(struct termios);
530 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
532 __hscore_sizeof_sigset_t( void )
534 return sizeof(sigset_t);
539 __hscore_echo( void )
550 __hscore_tcsanow( void )
561 __hscore_icanon( void )
570 INLINE int __hscore_vmin( void )
579 INLINE int __hscore_vtime( void )
588 INLINE int __hscore_sigttou( void )
597 INLINE int __hscore_sig_block( void )
606 INLINE int __hscore_sig_setmask( void )
616 __hscore_f_getfl( void )
626 __hscore_f_setfl( void )
635 // defined in rts/RtsStartup.c.
636 extern void* __hscore_get_saved_termios(int fd);
637 extern void __hscore_set_saved_termios(int fd, void* ts);
639 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
641 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
642 INLINE int __hsposix_SIGABRT() { return SIGABRT; }
643 INLINE int __hsposix_SIGALRM() { return SIGALRM; }
644 INLINE int __hsposix_SIGBUS() { return SIGBUS; }
645 INLINE int __hsposix_SIGCHLD() { return SIGCHLD; }
646 INLINE int __hsposix_SIGCONT() { return SIGCONT; }
647 INLINE int __hsposix_SIGFPE() { return SIGFPE; }
648 INLINE int __hsposix_SIGHUP() { return SIGHUP; }
649 INLINE int __hsposix_SIGILL() { return SIGILL; }
650 INLINE int __hsposix_SIGINT() { return SIGINT; }
651 INLINE int __hsposix_SIGKILL() { return SIGKILL; }
652 INLINE int __hsposix_SIGPIPE() { return SIGPIPE; }
653 INLINE int __hsposix_SIGQUIT() { return SIGQUIT; }
654 INLINE int __hsposix_SIGSEGV() { return SIGSEGV; }
655 INLINE int __hsposix_SIGSTOP() { return SIGSTOP; }
656 INLINE int __hsposix_SIGTERM() { return SIGTERM; }
657 INLINE int __hsposix_SIGTSTP() { return SIGTSTP; }
658 INLINE int __hsposix_SIGTTIN() { return SIGTTIN; }
659 INLINE int __hsposix_SIGTTOU() { return SIGTTOU; }
660 INLINE int __hsposix_SIGUSR1() { return SIGUSR1; }
661 INLINE int __hsposix_SIGUSR2() { return SIGUSR2; }
663 INLINE int __hsposix_SIGPOLL() { return SIGPOLL; }
665 INLINE int __hsposix_SIGPROF() { return SIGPROF; }
666 INLINE int __hsposix_SIGSYS() { return SIGSYS; }
667 INLINE int __hsposix_SIGTRAP() { return SIGTRAP; }
668 INLINE int __hsposix_SIGURG() { return SIGURG; }
669 INLINE int __hsposix_SIGVTALRM() { return SIGVTALRM; }
670 INLINE int __hsposix_SIGXCPU() { return SIGXCPU; }
671 INLINE int __hsposix_SIGXFSZ() { return SIGXFSZ; }
673 INLINE int __hsposix_SIG_BLOCK() { return SIG_BLOCK; }
674 INLINE int __hsposix_SIG_UNBLOCK() { return SIG_UNBLOCK; }
675 INLINE int __hsposix_SIG_SETMASK() { return SIG_SETMASK; }
676 #endif /* mingw32_TARGET_OS */
678 // These are wrapped because on some OSs (eg. Linux) they are
679 // macros which redirect to the 64-bit-off_t versions when large file
680 // support is enabled.
682 INLINE int __hscore_open(char *file, int how, mode_t mode) {
683 return (open(file,how,mode));
686 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
687 return (lseek(fd,off,whence));
690 INLINE int __hscore_stat(char *file, struct stat *buf) {
691 return (stat(file,buf));
694 INLINE int __hscore_fstat(int fd, struct stat *buf) {
695 return (fstat(fd,buf));
698 #endif /* __HSBASE_H__ */