-
Notifications
You must be signed in to change notification settings - Fork 0
/
p80.cpp
1545 lines (1163 loc) · 41 KB
/
p80.cpp
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
/**
@file p80.cpp
PUNY-80
TRS-80 disk image tool for Linux and other *nix operating systems
Copyright (c) 2024 Stefan Vogt and Shawn Sijnstra
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 3 of the License, or
(at your option) any later version.
See file LICENSE for details.
Based on TRS-80 Virtual Disk Kit by Miguel Dutra and Mike Gore.
*/
#define DEFINE_ERRORS_MSG
#include "windows.h"
#include <typeinfo>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include "p80.h"
#include "vdi.h"
#include "jv1.h"
#include "jv3.h"
#include "dmk.h"
#include "osi.h"
#include "td4.h"
#include "td3.h"
#include "td1.h"
#include "rd.h"
#include "md.h"
#include "nd.h"
#include "dd.h"
#include "cpm.h"
#define MAX_FILE_SIZE (40L*2L*18L*256L)
//---------------------------------------------------------------------------------
// Function Definitions
//---------------------------------------------------------------------------------
// Core operations
DWORD Dir();
DWORD Get();
DWORD Put();
DWORD Ren();
DWORD Del();
DWORD DumpDisk();
DWORD DumpFile();
// Auxiliary functions
DWORD LoadVDI();
DWORD LoadOSI();
void Dump(unsigned char* pBuffer, int nSize);
bool WildComp(const char* pSource, const char* pMask, BYTE nLength);
void WildCopy(const char* pSource, char* pTarget, const char* pMask, BYTE nLength);
void FmtName(const char szName[9], const char szType[4], const char* szDivider, char szNewName[13]);
void Win2TRS(const char* pWinName, char cTRSName[11]);
void Trim(char cField[8]);
// Command-line related
DWORD ParseCmdLine(int argc, char* argv[]);
DWORD SetCmd(void* pParam);
DWORD SetOpt(void* pParam);
DWORD SetVDI(void* pParam);
DWORD SetOSI(void* pParam);
void PrintHelp();
void PrintError(DWORD dwError);
//---------------------------------------------------------------------------------
// Data structures
//---------------------------------------------------------------------------------
char* gpFileSpec[4] = {};
DWORD (*gpCommand)() = NULL;
FILE *ghFile = NULL;
CVDI* gpVDI = NULL;
COSI* gpOSI = NULL;
DWORD gdwFlags = 0;
struct SWITCH
{
const char* cName;
DWORD (*pCall)(void*);
void* pParam;
const char* cDescr;
};
SWITCH gSwitches[] =
{
{ "-l", SetCmd, (void*)Dir, "List directory (default)" },
{ "-r", SetCmd, (void*)Get, "Read files" },
{ "-w", SetCmd, (void*)Put, "Write files" },
{ "-n", SetCmd, (void*)Ren, "Rename files" },
{ "-k", SetCmd, (void*)Del, "Delete files" },
{ "-f", SetCmd, (void*)DumpFile, "Dump file contents" },
{ "-d", SetCmd, (void*)DumpDisk, "Dump disk contents" },
{ "-s", SetOpt, (void*)V80_FLAG_SYSTEM, "Include system files" },
{ "-i", SetOpt, (void*)V80_FLAG_INVISIBLE, "Include invisible files" },
{ "-x", SetOpt, (void*)V80_FLAG_INFO, "Show extra information" },
{ "-p", SetOpt, (void*)V80_FLAG_CHKDSK, "Skip the disk parameters check" },
{ "-c", SetOpt, (void*)V80_FLAG_CHKDIR, "Skip the directory structure check" },
{ "-g", SetOpt, (void*)V80_FLAG_GATFIX, "Skip the GAT auto-fix in TRSDOS system disks" },
{ "-b", SetOpt, (void*)V80_FLAG_READBAD, "Read as much as possible from bad files" },
{ "-ss", SetOpt, (void*)V80_FLAG_SS, "Force the disk as single-sided" },
{ "-ds", SetOpt, (void*)V80_FLAG_DS, "Force the disk as double-sided" },
{ "-dmk", SetVDI, (void*)new CDMK, "Force the DMK disk interface" },
{ "-jv1", SetVDI, (void*)new CJV1, "Force the JV1 disk interface" },
{ "-jv3", SetVDI, (void*)new CJV3, "Force the JV3 disk interface" },
{ "-cpm", SetOSI, (void*)new CCPM, "Force the CP/M system interface (INCOMPLETE)" },
{ "-dd", SetOSI, (void*)new CDD, "Force the DoubleDOS system interface" },
{ "-md", SetOSI, (void*)new CMD, "Force the MicroDOS/OS-80 III system interface" },
{ "-nd", SetOSI, (void*)new CND, "Force the NewDOS/80 system interface" },
{ "-rd", SetOSI, (void*)new CRD, "Force the RapiDOS system interface" },
{ "-td1", SetOSI, (void*)new CTD1, "Force the TRSDOS Model I system interface" },
{ "-td3", SetOSI, (void*)new CTD3, "Force the TRSDOS Model III system interface" },
{ "-td4", SetOSI, (void*)new CTD4, "Force the TRSDOS Model 4 system interface" }
};
const char* gCategories[4] = { "Commands", "Options", "Disk Interfaces", "DOS Interfaces" };
//---------------------------------------------------------------------------------
// Main
//---------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
DWORD dwError;
// Print authoring information
puts("PUNY-80 | The TRS-80 Disk Image Tool [v1.0]");
puts("Copyright (c) 2024 Stefan Vogt and Shawn Sijnstra");
// Print usage instructions when no parameters are provided
if (argc == 1)
{
PrintHelp();
goto Exit_1;
}
// Process command line parameters
if ((dwError = ParseCmdLine(argc, argv)) != 0)
{
printf("ParseCmdLine: error\n");
goto Exit_1;
}
// Check whether user has provided a disk image filename
if (gpFileSpec[1] == NULL)
{
puts("Missing the disk image filename.");
goto Exit_1;
}
// Open disk image
if ( (ghFile = fopen(gpFileSpec[1], "r+")) == NULL )
{
printf("Can not open: %s\n", gpFileSpec[1]);
goto Exit_2;
}
// Set a command to execute if the user hasn`t indicated one (first one in the array)
if (gpCommand == NULL)
gpCommand = (DWORD (*)())gSwitches[0].pParam;
// Execute the command with parameters previously parsed from the command-line
dwError = gpCommand();
// Close the file
Exit_3:
if (ghFile != NULL)
fclose(ghFile);
// Print error message, if any
Exit_2:
// Exit
Exit_1:
return 0;
}
//---------------------------------------------------------------------------------
// List disk files
//---------------------------------------------------------------------------------
DWORD Dir()
{
OSI_FILE File;
char cMask[11];
char szFile[13];
void* pFile = NULL;
WORD wFiles = 0;
DWORD dwSize = 0;
DWORD dwError = 0;
DIR *dir;
dirent *ent;
struct stat st;
// Initialize the disk interface
if ((dwError = LoadVDI()) != 0)
goto Exit_0;
// Initialize the DOS interface
if ((dwError = LoadOSI()) != 0)
goto Exit_1;
// Print operation objective
printf("\r\nListing directory contents:\r\n\r\n");
// Print header
printf("Filename\t Size\tDate\t\tAttr\r\n");
printf("----------------------------------------------------\r\n");
// Convert Windows filespec to TRS standard
Win2TRS((gpFileSpec[2] != NULL ? gpFileSpec[2] : "*.*"), cMask);
// While OSI::Dir() returns a valid file pointer
while ((dwError = gpOSI->Dir(&pFile, (pFile == NULL ? OSI_DIR_FIND_FIRST : OSI_DIR_FIND_NEXT))) == 0)
{
// Get file properties
gpOSI->GetFile(pFile, File);
// Compare file attributes against user requests
if ((File.bSystem && !(gdwFlags & V80_FLAG_SYSTEM)) || (File.bInvisible && !(gdwFlags & V80_FLAG_INVISIBLE)))
continue;
// Compare the filename against the source filespec
if (!WildComp(File.szName, cMask, 8) || !WildComp(File.szType, &cMask[8], 3))
continue;
// Format the filename
FmtName(File.szName, File.szType, (strcmp(typeid(*gpOSI).name()+2, "CPM") ? "/" : "."), szFile);
// Print file information
printf("%-12s\t%8u\t%04d/%02d/%02d\t%c%c%c%d\r\n", szFile, File.dwSize, File.Date.wYear, File.Date.nMonth, File.Date.nDay, (File.bSystem?'S':'-'), (File.bInvisible?'I':'-'), (File.bModified?'M':'-'), File.nAccess);
// Update operation status variables
wFiles++;
dwSize += File.dwSize;
}
// Print operation summary
printf("\r\nTotal of %d bytes in %d files listed.\r\n\r\n", dwSize, wFiles);
// If exited on "No More Files" then "No Error"
if (dwError == ERROR_NO_MORE_FILES)
dwError = 0;
// Release the OSI object
if (gpOSI != NULL)
delete gpOSI;
// Release the VDI object
Exit_1:
if (gpVDI != NULL)
delete gpVDI;
Exit_0:
return dwError;
}
//---------------------------------------------------------------------------------
// Extract files from the disk
//---------------------------------------------------------------------------------
DWORD Get()
{
OSI_FILE File;
FILE *hFile;
char cMask[11];
char szTRSFile[13];
char szWinFile[13];
char szFile[MAX_PATH];
void* pFile = NULL;
WORD wFiles = 0;
DWORD dwSize = 0;
BYTE* pBuffer = NULL;
DWORD dwBytes;
DWORD dwError = 0;
// Initialize the disk interface
dwError = LoadVDI();
if (dwError)
goto Exit_0;
// Initialize the DOS interface
dwError = LoadOSI();
if (dwError)
goto Exit_1;
// 360KB should be more than enough for any TRS-80 file
dwBytes = (MAX_FILE_SIZE) + (V80_MEM - (MAX_FILE_SIZE) % V80_MEM);
// Allocate memory
if ((pBuffer = (BYTE*)calloc(dwBytes,1)) == NULL)
{
perror("Get Memory");
dwError = ERROR_OUTOFMEMORY;
goto Exit_2;
}
// Print operation objective
printf("\r\nReading files from disk:\r\n\r\n");
// Convert Windows filespec to TRS standard
Win2TRS((gpFileSpec[2] != NULL ? gpFileSpec[2] : "*.*"), cMask);
// While OSI::Dir() returns a valid file pointer
while ((dwError = gpOSI->Dir(&pFile, (pFile == NULL ? OSI_DIR_FIND_FIRST : OSI_DIR_FIND_NEXT))) == 0)
{
// Get file properties
gpOSI->GetFile(pFile, File);
// Compare file attributes against user requests
if ((File.bSystem && !(gdwFlags & V80_FLAG_SYSTEM)) || (File.bInvisible && !(gdwFlags & V80_FLAG_INVISIBLE)))
continue;
// Compare the filename against the source filespec
if (!WildComp(File.szName, cMask, 8) || !WildComp(File.szType, &cMask[8], 3))
continue;
// Format the filenames
FmtName(File.szName, File.szType, (strcmp(typeid(*gpOSI).name()+2, "CPM") ? "/" : "."), szTRSFile);
FmtName(File.szName, File.szType, ".", szWinFile);
// Add the user specified path (if any) to the Windows-based filename
sprintf(szFile, "%s/%s", (gpFileSpec[3] ? gpFileSpec[3] : "."), szWinFile);
// Print filenames
printf("%-12s -> %-12s\t", szTRSFile, szFile);
// Check if the file is not empty
if (File.dwSize == 0)
{
printf("%8d bytes\tSkipped\r\n", File.dwSize);
continue;
}
// Check whether the file size is valid (<360KB)
if (File.dwSize > MAX_FILE_SIZE)
{
printf("%8d bytes\tInvalid Size!\r\n", File.dwSize);
continue;
}
// Set file pointer
if ((dwError = gpOSI->Seek(pFile, 0)) != 0)
{
printf("Get seek error\n");
continue;
}
// Read file contents
if ((dwError = gpOSI->Read(pFile, pBuffer, File.dwSize)) != 0 && !(gdwFlags & V80_FLAG_READBAD))
{
printf("Get read error\n");
continue;
}
// Create Windows file
if ((hFile = fopen(szFile, "w")) == NULL)
{
printf("Can't open: %s\n", szFile);
continue;
}
dwBytes = fwrite(pBuffer, 1, File.dwSize, hFile);
// Write buffer contents to Windows file
if (dwBytes != File.dwSize)
{
printf("Write error: %s\n", szFile);
fclose(hFile);
continue;
}
// Close file handle
fclose(hFile);
// Print total number of bytes extracted
printf("%8d bytes\tOK\r\n", File.dwSize);
// Update operation status variables
wFiles++;
dwSize += File.dwSize;
}
// Print operation summary
printf("\r\nTotal of %d bytes read from %d files.\r\n\r\n", dwSize, wFiles);
// If exited on "No More Files" then "No Error"
if (dwError == ERROR_NO_MORE_FILES)
dwError = 0;
// Release allocated memory
if (pBuffer != NULL)
free(pBuffer);
// Release the OSI object
Exit_2:
if (gpOSI != NULL)
delete gpOSI;
// Release the VDI object
Exit_1:
if (gpVDI != NULL)
delete gpVDI;
if(dwError)
printf("Get dwError:%d\n", dwError);
// Return
Exit_0:
return dwError;
}
//---------------------------------------------------------------------------------
// Write files to the disk
//---------------------------------------------------------------------------------
DWORD Put()
{
HANDLE hFile;
char szFileSpec[MAX_PATH];
char* pFileName;
char cFile[11];
char szFile[13];
OSI_FILE File;
void* pFile;
WORD wFiles = 0;
DWORD dwSize = 0;
BYTE* pBuffer = NULL;
DWORD dwBytes;
DWORD dwError = 0;
DIR *dir;
class dirent *ent;
class stat st;
// Check whether the user informed a FROM filespec
if (gpFileSpec[2] == NULL)
{
dwError = ERROR_BAD_ARGUMENTS;
goto Exit_0;
}
// Initialize the disk interface
if ((dwError = LoadVDI()) != 0)
goto Exit_0;
// Initialize the DOS interface
if ((dwError = LoadOSI()) != 0)
goto Exit_1;
// 360KB should be more than enough for any TRS-80 file
dwBytes = (MAX_FILE_SIZE) + (V80_MEM - (MAX_FILE_SIZE) % V80_MEM);
// Allocate memory
if ((pBuffer = (BYTE*)calloc(1,dwBytes)) == NULL)
{
perror("Put");
dwError = ERROR_OUTOFMEMORY;
goto Exit_2;
}
// Print operation objective
printf("\r\nWriting files to disk:\r\n\r\n");
// Get the target file path
if( realpath(gpFileSpec[2], szFileSpec) == NULL )
{
perror("Put");
goto Exit_3;
}
pFileName = basename(szFileSpec);
dir = opendir(gpFileSpec[2]);
// Get the first file in the list
if (dir == NULL)
{
printf ("Cannot open directory '%s' because I am an idiot and want to open a dir while I want to grab a file.\n", gpFileSpec[2]);
printf ("This is pFileName: %s\n", basename(szFileSpec));
printf ("This is the real path: %s\n\n", realpath(gpFileSpec[2], szFileSpec));
goto Exit_3;
}
// While there are files pending for writing
while ((ent = readdir(dir)) != NULL) {
struct tm *t;
const char *file_name = ent->d_name;
if (file_name[0] == '.')
continue;
if (stat(file_name, &st) == -1)
continue;
if(st.st_mode & S_IFDIR)
continue;
t = gmtime((const time_t *)&st.st_mtime);
// Convert Windows filename to the TRS standard
Win2TRS(file_name, cFile);
// Set target filename
memcpy(File.szName, cFile, 8);
memcpy(File.szType, &cFile[8], 3);
// Set target file size
File.dwSize = st.st_size;
// Set target file date
File.Date.nDay = t->tm_wday;
File.Date.nMonth = t->tm_mon + 1;
File.Date.wYear = t->tm_year + 1900;
// Set target file attributes
File.bSystem = false;
File.bInvisible = false;
File.bModified = true;
File.nAccess = OSI_PROT_FULL;
// Format the filename for printing purposes
FmtName(File.szName, File.szType, (strcmp(typeid(*gpOSI).name()+2, "CPM") ? "/" : "."), szFile);
// Print the filenames
printf("%-12s -> %-12s\t", szFileSpec, szFile);
// Check whether the file size is valid
if (File.dwSize > MAX_FILE_SIZE )
{
puts("Invalid size!");
continue;
}
// Open the Windows file
if ( (hFile = fopen(szFileSpec, "r")) == NULL)
{
printf("Can't open: %s\n", szFileSpec);
continue;
}
dwBytes = fread(pBuffer, 1, File.dwSize, hFile);
// Read file contents to the buffer
if (dwBytes != File.dwSize)
{
fprintf(stderr,"Read error: %s\n", szFile);
fclose(hFile);
continue;
}
// Close file handle
fclose(hFile);
// Create a TRS file with the properties defined above
if ((dwError = gpOSI->Create(&pFile, File)) != 0)
goto Exit_4;
// Move the file pointer to the beginning
if ((dwError = gpOSI->Seek(pFile, 0)) != 0)
{
gpOSI->Delete(pFile);
goto Exit_4;
}
// Write the buffer contents to the new file
if ((dwError = gpOSI->Write(pFile, pBuffer, File.dwSize)) != 0)
{
gpOSI->Delete(pFile);
goto Exit_4;
}
// Print the total number of bytes written
printf("%8d bytes OK\r\n", File.dwSize);
// Update operation status variables
wFiles++;
dwSize += File.dwSize;
}
// Print operation summary
printf("\r\nTotal of %d bytes written in %d files.\r\n\r\n", dwSize, wFiles);
// Close find file handle
Exit_4:
if (dir != NULL)
closedir(dir);
// Release the allocated memory
Exit_3:
if (pBuffer != NULL)
free(pBuffer);
// Release the OSI object
Exit_2:
if (gpOSI != NULL)
delete gpOSI;
// Release the VDI object
Exit_1:
if (gpVDI != NULL)
delete gpVDI;
// Return
Exit_0:
return dwError;
}
//---------------------------------------------------------------------------------
// Rename files
//---------------------------------------------------------------------------------
DWORD Ren()
{
OSI_FILE File;
char cName[9];
char cType[4];
char cSource[11];
char cTarget[11];
char szFromFile[13];
char szToFile[13];
void* pFile = NULL;
WORD wFiles = 0;
DWORD dwError = 0;
// Clear variables cName and cType
memset(cName, 0, sizeof(cName));
memset(cType, 0, sizeof(cType));
// Check whether the user informed both the FROM and TO filespecs
if (gpFileSpec[2] == NULL || gpFileSpec[3] == NULL)
{
dwError = ERROR_BAD_ARGUMENTS;
goto Exit_0;
}
// Initialize the disk interface
if ((dwError = LoadVDI()) != 0)
goto Exit_0;
// Initialize the DOS interface
if ((dwError = LoadOSI()) != 0)
goto Exit_1;
// Print operation objective
printf("\r\nRenaming files:\r\n\r\n");
// Convert Windows filespecs to TRS standards
Win2TRS(gpFileSpec[2], cSource);
Win2TRS(gpFileSpec[3], cTarget);
// While OSI::Dir() returns a valid file pointer
while ((dwError = gpOSI->Dir(&pFile, (pFile == NULL ? OSI_DIR_FIND_FIRST : OSI_DIR_FIND_NEXT))) == 0)
{
// Get file properties
gpOSI->GetFile(pFile, File);
// Compare file attributes against user options
if ((File.bSystem && !(gdwFlags & V80_FLAG_SYSTEM)) || (File.bInvisible && !(gdwFlags & V80_FLAG_INVISIBLE)))
continue;
// Compare the filename against the source filespec
if (!WildComp(File.szName, cSource, 8) || !WildComp(File.szType, &cSource[8], 3))
continue;
// Copy the filename to the internal variable checking it against the target filespec
WildCopy(File.szName, cName, cTarget, 8);
WildCopy(File.szType, cType, &cTarget[8], 3);
// Format "FROM" filename
FmtName(File.szName, File.szType, (strcmp(typeid(*gpOSI).name()+2, "CPM") ? "/" : "."), szFromFile);
// Replace current filename with the new one
memcpy(File.szName, cName, sizeof(cName));
memcpy(File.szType, cType, sizeof(cType));
// Format "TO" filename
FmtName(cName, cType, (strcmp(typeid(*gpOSI).name()+2, "CPM") ? "/" : "."), szToFile);
// Print filenames
printf("%-12s -> %-12s\t", szFromFile, szToFile);
// Update file properties
if ((dwError = gpOSI->SetFile(pFile, File)) != 0)
goto Exit_2;
// Print OK if the file has been successfully renamed
printf("OK\r\n");
// Update operation status variable
wFiles++;
}
// Print operation summary
printf("\r\nTotal of %d files renamed.\r\n\r\n", wFiles);
// If exited on "No More Files" then "No Error"
if (dwError == ERROR_NO_MORE_FILES)
dwError = 0;
// Release the OSI object
Exit_2:
if (gpOSI != NULL)
delete gpOSI;
// Release the VDI object
Exit_1:
if (gpVDI != NULL)
delete gpVDI;
// Return
Exit_0:
return dwError;
}
//---------------------------------------------------------------------------------
// Delete files
//---------------------------------------------------------------------------------
DWORD Del()
{
OSI_FILE File;
char cMask[11];
char szFile[13];
void* pFile = NULL;
WORD wFiles = 0;
DWORD dwError = 0;
// Check whether the user informed a filespec
if (gpFileSpec[2] == NULL)
{
dwError = ERROR_BAD_ARGUMENTS;
goto Exit_0;
}
// Initialize the disk interface
if ((dwError = LoadVDI()) != 0)
goto Exit_0;
// Initialize the DOS interface
if ((dwError = LoadOSI()) != 0)
goto Exit_1;
// Print operation objective
printf("\r\nDeleting files:\r\n\r\n");
// Convert Windows filespec to TRS standard
Win2TRS(gpFileSpec[2], cMask);
// While OSI::Dir() returns a valid file pointer
while ((dwError = gpOSI->Dir(&pFile, (pFile == NULL ? OSI_DIR_FIND_FIRST : OSI_DIR_FIND_NEXT))) == 0)
{
// Get file properties
gpOSI->GetFile(pFile, File);
// Compare file attributes against user options
if ((File.bSystem && !(gdwFlags & V80_FLAG_SYSTEM)) || (File.bInvisible && !(gdwFlags & V80_FLAG_INVISIBLE)))
continue;
// Compare the filename against the source filespec
if (!WildComp(File.szName, cMask, 8) || !WildComp(File.szType, &cMask[8], 3))
continue;
// Format filename
FmtName(File.szName, File.szType, (strcmp(typeid(*gpOSI).name()+2, "CPM") ? "/" : "."), szFile);
// Print filename
printf("%-12s\t", szFile);
// Delete the file
dwError = gpOSI->Delete(pFile);
// Print error message
if (dwError != 0)
{
printf("Delete dwError:%d\n", dwError);
continue;
}
// Print OK if the file has been successfully deleted
printf("OK\r\n");
// Update operation status variable
wFiles++;
}
// Print operation summary
printf("\r\nTotal of %d files deleted.\r\n\r\n", wFiles);
// If exited on "No More Files" then "No Error"
if (dwError == ERROR_NO_MORE_FILES)
dwError = 0;
// Release the OSI object
if (gpOSI != NULL)
delete gpOSI;
// Release the VDI object
Exit_1:
if (gpVDI != NULL)
delete gpVDI;
if (dwError)
printf("Delete dwError:%d\n", dwError);
// Return
Exit_0:
return dwError;
}
//---------------------------------------------------------------------------------
// Dump file contents
//---------------------------------------------------------------------------------
DWORD DumpFile()
{
OSI_FILE File;
char cMask[11];
char szFile[13];
void* pFile = NULL;
BYTE* pBuffer = NULL;
DWORD dwBytes;
DWORD dwError = 0;
// Check whether the user informed a filespec
if (gpFileSpec[2] == NULL)
{
dwError = ERROR_BAD_ARGUMENTS;
goto Exit_0;
}
// Initialize the disk interface
if ((dwError = LoadVDI()) != 0)
goto Exit_0;
// Initialize the DOS interface
if ((dwError = LoadOSI()) != 0)
goto Exit_1;
// 360KB should be more than enough for any TRS-80 file
dwBytes = (MAX_FILE_SIZE) + (V80_MEM - (MAX_FILE_SIZE) % V80_MEM);
// Allocate memory
if ((pBuffer = (BYTE*)calloc(dwBytes,1)) == NULL)
{
perror("Dump File");
dwError = ERROR_OUTOFMEMORY;
goto Exit_2;
}
// Convert Windows filespec to TRS standard
Win2TRS(gpFileSpec[2], cMask);
// While OSI::Dir() returns a valid file pointer
while ((dwError = gpOSI->Dir(&pFile, (pFile == NULL ? OSI_DIR_FIND_FIRST : OSI_DIR_FIND_NEXT))) == 0)
{
// Get file properties
gpOSI->GetFile(pFile, File);
// Compare file attributes against user options
if ((File.bSystem && !(gdwFlags & V80_FLAG_SYSTEM)) || (File.bInvisible && !(gdwFlags & V80_FLAG_INVISIBLE)))
continue;
// Compare the filename against the source filespec
if (!WildComp(File.szName, cMask, 8) || !WildComp(File.szType, &cMask[8], 3))
continue;
// Format filename
FmtName(File.szName, File.szType, (strcmp(typeid(*gpOSI).name()+2, "CPM") ? "/" : "."), szFile);
// Print operation objective
printf("\r\nDumping contents of %s:\r\n\r\n", szFile);
// Set the file pointer
if ((dwError = gpOSI->Seek(pFile, 0)) != 0)
{
printf("Dump File Seek: dwError:%d\n", dwError);
continue;
}
// Read the file contents
if ((dwError = gpOSI->Read(pFile, pBuffer, File.dwSize)) != 0 && !(gdwFlags & V80_FLAG_READBAD))
{
printf("Dump File Read: dwError:%d\n", dwError);
continue;
}
// Dump the file contents
Dump(pBuffer, File.dwSize);
// Print operation summary
printf("\r\nTotal of %d bytes dumped.\r\n\r\n", File.dwSize);
}
// If exited on "No More Files" then "No Error"
if (dwError == ERROR_NO_MORE_FILES)
dwError = 0;
// Release the allocated memory
if (pBuffer != NULL)
free(pBuffer);
// Release the OSI object
Exit_2:
if (gpOSI != NULL)
delete gpOSI;
// Release the VDI object
Exit_1:
if (gpVDI != NULL)
delete gpVDI;
// Return
Exit_0:
return dwError;
}
//---------------------------------------------------------------------------------
// Dump disk sectors
//---------------------------------------------------------------------------------
DWORD DumpDisk()
{
VDI_GEOMETRY DG;
VDI_TRACK* pTrack;
BYTE Buffer[1024];
WORD wSectors = 0;
DWORD dwError;
// Initialize the disk interface
if ((dwError = LoadVDI()) != 0)
goto Done;
// Print operation objective
printf("\r\nDumping disk contents:\r\n\r\n");
// Get the disk geometry
gpVDI->GetDG(DG);
// For each track in the disk
for (int nTrack = DG.FT.nTrack; nTrack <= DG.LT.nTrack; nTrack++)
{
// Makes pTrack point to FirstTrack or LastTrack accordingly
pTrack = (nTrack == 0 ? &DG.FT : &DG.LT);
// For each side in the disk
for (int nSide = pTrack->nFirstSide; nSide <= pTrack->nLastSide; nSide++)
{ // For each sector in the track
for (int nSector = pTrack->nFirstSector; nSector <= pTrack->nLastSector; nSector++)
{ // Read sector
if (gpVDI->Read(nTrack, nSide, nSector, Buffer, sizeof(Buffer)) == 0)
{ // Dump sector data
printf("\r\n[%02d:%d:%02d]\r\n", nTrack, nSide, nSector);