use the spoofed uname compiling git.
[AROS-Contrib.git] / rexx / bsub / bio.c
blob292c5b45defa407f9e2f1daffac4869fd2142cde
1 /*
2 * $Header$
3 * $Log$
4 * Revision 1.1 2001/04/04 05:43:36 wang
5 * First commit: compiles on Linux, Amiga, Windows, Windows CE, generic gcc
7 * Revision 1.1 1999/09/13 15:06:41 bnv
8 * Initial revision
11 * This file provides some substitus for the common fxxxx I/O commands
12 * that unfortunatelly do not exist on the Windows CE of Visual C++.
15 #include <bio.h>
16 #include <bstr.h>
17 #include <string.h>
18 #include <stdlib.h>
20 static char buffer[128];
21 static int bufferpos=0;
22 static BOOL newline=FALSE;
24 /* ----- Bfopen ----- */
25 BFILE*
26 Bfopen( const char *filename, const char *mode )
28 #ifndef __BORLANDC__
29 TCHAR path[128];
30 #endif
31 BFILE *f;
32 HANDLE hnd;
33 int bitmode=0;
34 int textmode=1;
35 char *ch;
37 /* scan mode string to find options */
38 for (ch=(const char *)mode; *ch; ch++) {
39 switch (*ch) {
40 case 'r':
41 bitmode |= BIO_READ;
42 break;
43 case 'w':
44 bitmode |= BIO_WRITE;
45 break;
46 case 'b':
47 textmode = 0;
48 bitmode |= BIO_BINARY;
49 break;
50 case '+':
51 bitmode |= BIO_APPEND;
52 break;
53 case 'u':
54 bitmode |= BIO_UNICODE;
55 break;
58 if (textmode)
59 bitmode |= BIO_TEXT;
61 #if defined(__BORLANDC__)
62 if ((bitmode & (BIO_READ|BIO_WRITE)) == (BIO_READ|BIO_WRITE))
63 hnd = _lopen(filename,READ_WRITE);
64 else
65 if (bitmode & BIO_READ)
66 hnd = _lopen(filename,READ);
67 else
68 if (bitmode & BIO_WRITE)
69 hnd = _lopen(filename,WRITE);
70 else
71 return NULL;
72 if ((HFILE)hnd == HFILE_ERROR)
73 return NULL;
74 #else
75 mbstowcs(path,filename,STRLEN(filename)+1);
77 if ((bitmode & (BIO_READ|BIO_WRITE)) == (BIO_READ|BIO_WRITE)) {
78 hnd = CreateFile( path,
79 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL,
80 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
81 } else
82 if (bitmode & BIO_READ) {
83 hnd = CreateFile(path,
84 GENERIC_READ, FILE_SHARE_READ, NULL,
85 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
86 } else
87 if (bitmode & BIO_WRITE) {
88 hnd = CreateFile( path,
89 GENERIC_WRITE, FILE_SHARE_READ, NULL,
90 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
91 } else
92 return NULL;
94 if (hnd==INVALID_HANDLE_VALUE)
95 return NULL;
96 #endif
97 f = (BFILE*)malloc(sizeof(BFILE));
98 f->handle = hnd;
99 f->mode = bitmode;
100 return f;
101 } /* Bfopen */
103 /* ----- Bfclose ----- */
105 Bfclose( BFILE *stream )
107 if (!stream) return EOF;
108 #if defined(__BORLANDC__)
109 _lclose(stream->handle);
110 #else
111 CloseHandle(stream->handle);
112 #endif
113 free(stream);
114 return 0;
115 } /* Bfclose */
117 /* ----- Bfflush ----- */
119 Bfflush(BFILE *stream)
121 if (!stream) return EOF;
122 return SetEndOfFile(stream->handle);
123 } /* Bfflush */
125 /* ----- Bfeof ----- */
127 Bfeof(BFILE *stream)
129 return (stream->mode & BIO_EOF);
130 } /* Bfeof */
132 /* ----- Bfgetc ----- */
134 Bfgetc( BFILE *stream )
136 char c;
137 #if !defined(__BORLANDC__)
138 int len;
139 #endif
141 if (stream==NULL)
142 return Bgetchar();
144 #if defined(__BORLANDC__)
145 if (_lread(stream->handle, &c, 1)==0) {
146 /* mark eof */
147 stream->mode |= BIO_EOF;
148 return EOF;
150 #else
151 ReadFile(stream->handle, &c, 1, &len, NULL);
152 #endif
153 if ((c==0x0D) && (stream->mode & BIO_TEXT)) {
154 #if defined(__BORLANDC__)
155 if (_lread(stream->handle, &c, 1)==0) {
156 /* mark eof */
157 stream->mode |= BIO_EOF;
158 return EOF;
160 #else
161 ReadFile(stream->handle, &c, 1, &len, NULL);
162 #endif
164 #if !defined(__BORLANDC__)
165 if (len==0) {
166 /* mark eof */
167 stream->mode |= BIO_EOF;
168 return EOF;
170 #endif
171 return (BYTE)c;
172 } /* Bfgetc */
174 /* ----- Bputc ----- */
176 Bfputc( char ch, BFILE *stream )
178 #if !defined(__BORLANDC__)
179 int written;
180 #endif
181 TCHAR cr;
183 if (stream==NULL) {
184 PUTCHAR(ch);
185 return ch;
188 if (ch==0x0A && (stream->mode & BIO_TEXT)) {
189 cr = TEXT('\015'); // 0x0D
190 #if defined(__BORLANDC__)
191 _lwrite(stream->handle, &cr, 1);
193 _lwrite(stream->handle, &ch, 1);
194 #else
195 WriteFile(stream->handle, &cr, 1, &written, NULL);
197 WriteFile(stream->handle, &ch, 1, &written, NULL);
198 #endif
199 return ch;
200 } /* Bfgetc */
202 /* ------ Bfputs -------- */
203 void
204 Bfputs( const char *s, BFILE *stream )
206 while (*s)
207 Bfputc(*s++,stream);
208 } /* Bfputs */
210 /* ------ Read a Character -------- */
211 char
212 Bgetchar()
214 char c;
216 /* return a new character only a new line is found in the buffer */
217 while (!newline) {
218 c = WReadKey();
219 switch (c) {
220 case 0:
221 WReadKey();
222 continue;
224 case 8:
225 if (bufferpos) {
226 bufferpos--;
227 Bputch(c);
229 continue;
231 case 13:
232 case 10:
233 newline = TRUE;
234 c = '\n';
235 break;
236 case 27:
237 // Erase contents of buffer
238 WGotoXY(WWhereX()-bufferpos,WWhereY());
239 bufferpos = 0;
240 WClreol();
241 continue;
243 if (bufferpos<sizeof(buffer)) {
244 Bputch(c);
245 buffer[bufferpos++] = c;
246 } else
247 MessageBeep(0);
249 c = buffer[0];
250 if (c=='\n') newline = FALSE;
251 bufferpos--;
252 memmove(buffer, buffer+1, bufferpos);
253 return c;
254 } /* Bgetchar */
256 /* ------ Write an ASCII char ----- */
257 void
258 Bputch( char ch )
260 TCHAR tch;
261 tch = (TCHAR)ch & 0xFF;
262 WWriteBuf(&tch, 1);
263 } /* Bputch */
265 /* ------ Write an ASCII string ----- */
266 // Wanring: DIFFERENT behavior from puts(). It doesn't append a new line
267 void
268 Bputs( const char *str )
270 while (*str)
271 Bputch(*str++);
272 } /* Bputs */
274 /* ------ Write a number ----- */
275 void
276 Bputint( long num, int length, int radix )
278 char str[10];
279 Bputs(Bl2a(str,num,length,radix));
280 } /* Bputint */