-
Notifications
You must be signed in to change notification settings - Fork 15
/
scdos.c
471 lines (433 loc) · 8.97 KB
/
scdos.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
Select non-standard functionality commonly available in C compilers
for DOS in <dos.h>, <dir.h>, <conio.h>, <bios.h>, <string.h>, which
is not (yet?) provided by Smaller C's library.
*/
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include "scdos.h"
#ifndef __HUGE__
#ifndef __UNREAL__
#error This program requires the huge or unreal mode(l)
#endif
#endif
unsigned char inportb(int port)
{
asm("mov dx, [bp+8]\n"
"in al, dx\n"
"movzx eax, al");
}
void outportb(int port, unsigned char value)
{
asm("mov dx, [bp+8]\n"
"mov al, [bp+12]\n"
"out dx, al");
}
void setvect(int intno, void __interrupt isr())
{
unsigned addr = (unsigned)isr;
unsigned ofs = (addr >> 20 << 4) | (addr & 0xF);
unsigned seg = (addr >> 4) & 0xFFFF;
asm("pushfd\n"
"cli");
poke(0, intno * 4, ofs);
poke(0, intno * 4 + 2, seg);
asm("popfd");
}
void __interrupt (*getvect(int intno))()
{
asm("pushfd\n"
"cli");
unsigned ofs = peek(0, intno * 4);
unsigned seg = peek(0, intno * 4 + 2);
unsigned addr = (ofs & 0xF) | (seg << 4) | (ofs >> 4 << 20);
asm("popfd");
return (void __interrupt (*)())addr;
}
/* Calls the old ISR from the new ISR */
void callisr(void __interrupt isr())
{
asm("pushfd\n"
"cli\n"
"rol dword [bp + 8], 12\n"
"rol word [bp + 8], 4\n"
"pushf\n"
"call far [bp + 8]\n"
"popfd");
}
int stricmp(char* s1, char* s2)
{
while (toupper((unsigned char)*s1) == toupper((unsigned char)*s2))
{
if (!*s1)
return 0;
++s1;
++s2;
}
return ((unsigned char)*s1) - ((unsigned char)*s2);
}
void movedata(unsigned srcseg, unsigned srcoff, unsigned dstseg, unsigned dstoff, unsigned n)
{
memcpy(MK_FP(dstseg, dstoff), MK_FP(srcseg, srcoff), n);
}
/* REGS: unsigned short ax, bx, cx, dx, si, di, cflag, flags */
int int86(int intno, union REGS* inregs, union REGS* outregs)
{
asm(" db 0x66, 0x68\n" /* push dword next */
"patch_next:\n"
" dd next\n"
"section .relot\n"
" dd patch_next\n"
"section .text\n"
" movzx ax, byte [bp+8]\n"
" shl eax, 16\n"
" or eax, 0xEA00CD90\n" /* nop; int intno; jmp far absolute next */
" push eax\n"
" mov ax, sp\n"
" push ebp\n"
" push ss\n"
" push ax\n"
" mov edi, [bp+12]\n"
" ror edi, 4\n"
" mov ds, di\n"
" shr edi, 28\n"
" mov ax, [di]\n"
" mov bx, [di+2]\n"
" mov cx, [di+4]\n"
" mov dx, [di+6]\n"
" mov si, [di+8]\n"
" mov di, [di+10]\n"
" retf\n"
"next:\n"
" pop ebp\n"
" pushf\n"
" mov [bp+8], di\n"
" pop word [bp+12]\n"
" mov edi, [bp+16]\n"
" ror edi, 4\n"
" mov ds, di\n"
" shr edi, 28\n"
" mov [di], ax\n"
" mov [di+2], bx\n"
" mov [di+4], cx\n"
" mov [di+6], dx\n"
" mov [di+8], si\n"
" mov bx, [bp+8]\n"
" mov [di+10], bx\n"
" mov bx, [bp+12]\n"
" mov [di+14], bx\n"
" and bx, 1\n"
" mov [di+12], bx\n"
" add sp, 8\n"
" movzx eax, ax");
#ifdef __UNREAL__
asm("push dword 0\n"
"pop es\n"
"pop ds");
#endif
}
int kbhit(void)
{
asm("mov ah, 1\n"
"int 0x16\n"
"setnz al\n"
"movzx eax, al");
}
int bioskey(int cmd)
{
assert(cmd == 0);
asm("mov ah, 0\n"
"int 0x16\n"
"movzx eax, ax");
}
int getdisk(void)
{
asm("mov ah, 0x19\n"
"int 0x21\n"
"movzx eax, al");
}
int setdisk(int drive)
{
asm("mov ah, 0xE\n"
"mov dl, [bp+8]\n"
"int 0x21\n"
"movzx eax, al");
}
static
int DosGetCurDir(char buf[64], int drive, unsigned* error)
{
asm("mov ah, 0x47\n"
"mov esi, [bp + 8]\n"
"ror esi, 4\n"
"mov ds, si\n"
"shr esi, 28\n"
"mov dl, [bp + 12]\n"
"int 0x21");
asm("movzx ebx, ax\n"
"cmc\n"
"sbb ax, ax\n"
"and eax, 1\n"
"mov esi, [bp + 16]");
#ifdef __HUGE__
asm("ror esi, 4\n"
"mov ds, si\n"
"shr esi, 28\n"
"mov [si], ebx");
#else
asm("push word 0\n"
"pop ds\n"
"mov [esi], ebx");
#endif
}
char* getcwd(char* buf, int len)
{
/* Not setting errno! */
char b[3 + 64];
unsigned err;
int l;
b[0] = getdisk() + 'A';
b[1] = ':';
b[2] = '\\';
if (!DosGetCurDir(b + 3, 0/*current drive*/, &err))
return NULL;
l = strlen(b) + 1;
if (!buf)
{
if (!(buf = malloc(l)))
return NULL;
}
else if (len < l)
{
return NULL;
}
return memcpy(buf, b, l);
}
static
int DosChDir(char* path, unsigned* error)
{
asm("mov ah, 0x3B\n"
"mov edx, [bp + 8]\n"
"ror edx, 4\n"
"mov ds, dx\n"
"shr edx, 28\n"
"int 0x21");
asm("movzx ebx, ax\n"
"cmc\n"
"sbb ax, ax\n"
"and eax, 1\n"
"mov esi, [bp + 12]");
#ifdef __HUGE__
asm("ror esi, 4\n"
"mov ds, si\n"
"shr esi, 28\n"
"mov [si], ebx");
#else
asm("push word 0\n"
"pop ds\n"
"mov [esi], ebx");
#endif
}
int chdir(char* path)
{
/* Not setting errno! */
unsigned err;
if (!path || !DosChDir(path, &err))
return -1;
return 0;
}
static
void DosSetDTA(struct ffblk* dta)
{
asm("mov ah, 0x1A\n"
"mov edx, [bp + 8]\n"
"ror edx, 4\n"
"mov ds, dx\n"
"shr edx, 28\n"
"int 0x21");
#ifdef __UNREAL__
asm("push word 0\n"
"pop ds");
#endif
}
static
int DosFindFirst(char* path, int attr, unsigned* error)
{
asm("mov ah, 0x4E\n"
"mov edx, [bp + 8]\n"
"ror edx, 4\n"
"mov ds, dx\n"
"shr edx, 28\n"
"mov cx, [bp + 12]\n"
"int 0x21");
asm("movzx ebx, ax\n"
"cmc\n"
"sbb ax, ax\n"
"and eax, 1\n"
"mov esi, [bp + 16]");
#ifdef __HUGE__
asm("ror esi, 4\n"
"mov ds, si\n"
"shr esi, 28\n"
"mov [si], ebx");
#else
asm("push word 0\n"
"pop ds\n"
"mov [esi], ebx");
#endif
}
static
int DosFindNext(struct ffblk* dta, unsigned* error)
{
asm("mov ah, 0x4F\n"
"mov edx, [bp + 8]\n"
"ror edx, 4\n"
"mov ds, dx\n"
"shr edx, 28\n"
"int 0x21");
asm("movzx ebx, ax\n"
"cmc\n"
"sbb ax, ax\n"
"and eax, 1\n"
"mov esi, [bp + 12]");
#ifdef __HUGE__
asm("ror esi, 4\n"
"mov ds, si\n"
"shr esi, 28\n"
"mov [si], ebx");
#else
asm("push word 0\n"
"pop ds\n"
"mov [esi], ebx");
#endif
}
int findfirst(char* path, struct ffblk* ffblk, int attrib)
{
unsigned err;
if (!path || !ffblk)
return -1;
DosSetDTA(ffblk);
if (!DosFindFirst(path, attrib, &err))
return -1;
return 0;
}
int findnext(struct ffblk* ffblk)
{
unsigned err;
if (!ffblk || !DosFindNext(ffblk, &err))
return -1;
return 0;
}
void fnmerge(char* path, char* drive, char* dir, char* name, char* ext)
{
if (drive && *drive)
{
*path++ = *drive;
*path++ = ':';
}
if (dir && *dir)
{
size_t len = strlen(dir);
int c = dir[len - 1];
memcpy(path, dir, len);
path += len;
if (c != '\\' && c != '/')
*path++ = '\\';
}
if (name && *name)
{
size_t len = strlen(name);
memcpy(path, name, len);
path += len;
}
if (ext && *ext)
{
size_t len = strlen(ext);
if (*ext != '.')
*path++ = '.';
memcpy(path, ext, len);
path += len;
}
*path = '\0';
}
int fnsplit(char* path, char* drive, char* dir, char* name, char* ext)
{
int flags = 0;
char* p = path;
if (drive)
*drive = '\0';
if (dir)
*dir = '\0';
if (name)
*name = '\0';
if (ext)
*ext = '\0';
if (isalpha((unsigned char)*p) && p[1] == ':')
{
if (drive)
{
*drive++ = *p++;
*drive++ = *p++;
*drive = '\0';
}
flags |= DRIVE;
}
{
char* lastSlash = strrchr(p, '/');
char* lastBackslash = strrchr(p, '\\');
char* dirEnd;
/* Find the last (back)slash, if any. */
if (lastSlash && lastBackslash)
dirEnd = (lastSlash < lastBackslash) ? lastBackslash : lastSlash;
else
dirEnd = lastBackslash ? lastBackslash : lastSlash;
if (dirEnd)
{
size_t len = dirEnd - p + 1;
if (dir)
{
memcpy(dir, p, len);
dir[len] = '\0';
}
p += len;
flags |= DIRECTORY;
}
}
{
/* "." and ".." are directory names, not extensions. */
size_t len = 0;
if (*p == '.')
{
if (p[1] == '\0')
len = 1;
else if (p[1] == '.' && p[2] == '\0')
len = 2;
}
/* Filename is before the last dot, if any. */
if (!len)
{
char* lastDot = strrchr(p, '.');
len = lastDot ? (size_t)(lastDot - p) : strlen(p);
}
if (len)
{
if (name)
{
memcpy(name, p, len);
name[len] = '\0';
}
p += len;
flags |= FILENAME;
}
}
if (*p == '.')
{
if (ext)
strcpy(ext, p);
flags |= EXTENSION;
}
if (path[strcspn(path, "?*")])
flags |= WILDCARDS;
return flags;
}