Coding style cleanup
[platform/kernel/u-boot.git] / board / MAI / bios_emulator / scitech / src / pm / vdd / fileio.c
1 /****************************************************************************
2 *
3 *                   SciTech OS Portability Manager Library
4 *
5 *  ========================================================================
6 *
7 *    The contents of this file are subject to the SciTech MGL Public
8 *    License Version 1.0 (the "License"); you may not use this file
9 *    except in compliance with the License. You may obtain a copy of
10 *    the License at http://www.scitechsoft.com/mgl-license.txt
11 *
12 *    Software distributed under the License is distributed on an
13 *    "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 *    implied. See the License for the specific language governing
15 *    rights and limitations under the License.
16 *
17 *    The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
18 *
19 *    The Initial Developer of the Original Code is SciTech Software, Inc.
20 *    All Rights Reserved.
21 *
22 *  ========================================================================
23 *
24 * Language:     ANSI C
25 * Environment:  32-bit OS/2 VDD
26 *
27 * Description:  C library compatible I/O functions for use within a VDD.
28 *
29 ****************************************************************************/
30
31 #include "pmapi.h"
32 #include "vddfile.h"
33
34 /*------------------------ Main Code Implementation -----------------------*/
35
36 #define EOF -1
37
38 /* NB: none of the file VDHs are available during the DOS session          */
39 /* initialzation context!                                                  */
40
41 /* Macros for Open/Close APIs to allow using this module in both VDDs and  */
42 /* normal OS/2 applications. Unfortunately VDHRead/Write/Seek don't map to */
43 /* their Dos* counterparts so cleanly.                                     */
44 #ifdef __OS2_VDD__
45 #define _OS2Open    VDHOpen
46 #define _OS2Close   VDHClose
47 #else
48 #define _OS2Open    DosOpen
49 #define _OS2Close   DosClose
50 #endif
51
52 /****************************************************************************
53 REMARKS:
54 VDD implementation of the ANSI C fopen function.
55 ****************************************************************************/
56 FILE * fopen(
57     const char *filename,
58     const char *mode)
59 {
60     FILE    *f = PM_malloc(sizeof(FILE));
61     long    oldpos;
62     ULONG   rc, ulAction;
63     ULONG   omode, oflags;
64
65     if (f != NULL) {
66         f->offset = 0;
67         f->text = (mode[1] == 't' || mode[2] == 't');
68         f->writemode = (mode[0] == 'w') || (mode[0] == 'a');
69         f->unputc = EOF;
70         f->endp = f->buf + sizeof(f->buf);
71         f->curp = f->startp = f->buf;
72
73         if (mode[0] == 'r') {
74             #ifdef __OS2_VDD__
75             omode  = VDHOPEN_ACCESS_READONLY | VDHOPEN_SHARE_DENYNONE;
76             oflags = VDHOPEN_ACTION_OPEN_IF_EXISTS | VDHOPEN_ACTION_FAIL_IF_NEW;
77             #else
78             omode  = OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE;
79             oflags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
80             #endif
81             }
82         else if (mode[0] == 'w') {
83             #ifdef __OS2_VDD__
84             omode  = VDHOPEN_ACCESS_WRITEONLY | VDHOPEN_SHARE_DENYWRITE;
85             oflags = VDHOPEN_ACTION_REPLACE_IF_EXISTS | VDHOPEN_ACTION_CREATE_IF_NEW;
86             #else
87             omode  = OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYWRITE;
88             oflags = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
89             #endif
90             }
91         else {
92             #ifdef __OS2_VDD__
93             omode  = VDHOPEN_ACCESS_READWRITE | VDHOPEN_SHARE_DENYWRITE;
94             oflags = VDHOPEN_ACTION_OPEN_IF_EXISTS | VDHOPEN_ACTION_CREATE_IF_NEW;
95             #else
96             omode  = OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYWRITE;
97             oflags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
98             #endif
99             }
100         rc = _OS2Open((PSZ)filename, (PHFILE)&f->handle, &ulAction, 0, VDHOPEN_FILE_NORMAL, oflags, omode, NULL);
101         if (rc != 0) {
102             PM_free(f);
103             return NULL;
104             }
105
106         #ifdef __OS2_VDD__
107         f->filesize = VDHSeek((HFILE)f->handle, 0, VDHSK_END_OF_FILE);
108         #else
109         rc = DosSetFilePtr((HFILE)f->handle, 0, FILE_END, &f->filesize);
110         #endif
111
112         if (mode[0] == 'a')
113             fseek(f,0,2);
114     }
115     return f;
116 }
117
118 /****************************************************************************
119 REMARKS:
120 VDD implementation of the ANSI C fread function. Note that unlike Windows VxDs,
121 OS/2 VDDs are not limited to 64K reads or writes.
122 ****************************************************************************/
123 size_t fread(
124     void *ptr,
125     size_t size,
126     size_t n,
127     FILE *f)
128 {
129     char    *buf = ptr;
130     int     bytes,readbytes,totalbytes = 0;
131
132     /* First copy any data already read into our buffer */
133     if ((bytes = (f->curp - f->startp)) > 0) {
134         memcpy(buf,f->curp,bytes);
135         f->startp = f->curp = f->buf;
136         buf += bytes;
137         totalbytes += bytes;
138         bytes = (size * n) - bytes;
139         }
140     else
141         bytes = size * n;
142     if (bytes) {
143         #ifdef __OS2_VDD__
144         readbytes = VDHRead((HFILE)f->handle, buf, bytes);
145         #else
146         DosRead((HFILE)f->handle, buf, bytes, &readbytes);
147         #endif
148         totalbytes += readbytes;
149         f->offset += readbytes;
150         }
151     return totalbytes / size;
152 }
153
154 /****************************************************************************
155 REMARKS:
156 VDD implementation of the ANSI C fwrite function.
157 ****************************************************************************/
158 size_t fwrite(
159     void *ptr,
160     size_t size,
161     size_t n,
162     FILE *f)
163 {
164     char        *buf = ptr;
165     int         bytes,writtenbytes,totalbytes = 0;
166
167     /* Flush anything already in the buffer */
168     if (!f->writemode)
169         return 0;
170     fflush(f);
171     bytes = size * n;
172     #ifdef __OS2_VDD__
173     writtenbytes = VDHWrite((HFILE)f->handle, buf, bytes);
174     #else
175     DosWrite((HFILE)f->handle, buf, bytes, &writtenbytes);
176     #endif
177     totalbytes += writtenbytes;
178     f->offset += writtenbytes;
179     if (f->offset > f->filesize)
180         f->filesize = f->offset;
181     return totalbytes / size;
182 }
183
184 /****************************************************************************
185 REMARKS:
186 VxD implementation of the ANSI C fflush function.
187 ****************************************************************************/
188 int fflush(
189     FILE *f)
190 {
191     ULONG     bytes;
192
193     /* First copy any data already written into our buffer */
194     if (f->writemode && (bytes = (f->curp - f->startp)) > 0) {
195         #ifdef __OS2_VDD__
196         bytes = VDHWrite((HFILE)f->handle, f->startp, bytes);
197         #else
198         DosWrite((HFILE)f->handle, f->startp, bytes, &bytes);
199         #endif
200         f->offset += bytes;
201         if (f->offset > f->filesize)
202             f->filesize = f->offset;
203         f->startp = f->curp = f->buf;
204         }
205     return 0;
206 }
207
208 /****************************************************************************
209 REMARKS:
210 VDD implementation of the ANSI C fseek function.
211 ****************************************************************************/
212 int fseek(
213     FILE *f,
214     long int offset,
215     int whence)
216 {
217     fflush(f);
218
219     if (whence == 0)
220         f->offset = offset;
221     else if (whence == 1)
222         f->offset += offset;
223     else if (whence == 2)
224         f->offset = f->filesize + offset;
225
226     #ifdef __OS2_VDD__
227     VDHSeek((HFILE)f->handle, f->offset, VDHSK_ABSOLUTE);
228     #else
229     DosSetFilePtr((HFILE)f->handle, f->offset, FILE_BEGIN, NULL);
230     #endif
231
232     return 0;
233 }
234
235 /****************************************************************************
236 REMARKS:
237 VDD implementation of the ANSI C ftell function.
238 ****************************************************************************/
239 long ftell(
240     FILE *f)
241 {
242     long    offset;
243
244     offset = (f->curp - f->startp);
245     offset += f->offset;
246     return offset;
247 }
248
249 /****************************************************************************
250 REMARKS:
251 VDD implementation of the ANSI C feof function.
252 ****************************************************************************/
253 int feof(
254     FILE *f)
255 {
256     return (f->offset == f->filesize);
257 }
258
259 /****************************************************************************
260 REMARKS:
261 Read a single character from the input file buffer, including translation
262 of the character in text transation modes.
263 ****************************************************************************/
264 static int __getc(
265     FILE *f)
266 {
267     int c;
268
269     if (f->unputc != EOF) {
270         c = f->unputc;
271         f->unputc = EOF;
272         }
273     else {
274         if (f->startp == f->curp) {
275             int bytes = fread(f->buf,1,sizeof(f->buf),f);
276             if (bytes == 0)
277                 return EOF;
278             f->curp = f->startp + bytes;
279             }
280         c = *f->startp++;
281         if (f->text && c == '\r') {
282             int nc = __getc(f);
283             if (nc != '\n')
284                 f->unputc = nc;
285             }
286         }
287     return c;
288 }
289
290 /****************************************************************************
291 REMARKS:
292 Write a single character from to input buffer, including translation of the
293 character in text transation modes.
294 ****************************************************************************/
295 static int __putc(int c,FILE *f)
296 {
297     int count = 1;
298     if (f->text && c == '\n') {
299         __putc('\r',f);
300         count = 2;
301         }
302     if (f->curp == f->endp)
303         fflush(f);
304     *f->curp++ = c;
305     return count;
306 }
307
308 /****************************************************************************
309 REMARKS:
310 VxD implementation of the ANSI C fgets function.
311 ****************************************************************************/
312 char *fgets(
313     char *s,
314     int n,
315     FILE *f)
316 {
317     int c = 0;
318     char *cs;
319
320     cs = s;
321     while (--n > 0 && (c = __getc(f)) != EOF) {
322         *cs++ = c;
323         if (c == '\n')
324             break;
325         }
326     if (c == EOF && cs == s)
327         return NULL;
328     *cs = '\0';
329     return s;
330 }
331
332 /****************************************************************************
333 REMARKS:
334 VxD implementation of the ANSI C fputs function.
335 ****************************************************************************/
336 int fputs(
337     const char *s,
338     FILE *f)
339 {
340     int r = 0;
341     int c;
342
343     while ((c = *s++) != 0)
344         r = __putc(c, f);
345     return r;
346 }
347
348 /****************************************************************************
349 REMARKS:
350 VxD implementation of the ANSI C fclose function.
351 ****************************************************************************/
352 int fclose(
353     FILE *f)
354 {
355     fflush(f);
356     _OS2Close((HFILE)f->handle);
357     PM_free(f);
358     return 0;
359 }