Debian Codegraph @

src/dos/dos_files.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: dos_files.cpp,v 1.113 2009-08-31 18:03:08 qbix79 Exp $ */
20
21 #include <string.h>
22 #include <stdlib.h>
23 #include <time.h>
24 #include <ctype.h>
25
26 #include "dosbox.h"
27 #include "bios.h"
28 #include "mem.h"
29 #include "regs.h"
30 #include "dos_inc.h"
31 #include "drives.h"
32 #include "cross.h"
33
34 #define DOS_FILESTART 4
35
36 #define FCB_SUCCESS 0
37 #define FCB_READ_NODATA 1
38 #define FCB_READ_PARTIAL 3
39 #define FCB_ERR_NODATA 1
40 #define FCB_ERR_EOF 3
41 #define FCB_ERR_WRITE 1
42
43
44 DOS_File * Files[DOS_FILES];
45 DOS_Drive * Drives[DOS_DRIVES];
46
47 Bit8u DOS_GetDefaultDrive(void) {
48 // return DOS_SDA(DOS_SDA_SEG,DOS_SDA_OFS).GetDrive();
49 Bit8u d = DOS_SDA(DOS_SDA_SEG,DOS_SDA_OFS).GetDrive();
50 if( d != dos.current_drive ) LOG(LOG_DOSMISC,LOG_ERROR)("SDA drive %d not the same as dos.current_drive %d",d,dos.current_drive);
51 return dos.current_drive;
52 }
53
54 void DOS_SetDefaultDrive(Bit8u drive) {
55 // if (drive<=DOS_DRIVES && ((drive<2) || Drives[drive])) DOS_SDA(DOS_SDA_SEG,DOS_SDA_OFS).SetDrive(drive);
56 if (drive<DOS_DRIVES && ((drive<2) || Drives[drive])) {dos.current_drive = drive; DOS_SDA(DOS_SDA_SEG,DOS_SDA_OFS).SetDrive(drive);}
57 }
58
59 bool DOS_MakeName(char const * const name,char * const fullname,Bit8u * drive) {
60 if(!name || *name == 0 || *name == ' ') {
61 /* Both \0 and space are seperators and
62 * empty filenames report file not found */
63 DOS_SetError(DOSERR_FILE_NOT_FOUND);
64 return false;
65 }
66 const char * name_int = name;
67 char tempdir[DOS_PATHLENGTH];
68 char upname[DOS_PATHLENGTH];
69 Bitu r,w;
70 *drive = DOS_GetDefaultDrive();
71 /* First get the drive */
72 if (name_int[1]==':') {
73 *drive=(name_int[0] | 0x20)-'a';
74 name_int+=2;
75 }
76 if (*drive>=DOS_DRIVES || !Drives[*drive]) {
77 DOS_SetError(DOSERR_PATH_NOT_FOUND);
78 return false;
79 }
80 r=0;w=0;
81 while (name_int[r]!=0 && (r<DOS_PATHLENGTH)) {
82 Bit8u c=name_int[r++];
83 if ((c>='a') && (c<='z')) {upname[w++]=c-32;continue;}
84 if ((c>='A') && (c<='Z')) {upname[w++]=c;continue;}
85 if ((c>='0') && (c<='9')) {upname[w++]=c;continue;}
86 switch (c) {
87 case '/':
88 upname[w++]='\\';
89 break;
90 case ' ': /* should be seperator */
91 break;
92 case '\\': case '$': case '#': case '@': case '(': case ')':
93 case '!': case '%': case '{': case '}': case '`': case '~':
94 case '_': case '-': case '.': case '*': case '?': case '&':
95 case '\'': case '+': case '^': case 246: case 255: case 0xa0:
96 case 0xe5: case 0xbd:
97 upname[w++]=c;
98 break;
99 default:
100 LOG(LOG_FILES,LOG_NORMAL)("Makename encountered an illegal char %c hex:%X in %s!",c,c,name);
101 DOS_SetError(DOSERR_PATH_NOT_FOUND);return false;
102 break;
103 }
104 }
105 if (r>=DOS_PATHLENGTH) { DOS_SetError(DOSERR_PATH_NOT_FOUND);return false; }
106 upname[w]=0;
107 /* Now parse the new file name to make the final filename */
108 if (upname[0]!='\\') strcpy(fullname,Drives[*drive]->curdir);
109 else fullname[0]=0;
110 Bit32u lastdir=0;Bit32u t=0;
111 while (fullname[t]!=0) {
112 if ((fullname[t]=='\\') && (fullname[t+1]!=0)) lastdir=t;
113 t++;
114 };
115 r=0;w=0;
116 tempdir[0]=0;
117 bool stop=false;
118 while (!stop) {
119 if (upname[r]==0) stop=true;
120 if ((upname[r]=='\\') || (upname[r]==0)){
121 tempdir[w]=0;
122 if (tempdir[0]==0) { w=0;r++;continue;}
123 if (strcmp(tempdir,".")==0) {
124 tempdir[0]=0;
125 w=0;r++;
126 continue;
127 }
128
129 Bit32s iDown;
130 bool dots = true;
131 Bit32s templen=(Bit32s)strlen(tempdir);
132 for(iDown=0;(iDown < templen) && dots;iDown++)
133 if(tempdir[iDown] != '.')
134 dots = false;
135
136 // only dots?
137 if (dots && (templen > 1)) {
138 Bit32s cDots = templen - 1;
139 for(iDown=(Bit32s)strlen(fullname)-1;iDown>=0;iDown--) {
140 if(fullname[iDown]=='\\' || iDown==0) {
141 lastdir = iDown;
142 cDots--;
143 if(cDots==0)
144 break;
145 }
146 }
147 fullname[lastdir]=0;
148 t=0;lastdir=0;
149 while (fullname[t]!=0) {
150 if ((fullname[t]=='\\') && (fullname[t+1]!=0)) lastdir=t;
151 t++;
152 }
153 tempdir[0]=0;
154 w=0;r++;
155 continue;
156 }
157
158
159 lastdir=(Bit32u)strlen(fullname);
160
161 if (lastdir!=0) strcat(fullname,"\\");
162 char * ext=strchr(tempdir,'.');
163 if (ext) {
164 if(strchr(ext+1,'.')) {
165 //another dot in the extension =>file not found
166 //Or path not found depending on wether
167 //we are still in dir check stage or file stage
168 if(stop)
169 DOS_SetError(DOSERR_FILE_NOT_FOUND);
170 else
171 DOS_SetError(DOSERR_PATH_NOT_FOUND);
172 return false;
173 }
174
175 ext[4] = 0;
176 if((strlen(tempdir) - strlen(ext)) > 8) memmove(tempdir + 8, ext, 5);
177 } else tempdir[8]=0;
178
179 if (strlen(fullname)+strlen(tempdir)>=DOS_PATHLENGTH) {
180 DOS_SetError(DOSERR_PATH_NOT_FOUND);return false;
181 }
182
183 strcat(fullname,tempdir);
184 tempdir[0]=0;
185 w=0;r++;
186 continue;
187 }
188 tempdir[w++]=upname[r++];
189 }
190 return true;
191 }
192
193 bool DOS_GetCurrentDir(Bit8u drive,char * const buffer) {
194 if (drive==0) drive=DOS_GetDefaultDrive();
195 else drive--;
196 if ((drive>=DOS_DRIVES) || (!Drives[drive])) {
197 DOS_SetError(DOSERR_INVALID_DRIVE);
198 return false;
199 }
200 strcpy(buffer,Drives[drive]->curdir);
201 return true;
202 }
203
204 bool DOS_ChangeDir(char const * const dir) {
205 Bit8u drive;char fulldir[DOS_PATHLENGTH];
206 if (!DOS_MakeName(dir,fulldir,&drive)) return false;
207
208 if (Drives[drive]->TestDir(fulldir)) {
209 strcpy(Drives[drive]->curdir,fulldir);
210 return true;
211 } else {
212 DOS_SetError(DOSERR_PATH_NOT_FOUND);
213 }
214 return false;
215 }
216
217 bool DOS_MakeDir(char const * const dir) {
218 Bit8u drive;char fulldir[DOS_PATHLENGTH];
219 size_t len = strlen(dir);
220 if(!len || dir[len-1] == '\\') {
221 DOS_SetError(DOSERR_PATH_NOT_FOUND);
222 return false;
223 }
224 if (!DOS_MakeName(dir,fulldir,&drive)) return false;
225 if(Drives[drive]->MakeDir(fulldir)) return true;
226
227 /* Determine reason for failing */
228 if(Drives[drive]->TestDir(fulldir))
229 DOS_SetError(DOSERR_ACCESS_DENIED);
230 else
231 DOS_SetError(DOSERR_PATH_NOT_FOUND);
232 return false;
233 }
234
235 bool DOS_RemoveDir(char const * const dir) {
236 /* We need to do the test before the removal as can not rely on
237 * the host to forbid removal of the current directory.
238 * We never change directory. Everything happens in the drives.
239 */
240 Bit8u drive;char fulldir[DOS_PATHLENGTH];
241 if (!DOS_MakeName(dir,fulldir,&drive)) return false;
242 /* Check if exists */
243 if(!Drives[drive]->TestDir(fulldir)) {
244 DOS_SetError(DOSERR_PATH_NOT_FOUND);
245 return false;
246 }
247 /* See if it's current directory */
248 char currdir[DOS_PATHLENGTH]= { 0 };
249 DOS_GetCurrentDir(drive + 1 ,currdir);
250 if(strcmp(currdir,fulldir) == 0) {
251 DOS_SetError(DOSERR_REMOVE_CURRENT_DIRECTORY);
252 return false;
253 }
254
255 if(Drives[drive]->RemoveDir(fulldir)) return true;
256
257 /* Failed. We know it exists and it's not the current dir */
258 /* Assume non empty */
259 DOS_SetError(DOSERR_ACCESS_DENIED);
260 return false;
261 }
262
263 bool DOS_Rename(char const * const oldname,char const * const newname) {
264 Bit8u driveold;char fullold[DOS_PATHLENGTH];
265 Bit8u drivenew;char fullnew[DOS_PATHLENGTH];
266 if (!DOS_MakeName(oldname,fullold,&driveold)) return false;
267 if (!DOS_MakeName(newname,fullnew,&drivenew)) return false;
268 /* No tricks with devices */
269 if ( (DOS_FindDevice(oldname) != DOS_DEVICES) ||
270 (DOS_FindDevice(newname) != DOS_DEVICES) ) {
271 DOS_SetError(DOSERR_FILE_NOT_FOUND);
272 return false;
273 }
274 /* Must be on the same drive */
275 if(driveold != drivenew) {
276 DOS_SetError(DOSERR_NOT_SAME_DEVICE);
277 return false;
278 }
279 /*Test if target exists => no access */
280 Bit16u attr;
281 if(Drives[drivenew]->GetFileAttr(fullnew,&attr)) {
282 DOS_SetError(DOSERR_ACCESS_DENIED);
283 return false;
284 }
285 /* Source must exist, check for path ? */
286 if (!Drives[driveold]->GetFileAttr( fullold, &attr ) ) {
287 DOS_SetError(DOSERR_FILE_NOT_FOUND);
288 return false;
289 }
290
291 if (Drives[drivenew]->Rename(fullold,fullnew)) return true;
292 /* If it still fails. which error should we give ? PATH NOT FOUND or EACCESS */
293 LOG(LOG_FILES,LOG_NORMAL)("Rename fails for %s to %s, no proper errorcode returned.",oldname,newname);
294 DOS_SetError(DOSERR_FILE_NOT_FOUND);
295 return false;
296 }
297
298 bool DOS_FindFirst(char * search,Bit16u attr,bool fcb_findfirst) {
299 DOS_DTA dta(dos.dta());
300 Bit8u drive;char fullsearch[DOS_PATHLENGTH];
301 char dir[DOS_PATHLENGTH];char pattern[DOS_PATHLENGTH];
302 size_t len = strlen(search);
303 if(len && search[len - 1] == '\\' && !( (len > 2) && (search[len - 2] == ':') && (attr == DOS_ATTR_VOLUME) )) {
304 //Dark Forces installer, but c:\ is allright for volume labels(exclusively set)
305 DOS_SetError(DOSERR_NO_MORE_FILES);
306 return false;
307 }
308 if (!DOS_MakeName(search,fullsearch,&drive)) return false;
309 //Check for devices. FindDevice checks for leading subdir as well
310 bool device = (DOS_FindDevice(search) != DOS_DEVICES);
311
312 /* Split the search in dir and pattern */
313 char * find_last;
314 find_last=strrchr(fullsearch,'\\');
315 if (!find_last) { /*No dir */
316 strcpy(pattern,fullsearch);
317 dir[0]=0;
318 } else {
319 *find_last=0;
320 strcpy(pattern,find_last+1);
321 strcpy(dir,fullsearch);
322 }
323
324 dta.SetupSearch(drive,(Bit8u)attr,pattern);
325
326 if(device) {
327 find_last = strrchr(pattern,'.');
328 if(find_last) *find_last = 0;
329 //TODO use current date and time
330 dta.SetResult(pattern,0,0,0,DOS_ATTR_DEVICE);
331 LOG(LOG_DOSMISC,LOG_WARN)("finding device %s",pattern);
332 return true;
333 }
334
335 if (Drives[drive]->FindFirst(dir,dta,fcb_findfirst)) return true;
336
337 return false;
338 }
339
340 bool DOS_FindNext(void) {
341 DOS_DTA dta(dos.dta());
342 Bit8u i = dta.GetSearchDrive();
343 if(i >= DOS_DRIVES || !Drives[i]) {
344 /* Corrupt search. */
345 LOG(LOG_FILES,LOG_ERROR)("Corrupt search!!!!");
346 DOS_SetError(DOSERR_NO_MORE_FILES);
347 return false;
348 }
349 if (Drives[i]->FindNext(dta)) return true;
350 return false;
351 }
352
353
354 bool DOS_ReadFile(Bit16u entry,Bit8u * data,Bit16u * amount) {
355 Bit32u handle=RealHandle(entry);
356 if (handle>=DOS_FILES) {
357 DOS_SetError(DOSERR_INVALID_HANDLE);
358 return false;
359 };
360 if (!Files[handle] || !Files[handle]->IsOpen()) {
361 DOS_SetError(DOSERR_INVALID_HANDLE);
362 return false;
363 };
364 /*
365 if ((Files[handle]->flags & 0x0f) == OPEN_WRITE)) {
366 DOS_SetError(DOSERR_INVALID_HANDLE);
367 return false;
368 }
369 */
370 Bit16u toread=*amount;
371 bool ret=Files[handle]->Read(data,&toread);
372 *amount=toread;
373 return ret;
374 }
375
376 bool DOS_WriteFile(Bit16u entry,Bit8u * data,Bit16u * amount) {
377 Bit32u handle=RealHandle(entry);
378 if (handle>=DOS_FILES) {
379 DOS_SetError(DOSERR_INVALID_HANDLE);
380 return false;
381 };
382 if (!Files[handle] || !Files[handle]->IsOpen()) {
383 DOS_SetError(DOSERR_INVALID_HANDLE);
384 return false;
385 };
386 /*
387 if ((Files[handle]->flags & 0x0f) == OPEN_READ)) {
388 DOS_SetError(DOSERR_INVALID_HANDLE);
389 return false;
390 }
391 */
392 Bit16u towrite=*amount;
393 bool ret=Files[handle]->Write(data,&towrite);
394 *amount=towrite;
395 return ret;
396 }
397
398 bool DOS_SeekFile(Bit16u entry,Bit32u * pos,Bit32u type) {
399 Bit32u handle=RealHandle(entry);
400 if (handle>=DOS_FILES) {
401 DOS_SetError(DOSERR_INVALID_HANDLE);
402 return false;
403 };
404 if (!Files[handle] || !Files[handle]->IsOpen()) {
405 DOS_SetError(DOSERR_INVALID_HANDLE);
406 return false;
407 };
408 return Files[handle]->Seek(pos,type);
409 }
410
411 bool DOS_CloseFile(Bit16u entry) {
412 Bit32u handle=RealHandle(entry);
413 if (handle>=DOS_FILES) {
414 DOS_SetError(DOSERR_INVALID_HANDLE);
415 return false;
416 };
417 if (!Files[handle]) {
418 DOS_SetError(DOSERR_INVALID_HANDLE);
419 return false;
420 };
421 if (Files[handle]->IsOpen()) {
422 Files[handle]->Close();
423 }
424 DOS_PSP psp(dos.psp());
425 psp.SetFileHandle(entry,0xff);
426 if (Files[handle]->RemoveRef()<=0) {
427 delete Files[handle];
428 Files[handle]=0;
429 }
430 return true;
431 }
432
433 bool DOS_FlushFile(Bit16u entry) {
434 Bit32u handle=RealHandle(entry);
435 if (handle>=DOS_FILES) {
436 DOS_SetError(DOSERR_INVALID_HANDLE);
437 return false;
438 };
439 if (!Files[handle] || !Files[handle]->IsOpen()) {
440 DOS_SetError(DOSERR_INVALID_HANDLE);
441 return false;
442 };
443 LOG(LOG_DOSMISC,LOG_NORMAL)("FFlush used.");
444 return true;
445 }
446
447 static bool PathExists(char const * const name) {
448 const char* leading = strrchr(name,'\\');
449 if(!leading) return true;
450 char temp[CROSS_LEN];
451 strcpy(temp,name);
452 char * lead = strrchr(temp,'\\');
453 if (lead == temp) return true;
454 *lead = 0;
455 Bit8u drive;char fulldir[DOS_PATHLENGTH];
456 if (!DOS_MakeName(temp,fulldir,&drive)) return false;
457 if(!Drives[drive]->TestDir(fulldir)) return false;
458 return true;
459 }
460
461
462 bool DOS_CreateFile(char const * name,Bit16u attributes,Bit16u * entry) {
463 // Creation of a device is the same as opening it
464 // Tc201 installer
465 if (DOS_FindDevice(name) != DOS_DEVICES)
466 return DOS_OpenFile(name, OPEN_READ, entry);
467
468 LOG(LOG_FILES,LOG_NORMAL)("file create attributes %X file %s",attributes,name);
469 char fullname[DOS_PATHLENGTH];Bit8u drive;
470 DOS_PSP psp(dos.psp());
471 if (!DOS_MakeName(name,fullname,&drive)) return false;
472 /* Check for a free file handle */
473 Bit8u handle=DOS_FILES;Bit8u i;
474 for (i=0;i<DOS_FILES;i++) {
475 if (!Files[i]) {
476 handle=i;
477 break;
478 }
479 }
480 if (handle==DOS_FILES) {
481 DOS_SetError(DOSERR_TOO_MANY_OPEN_FILES);
482 return false;
483 }
484 /* We have a position in the main table now find one in the psp table */
485 *entry = psp.FindFreeFileEntry();
486 if (*entry==0xff) {
487 DOS_SetError(DOSERR_TOO_MANY_OPEN_FILES);
488 return false;
489 }
490 /* Don't allow directories to be created */
491 if (attributes&DOS_ATTR_DIRECTORY) {
492 DOS_SetError(DOSERR_ACCESS_DENIED);
493 return false;
494 }
495 bool foundit=Drives[drive]->FileCreate(&Files[handle],fullname,attributes);
496 if (foundit) {
497 Files[handle]->SetDrive(drive);
498 Files[handle]->AddRef();
499 psp.SetFileHandle(*entry,handle);
500 return true;
501 } else {
502 if(!PathExists(name)) DOS_SetError(DOSERR_PATH_NOT_FOUND);
503 else DOS_SetError(DOSERR_FILE_NOT_FOUND);
504 return false;
505 }
506 }
507
508 bool DOS_OpenFile(char const * name,Bit8u flags,Bit16u * entry) {
509 /* First check for devices */
510 if (flags>2) LOG(LOG_FILES,LOG_ERROR)("Special file open command %X file %s",flags,name);
511 else LOG(LOG_FILES,LOG_NORMAL)("file open command %X file %s",flags,name);
512
513 DOS_PSP psp(dos.psp());
514 Bit16u attr = 0;
515 Bit8u devnum = DOS_FindDevice(name);
516 bool device = (devnum != DOS_DEVICES);
517 if(!device && DOS_GetFileAttr(name,&attr)) {
518 //DON'T ALLOW directories to be openened.(skip test if file is device).
519 if((attr & DOS_ATTR_DIRECTORY) || (attr & DOS_ATTR_VOLUME)){
520 DOS_SetError(DOSERR_ACCESS_DENIED);
521 return false;
522 }
523 }
524
525 char fullname[DOS_PATHLENGTH];Bit8u drive;Bit8u i;
526 /* First check if the name is correct */
527 if (!DOS_MakeName(name,fullname,&drive)) return false;
528 Bit8u handle=255;
529 /* Check for a free file handle */
530 for (i=0;i<DOS_FILES;i++) {
531 if (!Files[i]) {
532 handle=i;
533 break;
534 }
535 }
536 if (handle==255) {
537 DOS_SetError(DOSERR_TOO_MANY_OPEN_FILES);
538 return false;
539 }
540 /* We have a position in the main table now find one in the psp table */
541 *entry = psp.FindFreeFileEntry();
542
543 if (*entry==0xff) {
544 DOS_SetError(DOSERR_TOO_MANY_OPEN_FILES);
545 return false;
546 }
547 bool exists=false;
548 if (device) {
549 Files[handle]=new DOS_Device(*Devices[devnum]);
550 } else {
551 exists=Drives[drive]->FileOpen(&Files[handle],fullname,flags);
552 if (exists) Files[handle]->SetDrive(drive);
553 }
554 if (exists || device ) {
555 Files[handle]->AddRef();
556 psp.SetFileHandle(*entry,handle);
557 return true;
558 } else {
559 //Test if file exists, but opened in read-write mode (and writeprotected)
560 if(((flags&3) != OPEN_READ) && Drives[drive]->FileExists(fullname))
561 DOS_SetError(DOSERR_ACCESS_DENIED);
562 else {
563 if(!PathExists(name)) DOS_SetError(DOSERR_PATH_NOT_FOUND);
564 else DOS_SetError(DOSERR_FILE_NOT_FOUND);
565 }
566 return false;
567 }
568 }
569
570 bool DOS_OpenFileExtended(char const * name, Bit16u flags, Bit16u createAttr, Bit16u action, Bit16u *entry, Bit16u* status) {
571 // FIXME: Not yet supported : Bit 13 of flags (int 0x24 on critical error)
572 Bit16u result = 0;
573 if (action==0) {
574 // always fail setting
575 DOS_SetError(DOSERR_FUNCTION_NUMBER_INVALID);
576 return false;
577 } else {
578 if (((action & 0x0f)>2) || ((action & 0xf0)>0x10)) {
579 // invalid action parameter
580 DOS_SetError(DOSERR_FUNCTION_NUMBER_INVALID);
581 return false;
582 }
583 }
584 if (DOS_OpenFile(name, (Bit8u)(flags&0xff), entry)) {
585 // File already exists
586 switch (action & 0x0f) {
587 case 0x00: // failed
588 DOS_SetError(DOSERR_FILE_ALREADY_EXISTS);
589 return false;
590 case 0x01: // file open (already done)
591 result = 1;
592 break;
593 case 0x02: // replace
594 DOS_CloseFile(*entry);
595 if (!DOS_CreateFile(name, createAttr, entry)) return false;
596 result = 3;
597 break;
598 default:
599 DOS_SetError(DOSERR_FUNCTION_NUMBER_INVALID);
600 E_Exit("DOS: OpenFileExtended: Unknown action.");
601 break;
602 }
603 } else {
604 // File doesn't exist
605 if ((action & 0xf0)==0) {
606 // uses error code from failed open
607 return false;
608 }
609 // Create File
610 if (!DOS_CreateFile(name, createAttr, entry)) {
611 // uses error code from failed create
612 return false;
613 }
614 result = 2;
615 }
616 // success
617 *status = result;
618 return true;
619 }
620
621 bool DOS_UnlinkFile(char const * const name) {
622 char fullname[DOS_PATHLENGTH];Bit8u drive;
623 if (!DOS_MakeName(name,fullname,&drive)) return false;
624 if(Drives[drive]->FileUnlink(fullname)){
625 return true;
626 } else {
627 DOS_SetError(DOSERR_FILE_NOT_FOUND);
628 return false;
629 }
630 }
631
632 bool DOS_GetFileAttr(char const * const name,Bit16u * attr) {
633 char fullname[DOS_PATHLENGTH];Bit8u drive;
634 if (!DOS_MakeName(name,fullname,&drive)) return false;
635 if (Drives[drive]->GetFileAttr(fullname,attr)) {
636 return true;
637 } else {
638 DOS_SetError(DOSERR_FILE_NOT_FOUND);
639 return false;
640 }
641 }
642
643 bool DOS_SetFileAttr(char const * const name,Bit16u /*attr*/)
644 // this function does not change the file attributs
645 // it just does some tests if file is available
646 // returns false when using on cdrom (stonekeep)
647 {
648 Bit16u attrTemp;
649 char fullname[DOS_PATHLENGTH];Bit8u drive;
650 if (!DOS_MakeName(name,fullname,&drive)) return false;
651 if (strncmp(Drives[drive]->GetInfo(),"CDRom ",6)==0 || strncmp(Drives[drive]->GetInfo(),"isoDrive ",9)==0) {
652 DOS_SetError(DOSERR_ACCESS_DENIED);
653 return false;
654 }
655 return Drives[drive]->GetFileAttr(fullname,&attrTemp);
656 }
657
658 bool DOS_Canonicalize(char const * const name,char * const big) {
659 //TODO Add Better support for devices and shit but will it be needed i doubt it :)
660 Bit8u drive;
661 char fullname[DOS_PATHLENGTH];
662 if (!DOS_MakeName(name,fullname,&drive)) return false;
663 big[0]=drive+'A';
664 big[1]=':';
665 big[2]='\\';
666 strcpy(&big[3],fullname);
667 return true;
668 }
669
670 bool DOS_GetFreeDiskSpace(Bit8u drive,Bit16u * bytes,Bit8u * sectors,Bit16u * clusters,Bit16u * free) {
671 if (drive==0) drive=DOS_GetDefaultDrive();
672 else drive--;
673 if ((drive>=DOS_DRIVES) || (!Drives[drive])) {
674 DOS_SetError(DOSERR_INVALID_DRIVE);
675 return false;
676 }
677 return Drives[drive]->AllocationInfo(bytes,sectors,clusters,free);
678 }
679
680 bool DOS_DuplicateEntry(Bit16u entry,Bit16u * newentry) {
681 // Dont duplicate console handles
682 /* if (entry<=STDPRN) {
683 *newentry = entry;
684 return true;
685 };
686 */
687 Bit8u handle=RealHandle(entry);
688 if (handle>=DOS_FILES) {
689 DOS_SetError(DOSERR_INVALID_HANDLE);
690 return false;
691 };
692 if (!Files[handle] || !Files[handle]->IsOpen()) {
693 DOS_SetError(DOSERR_INVALID_HANDLE);
694 return false;
695 };
696 DOS_PSP psp(dos.psp());
697 *newentry = psp.FindFreeFileEntry();
698 if (*newentry==0xff) {
699 DOS_SetError(DOSERR_TOO_MANY_OPEN_FILES);
700 return false;
701 }
702 Files[handle]->AddRef();
703 psp.SetFileHandle(*newentry,handle);
704 return true;
705 }
706
707 bool DOS_ForceDuplicateEntry(Bit16u entry,Bit16u newentry) {
708 if(entry == newentry) {
709 DOS_SetError(DOSERR_INVALID_HANDLE);
710 return false;
711 }
712 Bit8u orig = RealHandle(entry);
713 if (orig >= DOS_FILES) {
714 DOS_SetError(DOSERR_INVALID_HANDLE);
715 return false;
716 };
717 if (!Files[orig] || !Files[orig]->IsOpen()) {
718 DOS_SetError(DOSERR_INVALID_HANDLE);
719 return false;
720 };
721 Bit8u newone = RealHandle(newentry);
722 if (newone < DOS_FILES && Files[newone]) {
723 DOS_CloseFile(newentry);
724 }
725 DOS_PSP psp(dos.psp());
726 Files[orig]->AddRef();
727 psp.SetFileHandle(newentry,orig);
728 return true;
729 }
730
731
732 bool DOS_CreateTempFile(char * const name,Bit16u * entry) {
733 size_t namelen=strlen(name);
734 char * tempname=name+namelen;
735 if (namelen==0) {
736 // temp file created in root directory
737 tempname[0]='\\';
738 tempname++;
739 } else {
740 if ((name[namelen-1]!='\\') && (name[namelen-1]!='/')) {
741 tempname[0]='\\';
742 tempname++;
743 }
744 }
745 dos.errorcode=0;
746 /* add random crap to the end of the name and try to open */
747 do {
748 Bit32u i;
749 for (i=0;i<8;i++) {
750 tempname[i]=(rand()%26)+'A';
751 }
752 tempname[8]=0;
753 } while ((!DOS_CreateFile(name,0,entry)) && (dos.errorcode==DOSERR_FILE_ALREADY_EXISTS));
754 if (dos.errorcode) return false;
755 return true;
756 }
757
758 #define FCB_SEP ":.;,=+"
759 #define ILLEGAL ":.;,=+ \t/\"[]<>|"
760
761 static bool isvalid(const char in){
762 const char ill[]=ILLEGAL;
763 return (Bit8u(in)>0x1F) && (!strchr(ill,in));
764 }
765
766 #define PARSE_SEP_STOP 0x01
767 #define PARSE_DFLT_DRIVE 0x02
768 #define PARSE_BLNK_FNAME 0x04
769 #define PARSE_BLNK_FEXT 0x08
770
771 #define PARSE_RET_NOWILD 0
772 #define PARSE_RET_WILD 1
773 #define PARSE_RET_BADDRIVE 0xff
774
775 Bit8u FCB_Parsename(Bit16u seg,Bit16u offset,Bit8u parser ,char *string, Bit8u *change) {
776 char * string_begin=string;
777 Bit8u ret=0;
778 if (!(parser & PARSE_DFLT_DRIVE)) {
779 // default drive forced, this intentionally invalidates an extended FCB
780 mem_writeb(PhysMake(seg,offset),0);
781 }
782 DOS_FCB fcb(seg,offset,false); // always a non-extended FCB
783 bool hasdrive,hasname,hasext,finished;
784 hasdrive=hasname=hasext=finished=false;
785 Bitu index=0;
786 Bit8u fill=' ';
787 /* First get the old data from the fcb */
788 #ifdef _MSC_VER
789 #pragma pack (1)
790 #endif
791 union {
792 struct {
793 char drive[2];
794 char name[9];
795 char ext[4];
796 } GCC_ATTRIBUTE (packed) part;
797 char full[DOS_FCBNAME];
798 } fcb_name;
799 #ifdef _MSC_VER
800 #pragma pack()
801 #endif
802 /* Get the old information from the previous fcb */
803 fcb.GetName(fcb_name.full);
804 fcb_name.part.drive[0]-='A'-1;fcb_name.part.drive[1]=0;
805 fcb_name.part.name[8]=0;fcb_name.part.ext[3]=0;
806 /* Strip of the leading sepetaror */
807 if((parser & PARSE_SEP_STOP) && *string) { //ignore leading seperator
808 char sep[] = FCB_SEP;char a[2];
809 a[0]= *string;a[1]='\0';
810 if (strcspn(a,sep)==0) string++;
811 }
812 /* strip leading spaces */
813 while((*string==' ')||(*string=='\t')) string++;
814 /* Check for a drive */
815 if (string[1]==':') {
816 fcb_name.part.drive[0]=0;
817 hasdrive=true;
818 if (isalpha(string[0]) && Drives[toupper(string[0])-'A']) {
819 fcb_name.part.drive[0]=(char)(toupper(string[0])-'A'+1);
820 } else ret=0xff;
821 string+=2;
822 }
823 /* Special checks for . and .. */
824 if (string[0]=='.') {
825 string++;
826 if (!string[0]) {
827 hasname=true;
828 ret=PARSE_RET_NOWILD;
829 strcpy(fcb_name.part.name,". ");
830 goto savefcb;
831 }
832 if (string[1]=='.' && !string[1]) {
833 string++;
834 hasname=true;
835 ret=PARSE_RET_NOWILD;
836 strcpy(fcb_name.part.name,".. ");
837 goto savefcb;
838 }
839 goto checkext;
840 }
841 /* Copy the name */
842 hasname=true;finished=false;fill=' ';index=0;
843 while (index<8) {
844 if (!finished) {
845 if (string[0]=='*') {fill='?';fcb_name.part.name[index]='?';if (!ret) ret=1;finished=true;}
846 else if (string[0]=='?') {fcb_name.part.name[index]='?';if (!ret) ret=1;}
847 else if (isvalid(string[0])) {fcb_name.part.name[index]=(char)(toupper(string[0]));}
848 else { finished=true;continue; }
849 string++;
850 } else {
851 fcb_name.part.name[index]=fill;
852 }
853 index++;
854 }
855 if (!(string[0]=='.')) goto savefcb;
856 string++;
857 checkext:
858 /* Copy the extension */
859 hasext=true;finished=false;fill=' ';index=0;
860 while (index<3) {
861 if (!finished) {
862 if (string[0]=='*') {fill='?';fcb_name.part.ext[index]='?';finished=true;}
863 else if (string[0]=='?') {fcb_name.part.ext[index]='?';if (!ret) ret=1;}
864 else if (isvalid(string[0])) {fcb_name.part.ext[index]=(char)(toupper(string[0]));}
865 else { finished=true;continue; }
866 string++;
867 } else {
868 fcb_name.part.ext[index]=fill;
869 }
870 index++;
871 }
872 savefcb:
873 if (!hasdrive & !(parser & PARSE_DFLT_DRIVE)) fcb_name.part.drive[0] = 0;
874 if (!hasname & !(parser & PARSE_BLNK_FNAME)) strcpy(fcb_name.part.name," ");
875 if (!hasext & !(parser & PARSE_BLNK_FEXT)) strcpy(fcb_name.part.ext," ");
876 fcb.SetName(fcb_name.part.drive[0],fcb_name.part.name,fcb_name.part.ext);
877 *change=(Bit8u)(string-string_begin);
878 return ret;
879 }
880
881 static void DTAExtendName(char * const name,char * const filename,char * const ext) {
882 char * find=strchr(name,'.');
883 if (find) {
884 strcpy(ext,find+1);
885 *find=0;
886 } else ext[0]=0;
887 strcpy(filename,name);
888 size_t i;
889 for (i=strlen(name);i<8;i++) filename[i]=' ';
890 filename[8]=0;
891 for (i=strlen(ext);i<3;i++) ext[i]=' ';
892 ext[3]=0;
893 }
894
895 static void SaveFindResult(DOS_FCB & find_fcb) {
896 DOS_DTA find_dta(dos.tables.tempdta);
897 char name[DOS_NAMELENGTH_ASCII];Bit32u size;Bit16u date;Bit16u time;Bit8u attr;Bit8u drive;
898 char file_name[9];char ext[4];
899 find_dta.GetResult(name,size,date,time,attr);
900 drive=find_fcb.GetDrive()+1;
901 /* Create a correct file and extention */
902 DTAExtendName(name,file_name,ext);
903 DOS_FCB fcb(RealSeg(dos.dta()),RealOff(dos.dta()));//TODO
904 fcb.Create(find_fcb.Extended());
905 fcb.SetName(drive,file_name,ext);
906 fcb.SetAttr(attr); /* Only adds attribute if fcb is extended */
907 fcb.SetSizeDateTime(size,date,time);
908 }
909
910 bool DOS_FCBCreate(Bit16u seg,Bit16u offset) {
911 DOS_FCB fcb(seg,offset);
912 char shortname[DOS_FCBNAME];Bit16u handle;
913 fcb.GetName(shortname);
914 if (!DOS_CreateFile(shortname,DOS_ATTR_ARCHIVE,&handle)) return false;
915 fcb.FileOpen((Bit8u)handle);
916 return true;
917 }
918
919 bool DOS_FCBOpen(Bit16u seg,Bit16u offset) {
920 DOS_FCB fcb(seg,offset);
921 char shortname[DOS_FCBNAME];Bit16u handle;
922 fcb.GetName(shortname);
923
924 /* First check if the name is correct */
925 Bit8u drive;
926 char fullname[DOS_PATHLENGTH];
927 if (!DOS_MakeName(shortname,fullname,&drive)) return false;
928
929 /* Check, if file is already opened */
930 for (Bit8u i=0;i<DOS_FILES;i++) {
931 DOS_PSP psp(dos.psp());
932 if (Files[i] && Files[i]->IsOpen() && Files[i]->IsName(fullname)) {
933 handle = psp.FindEntryByHandle(i);
934 if (handle==0xFF) {
935 // This shouldnt happen
936 LOG(LOG_FILES,LOG_ERROR)("DOS: File %s is opened but has no psp entry.",shortname);
937 return false;
938 }
939 fcb.FileOpen((Bit8u)handle);
940 return true;
941 }
942 }
943
944 if (!DOS_OpenFile(shortname,OPEN_READWRITE,&handle)) return false;
945 fcb.FileOpen((Bit8u)handle);
946 return true;
947 }
948
949 bool DOS_FCBClose(Bit16u seg,Bit16u offset) {
950 DOS_FCB fcb(seg,offset);
951 if(!fcb.Valid()) return false;
952 Bit8u fhandle;
953 fcb.FileClose(fhandle);
954 DOS_CloseFile(fhandle);
955 return true;
956 }
957
958 bool DOS_FCBFindFirst(Bit16u seg,Bit16u offset) {
959 DOS_FCB fcb(seg,offset);
960 RealPt old_dta=dos.dta();dos.dta(dos.tables.tempdta);
961 char name[DOS_FCBNAME];fcb.GetName(name);
962 Bit8u attr = DOS_ATTR_ARCHIVE;
963 fcb.GetAttr(attr); /* Gets search attributes if extended */
964 bool ret=DOS_FindFirst(name,attr,true);
965 dos.dta(old_dta);
966 if (ret) SaveFindResult(fcb);
967 return ret;
968 }
969
970 bool DOS_FCBFindNext(Bit16u seg,Bit16u offset) {
971 DOS_FCB fcb(seg,offset);
972 RealPt old_dta=dos.dta();dos.dta(dos.tables.tempdta);
973 bool ret=DOS_FindNext();
974 dos.dta(old_dta);
975 if (ret) SaveFindResult(fcb);
976 return ret;
977 }
978
979 Bit8u DOS_FCBRead(Bit16u seg,Bit16u offset,Bit16u recno) {
980 DOS_FCB fcb(seg,offset);
981 Bit8u fhandle,cur_rec;Bit16u cur_block,rec_size;
982 fcb.GetSeqData(fhandle,rec_size);
983 fcb.GetRecord(cur_block,cur_rec);
984 Bit32u pos=((cur_block*128)+cur_rec)*rec_size;
985 if (!DOS_SeekFile(fhandle,&pos,DOS_SEEK_SET)) return FCB_READ_NODATA;
986 Bit16u toread=rec_size;
987 if (!DOS_ReadFile(fhandle,dos_copybuf,&toread)) return FCB_READ_NODATA;
988 if (toread==0) return FCB_READ_NODATA;
989 if (toread < rec_size) { //Zero pad copybuffer to rec_size
990 Bitu i = toread;
991 while(i < rec_size) dos_copybuf[i++] = 0;
992 }
993 MEM_BlockWrite(Real2Phys(dos.dta())+recno*rec_size,dos_copybuf,rec_size);
994 if (++cur_rec>127) { cur_block++;cur_rec=0; }
995 fcb.SetRecord(cur_block,cur_rec);
996 if (toread==rec_size) return FCB_SUCCESS;
997 if (toread==0) return FCB_READ_NODATA;
998 return FCB_READ_PARTIAL;
999 }
1000
1001 Bit8u DOS_FCBWrite(Bit16u seg,Bit16u offset,Bit16u recno) {
1002 DOS_FCB fcb(seg,offset);
1003 Bit8u fhandle,cur_rec;Bit16u cur_block,rec_size;
1004 fcb.GetSeqData(fhandle,rec_size);
1005 fcb.GetRecord(cur_block,cur_rec);
1006 Bit32u pos=((cur_block*128)+cur_rec)*rec_size;
1007 if (!DOS_SeekFile(fhandle,&pos,DOS_SEEK_SET)) return FCB_ERR_WRITE;
1008 MEM_BlockRead(Real2Phys(dos.dta())+recno*rec_size,dos_copybuf,rec_size);
1009 Bit16u towrite=rec_size;
1010 if (!DOS_WriteFile(fhandle,dos_copybuf,&towrite)) return FCB_ERR_WRITE;
1011 Bit32u size;Bit16u date,time;
1012 fcb.GetSizeDateTime(size,date,time);
1013 if (pos+towrite>size) size=pos+towrite;
1014 //time doesn't keep track of endofday
1015 date = DOS_PackDate(dos.date.year,dos.date.month,dos.date.day);
1016 Bit32u ticks = mem_readd(BIOS_TIMER);
1017 Bit32u seconds = (ticks*10)/182;
1018 Bit16u hour = (Bit16u)(seconds/3600);
1019 Bit16u min = (Bit16u)((seconds % 3600)/60);
1020 Bit16u sec = (Bit16u)(seconds % 60);
1021 time = DOS_PackTime(hour,min,sec);
1022 Bit8u temp=RealHandle(fhandle);
1023 Files[temp]->time=time;
1024 Files[temp]->date=date;
1025 fcb.SetSizeDateTime(size,date,time);
1026 if (++cur_rec>127) { cur_block++;cur_rec=0; }
1027 fcb.SetRecord(cur_block,cur_rec);
1028 return FCB_SUCCESS;
1029 }
1030
1031 Bit8u DOS_FCBIncreaseSize(Bit16u seg,Bit16u offset) {
1032 DOS_FCB fcb(seg,offset);
1033 Bit8u fhandle,cur_rec;Bit16u cur_block,rec_size;
1034 fcb.GetSeqData(fhandle,rec_size);
1035 fcb.GetRecord(cur_block,cur_rec);
1036 Bit32u pos=((cur_block*128)+cur_rec)*rec_size;
1037 if (!DOS_SeekFile(fhandle,&pos,DOS_SEEK_SET)) return FCB_ERR_WRITE;
1038 Bit16u towrite=0;
1039 if (!DOS_WriteFile(fhandle,dos_copybuf,&towrite)) return FCB_ERR_WRITE;
1040 Bit32u size;Bit16u date,time;
1041 fcb.GetSizeDateTime(size,date,time);
1042 if (pos+towrite>size) size=pos+towrite;
1043 //time doesn't keep track of endofday
1044 date = DOS_PackDate(dos.date.year,dos.date.month,dos.date.day);
1045 Bit32u ticks = mem_readd(BIOS_TIMER);
1046 Bit32u seconds = (ticks*10)/182;
1047 Bit16u hour = (Bit16u)(seconds/3600);
1048 Bit16u min = (Bit16u)((seconds % 3600)/60);
1049 Bit16u sec = (Bit16u)(seconds % 60);
1050 time = DOS_PackTime(hour,min,sec);
1051 Bit8u temp=RealHandle(fhandle);
1052 Files[temp]->time=time;
1053 Files[temp]->date=date;
1054 fcb.SetSizeDateTime(size,date,time);
1055 fcb.SetRecord(cur_block,cur_rec);
1056 return FCB_SUCCESS;
1057 }
1058
1059 Bit8u DOS_FCBRandomRead(Bit16u seg,Bit16u offset,Bit16u numRec,bool restore) {
1060 /* if restore is true :random read else random blok read.
1061 * random read updates old block and old record to reflect the random data
1062 * before the read!!!!!!!!! and the random data is not updated! (user must do this)
1063 * Random block read updates these fields to reflect the state after the read!
1064 */
1065
1066 /* BUG: numRec should return the amount of records read!
1067 * Not implemented yet as I'm unsure how to count on error states (partial/failed)
1068 */
1069
1070 DOS_FCB fcb(seg,offset);
1071 Bit32u random;
1072 Bit16u old_block=0;
1073 Bit8u old_rec=0;
1074 Bit8u error=0;
1075
1076 /* Set the correct record from the random data */
1077 fcb.GetRandom(random);
1078 fcb.SetRecord((Bit16u)(random / 128),(Bit8u)(random & 127));
1079 if (restore) fcb.GetRecord(old_block,old_rec);//store this for after the read.
1080 // Read records
1081 for (int i=0; i<numRec; i++) {
1082 error = DOS_FCBRead(seg,offset,(Bit16u)i);
1083 if (error!=0x00) break;
1084 }
1085 Bit16u new_block;Bit8u new_rec;
1086 fcb.GetRecord(new_block,new_rec);
1087 if (restore) fcb.SetRecord(old_block,old_rec);
1088 /* Update the random record pointer with new position only when restore is false*/
1089 if(!restore) fcb.SetRandom(new_block*128+new_rec);
1090 return error;
1091 }
1092
1093 Bit8u DOS_FCBRandomWrite(Bit16u seg,Bit16u offset,Bit16u numRec,bool restore) {
1094 /* see FCB_RandomRead */
1095 DOS_FCB fcb(seg,offset);
1096 Bit32u random;
1097 Bit16u old_block=0;
1098 Bit8u old_rec=0;
1099 Bit8u error=0;
1100
1101 /* Set the correct record from the random data */
1102 fcb.GetRandom(random);
1103 fcb.SetRecord((Bit16u)(random / 128),(Bit8u)(random & 127));
1104 if (restore) fcb.GetRecord(old_block,old_rec);
1105 if (numRec>0) {
1106 /* Write records */
1107 for (int i=0; i<numRec; i++) {
1108 error = DOS_FCBWrite(seg,offset,(Bit16u)i);// dos_fcbwrite return 0 false when true...
1109 if (error!=0x00) break;
1110 }
1111 } else {
1112 DOS_FCBIncreaseSize(seg,offset);
1113 }
1114 Bit16u new_block;Bit8u new_rec;
1115 fcb.GetRecord(new_block,new_rec);
1116 if (restore) fcb.SetRecord(old_block,old_rec);
1117 /* Update the random record pointer with new position only when restore is false */
1118 if(!restore) fcb.SetRandom(new_block*128+new_rec);
1119 return error;
1120 }
1121
1122 bool DOS_FCBGetFileSize(Bit16u seg,Bit16u offset) {
1123 char shortname[DOS_PATHLENGTH];Bit16u entry;Bit8u handle;Bit16u rec_size;
1124 DOS_FCB fcb(seg,offset);
1125 fcb.GetName(shortname);
1126 if (!DOS_OpenFile(shortname,OPEN_READ,&entry)) return false;
1127 handle = RealHandle(entry);
1128 Bit32u size = 0;
1129 Files[handle]->Seek(&size,DOS_SEEK_END);
1130 DOS_CloseFile(entry);fcb.GetSeqData(handle,rec_size);
1131 Bit32u random=(size/rec_size);
1132 if (size % rec_size) random++;
1133 fcb.SetRandom(random);
1134 return true;
1135 }
1136
1137 bool DOS_FCBDeleteFile(Bit16u seg,Bit16u offset){
1138 /* FCB DELETE honours wildcards. it will return true if one or more
1139 * files get deleted.
1140 * To get this: the dta is set to temporary dta in which found files are
1141 * stored. This can not be the tempdta as that one is used by fcbfindfirst
1142 */
1143 RealPt old_dta=dos.dta();dos.dta(dos.tables.tempdta_fcbdelete);
1144 DOS_FCB fcb(RealSeg(dos.dta()),RealOff(dos.dta()));
1145 bool nextfile = false;
1146 bool return_value = false;
1147 nextfile = DOS_FCBFindFirst(seg,offset);
1148 while(nextfile) {
1149 char shortname[DOS_FCBNAME] = { 0 };
1150 fcb.GetName(shortname);
1151 bool res=DOS_UnlinkFile(shortname);
1152 if(!return_value && res) return_value = true; //at least one file deleted
1153 nextfile = DOS_FCBFindNext(seg,offset);
1154 }
1155 dos.dta(old_dta); /*Restore dta */
1156 return return_value;
1157 }
1158
1159 bool DOS_FCBRenameFile(Bit16u seg, Bit16u offset){
1160 DOS_FCB fcbold(seg,offset);
1161 DOS_FCB fcbnew(seg,offset+16);
1162 char oldname[DOS_FCBNAME];
1163 char newname[DOS_FCBNAME];
1164 fcbold.GetName(oldname);fcbnew.GetName(newname);
1165 return DOS_Rename(oldname,newname);
1166 }
1167
1168 void DOS_FCBSetRandomRecord(Bit16u seg, Bit16u offset) {
1169 DOS_FCB fcb(seg,offset);
1170 Bit16u block;Bit8u rec;
1171 fcb.GetRecord(block,rec);
1172 fcb.SetRandom(block*128+rec);
1173 }
1174
1175
1176 bool DOS_FileExists(char const * const name) {
1177 char fullname[DOS_PATHLENGTH];Bit8u drive;
1178 if (!DOS_MakeName(name,fullname,&drive)) return false;
1179 return Drives[drive]->FileExists(fullname);
1180 }
1181
1182 bool DOS_GetAllocationInfo(Bit8u drive,Bit16u * _bytes_sector,Bit8u * _sectors_cluster,Bit16u * _total_clusters) {
1183 if (!drive) drive = DOS_GetDefaultDrive();
1184 else drive--;
1185 if (drive >= DOS_DRIVES || !Drives[drive]) return false;
1186 Bit16u _free_clusters;
1187 Drives[drive]->AllocationInfo(_bytes_sector,_sectors_cluster,_total_clusters,&_free_clusters);
1188 SegSet16(ds,RealSeg(dos.tables.mediaid));
1189 reg_bx=RealOff(dos.tables.mediaid+drive*2);
1190 return true;
1191 }
1192
1193 bool DOS_SetDrive(Bit8u drive) {
1194 if (Drives[drive]) {
1195 DOS_SetDefaultDrive(drive);
1196 return true;
1197 } else {
1198 return false;
1199 }
1200 }
1201
1202 bool DOS_GetFileDate(Bit16u entry, Bit16u* otime, Bit16u* odate) {
1203 Bit32u handle=RealHandle(entry);
1204 if (handle>=DOS_FILES) {
1205 DOS_SetError(DOSERR_INVALID_HANDLE);
1206 return false;
1207 };
1208 if (!Files[handle] || !Files[handle]->IsOpen()) {
1209 DOS_SetError(DOSERR_INVALID_HANDLE);
1210 return false;
1211 };
1212 if (!Files[handle]->UpdateDateTimeFromHost()) {
1213 DOS_SetError(DOSERR_INVALID_HANDLE);
1214 return false;
1215 }
1216 *otime = Files[handle]->time;
1217 *odate = Files[handle]->date;
1218 return true;
1219 }
1220
1221 void DOS_SetupFiles (void) {
1222 /* Setup the File Handles */
1223 Bit32u i;
1224 for (i=0;i<DOS_FILES;i++) {
1225 Files[i]=0;
1226 }
1227 /* Setup the Virtual Disk System */
1228 for (i=0;i<DOS_DRIVES;i++) {
1229 Drives[i]=0;
1230 }
1231 Drives[25]=new Virtual_Drive();
1232 }