forked from Tronix286/DOSMID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fio.c
275 lines (266 loc) · 7.04 KB
/
fio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* File I/O, based on DOS int 21h calls
* This file is part of the DOSMid project
* http://dosmid.sourceforge.net
* Modified by Rivoreo
* https://sourceforge.net/p/rivoreo/dosmid-code/
* Copyright (C) 2018 Mateusz Viste
* Copyright 2015-2024 Rivoreo
*/
#include "defines.h"
#ifdef MSDOS
#include <dos.h> /* REGS */
#else
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define _fmemcpy memcpy
#endif
#include <string.h> /* _fmemcpy() */
#include "fio.h" /* include self for control */
#ifndef MSDOS
static int sync_read(int fd, void *buffer, size_t count) {
char *p = buffer;
do {
int s = read(fd, p, count);
if(s < 0) {
if(errno == EINTR) continue;
if(p == (char *)buffer) return -1;
break;
}
if(!s) break;
count -= s;
p += s;
} while(count > 0);
return p - (char *)buffer;
}
#endif
static void fio_seek_sync(struct fiofile *f) {
#ifdef MSDOS
/* DOS 2+ - LSEEK - SET CURRENT FILE POSITION
AH = 42h
AL = origin of move
00h start of file
01h current file position
02h end of file
BX = file handle
CX:DX = (signed) offset from origin of new file position
Return on success:
CF clear
DX:AX = new file position in bytes from start of file
Return on error:
CF set
AX = error code */
union REGS regs;
unsigned short *off;
long offset;
#endif
if ((f->flags & FIO_FLAG_SEEKSYNC) == 0) return;
#ifdef MSDOS
offset = f->curpos;
off = (unsigned short *)(&offset);
regs.x.ax = 0x4200u;
regs.x.bx = f->fh;
regs.x.cx = off[1];
regs.x.dx = off[0];
int86(0x21, ®s, ®s);
f->flags &= ~FIO_FLAG_SEEKSYNC;
/* if (regs.x.cflag != 0) return(0 - regs.x.ax);
return(((long)regs.x.dx << 16) | regs.x.ax); */
#else
lseek(f->fh, f->curpos, SEEK_SET);
#endif
}
/* seek to offset position of file pointed at by fhandle. returns current file position on success, a negative error otherwise */
signed long int fio_seek(struct fiofile *f, unsigned short int origin, signed long int offset) {
switch (origin) {
case FIO_SEEK_START:
if (offset < 1) {
f->curpos = 0;
} else {
f->curpos = offset;
}
break;
case FIO_SEEK_END:
f->curpos = f->flen;
case FIO_SEEK_CUR:
f->curpos += offset;
break;
}
f->flags |= FIO_FLAG_SEEKSYNC;
return(f->curpos);
}
/* reads a line from file pointed at by fhandle, fill buff up to buflen bytes. returns the line length (possibly longer than buflen), or -1 on EOF */
int fio_getline(struct fiofile *f, void far *buff, short int buflen) {
unsigned char bytebuf;
short linelen = 0;
buflen--; /* leave space for the zero terminator */
for (;;) {
if (fio_read(f, &bytebuf, 1) == 0) { /* EOF */
if (linelen == 0) linelen = -1;
break;
}
if (bytebuf == '\n') break;
if (bytebuf == '\r') continue;
linelen++;
if (buflen > 0) {
buflen--;
*((unsigned char far *)buff) = bytebuf;
buff = ((unsigned char far *)buff) + 1;
}
}
*((unsigned char far *)buff) = 0;
return(linelen);
}
static void loadcache(struct fiofile *f) {
#ifdef MSDOS
union REGS regs;
struct SREGS sregs;
#endif
fio_seek_sync(f);
f->flags |= FIO_FLAG_SEEKSYNC;
f->bufoffs = f->curpos;
#ifdef MSDOS
regs.h.ah = 0x3f;
regs.x.bx = f->fh;
regs.x.cx = FIO_CACHE;
#ifdef _QC
segread(&sregs);
regs.x.dx = (unsigned int)f->buff;
#else
sregs.ds = FP_SEG(f->buff);
regs.x.dx = FP_OFF(f->buff);
#endif
int86x(0x21, ®s, ®s, &sregs);
#else
sync_read(f->fh, f->buff, FIO_CACHE);
#endif
}
/* open file fname and set fhandle with the associated file handle. returns 0 on success, non-zero otherwise */
int fio_open(const char far *fname, int mode, struct fiofile *f) {
#ifdef MSDOS
/* DOS 2+ - OPEN - OPEN EXISTING FILE
AH = 3Dh
AL = access and sharing modes
DS:DX -> ASCIZ filename
CL = attribute mask of files to look for (server call only)
Return:
CF clear if successful
AX = file handle or error code
CF set on error */
union REGS regs;
struct SREGS sregs;
/* */
regs.h.ah = 0x3d;
regs.h.al = mode;
sregs.ds = FP_SEG(fname);
regs.x.dx = FP_OFF(fname);
int86x(0x21, ®s, ®s, &sregs);
f->fh = regs.x.ax;
if (regs.x.cflag != 0) return(-1);
#else
int flags;
switch(mode) {
case FIO_OPEN_RD:
flags = O_RDONLY;
break;
case FIO_OPEN_WR:
flags = O_WRONLY;
break;
case FIO_OPEN_RW:
flags = O_RDWR;
break;
default:
return -1;
}
int fd = open(fname, flags);
if(fd == -1) return -1;
f->fh = fd;
#endif
f->curpos = 0;
loadcache(f);
/* fseek to end so I know the file length */
#ifdef MSDOS
regs.x.ax = 0x4202u;
regs.x.bx = f->fh;
regs.x.cx = 0;
regs.x.dx = 0;
int86(0x21, ®s, ®s);
f->flen = (((long)regs.x.dx << 16) | regs.x.ax);
/* fseek to start */
regs.x.ax = 0x4200u;
regs.x.bx = f->fh;
regs.x.cx = 0;
regs.x.dx = 0;
int86(0x21, ®s, ®s);
#else
off_t len = lseek(fd, 0, SEEK_END);
if(len != (off_t)-1) f->flen = len;
lseek(fd, 0, SEEK_SET);
#endif
f->flags = FIO_FLAG_SEEKSYNC;
return(0);
}
/* reads count bytes from file pointed at by fhandle, and writes the data into buff. returns the number of bytes actually read, or a negative number on error */
int fio_read(struct fiofile *f, void far *buff, int count) {
#ifdef MSDOS
/* DOS 2+ - READ - READ FROM FILE OR DEVICE
* AH = 3Fh
* BX = file handle
* CX = number of bytes to read
* DS:DX -> buffer for data
* Return:
* CF clear if successful
* AX = number of bytes actually read (0 if at EOF before call)
* CF set on error
* AX = error code (05h,06h) (see #01680 at AH=59h/BX=0000h) */
union REGS regs;
struct SREGS sregs;
#endif
if (f->curpos + count > f->flen) count = f->flen - f->curpos;
if (count == 0) return(0);
if (count <= FIO_CACHE) {
if ((f->curpos < f->bufoffs) || (f->curpos + count > f->bufoffs + FIO_CACHE)) {
loadcache(f);
}
_fmemcpy(buff, f->buff + (f->curpos - f->bufoffs), count);
f->curpos += count;
return(count);
}
fio_seek_sync(f);
#ifdef MSDOS
regs.h.ah = 0x3f;
regs.x.bx = f->fh;
regs.x.cx = count;
sregs.ds = FP_SEG(buff);
regs.x.dx = FP_OFF(buff);
int86x(0x21, ®s, ®s, &sregs);
if (regs.x.cflag != 0) return(0 - regs.x.ax);
f->curpos += regs.x.ax;
return(regs.x.ax);
#else
return sync_read(f->fh, buff, count);
#endif
}
/* close file handle. returns 0 on success, non-zero otherwise */
int fio_close(struct fiofile *f) {
#ifdef MSDOS
/* DOS 2+ - CLOSE - CLOSE FILE
AH = 3Eh
BX = file handle
Return on success:
CF clear
AX destroyed
On error:
CF set
AX = error code */
union REGS regs;
regs.h.ah = 0x3e;
regs.x.bx = f->fh;
int86(0x21, ®s, ®s);
if (regs.x.cflag != 0) return(0 - regs.x.ax);
return(0);
#else
return close(f->fh);
#endif
}