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"
13 #include "HsBaseConfig.h"
21 #include <sys/types.h>
37 /* Ultra-ugly: OpenBSD uses broken macros for sigemptyset and sigfillset (missing casts) */
55 #if HAVE_SYS_UTSNAME_H
56 #include <sys/utsname.h>
60 # include <sys/time.h>
63 # if HAVE_SYS_TIMERS_H
65 # include <sys/timers.h>
72 #include <sys/timeb.h>
78 #include <sys/times.h>
80 #if HAVE_WINSOCK_H && defined(mingw32_TARGET_OS)
90 #if !defined(mingw32_TARGET_OS) && !defined(irix_TARGET_OS)
91 # if HAVE_SYS_RESOURCE_H
92 # include <sys/resource.h>
97 #include <sys/syscall.h>
98 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
99 #define HAVE_GETRUSAGE
104 #include <sys/wait.h>
109 #include "lockFile.h"
110 #include "dirUtils.h"
112 #if defined(mingw32_TARGET_OS)
115 #include "timeUtils.h"
120 HsInt systemCmd(HsAddr cmd);
123 #if defined(mingw32_TARGET_OS)
124 HsInt rawSystem(HsAddr cmd);
126 HsInt rawSystem(HsAddr cmd, HsAddr args);
129 /* in inputReady.c */
130 int inputReady(int fd, int msecs, int isSock);
132 /* in writeError.c */
133 void writeErrString__(HsAddr msg, HsInt len);
136 extern HsInt nocldstop;
138 /* -----------------------------------------------------------------------------
139 64-bit operations, defined in longlong.c
140 -------------------------------------------------------------------------- */
142 #ifdef SUPPORT_LONG_LONGS
144 StgInt stg_gtWord64 (StgWord64, StgWord64);
145 StgInt stg_geWord64 (StgWord64, StgWord64);
146 StgInt stg_eqWord64 (StgWord64, StgWord64);
147 StgInt stg_neWord64 (StgWord64, StgWord64);
148 StgInt stg_ltWord64 (StgWord64, StgWord64);
149 StgInt stg_leWord64 (StgWord64, StgWord64);
151 StgInt stg_gtInt64 (StgInt64, StgInt64);
152 StgInt stg_geInt64 (StgInt64, StgInt64);
153 StgInt stg_eqInt64 (StgInt64, StgInt64);
154 StgInt stg_neInt64 (StgInt64, StgInt64);
155 StgInt stg_ltInt64 (StgInt64, StgInt64);
156 StgInt stg_leInt64 (StgInt64, StgInt64);
158 StgWord64 stg_remWord64 (StgWord64, StgWord64);
159 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
161 StgInt64 stg_remInt64 (StgInt64, StgInt64);
162 StgInt64 stg_quotInt64 (StgInt64, StgInt64);
163 StgInt64 stg_negateInt64 (StgInt64);
164 StgInt64 stg_plusInt64 (StgInt64, StgInt64);
165 StgInt64 stg_minusInt64 (StgInt64, StgInt64);
166 StgInt64 stg_timesInt64 (StgInt64, StgInt64);
168 StgWord64 stg_and64 (StgWord64, StgWord64);
169 StgWord64 stg_or64 (StgWord64, StgWord64);
170 StgWord64 stg_xor64 (StgWord64, StgWord64);
171 StgWord64 stg_not64 (StgWord64);
173 StgWord64 stg_uncheckedShiftL64 (StgWord64, StgInt);
174 StgWord64 stg_uncheckedShiftRL64 (StgWord64, StgInt);
175 StgInt64 stg_uncheckedIShiftL64 (StgInt64, StgInt);
176 StgInt64 stg_uncheckedIShiftRL64 (StgInt64, StgInt);
177 StgInt64 stg_uncheckedIShiftRA64 (StgInt64, StgInt);
179 StgInt64 stg_intToInt64 (StgInt);
180 StgInt stg_int64ToInt (StgInt64);
181 StgWord64 stg_int64ToWord64 (StgInt64);
183 StgWord64 stg_wordToWord64 (StgWord);
184 StgWord stg_word64ToWord (StgWord64);
185 StgInt64 stg_word64ToInt64 (StgWord64);
187 StgInt64 stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
188 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
190 #endif /* SUPPORT_LONG_LONGS */
192 /* -----------------------------------------------------------------------------
195 These functions are given as inlines here for when compiling via C,
196 but we also generate static versions into the cbits library for
197 when compiling to native code.
198 -------------------------------------------------------------------------- */
201 # if defined(_MSC_VER)
202 # define INLINE extern __inline
203 # elif defined(__HUGS__)
204 # define INLINE INLINE_ONLY
206 # define INLINE extern inline
210 INLINE int __hscore_get_errno(void) { return errno; }
211 INLINE void __hscore_set_errno(int e) { errno = e; }
213 #if !defined(_MSC_VER)
214 INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
215 INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
216 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
217 INLINE int __hscore_s_isblk(m) { return S_ISBLK(m); }
218 INLINE int __hscore_s_ischr(m) { return S_ISCHR(m); }
220 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
224 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
226 __hscore_sigemptyset( sigset_t *set )
227 { return sigemptyset(set); }
230 __hscore_sigfillset( sigset_t *set )
231 { return sigfillset(set); }
234 __hscore_sigaddset( sigset_t * set, int s )
235 { return sigaddset(set,s); }
238 __hscore_sigdelset( sigset_t * set, int s )
239 { return sigdelset(set,s); }
242 __hscore_sigismember( sigset_t * set, int s )
243 { return sigismember(set,s); }
247 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
248 { return memcpy(dst+dst_off, src, sz); }
251 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
252 { return memcpy(dst, src+src_off, sz); }
255 __hscore_supportsTextMode()
257 #if defined(mingw32_TARGET_OS)
258 return HS_BOOL_FALSE;
279 #if defined(_MSC_VER)
282 return CONST_O_BINARY;
297 __hscore_o_wronly( void )
307 __hscore_o_rdwr( void )
317 __hscore_o_append( void )
327 __hscore_o_creat( void )
337 __hscore_o_excl( void )
347 __hscore_o_trunc( void )
357 __hscore_o_noctty( void )
367 __hscore_o_nonblock( void )
377 __hscore_seek_set( void )
383 __hscore_seek_end( void )
389 __hscore_setmode( HsInt fd, HsBool toBin )
391 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
392 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
399 __hscore_PrelHandle_write( HsInt fd, HsAddr ptr, HsInt off, int sz )
401 return write(fd,(char *)ptr + off, sz);
405 __hscore_PrelHandle_read( HsInt fd, HsAddr ptr, HsInt off, int sz )
407 return read(fd,(char *)ptr + off, sz);
411 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
413 __hscore_PrelHandle_send( HsInt fd, HsAddr ptr, HsInt off, int sz )
415 return send(fd,(char *)ptr + off, sz, 0);
419 __hscore_PrelHandle_recv( HsInt fd, HsAddr ptr, HsInt off, int sz )
421 return recv(fd,(char *)ptr + off, sz, 0);
425 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
427 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
430 __hscore_Time_ghcTzname( void ) { return _tzname; }
434 __hscore_mkdir( HsAddr pathName, HsInt mode )
436 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
437 return mkdir(pathName);
439 return mkdir(pathName,mode);
444 __hscore_lstat( HsAddr fname, HsAddr st )
447 return lstat((const char*)fname, (struct stat*)st);
449 return stat((const char*)fname, (struct stat*)st);
454 /* A size that will contain many path names, but not necessarily all
455 * (PATH_MAX is not defined on systems with unlimited path length,
458 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
460 INLINE HsInt __hscore_long_path_size() { return 4096; }
464 INLINE mode_t __hscore_R_OK() { return R_OK; }
467 INLINE mode_t __hscore_W_OK() { return W_OK; }
470 INLINE mode_t __hscore_X_OK() { return X_OK; }
474 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
477 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
480 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
484 __hscore_d_name( struct dirent* d )
486 #if defined(STRUCT_DIRENT_FLAT_LAYOUT)
487 return (HsAddr)(&d->d_name);
489 return (HsAddr)(d->d_name);
494 __hscore_end_of_dir( void )
496 return READDIR_ERRNO_EOF;
500 __hscore_free_dirent(HsAddr dEnt)
508 __hscore_sizeof_stat( void )
510 return sizeof(struct stat);
513 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
514 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
515 #if !defined(_MSC_VER)
516 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
520 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
523 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
525 INLINE unsigned char*
526 __hscore_ptr_c_cc( struct termios* ts )
527 { return (unsigned char*) &ts->c_cc; }
530 __hscore_sizeof_termios( void )
532 #ifndef mingw32_TARGET_OS
533 return sizeof(struct termios);
540 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
542 __hscore_sizeof_sigset_t( void )
544 return sizeof(sigset_t);
549 __hscore_echo( void )
560 __hscore_tcsanow( void )
571 __hscore_icanon( void )
580 INLINE int __hscore_vmin( void )
589 INLINE int __hscore_vtime( void )
598 INLINE int __hscore_sigttou( void )
607 INLINE int __hscore_sig_block( void )
616 INLINE int __hscore_sig_setmask( void )
626 __hscore_f_getfl( void )
636 __hscore_f_setfl( void )
645 // defined in rts/RtsStartup.c.
646 extern void* __hscore_get_saved_termios(int fd);
647 extern void __hscore_set_saved_termios(int fd, void* ts);
649 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
651 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
652 INLINE int __hsposix_SIGABRT() { return SIGABRT; }
653 INLINE int __hsposix_SIGALRM() { return SIGALRM; }
654 INLINE int __hsposix_SIGBUS() { return SIGBUS; }
655 INLINE int __hsposix_SIGCHLD() { return SIGCHLD; }
656 INLINE int __hsposix_SIGCONT() { return SIGCONT; }
657 INLINE int __hsposix_SIGFPE() { return SIGFPE; }
658 INLINE int __hsposix_SIGHUP() { return SIGHUP; }
659 INLINE int __hsposix_SIGILL() { return SIGILL; }
660 INLINE int __hsposix_SIGINT() { return SIGINT; }
661 INLINE int __hsposix_SIGKILL() { return SIGKILL; }
662 INLINE int __hsposix_SIGPIPE() { return SIGPIPE; }
663 INLINE int __hsposix_SIGQUIT() { return SIGQUIT; }
664 INLINE int __hsposix_SIGSEGV() { return SIGSEGV; }
665 INLINE int __hsposix_SIGSTOP() { return SIGSTOP; }
666 INLINE int __hsposix_SIGTERM() { return SIGTERM; }
667 INLINE int __hsposix_SIGTSTP() { return SIGTSTP; }
668 INLINE int __hsposix_SIGTTIN() { return SIGTTIN; }
669 INLINE int __hsposix_SIGTTOU() { return SIGTTOU; }
670 INLINE int __hsposix_SIGUSR1() { return SIGUSR1; }
671 INLINE int __hsposix_SIGUSR2() { return SIGUSR2; }
673 INLINE int __hsposix_SIGPOLL() { return SIGPOLL; }
675 INLINE int __hsposix_SIGPROF() { return SIGPROF; }
676 INLINE int __hsposix_SIGSYS() { return SIGSYS; }
677 INLINE int __hsposix_SIGTRAP() { return SIGTRAP; }
678 INLINE int __hsposix_SIGURG() { return SIGURG; }
679 INLINE int __hsposix_SIGVTALRM() { return SIGVTALRM; }
680 INLINE int __hsposix_SIGXCPU() { return SIGXCPU; }
681 INLINE int __hsposix_SIGXFSZ() { return SIGXFSZ; }
683 INLINE int __hsposix_SIG_BLOCK() { return SIG_BLOCK; }
684 INLINE int __hsposix_SIG_UNBLOCK() { return SIG_UNBLOCK; }
685 INLINE int __hsposix_SIG_SETMASK() { return SIG_SETMASK; }
688 INLINE int __hsposix_SIGINFO() { return SIGINFO; }
691 INLINE int __hsposix_SIGWINCH() { return SIGWINCH; }
693 #endif /* mingw32_TARGET_OS */
695 // These are wrapped because on some OSs (eg. Linux) they are
696 // macros which redirect to the 64-bit-off_t versions when large file
697 // support is enabled.
699 INLINE int __hscore_open(char *file, int how, mode_t mode) {
700 return (open(file,how,mode));
703 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
704 return (lseek(fd,off,whence));
707 INLINE int __hscore_stat(char *file, struct stat *buf) {
708 return (stat(file,buf));
711 INLINE int __hscore_fstat(int fd, struct stat *buf) {
712 return (fstat(fd,buf));
715 #if defined(mingw32_TARGET_OS)
717 /* Make sure we've got the reqd CSIDL_ constants in scope;
718 * w32api header files are lagging a bit in defining the full set.
720 #if !defined(CSIDL_APPDATA)
721 #define CSIDL_APPDATA 0x001a
723 #if !defined(CSIDL_PERSONAL)
724 #define CSIDL_PERSONAL 0x0005
726 #if !defined(CSIDL_PROFILE)
727 #define CSIDL_PROFILE 0x0028
729 #if !defined(CSIDL_WINDOWS)
730 #define CSIDL_WINDOWS 0x0024
733 INLINE int __hscore_CSIDL_PROFILE() { return CSIDL_PROFILE; }
734 INLINE int __hscore_CSIDL_APPDATA() { return CSIDL_APPDATA; }
735 INLINE int __hscore_CSIDL_WINDOWS() { return CSIDL_WINDOWS; }
736 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
739 #endif /* __HSBASE_H__ */