1 /****************************************************************************
3 * SciTech OS Portability Manager Library
5 * ========================================================================
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
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.
17 * The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
19 * The Initial Developer of the Original Code is SciTech Software, Inc.
20 * All Rights Reserved.
22 * ========================================================================
25 * Environment: 32-bit OS/2 VDD
27 * Description: C library compatible I/O functions for use within a VDD.
29 ****************************************************************************/
34 /*------------------------ Main Code Implementation -----------------------*/
38 /* NB: none of the file VDHs are available during the DOS session */
39 /* initialzation context! */
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. */
45 #define _OS2Open VDHOpen
46 #define _OS2Close VDHClose
48 #define _OS2Open DosOpen
49 #define _OS2Close DosClose
52 /****************************************************************************
54 VDD implementation of the ANSI C fopen function.
55 ****************************************************************************/
60 FILE *f = PM_malloc(sizeof(FILE));
67 f->text = (mode[1] == 't' || mode[2] == 't');
68 f->writemode = (mode[0] == 'w') || (mode[0] == 'a');
70 f->endp = f->buf + sizeof(f->buf);
71 f->curp = f->startp = f->buf;
75 omode = VDHOPEN_ACCESS_READONLY | VDHOPEN_SHARE_DENYNONE;
76 oflags = VDHOPEN_ACTION_OPEN_IF_EXISTS | VDHOPEN_ACTION_FAIL_IF_NEW;
78 omode = OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE;
79 oflags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
82 else if (mode[0] == 'w') {
84 omode = VDHOPEN_ACCESS_WRITEONLY | VDHOPEN_SHARE_DENYWRITE;
85 oflags = VDHOPEN_ACTION_REPLACE_IF_EXISTS | VDHOPEN_ACTION_CREATE_IF_NEW;
87 omode = OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYWRITE;
88 oflags = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
93 omode = VDHOPEN_ACCESS_READWRITE | VDHOPEN_SHARE_DENYWRITE;
94 oflags = VDHOPEN_ACTION_OPEN_IF_EXISTS | VDHOPEN_ACTION_CREATE_IF_NEW;
96 omode = OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYWRITE;
97 oflags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
100 rc = _OS2Open((PSZ)filename, (PHFILE)&f->handle, &ulAction, 0, VDHOPEN_FILE_NORMAL, oflags, omode, NULL);
107 f->filesize = VDHSeek((HFILE)f->handle, 0, VDHSK_END_OF_FILE);
109 rc = DosSetFilePtr((HFILE)f->handle, 0, FILE_END, &f->filesize);
118 /****************************************************************************
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 ****************************************************************************/
130 int bytes,readbytes,totalbytes = 0;
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;
138 bytes = (size * n) - bytes;
144 readbytes = VDHRead((HFILE)f->handle, buf, bytes);
146 DosRead((HFILE)f->handle, buf, bytes, &readbytes);
148 totalbytes += readbytes;
149 f->offset += readbytes;
151 return totalbytes / size;
154 /****************************************************************************
156 VDD implementation of the ANSI C fwrite function.
157 ****************************************************************************/
165 int bytes,writtenbytes,totalbytes = 0;
167 /* Flush anything already in the buffer */
173 writtenbytes = VDHWrite((HFILE)f->handle, buf, bytes);
175 DosWrite((HFILE)f->handle, buf, bytes, &writtenbytes);
177 totalbytes += writtenbytes;
178 f->offset += writtenbytes;
179 if (f->offset > f->filesize)
180 f->filesize = f->offset;
181 return totalbytes / size;
184 /****************************************************************************
186 VxD implementation of the ANSI C fflush function.
187 ****************************************************************************/
193 /* First copy any data already written into our buffer */
194 if (f->writemode && (bytes = (f->curp - f->startp)) > 0) {
196 bytes = VDHWrite((HFILE)f->handle, f->startp, bytes);
198 DosWrite((HFILE)f->handle, f->startp, bytes, &bytes);
201 if (f->offset > f->filesize)
202 f->filesize = f->offset;
203 f->startp = f->curp = f->buf;
208 /****************************************************************************
210 VDD implementation of the ANSI C fseek function.
211 ****************************************************************************/
221 else if (whence == 1)
223 else if (whence == 2)
224 f->offset = f->filesize + offset;
227 VDHSeek((HFILE)f->handle, f->offset, VDHSK_ABSOLUTE);
229 DosSetFilePtr((HFILE)f->handle, f->offset, FILE_BEGIN, NULL);
235 /****************************************************************************
237 VDD implementation of the ANSI C ftell function.
238 ****************************************************************************/
244 offset = (f->curp - f->startp);
249 /****************************************************************************
251 VDD implementation of the ANSI C feof function.
252 ****************************************************************************/
256 return (f->offset == f->filesize);
259 /****************************************************************************
261 Read a single character from the input file buffer, including translation
262 of the character in text transation modes.
263 ****************************************************************************/
269 if (f->unputc != EOF) {
274 if (f->startp == f->curp) {
275 int bytes = fread(f->buf,1,sizeof(f->buf),f);
278 f->curp = f->startp + bytes;
281 if (f->text && c == '\r') {
290 /****************************************************************************
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)
298 if (f->text && c == '\n') {
302 if (f->curp == f->endp)
308 /****************************************************************************
310 VxD implementation of the ANSI C fgets function.
311 ****************************************************************************/
321 while (--n > 0 && (c = __getc(f)) != EOF) {
326 if (c == EOF && cs == s)
332 /****************************************************************************
334 VxD implementation of the ANSI C fputs function.
335 ****************************************************************************/
343 while ((c = *s++) != 0)
348 /****************************************************************************
350 VxD implementation of the ANSI C fclose function.
351 ****************************************************************************/
356 _OS2Close((HFILE)f->handle);