d1f64ba32707ed41c6822fa54d8c6993954e4243
[ghc-hetmet.git] / ghc / lib / std / cbits / ilxstubs.c
1 /* 
2  * (c) The GHC Team 2001
3  *
4  * $Id: ilxstubs.c,v 1.3 2001/08/15 09:23:25 rrt Exp $
5  *
6  * ILX stubs for external function calls
7  */
8
9 /*
10   All foreign imports from the C standard library are stubbed out here,
11   so that they are all in the same DLL (HSstd_cbits), and the ILX code
12   generator doesn't have to be told or guess which DLL they are in.
13   Calls to the Win32 API are annotated with the DLL they come from.
14
15   The general rule is that all foreign imports are assumed to be in
16   <current_package>_cbits.dll unless a DLL is explicitly given.
17 */
18
19
20 #include "Stg.h"
21 #include "HsStd.h"
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <dirent.h>
25 #include <limits.h>
26
27 /* From the RTS */
28
29     /* StgPrimFloat Add to mini-RTS, which is put in a DLL */
30
31     /* Need to be implemented in ILX RTS */
32 /*../PrelStable.lhs:37:foreign import unsafe freeStablePtr :: StablePtr a -> IO ()
33 ../PrelTopHandler.lhs:49:foreign import ccall "shutdownHaskellAndExit" 
34 ../PrelTopHandler.lhs:77:foreign import ccall "stackOverflow" unsafe
35 ../PrelTopHandler.lhs:80:foreign import ccall "stg_exit" unsafe */
36
37 /* Fake this for now, as a DLL can't refer to main() */
38 int argc = 0;
39 char **argv = NULL;
40
41 int
42 prog_argc_addr(void)
43 {
44   return &argc;
45 }
46
47 char ***
48 prog_argv_addr(void)
49 {
50   return &argv;
51 }
52
53 void
54 stg_exit(I_ n)
55 {
56   fprintf(stderr, "doing stg_exit(%d)\n", n);
57   exit(n);
58 }
59
60 /* The code is in includes/Stable.h [sic] */
61 void
62 freeStablePtr(StgStablePtr sp)
63 {
64   fprintf(stderr, "Freeing stable ptr %p (NOT!)\n", sp);
65 }
66
67 void
68 shutdownHaskellAndExit(int n)
69 {
70   stg_exit(n);
71 }
72
73 void 
74 stackOverflow(void)
75 {
76 }
77
78 void
79 ErrorHdrHook (long fd)
80 {
81     const char msg[] = "\nFail: ";
82     write(fd, msg, sizeof(msg)-1);
83 }
84
85
86
87 /* Import directly from correct DLL */
88
89      /*../CPUTime.hsc:107:foreign import "GetCurrentProcess" unsafe getCurrentProcess :: IO (Ptr HANDLE)
90        ../CPUTime.hsc:108:foreign import "GetProcessTimes" unsafe getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt */
91
92 int s_mkdir(const char *s) { return mkdir(s); }
93 int s_chmod(const char *s, mode_t m) { return chmod(s, m); }
94 int s_access(const char *s, int m) { return access(s, m); }
95 char *s_getcwd(char *s, size_t n) { return getcwd(s, n); }
96 int s_rmdir(const char *s) { return rmdir(s); }
97 int s_chdir(const char *s) { return chdir(s); }
98 int s_unlink(const char *s) { return unlink(s); }
99 int s_rename(const char *s1, const char *s2) { return rename(s1, s2); }
100 DIR *s_opendir(const char *s) { return opendir(s); }
101 struct dirent *s_readdir(DIR *d) { return readdir(d); }
102 int s_closedir(DIR *d) { return closedir(d); }
103 int s_stat(const char *s, struct stat *buf) { return stat(s, buf); }
104 int s_fstat(int f, struct stat* buf) { return fstat(f, buf); }
105 int s_open(const char *s, int f) { return open(s, f); }
106 int s_close(int f) { return close(f); }
107 int s_write(int f, const void *buf, size_t n) { return write(f, buf, n); }
108 int s_read(int f, void *buf, size_t n) { return read(f, buf, n); }
109 int s_lseek(int f, off_t off, int w) { return lseek(f, off, w); }
110 int s_isatty(int f) { return isatty(f); }
111 void *s_memcpy(void *d, const void *s, size_t n) { return memcpy(d, s, n); }
112 void *s_memmove(void *d, const void *s, size_t n) { return memmove(d, s, n); }
113 char *s_strerror(int e) { return strerror(e); }
114 int s_setmode(int a, int b) { return setmode(a,b); }
115 void *s_malloc(size_t n) { return malloc(n); }
116 void *s_realloc(void *p, size_t n) { return realloc(p, n); }
117 void s_free(void *p) { free(p); }
118 char *s_getenv(const char *s) { return getenv(s); }
119 struct tm *s_localtime(const time_t *p) { return localtime(p); }
120 struct tm *s_gmtime(const time_t *p) { return gmtime(p); }
121 time_t s_mktime(struct tm *p) { return mktime(p); }
122 time_t s_time(time_t *p) { return time(p); }
123 void s_ftime(struct timeb *p) { ftime(p); }