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 Windows VxD
27 * Description: C library compatible I/O functions for use within a VxD.
29 ****************************************************************************/
34 /*------------------------ Main Code Implementation -----------------------*/
38 /****************************************************************************
40 VxD implementation of the ANSI C fopen function.
41 ****************************************************************************/
46 FILE *f = PM_malloc(sizeof(FILE));
51 f->text = (mode[1] == 't' || mode[2] == 't');
52 f->writemode = (mode[0] == 'w') || (mode[0] == 'a');
58 omode = OPEN_ACCESS_READONLY | OPEN_SHARE_COMPATIBLE;
59 action = ACTION_IFEXISTS_OPEN | ACTION_IFNOTEXISTS_FAIL;
61 else if (mode[0] == 'w') {
62 omode = OPEN_ACCESS_WRITEONLY | OPEN_SHARE_COMPATIBLE;
63 action = ACTION_IFEXISTS_TRUNCATE | ACTION_IFNOTEXISTS_CREATE;
66 omode = OPEN_ACCESS_READWRITE | OPEN_SHARE_COMPATIBLE;
67 action = ACTION_IFEXISTS_OPEN | ACTION_IFNOTEXISTS_CREATE;
69 f->handle = (int)R0_OpenCreateFile(false,(char*)filename,omode,ATTR_NORMAL,action,0,&error,&action);
74 f->filesize = R0_GetFileSize((HANDLE)f->handle,&error);
85 else if (mode[0] == 'w') {
87 oflag = _O_WRONLY | _O_CREAT | _O_TRUNC;
91 oflag = _O_RDWR | _O_CREAT | _O_APPEND;
97 if ((f->handle = i_open(filename,oflag,pmode)) == -1) {
101 oldpos = i_lseek(f->handle,0,1);
102 f->filesize = i_lseek(f->handle,0,2);
103 i_lseek(f->handle,oldpos,0);
109 /****************************************************************************
111 VxD implementation of the ANSI C fread function. Note that the VxD file I/O
112 functions are layered on DOS, so can only read up to 64K at a time. Since
113 we are expected to handle much larger chunks than this, we handle larger
114 blocks automatically in here.
115 ****************************************************************************/
124 int bytes = size * n;
125 int readbytes,totalbytes = 0;
127 while (bytes > 0x10000) {
129 readbytes = R0_ReadFile(false,(HANDLE)f->handle,buf,0x8000,f->offset,&error);
130 readbytes += R0_ReadFile(false,(HANDLE)f->handle,buf+0x8000,0x8000,f->offset+0x8000,&error);
133 readbytes = i_read(f->handle,buf,0x8000);
134 readbytes += i_read(f->handle,buf+0x8000,0x8000);
136 totalbytes += readbytes;
137 f->offset += readbytes;
143 readbytes = R0_ReadFile(false,(HANDLE)f->handle,buf,bytes,f->offset,&error);
145 readbytes = i_read(f->handle,buf,bytes);
146 totalbytes += readbytes;
147 f->offset += readbytes;
149 return totalbytes / size;
152 /****************************************************************************
154 VxD implementation of the ANSI C fwrite function. Note that the VxD file I/O
155 functions are layered on DOS, so can only read up to 64K at a time. Since
156 we are expected to handle much larger chunks than this, we handle larger
157 blocks automatically in here.
158 ****************************************************************************/
165 const char *buf = ptr;
167 int bytes = size * n;
168 int writtenbytes,totalbytes = 0;
172 while (bytes > 0x10000) {
174 writtenbytes = R0_WriteFile(false,(HANDLE)f->handle,buf,0x8000,f->offset,&error);
175 writtenbytes += R0_WriteFile(false,(HANDLE)f->handle,buf+0x8000,0x8000,f->offset+0x8000,&error);
178 writtenbytes = i_write(f->handle,buf,0x8000);
179 writtenbytes += i_write(f->handle,buf+0x8000,0x8000);
181 totalbytes += writtenbytes;
182 f->offset += writtenbytes;
187 writtenbytes = R0_WriteFile(false,(HANDLE)f->handle,buf,bytes,f->offset,&error);
189 writtenbytes = i_write(f->handle,buf,bytes);
190 totalbytes += writtenbytes;
191 f->offset += writtenbytes;
192 if (f->offset > f->filesize)
193 f->filesize = f->offset;
194 return totalbytes / size;
197 /****************************************************************************
199 VxD implementation of the ANSI C fflush function.
200 ****************************************************************************/
204 /* Nothing to do since we are not doing buffered file I/O */
209 /****************************************************************************
211 VxD implementation of the ANSI C fseek function.
212 ****************************************************************************/
220 else if (whence == 1)
222 else if (whence == 2)
223 f->offset = f->filesize + offset;
225 i_lseek(f->handle,f->offset,0);
229 /****************************************************************************
231 VxD implementation of the ANSI C ftell function.
232 ****************************************************************************/
239 /****************************************************************************
241 VxD implementation of the ANSI C feof function.
242 ****************************************************************************/
246 return (f->offset == f->filesize);
249 /****************************************************************************
251 NT driver implementation of the ANSI C fgets function.
252 ****************************************************************************/
261 /* Read the entire buffer into memory (our functions are unbuffered!) */
262 if ((len = fread(s,1,n,f)) == 0)
265 /* Search for '\n' or end of string */
278 /****************************************************************************
280 NT driver implementation of the ANSI C fputs function.
281 ****************************************************************************/
286 return fwrite(s,1,strlen(s),f);
289 /****************************************************************************
291 VxD implementation of the ANSI C fclose function.
292 ****************************************************************************/
299 R0_CloseFile((HANDLE)f->handle,&error);