-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredir.c
1355 lines (1116 loc) · 33 KB
/
redir.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* VMSMOUNT - A network redirector for mounting VMware's Shared Folders in DOS
* Copyright (C) 2011-2022 Eduardo Casino
*
* redir.c: Redirector interface
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <dos.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "dosdefs.h"
#include "globals.h"
#include "redir.h"
#include "vmint.h"
#include "vmshf.h"
#include "debug.h"
#include "lfn.h"
#include "miniclib.h"
#include "vmcall.h"
#include "vmdos.h"
#include "vmtool.h"
#define SECTOR_SIZE 32768
typedef void (*redirFunction)(void);
// We place it here so if there is a stack overflow, we'll notice at the
// first "DIR" :-)
//
static char volLabel[12] = "SharedFldrs";
// Stack management
//
uint8_t newStack[STACK_SIZE] = {
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC,
0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC, 0xEC};
static uint16_t __far *fpStackParam; // Far pointer to top os stack at entry
static uint16_t dosSS;
static uint16_t dosSP;
static uint16_t dosBP;
static uint16_t curSP;
void(__interrupt __far *fpPrevInt2fHandler)();
static union INTPACK __far *r;
static redirFunction currFunction;
static __segment dosDS;
__segment myDS;
uint8_t lfn = 0;
uint8_t driveNum = 0xFF;
CDS __far *fpCDS; // CDS for this drive
SDA __far *fpSDA;
SDB __far *fpSDB;
FDB __far *fpFDB;
SFTT __far *fpFileTable;
char __far *fpFcbName1;
char __far *fpFcbName2;
char __far *fpFileName1;
char __far *fpFileName2;
char __far *fpCurrentPath;
char __far *fpLongFileName1;
char __far *fpLongFileName2;
static char fcbName[12];
static void SetSftOwner(void);
#pragma aux SetSftOwner = \
/* Set DOS DS and stack */ \
"mov curSP, sp" /* Save current SP */ \
"mov ss, dosSS" \
"mov sp, dosSP" \
"push ds" \
"mov ds, dosDS" \
\
/* Call int2F120C "OPEN DEVICE AND SET SFT OWNER/MODE" */ \
/* needs DS = DOS DS and a DOS internal stack */ \
/* seems that it destroys ES and DI */ \
"mov ax, 0x120C" \
"int 0x2F" \
\
/* Restore our DS and internal stack */ \
"pop bx" /* Restore saved DS */ \
"mov ds, bx" \
"mov ss, bx" /* Restore saved SS, same as DS */ \
"mov sp, curSP" /* Restore saved SP */ \
__modify [__ax __bx __es __di];
static uint32_t GetDosTime(void);
#pragma aux GetDosTime = /* Set DOS DS and stack */ \
"mov curSP, sp" /* Save current SP */ \
"mov ss, dosSS" \
"mov sp, dosSP" \
"push ds" \
"mov ds, dosDS" \
\
/* Call int2F120D "GET DATE AND TIME" */ \
/* needs DS = DOS DS and a DOS internal stack */ \
"mov ax, 0x120D" \
"int 0x2F" \
"xchg ax, dx" \
\
/* Restore our DS and internal stack */ \
"pop bx" /* Restore saved DS */ \
"mov ds, bx" \
"mov ss, bx" /* Restore saved SS, same as DS */ \
"mov sp, curSP" /* Restore saved SP */ \
__value [__ax __dx] \
__modify [__bx];
static inline void Success(void)
{
r->w.flags &= ~INTR_CF;
r->w.ax = 0;
return;
}
static inline void Failure(uint16_t error)
{
r->w.flags |= INTR_CF;
r->w.ax = error;
return;
}
inline void FillFcbName(char __far *fcbName, char __far *fileName)
{
int i;
char __far *p = _fstrrchr_local(fileName, '\\') + 1;
for (i = 0; *p; ++p)
{
if (*p == '.')
{
while (i < 8)
{
fcbName[i++] = ' ';
}
}
else
{
fcbName[i++] = *p;
}
}
while (i < 11)
{
fcbName[i++] = ' ';
}
return;
}
static void InstallationCheck(void)
{
DPUTS("InstallationCheck()");
r->w.ax = (uint16_t)0x00FF; // Installed
return;
}
static void RmDir(void)
{
int ret;
uint32_t status;
DPUTS("RmDir()");
ret = VMShfDeleteDir(lfn ? LfnGetTrueLongName(fpLongFileName1, fpFileName1) : fpFileName1, lfn, &status);
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS)
{
Failure(VmshfStatusToDosError(status));
if (r->w.ax == DOS_FILENOTFND)
{
r->w.ax = DOS_PATHNOTFND;
}
}
return;
}
static void MkDir(void)
{
int ret;
uint32_t status;
DPUTS("MkDir()");
ret = VMShfCreateDir(VMSHF_FILEMODE_ALL, lfn ? LfnGetTrueLongName(fpLongFileName1, fpFileName1) : fpFileName1,
lfn, &status);
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS)
{
Failure(VmshfStatusToDosError(status));
if (r->w.ax == DOS_FILENOTFND)
{
r->w.ax = DOS_PATHNOTFND;
}
}
return;
}
static void ChDir(void)
{
uint32_t status;
VMShfAttr *fAttr;
uint8_t fatAttr;
int ret;
DPUTS("ChDir()");
if (*(uint16_t __far *)fpFileName1 != '\0\\')
{
// Check if path exists and is a directory
//
ret = VMShfGetAttr(lfn ? LfnGetTrueLongName(fpLongFileName1, fpFileName1) : fpFileName1, lfn,
VMSHF_INVALID_HANDLE, &status, &fAttr);
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS)
{
Failure(VmshfStatusToDosError(status));
if (r->w.ax == DOS_FILENOTFND)
{
r->w.ax = DOS_PATHNOTFND;
}
return;
}
else
{
fatAttr = FModeToFatAttr(fAttr);
if (!(fatAttr & _A_SUBDIR))
{
Failure(DOS_PATHNOTFND);
return;
}
}
}
_fstrcpy_local(fpCurrentPath, fpFileName1);
return;
}
static void _CloseFile(SFT __far *fpSFT)
{
uint32_t status;
static VMShfAttr newAttr = {0};
// Check if date/time is to be set from clock at CLOSE.
if (!(fpSFT->flags & SFT_FCLEAN))
{
if (!(fpSFT->flags & SFT_FDATE))
{
fpSFT->fileTime = GetDosTime();
}
}
if ((fpSFT->flags & (SFT_FCLEAN | SFT_FDATE)) != SFT_FCLEAN)
{
// Set modification time
newAttr.mask = VMSHF_VALID_ATTR_UTIME;
newAttr.utime = FatTimeToFTime(fpSFT->fileTime);
if (!(fpSFT->fileAttr & _A_RDONLY))
{
(void)VMShfSetAttr(&newAttr, NULL, 0, fpSFT->handle, &status);
}
else
{
(void)VMShfSetAttr(&newAttr, lfn ? LfnGetTrueLongName(fpLongFileName1, fpFileName1) : fpFileName1,
lfn, VMSHF_INVALID_HANDLE, &status);
}
}
if (fpSFT->handleCount) // Decrement handle count
{
--fpSFT->handleCount;
}
(void)VMShfCloseFile(fpSFT->handle, &status);
return;
}
static void CloseFile(void)
{
SFT __far *fpSFT = (SFT __far *)MK_FP(r->w.es, r->w.di);
DPUTS("CloseFile()");
_CloseFile(fpSFT);
}
static void CommitFile(void)
{
DPUTS("CommitFile()");
// Just succeed
return;
}
static void ReadFile(void)
{
int ret;
uint32_t status;
uint32_t length, toread;
char *data;
char __far *buffer;
SFT __far *fpSFT = (SFT __far *)MK_FP(r->w.es, r->w.di);
DPUTS("ReadFile()");
if (fpSFT->openMode & O_WRONLY)
{
Failure(DOS_ACCESS);
return;
}
if ((fpSFT->filePos + r->w.cx) > fpSFT->fileSize)
{
// Can't read past the EOF
r->w.cx = (uint16_t)(fpSFT->fileSize - fpSFT->filePos);
}
if (!r->w.cx) // Anything to read?
{
return;
}
buffer = fpSDA->fpCurrentDTA;
toread = (uint32_t)r->w.cx;
do
{
length = toread;
ret = VMShfReadFile(fpSFT->handle, (uint64_t)fpSFT->filePos, &length, &data, &status);
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS)
{
Failure(VmshfStatusToDosError(status));
break;
}
else
{
if (!length) // Can't read more data
{
break;
}
_fmemcpy_local(buffer, MK_FP(myDS, data), length);
buffer += length;
fpSFT->filePos += length;
toread -= length;
}
} while (toread);
r->w.cx -= (uint16_t)toread;
return;
}
static void WriteFile(void)
{
int ret;
uint32_t status;
uint32_t length, towrite;
char __far *buffer;
static VMShfAttr newAttr;
SFT __far *fpSFT = (SFT __far *)MK_FP(r->w.es, r->w.di);
DPRINTF("WriteFile(), r->w.cx = %d", r->w.cx);
if (!(fpSFT->openMode & (O_WRONLY | O_RDWR)))
{
Failure(DOS_ACCESS);
return;
}
// mark file as modified and set date not valid any more
fpSFT->flags &= ~(SFT_FCLEAN | SFT_FDATE);
// Tricky: If size is 0, truncate to current file position
//
if (!r->w.cx)
{
newAttr.fsize = (uint64_t)fpSFT->filePos;
newAttr.mask = VMSHF_VALID_ATTR_FSIZE;
ret = VMShfSetAttr(&newAttr, NULL, 0, fpSFT->handle, &status);
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS)
{
Failure(DOS_ACCESS);
return;
}
fpSFT->fileSize = fpSFT->filePos;
return;
}
buffer = fpSDA->fpCurrentDTA;
towrite = (uint32_t)r->w.cx;
do
{
length = towrite;
ret = VMShfWriteFile(fpSFT->handle, 0, (uint64_t)fpSFT->filePos, &length, buffer, &status);
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS)
{
Failure(VmshfStatusToDosError(status));
break;
}
else
{
if (!length) // Can't write more data
{
break;
}
buffer += length;
fpSFT->filePos += length;
towrite -= length;
}
} while (towrite);
if (fpSFT->filePos > fpSFT->fileSize)
{
fpSFT->fileSize = fpSFT->filePos;
}
r->w.cx -= (uint16_t)towrite;
return;
}
static void LockFile(void)
{
DPUTS("LockFile()");
return;
}
static void UnlockFile(void)
{
DPUTS("UnlockFile()");
return;
}
static void GetDiskSpace(void)
{
char __far *path;
uint32_t status;
uint64_t avail;
uint64_t total;
uint32_t sectSize = SECTOR_SIZE;
DPUTS("GetDiskSpace()");
if (MK_FP(r->w.es, r->w.di) == fpSDA->currentCDS)
{
// Called on current drive
path = lfn ? LfnGetTrueLongName(fpLongFileName2, fpCurrentPath) : fpCurrentPath;
}
else
{
path = (char __far *)NULL;
}
if (VMShfGetDirSize(path, lfn, &status, &avail, &total))
{
Failure(r->w.ax);
}
else
{
(void)u64div32(&avail, avail, sectSize);
(void)u64div32(&total, total, sectSize);
r->w.ax = 1; // Sectors per cluster
r->w.bx = (total > 0xffffui64) ? 0xffff : (uint16_t)(total); // Total sectors
r->w.cx = sectSize; // Sector size
r->w.dx = (avail > 0xffffui64) ? 0xffff : (uint16_t)(avail); // Sectors free
}
return;
}
static void _GetFileAttrib(void)
{
int ret;
uint32_t status;
VMShfAttr *fAttr;
ret = VMShfGetAttr(lfn ? LfnGetTrueLongName(fpLongFileName1, fpFileName1) : fpFileName1, lfn, VMSHF_INVALID_HANDLE,
&status, &fAttr);
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS)
{
Failure(VmshfStatusToDosError(status));
}
else
{
r->w.ax = (uint16_t)FModeToFatAttr(fAttr);
}
return;
}
static void GetFileAttrib(void)
{
DPUTS("GetFileAttrib()");
_GetFileAttrib();
}
static void SetFileAttrib(void)
{
int ret;
uint32_t status;
DPUTS("SetFileAttrib()");
_GetFileAttrib();
if (r->w.flags & INTR_CF)
{
return;
}
// Check if file is not a directory and we are trying to set the _A_SUBDIR attribute
//
if ((!(r->w.ax & _A_SUBDIR)) && (r->w.cx & _A_SUBDIR))
{
Failure(DOS_ACCESS);
return;
}
ret = VMShfSetAttr(FatAttrToFMode((uint8_t)*fpStackParam),
lfn ? LfnGetTrueLongName(fpLongFileName1, fpFileName1) : fpFileName1, lfn, VMSHF_INVALID_HANDLE,
&status);
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS)
{
Failure(VmshfStatusToDosError(status));
}
return;
}
static void OpenOrCreateFile(uint16_t accessMode, uint32_t action, VMShfAttr *openAttr)
{
int ret;
uint32_t status;
uint32_t handle;
VMShfAttr *fAttr;
uint8_t fatAttr;
SFT __far *fpSFT = (SFT __far *)MK_FP(r->w.es, r->w.di);
ret = VMShfOpenFile((uint32_t)accessMode, action, openAttr->fmode, openAttr->attr,
lfn ? LfnGetTrueLongName(fpLongFileName1, fpFileName1) : fpFileName1, lfn, &status, &handle);
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS)
{
Failure(VmshfStatusToDosError(status));
}
else
{
fpSFT->flags &= ~SFT_FDATE;
// use FCLEAN even on replaced/created files: the bit is reset
// if the file is written to later
fpSFT->flags |= SFT_FCLEAN;
// Get file attributes using file handle
//
ret = VMShfGetAttr(NULL, 0, handle, &status, &fAttr);
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS)
{
Failure(VmshfStatusToDosError(status));
(void)VMShfCloseFile(handle, &status);
}
else
{
fatAttr = FModeToFatAttr(fAttr);
if (fatAttr & (_A_SUBDIR | _A_VOLID))
{
Failure(DOS_ACCESS);
(void)VMShfCloseFile(handle, &status);
}
else
{
fpSFT->openMode = accessMode;
fpSFT->fileAttr = fatAttr;
fpSFT->flags = (NETWORK | UNWRITTEN | driveNum);
fpSFT->devDrvrPtr = (char __far *)NULL;
fpSFT->fileTime = FTimeToFatTime(fAttr->utime);
fpSFT->fileSize = (fAttr->fsize > 0xffffffffui64) ? 0xffffffff : (uint32_t)fAttr->fsize;
fpSFT->filePos = 0;
fpSFT->handle = handle;
_fmemcpy_local(&fpSFT->file_name, fpFcbName1, 11);
if (fpSFT->openMode & 0x8000)
{
/* File opened via FCB */
fpSFT->openMode |= 0x00F0;
SetSftOwner();
}
else
{
fpSFT->openMode &= 0x000F;
}
} // ! fatAttr & _A_SUBDIR
} // VMShfGetAttr() OK
} // VMShfOpenFile() OK
return;
}
static void OpenFile(void)
{
DPUTS("OpenFile()");
OpenOrCreateFile(fpSDA->openMode & 3, VMSHF_ACTION_O_EXIST,
FatAttrToFMode(_A_NORMAL | _A_ARCH | _A_RDONLY | _A_HIDDEN | _A_SYSTEM));
return;
}
static void CreateFile(void)
{
DPUTS("CreateFile()");
OpenOrCreateFile(VMSHF_ACCESS_READWRITE, VMSHF_ACTION_C_ALWAYS, FatAttrToFMode((uint8_t)*fpStackParam));
return;
}
inline int MatchAttrMask(uint8_t fatAttr, uint8_t mask)
{
// _A_RDONLY and _A_ARCH should always match
//
return !( ((fatAttr & _A_HIDDEN) && (!(mask & _A_HIDDEN))) || ((fatAttr & _A_SYSTEM) && (!(mask & _A_SYSTEM))) ||
((fatAttr & _A_VOLID) && (!(mask & _A_VOLID))) || ((fatAttr & _A_SUBDIR) && (!(mask & _A_SUBDIR))));
}
inline int MatchNameMask(char *fname, char __far *mask)
{
int i;
for (i = 0; i < 11; ++i)
{
if ((fname[i] != mask[i]) && (mask[i] != '?'))
{
return 0;
}
}
return 1;
}
inline int FcbNameHasWildCards(char __far *fcbName)
{
int i;
for (i = 0; i < 11; ++i)
{
if (fcbName[i] == '?')
{
return 1;
}
}
return 0;
}
static void _FindNext(void)
{
int ret;
uint32_t status = VMSHF_SUCCESS;
VMShfAttr *fAttr;
uint8_t fatAttr;
uint32_t fNameLen;
char *fName;
int i = fpSDB->dirEntryNum;
if (fpSDB->dirHandle == VMSHF_INVALID_HANDLE)
{
Failure(DOS_NFILES);
return;
}
for (;;)
{
ret = VMShfReadDir(fpSDB->dirHandle, ++i, &status, &fAttr, &fName, &fNameLen);
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS || 0 == fName)
{
Failure((0 == fName) ? DOS_NFILES : VmshfStatusToDosError(status));
(void)VMShfCloseDir(fpSDB->dirHandle, &status);
fpSDB->dirHandle = VMSHF_INVALID_HANDLE;
break;
}
fatAttr = FModeToFatAttr(fAttr);
if (FNameToFcbName(fcbName, fName, (uint16_t)fNameLen, fpSDB->isRoot, lfn) &&
MatchNameMask(fcbName, fpSDB->searchMask) && MatchAttrMask(fatAttr, fpSDB->attrMask))
{
_fmemcpy_local(fpFDB->fileName, MK_FP(myDS, fcbName), 11);
fpFDB->fileAttr = fatAttr;
fpFDB->fileTime = FTimeToFatTime(fAttr->utime);
fpFDB->fileSize = (fAttr->fsize > 0xffffffffui64) ? 0xffffffff : (uint32_t)fAttr->fsize;
fpFDB->unused = 0;
fpSDB->dirEntryNum = i;
if (!FcbNameHasWildCards(fpSDB->searchMask))
{
// If attrMask has no wildcards, then this will be the last match
// close directory search
(void)VMShfCloseDir(fpSDB->dirHandle, &status);
fpSDB->dirHandle = VMSHF_INVALID_HANDLE;
}
break;
}
}
return;
}
static void FindNext(void)
{
DPUTS("FindNext()");
_FindNext();
}
static void _FindFirst(void)
{
uint32_t status = VMSHF_SUCCESS;
char __far *p;
VMShfAttr *fAttr;
uint8_t fatAttr;
uint32_t handle;
int ret;
// Special case: Get Volume ID
//
if (fpSDA->attrMask == _A_VOLID)
{
fpSDB->driveNumber = driveNum | 0x80;
fpSDB->dirEntryNum = 0;
_fmemcpy_local(fpSDB->searchMask, fpFcbName1, 11);
fpFDB->fileAttr = _A_VOLID;
fpFDB->fileTime = GetDosTime();
fpFDB->fileSize = 0;
_fmemcpy_local(fpFDB->fileName, MK_FP(myDS, volLabel), 11);
return;
}
// Open dir to perform search
//
*(p = _fstrrchr_local(fpFileName1, '\\')) = 0; // Get the directory part
fpSDB->isRoot = (*fpFileName1 == '\0') ? 1 : 0;
ret = VMShfOpenDir(lfn ? LfnGetTrueLongName(fpLongFileName1, fpFileName1) : fpFileName1, lfn, &status, &handle);
*p = '\\';
if (ret != VMTOOL_SUCCESS)
{
Failure(DOS_NOTREADY);
}
else if (status != VMSHF_SUCCESS)
{
Failure(VmshfStatusToDosError(status));
if (r->w.ax == DOS_FILENOTFND)
{
r->w.ax = DOS_PATHNOTFND;
}
}
else
{
// Initialise FindFirst/FindNext data block
//
_fmemcpy_local(fpSDB->searchMask, fpFcbName1, 11);
fpSDB->driveNumber = driveNum | 0xC0;
fpSDB->attrMask = fpSDA->attrMask;
fpSDB->dirEntryNum = -1;
fpSDB->dirHandle = handle;
_FindNext();
if (DOS_NFILES == r->w.ax)
{
r->w.ax = DOS_FILENOTFND;
}
}
return;
}
static void FindFirst(void)
{
DPUTS("FindFirst()");
_FindFirst();
}
// Index to SFT functions. Copied from FreeDOS kernel, lazy me
//
static inline int idx_to_sft_(int sftIndex, SFT __far **fpCurSFT)
{
SFTT __far *sp;
*fpCurSFT = (SFT __far *)-1;
if (sftIndex < 0)
{
return -1;
}
/* Get the SFT block that contains the SFT */
for (sp = fpFileTable; sp != (SFTT __far *)-1; sp = sp->nextSFTT)
{
if (sftIndex < sp->sfttCount)
{
/* finally, point to the right entry */
*fpCurSFT = (SFT __far *)&(sp->entries[sftIndex]);
return sftIndex;
}
sftIndex -= sp->sfttCount;
}
/* If not found, return an error */
return -1;
}
SFT __far *idx_to_sft(int sftIndex)
{
SFT __far *fpCurSFT;
/* called internally only */
sftIndex = idx_to_sft_(sftIndex, &fpCurSFT);
/* if not opened, the SFT is useless */
if (sftIndex == -1 || fpCurSFT->handleCount == 0)
{
return (SFT __far *)-1;
}
return fpCurSFT;
}
static void CloseAll(void)
{
PSP __far *fpPSP = (PSP __far *)MK_FP(fpSDA->currentPSP, 0);
int i;
DPUTS("CloseAll()");
if (fpPSP == NULL)
{
return;
}
for (i = 5; i < fpPSP->jftSize; ++i)
{
uint8_t idx = fpPSP->fpExtendedJFT[i];
if (idx != 0xFF)
{
SFT __far *fpSFT = idx_to_sft(idx);
if (fpSFT != (SFT __far *)-1)
{
_CloseFile(fpSFT);
}
}
}
}
static void ProcessEnd(void)
{
DPUTS("ProcessEnd()");
}
static void MakeFullPath(char __far *fullPath, char __far *dirName, char __far *fcbName)
{
int i;
while (*dirName)
{
*(fullPath++) = *(dirName++);
}
*(fullPath++) = '\\';
for (i = 0; i < 11; ++i)
{
if (fcbName[i] != ' ')
{
if (i == 8)
{
*(fullPath++) = '.';
}
*(fullPath++) = fcbName[i];
}
}
*fullPath = '\0';