d8be2e57de2b1616ab540711bddf849973f1c8da
[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
13 int nh_stdin ( void )
14 {
15    errno = 0;
16    return (int)stdin;
17 }
18
19 int nh_stdout ( void )
20 {
21    errno = 0;
22    return (int)stdout;
23 }
24
25 int nh_stderr ( void )
26 {
27    errno = 0;
28    return (int)stderr;
29 }
30
31 int nh_open ( char* fname, int wr )
32 {
33    FILE* f;
34    errno = 0;
35    f = fopen ( fname, (wr==0) ? "r":  ((wr==1) ? "w" : "a") );
36    return (int)f;
37 }
38
39 void nh_close ( FILE* f )
40 {
41    errno = 0;
42    fflush ( f );
43    fclose ( f );
44 }
45
46 void nh_flush ( FILE* f )
47 {
48    errno = 0;
49    fflush ( f );
50 }
51
52 void nh_write ( FILE* f, int c )
53 {
54    errno = 0;
55    fputc(c,f);
56    if (f==stderr) { fflush(f); } 
57    else if (f==stdin && isspace(c)) { fflush(f); };
58
59 }
60
61 int nh_read ( FILE* f )
62 {
63    errno = 0;
64    return fgetc(f);
65 }
66
67 int nh_errno ( void )
68 {
69    return errno;
70 }
71
72 int nh_malloc ( int n )
73 {
74    char* p = malloc(n);
75    return (int)p;
76 }
77
78 void nh_free ( int n )
79 {
80    free ( (char*)n );
81 }
82
83 void nh_store ( int p, int ch )
84 {
85    *(char*)p = (char)ch;
86 }
87
88 int nh_load ( int p )
89 {
90    return (int)(*(char*)p);
91 }
92
93 int nh_getenv ( int p )
94 {
95    return (int)getenv ( (const char *)p );
96 }
97
98