e8b7ae3a3cb501a5307ce4c5043158ca3ffb7c16
[ghc-base.git] / include / HsBase.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The University of Glasgow 2001-2004
4  *
5  * Definitions for package `base' which are visible in Haskell land.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #ifndef __HSBASE_H__
10 #define __HSBASE_H__
11
12 #include "ghcconfig.h"
13
14 #include "HsBaseConfig.h"
15
16 /* ultra-evil... */
17 #undef PACKAGE_BUGREPORT
18 #undef PACKAGE_NAME
19 #undef PACKAGE_STRING
20 #undef PACKAGE_TARNAME
21 #undef PACKAGE_VERSION
22
23 #include "HsFFI.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28
29 #if HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #if HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #if HAVE_SYS_STAT_H
36 #include <sys/stat.h>
37 #endif
38 #if HAVE_FCNTL_H
39 # include <fcntl.h>
40 #endif
41 #if HAVE_TERMIOS_H
42 #include <termios.h>
43 #endif
44 #if HAVE_SIGNAL_H
45 #include <signal.h>
46 /* Ultra-ugly: OpenBSD uses broken macros for sigemptyset and sigfillset (missing casts) */
47 #if __OpenBSD__
48 #undef sigemptyset
49 #undef sigfillset
50 #endif
51 #endif
52 #if HAVE_ERRNO_H
53 #include <errno.h>
54 #endif
55 #if HAVE_STRING_H
56 #include <string.h>
57 #endif
58 #if HAVE_DIRENT_H
59 #include <dirent.h>
60 #endif
61 #if HAVE_UTIME_H
62 #include <utime.h>
63 #endif
64 #if HAVE_SYS_UTSNAME_H
65 #include <sys/utsname.h>
66 #endif
67 #if HAVE_GETTIMEOFDAY
68 #  if HAVE_SYS_TIME_H
69 #   include <sys/time.h>
70 #  endif
71 #elif HAVE_GETCLOCK
72 # if HAVE_SYS_TIMERS_H
73 #  define POSIX_4D9 1
74 #  include <sys/timers.h>
75 # endif
76 #endif
77 #if HAVE_TIME_H
78 #include <time.h>
79 #endif
80 #if HAVE_SYS_TIMEB_H
81 #include <sys/timeb.h>
82 #endif
83 #if HAVE_WINDOWS_H
84 #include <windows.h>
85 #endif
86 #if HAVE_SYS_TIMES_H
87 #include <sys/times.h>
88 #endif
89 #if HAVE_WINSOCK_H && defined(mingw32_HOST_OS)
90 #include <winsock.h>
91 #endif
92 #if HAVE_LIMITS_H
93 #include <limits.h>
94 #endif
95 #if HAVE_WCTYPE_H
96 #include <wctype.h>
97 #endif
98
99 #if !defined(mingw32_HOST_OS) && !defined(irix_HOST_OS)
100 # if HAVE_SYS_RESOURCE_H
101 #  include <sys/resource.h>
102 # endif
103 #endif
104
105 #ifdef hpux_HOST_OS
106 #include <sys/syscall.h>
107 #define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
108 #define HAVE_GETRUSAGE
109 #endif
110
111 /* For System */
112 #if HAVE_SYS_WAIT_H
113 #include <sys/wait.h>
114 #endif
115 #if HAVE_VFORK_H
116 #include <vfork.h>
117 #endif
118 #include "lockFile.h"
119 #include "dirUtils.h"
120
121 #include "runProcess.h"
122
123 #if defined(mingw32_HOST_OS)
124 #include <io.h>
125 #include <fcntl.h>
126 #include "timeUtils.h"
127 #include <shlobj.h>
128 #include <share.h>
129 #endif
130
131 #if HAVE_SYS_SELECT_H
132 #include <sys/select.h>
133 #endif
134
135 /* in inputReady.c */
136 int inputReady(int fd, int msecs, int isSock);
137
138 /* in writeError.c */
139 void writeErrString__(HsAddr msg, HsInt len);
140
141 /* in Signals.c */
142 extern HsInt nocldstop;
143
144 #if !defined(mingw32_HOST_OS)
145 /* in execvpe.c */
146 extern int execvpe(char *name, char *const argv[], char **envp);
147 extern void pPrPr_disableITimers (void);
148 #endif
149
150 /* -----------------------------------------------------------------------------
151    64-bit operations, defined in longlong.c
152    -------------------------------------------------------------------------- */
153
154 #ifdef SUPPORT_LONG_LONGS
155
156 StgInt stg_gtWord64 (StgWord64, StgWord64);
157 StgInt stg_geWord64 (StgWord64, StgWord64);
158 StgInt stg_eqWord64 (StgWord64, StgWord64);
159 StgInt stg_neWord64 (StgWord64, StgWord64);
160 StgInt stg_ltWord64 (StgWord64, StgWord64);
161 StgInt stg_leWord64 (StgWord64, StgWord64);
162
163 StgInt stg_gtInt64 (StgInt64, StgInt64);
164 StgInt stg_geInt64 (StgInt64, StgInt64);
165 StgInt stg_eqInt64 (StgInt64, StgInt64);
166 StgInt stg_neInt64 (StgInt64, StgInt64);
167 StgInt stg_ltInt64 (StgInt64, StgInt64);
168 StgInt stg_leInt64 (StgInt64, StgInt64);
169
170 StgWord64 stg_remWord64  (StgWord64, StgWord64);
171 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
172
173 StgInt64 stg_remInt64    (StgInt64, StgInt64);
174 StgInt64 stg_quotInt64   (StgInt64, StgInt64);
175 StgInt64 stg_negateInt64 (StgInt64);
176 StgInt64 stg_plusInt64   (StgInt64, StgInt64);
177 StgInt64 stg_minusInt64  (StgInt64, StgInt64);
178 StgInt64 stg_timesInt64  (StgInt64, StgInt64);
179
180 StgWord64 stg_and64  (StgWord64, StgWord64);
181 StgWord64 stg_or64   (StgWord64, StgWord64);
182 StgWord64 stg_xor64  (StgWord64, StgWord64);
183 StgWord64 stg_not64  (StgWord64);
184
185 StgWord64 stg_uncheckedShiftL64   (StgWord64, StgInt);
186 StgWord64 stg_uncheckedShiftRL64  (StgWord64, StgInt);
187 StgInt64  stg_uncheckedIShiftL64  (StgInt64, StgInt);
188 StgInt64  stg_uncheckedIShiftRL64 (StgInt64, StgInt);
189 StgInt64  stg_uncheckedIShiftRA64 (StgInt64, StgInt);
190
191 StgInt64  stg_intToInt64    (StgInt);
192 StgInt    stg_int64ToInt    (StgInt64);
193 StgWord64 stg_int64ToWord64 (StgInt64);
194
195 StgWord64 stg_wordToWord64  (StgWord);
196 StgWord   stg_word64ToWord  (StgWord64);
197 StgInt64  stg_word64ToInt64 (StgWord64);
198
199 StgInt64  stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
200 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
201
202 #endif /* SUPPORT_LONG_LONGS */
203
204 /* -----------------------------------------------------------------------------
205    INLINE functions.
206
207    These functions are given as inlines here for when compiling via C,
208    but we also generate static versions into the cbits library for
209    when compiling to native code.
210    -------------------------------------------------------------------------- */
211
212 #ifndef INLINE
213 # if defined(_MSC_VER)
214 #  define INLINE extern __inline
215 # elif defined(__HUGS__)
216 #  define INLINE INLINE_ONLY
217 # else
218 #  define INLINE extern inline
219 # endif
220 #endif
221
222 INLINE int __hscore_get_errno(void) { return errno; }
223 INLINE void __hscore_set_errno(int e) { errno = e; }
224
225 #if !defined(_MSC_VER)
226 INLINE int __hscore_s_isreg(m)  { return S_ISREG(m);  }
227 INLINE int __hscore_s_isdir(m)  { return S_ISDIR(m);  }
228 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
229 INLINE int __hscore_s_isblk(m)  { return S_ISBLK(m);  }
230 INLINE int __hscore_s_ischr(m)  { return S_ISCHR(m);  }
231 #ifdef S_ISSOCK
232 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
233 #endif
234 #endif
235
236 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
237 INLINE int
238 __hscore_sigemptyset( sigset_t *set )
239 { return sigemptyset(set); }
240
241 INLINE int
242 __hscore_sigfillset( sigset_t *set )
243 { return sigfillset(set); }
244
245 INLINE int
246 __hscore_sigaddset( sigset_t * set, int s )
247 { return sigaddset(set,s); }
248
249 INLINE int
250 __hscore_sigdelset( sigset_t * set, int s )
251 { return sigdelset(set,s); }
252
253 INLINE int
254 __hscore_sigismember( sigset_t * set, int s )
255 { return sigismember(set,s); }
256 #endif
257
258 INLINE void *
259 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
260 { return memcpy(dst+dst_off, src, sz); }
261
262 INLINE void *
263 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
264 { return memcpy(dst, src+src_off, sz); }
265
266 INLINE HsBool
267 __hscore_supportsTextMode()
268 {
269 #if defined(mingw32_HOST_OS)
270   return HS_BOOL_FALSE;
271 #else
272   return HS_BOOL_TRUE;
273 #endif
274 }
275
276 INLINE HsInt
277 __hscore_bufsiz()
278 {
279   return BUFSIZ;
280 }
281
282 INLINE HsInt
283 __hscore_seek_cur()
284 {
285   return SEEK_CUR;
286 }
287
288 INLINE HsInt
289 __hscore_o_binary()
290 {
291 #if defined(_MSC_VER)
292   return O_BINARY;
293 #else
294   return CONST_O_BINARY;
295 #endif
296 }
297
298 INLINE int
299 __hscore_o_rdonly()
300 {
301 #ifdef O_RDONLY
302   return O_RDONLY;
303 #else
304   return 0;
305 #endif
306 }
307
308 INLINE int
309 __hscore_o_wronly( void )
310 {
311 #ifdef O_WRONLY
312   return O_WRONLY;
313 #else
314   return 0;
315 #endif
316 }
317
318 INLINE int
319 __hscore_o_rdwr( void )
320 {
321 #ifdef O_RDWR
322   return O_RDWR;
323 #else
324   return 0;
325 #endif
326 }
327
328 INLINE int
329 __hscore_o_append( void )
330 {
331 #ifdef O_APPEND
332   return O_APPEND;
333 #else
334   return 0;
335 #endif
336 }
337
338 INLINE int
339 __hscore_o_creat( void )
340 {
341 #ifdef O_CREAT
342   return O_CREAT;
343 #else
344   return 0;
345 #endif
346 }
347
348 INLINE int
349 __hscore_o_excl( void )
350 {
351 #ifdef O_EXCL
352   return O_EXCL;
353 #else
354   return 0;
355 #endif
356 }
357
358 INLINE int
359 __hscore_o_trunc( void )
360 {
361 #ifdef O_TRUNC
362   return O_TRUNC;
363 #else
364   return 0;
365 #endif
366 }
367
368 INLINE int
369 __hscore_o_noctty( void )
370 {
371 #ifdef O_NOCTTY
372   return O_NOCTTY;
373 #else
374   return 0;
375 #endif
376 }
377
378 INLINE int
379 __hscore_o_nonblock( void )
380 {
381 #ifdef O_NONBLOCK
382   return O_NONBLOCK;
383 #else
384   return 0;
385 #endif
386 }
387
388 INLINE HsInt
389 __hscore_seek_set( void )
390 {
391   return SEEK_SET;
392 }
393
394 INLINE HsInt
395 __hscore_seek_end( void )
396 {
397   return SEEK_END;
398 }
399
400 INLINE int
401 __hscore_ftruncate( int fd, off_t where )
402 {
403 #if defined(HAVE_FTRUNCATE)
404   return ftruncate(fd,where);
405 #elif defined(HAVE__CHSIZE)
406   return _chsize(fd,where);
407 #else
408 #error at least ftruncate or _chsize functions are required to build
409 #endif
410 }
411
412 INLINE HsInt
413 __hscore_setmode( HsInt fd, HsBool toBin )
414 {
415 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
416   return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
417 #else
418   return 0;
419 #endif
420 }
421
422 INLINE HsInt
423 __hscore_PrelHandle_write( HsInt fd, HsAddr ptr, HsInt off, int sz )
424 {
425   return write(fd,(char *)ptr + off, sz);
426 }
427
428 INLINE HsInt
429 __hscore_PrelHandle_read( HsInt fd, HsAddr ptr, HsInt off, int sz )
430 {
431   return read(fd,(char *)ptr + off, sz);
432
433 }
434
435 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
436 INLINE HsInt
437 __hscore_PrelHandle_send( HsInt fd, HsAddr ptr, HsInt off, int sz )
438 {
439     return send(fd,(char *)ptr + off, sz, 0);
440 }
441
442 INLINE HsInt
443 __hscore_PrelHandle_recv( HsInt fd, HsAddr ptr, HsInt off, int sz )
444 {
445     return recv(fd,(char *)ptr + off, sz, 0);
446 }
447 #endif
448
449 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
450 INLINE long *
451 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
452
453 INLINE char **
454 __hscore_Time_ghcTzname( void ) { return _tzname; }
455 #endif
456
457 INLINE HsInt
458 __hscore_mkdir( HsAddr pathName, HsInt mode )
459 {
460 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
461   return mkdir(pathName);
462 #else
463   return mkdir(pathName,mode);
464 #endif
465 }
466
467 INLINE HsInt
468 __hscore_lstat( HsAddr fname, HsAddr st )
469 {
470 #if HAVE_LSTAT
471   return lstat((const char*)fname, (struct stat*)st);
472 #else
473   return stat((const char*)fname, (struct stat*)st);
474 #endif
475 }
476
477 #ifdef PATH_MAX
478 /* A size that will contain many path names, but not necessarily all
479  * (PATH_MAX is not defined on systems with unlimited path length,
480  * e.g. the Hurd).
481  */
482 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
483 #else
484 INLINE HsInt __hscore_long_path_size() { return 4096; }
485 #endif
486
487 #ifdef R_OK
488 INLINE mode_t __hscore_R_OK() { return R_OK; }
489 #endif
490 #ifdef W_OK
491 INLINE mode_t __hscore_W_OK() { return W_OK; }
492 #endif
493 #ifdef X_OK
494 INLINE mode_t __hscore_X_OK() { return X_OK; }
495 #endif
496
497 #ifdef S_IRUSR
498 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
499 #endif
500 #ifdef S_IWUSR
501 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
502 #endif
503 #ifdef S_IXUSR
504 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
505 #endif
506
507 INLINE HsAddr
508 __hscore_d_name( struct dirent* d )
509 {
510   return (HsAddr)(d->d_name);
511 }
512
513 INLINE HsInt
514 __hscore_end_of_dir( void )
515 {
516   return READDIR_ERRNO_EOF;
517 }
518
519 INLINE void
520 __hscore_free_dirent(HsAddr dEnt)
521 {
522 #if HAVE_READDIR_R
523   free(dEnt);
524 #endif
525 }
526
527 INLINE HsInt
528 __hscore_sizeof_stat( void )
529 {
530   return sizeof(struct stat);
531 }
532
533 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
534 INLINE off_t  __hscore_st_size  ( struct stat* st ) { return st->st_size; }
535 #if !defined(_MSC_VER)
536 INLINE mode_t __hscore_st_mode  ( struct stat* st ) { return st->st_mode; }
537 #endif
538
539 #if HAVE_TERMIOS_H
540 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
541
542 INLINE void
543 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
544
545 INLINE unsigned char*
546 __hscore_ptr_c_cc( struct termios* ts )
547 { return (unsigned char*) &ts->c_cc; }
548
549 INLINE HsInt
550 __hscore_sizeof_termios( void )
551 {
552 #ifndef mingw32_HOST_OS
553   return sizeof(struct termios);
554 #else
555   return 0;
556 #endif
557 }
558 #endif
559
560 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
561 INLINE HsInt
562 __hscore_sizeof_sigset_t( void )
563 {
564   return sizeof(sigset_t);
565 }
566 #endif
567
568 INLINE int
569 __hscore_echo( void )
570 {
571 #ifdef ECHO
572   return ECHO;
573 #else
574   return 0;
575 #endif
576
577 }
578
579 INLINE int
580 __hscore_tcsanow( void )
581 {
582 #ifdef TCSANOW
583   return TCSANOW;
584 #else
585   return 0;
586 #endif
587
588 }
589
590 INLINE int
591 __hscore_icanon( void )
592 {
593 #ifdef ICANON
594   return ICANON;
595 #else
596   return 0;
597 #endif
598 }
599
600 INLINE int __hscore_vmin( void )
601 {
602 #ifdef VMIN
603   return VMIN;
604 #else
605   return 0;
606 #endif
607 }
608
609 INLINE int __hscore_vtime( void )
610 {
611 #ifdef VTIME
612   return VTIME;
613 #else
614   return 0;
615 #endif
616 }
617
618 INLINE int __hscore_sigttou( void )
619 {
620 #ifdef SIGTTOU
621   return SIGTTOU;
622 #else
623   return 0;
624 #endif
625 }
626
627 INLINE int __hscore_sig_block( void )
628 {
629 #ifdef SIG_BLOCK
630   return SIG_BLOCK;
631 #else
632   return 0;
633 #endif
634 }
635
636 INLINE int __hscore_sig_setmask( void )
637 {
638 #ifdef SIG_SETMASK
639   return SIG_SETMASK;
640 #else
641   return 0;
642 #endif
643 }
644
645 INLINE int
646 __hscore_f_getfl( void )
647 {
648 #ifdef F_GETFL
649   return F_GETFL;
650 #else
651   return 0;
652 #endif
653 }
654
655 INLINE int
656 __hscore_f_setfl( void )
657 {
658 #ifdef F_SETFL
659   return F_SETFL;
660 #else
661   return 0;
662 #endif
663 }
664
665 // defined in rts/RtsStartup.c.
666 extern void* __hscore_get_saved_termios(int fd);
667 extern void __hscore_set_saved_termios(int fd, void* ts);
668
669 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
670
671 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
672 INLINE int __hsposix_SIGABRT()   { return SIGABRT; }
673 INLINE int __hsposix_SIGALRM()   { return SIGALRM; }
674 INLINE int __hsposix_SIGBUS()    { return SIGBUS; }
675 INLINE int __hsposix_SIGCHLD()   { return SIGCHLD; }
676 INLINE int __hsposix_SIGCONT()   { return SIGCONT; }
677 INLINE int __hsposix_SIGFPE()    { return SIGFPE; }
678 INLINE int __hsposix_SIGHUP()    { return SIGHUP; }
679 INLINE int __hsposix_SIGILL()    { return SIGILL; }
680 INLINE int __hsposix_SIGINT()    { return SIGINT; }
681 INLINE int __hsposix_SIGKILL()   { return SIGKILL; }
682 INLINE int __hsposix_SIGPIPE()   { return SIGPIPE; }
683 INLINE int __hsposix_SIGQUIT()   { return SIGQUIT; }
684 INLINE int __hsposix_SIGSEGV()   { return SIGSEGV; }
685 INLINE int __hsposix_SIGSTOP()   { return SIGSTOP; }
686 INLINE int __hsposix_SIGTERM()   { return SIGTERM; }
687 INLINE int __hsposix_SIGTSTP()   { return SIGTSTP; }
688 INLINE int __hsposix_SIGTTIN()   { return SIGTTIN; }
689 INLINE int __hsposix_SIGTTOU()   { return SIGTTOU; }
690 INLINE int __hsposix_SIGUSR1()   { return SIGUSR1; }
691 INLINE int __hsposix_SIGUSR2()   { return SIGUSR2; }
692 #ifdef SIGPOLL
693 INLINE int __hsposix_SIGPOLL()   { return SIGPOLL; }
694 #endif
695 INLINE int __hsposix_SIGPROF()   { return SIGPROF; }
696 INLINE int __hsposix_SIGSYS()    { return SIGSYS; }
697 INLINE int __hsposix_SIGTRAP()   { return SIGTRAP; }
698 INLINE int __hsposix_SIGURG()    { return SIGURG; }
699 INLINE int __hsposix_SIGVTALRM() { return SIGVTALRM; }
700 INLINE int __hsposix_SIGXCPU()   { return SIGXCPU; }
701 INLINE int __hsposix_SIGXFSZ()   { return SIGXFSZ; }
702
703 INLINE int __hsposix_SIG_BLOCK()   { return SIG_BLOCK; }
704 INLINE int __hsposix_SIG_UNBLOCK() { return SIG_UNBLOCK; }
705 INLINE int __hsposix_SIG_SETMASK() { return SIG_SETMASK; }
706
707 #endif /* mingw32_HOST_OS */
708
709 INLINE int __hscore_open(char *file, int how, mode_t mode) {
710 #ifdef mingw32_HOST_OS
711         if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
712           return _sopen(file,how,_SH_DENYRW,mode);
713         else
714           return _sopen(file,how,_SH_DENYWR,mode);
715 #else
716         return open(file,how,mode);
717 #endif
718 }
719
720 // These are wrapped because on some OSs (eg. Linux) they are
721 // macros which redirect to the 64-bit-off_t versions when large file
722 // support is enabled.
723 //
724 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
725         return (lseek(fd,off,whence));
726 }
727
728 INLINE int __hscore_stat(char *file, struct stat *buf) {
729         return (stat(file,buf));
730 }
731
732 INLINE int __hscore_fstat(int fd, struct stat *buf) {
733         return (fstat(fd,buf));
734 }
735
736 // select-related stuff
737
738 #if !defined(mingw32_HOST_OS)
739 INLINE void hsFD_CLR(int fd, fd_set *fds) { FD_CLR(fd, fds); }
740 INLINE int  hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
741 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
742 INLINE int  sizeof_fd_set(void) { return sizeof(fd_set); }
743 extern void hsFD_ZERO(fd_set *fds);
744 #endif
745
746 // gettimeofday()-related
747
748 #if !defined(mingw32_HOST_OS)
749 #define TICK_FREQ  50
750
751 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
752
753 INLINE HsInt getTicksOfDay(void)
754 {
755     struct timeval tv;
756     gettimeofday(&tv, (struct timezone *) NULL);
757     return (tv.tv_sec * TICK_FREQ +
758             tv.tv_usec * TICK_FREQ / 1000000);
759 }
760
761 INLINE void setTimevalTicks(struct timeval *p, HsInt ticks)
762 {
763     p->tv_sec  = ticks / TICK_FREQ;
764     p->tv_usec = (ticks % TICK_FREQ) * (1000000 / TICK_FREQ);
765 }
766 #endif // !defined(mingw32_HOST_OS)
767
768 // Directory-related
769
770 #if defined(mingw32_HOST_OS)
771
772 /* Make sure we've got the reqd CSIDL_ constants in scope;
773  * w32api header files are lagging a bit in defining the full set.
774  */
775 #if !defined(CSIDL_APPDATA)
776 #define CSIDL_APPDATA 0x001a
777 #endif
778 #if !defined(CSIDL_PERSONAL)
779 #define CSIDL_PERSONAL 0x0005
780 #endif
781 #if !defined(CSIDL_PROFILE)
782 #define CSIDL_PROFILE 0x0028
783 #endif
784 #if !defined(CSIDL_WINDOWS)
785 #define CSIDL_WINDOWS 0x0024
786 #endif
787
788 INLINE int __hscore_CSIDL_PROFILE()  { return CSIDL_PROFILE;  }
789 INLINE int __hscore_CSIDL_APPDATA()  { return CSIDL_APPDATA;  }
790 INLINE int __hscore_CSIDL_WINDOWS()  { return CSIDL_WINDOWS;  }
791 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
792 #endif
793
794 /* ToDo: write a feature test that doesn't assume 'environ' to
795  *    be in scope at link-time. */
796 extern char** environ;
797 INLINE char **__hscore_environ() { return environ; }
798
799 #endif /* __HSBASE_H__ */
800