[project @ 2001-01-17 15:11:04 by simonmar]
[ghc-hetmet.git] / ghc / interpreter / nHandle.c
1
2 /* This is a hack.  I totally deny writing it.  If this code breaks,
3  * you get to keep all the pieces.  JRS, 23 feb 99.
4  */
5
6 #include <stdio.h>
7 #include <errno.h>
8 #include <assert.h>
9 #include <malloc.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #ifndef _WIN32
13 #include <sys/times.h>
14 #include <sys/resource.h>
15 #include <sys/stat.h>
16 #include <time.h>
17 #endif
18 #include <unistd.h>
19
20 #ifndef _WIN32
21 double nh_getCPUtime ( void )
22 {
23    double usertime;
24    struct rusage usage;
25    getrusage ( RUSAGE_SELF, &usage );
26    usertime = (double)usage.ru_utime.tv_sec +
27               (double)usage.ru_utime.tv_usec / 1000000.0;
28    return usertime;
29 }
30
31 double nh_getCPUprec ( void )
32 {
33    /* or perhaps CLOCKS_PER_SEC ? */
34    return 1.0 / (double)(CLK_TCK);
35 }
36 #else
37 double nh_getCPUtime ( void )
38 {
39    return 1;
40 }
41
42 double nh_getCPUprec ( void )
43 {
44    return 1;
45 }
46 #endif
47
48 int nh_getPID ( void )
49 {
50 #ifndef _WIN32
51    return (int) getpid();
52 #else
53    return (int) 0;
54 #endif
55 }
56
57 void nh_exitwith ( int code )
58 {
59    exit(code);
60 }
61
62 int nh_system ( char* cmd )
63 {
64    return system ( cmd );
65 }
66
67 int nh_iseof ( FILE* f )
68 {
69    int c;
70    errno = 0;
71    c = fgetc ( f );
72    if (c == EOF) return 1;
73    ungetc ( c, f );
74    return 0;
75 }
76
77 int nh_filesize ( FILE* f )
78 {
79 #ifndef _WIN32
80    struct stat buf;
81    errno = 0;
82    fstat ( fileno(f), &buf );
83    return buf.st_size;
84 #else
85    errno = EPERM;
86    return 0;
87 #endif
88 }
89
90 int nh_stdin ( void )
91 {
92    errno = 0;
93    return (int)stdin;
94 }
95
96 int nh_stdout ( void )
97 {
98    errno = 0;
99    return (int)stdout;
100 }
101
102 int nh_stderr ( void )
103 {
104    errno = 0;
105    return (int)stderr;
106 }
107
108 int nh_open ( char* fname, int wr )
109 {
110    FILE* f;
111    errno = 0;
112    f = fopen ( fname, (wr==0) ? "r":  ((wr==1) ? "w" : "a") );
113    return (int)f;
114 }
115
116 void nh_close ( FILE* f )
117 {
118    errno = 0;
119    fflush ( f );
120    fclose ( f );
121 }
122
123 void nh_flush ( FILE* f )
124 {
125    errno = 0;
126    fflush ( f );
127 }
128
129 void nh_write ( FILE* f, int c )
130 {
131    errno = 0;
132    fputc(c,f);
133    if (f==stderr) { fflush(f); } 
134    if (f==stdout) { fflush(f); } 
135 }
136
137 int nh_read ( FILE* f )
138 {
139    errno = 0;
140    return fgetc(f);
141 }
142
143 int nh_errno ( void )
144 {
145    int t = errno;
146    errno = 0;
147    return t;
148 }
149
150 int nh_malloc ( int n )
151 {
152    char* p = malloc(n);
153    return (int)p;
154 }
155
156 void nh_free ( int n )
157 {
158    free ( (char*)n );
159 }
160
161 void nh_store ( int p, int ch )
162 {
163    *(char*)p = (char)ch;
164 }
165
166 int nh_load ( int p )
167 {
168    return (int)(*(char*)p);
169 }
170
171 int nh_getenv ( int p )
172 {
173    return (int)getenv ( (const char *)p );
174 }
175