forked from deater/dos33fsprogs
/
dos33.c
1557 lines (1219 loc) · 45.4 KB
/
dos33.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
#include <stdio.h>
#include <stdlib.h> /* exit() */
#include <string.h> /* strncpy() */
#include <sys/stat.h> /* struct stat */
#include <fcntl.h> /* O_RDONLY */
#include <unistd.h> /* lseek() */
#include <ctype.h> /* toupper() */
#include "version.h"
#include "dos33.h"
static unsigned char sector_buffer[BYTES_PER_SECTOR];
static int ones_lookup[16]={
/* 0x0 = 0000 */ 0,
/* 0x1 = 0001 */ 1,
/* 0x2 = 0010 */ 1,
/* 0x3 = 0011 */ 2,
/* 0x4 = 0100 */ 1,
/* 0x5 = 0101 */ 2,
/* 0x6 = 0110 */ 2,
/* 0x7 = 0111 */ 3,
/* 0x8 = 1000 */ 1,
/* 0x9 = 1001 */ 2,
/* 0xA = 1010 */ 2,
/* 0xB = 1011 */ 3,
/* 0xC = 1100 */ 2,
/* 0xd = 1101 */ 3,
/* 0xe = 1110 */ 3,
/* 0xf = 1111 */ 4,
};
static unsigned char get_high_byte(int value) {
return (value>>8)&0xff;
}
static unsigned char get_low_byte(int value) {
return (value&0xff);
}
#define FILE_NORMAL 0
#define FILE_DELETED 1
static unsigned char dos33_file_type(int value) {
unsigned char result;
switch(value&0x7f){
case 0x0: result='T'; break;
case 0x1: result='I'; break;
case 0x2: result='A'; break;
case 0x4: result='B'; break;
case 0x8: result='S'; break;
case 0x10: result='R'; break;
case 0x20: result='N'; break;
case 0x40: result='L'; break;
default: result='?'; break;
}
return result;
}
static unsigned char dos33_char_to_type(char type,int lock) {
unsigned char result,temp_type;
temp_type=type;
/* Covert to upper case */
if (temp_type>='a') temp_type=temp_type-0x20;
switch(temp_type) {
case 'T': result=0x0; break;
case 'I': result=0x1; break;
case 'A': result=0x2; break;
case 'B': result=0x4; break;
case 'S': result=0x8; break;
case 'R': result=0x10; break;
case 'N': result=0x20; break;
case 'L': result=0x40; break;
default: result=0x0;
}
if (lock) result|=0x80;
return result;
}
/* dos33 filenames have top bit set on ascii chars */
/* and are padded with spaces */
static char *dos33_filename_to_ascii(char *dest,unsigned char *src,int len) {
int i=0,last_nonspace=0;
for(i=0;i<len;i++) if (src[i]!=0xA0) last_nonspace=i;
for(i=0;i<last_nonspace+1;i++) {
dest[i]=src[i]^0x80; /* toggle top bit */
}
dest[i]='\0';
return dest;
}
/* Read VTOC into a buffer */
static int dos33_read_vtoc(int fd) {
int result;
/* Seek to VTOC */
lseek(fd,DISK_OFFSET(VTOC_TRACK,VTOC_SECTOR),SEEK_SET);
/* read in VTOC */
result=read(fd,§or_buffer,BYTES_PER_SECTOR);
if (result<0) fprintf(stderr,"Error on I/O\n");
return 0;
}
/* Calculate available freespace */
static int dos33_free_space(int fd) {
unsigned char bitmap[4];
int i,sectors_free=0;
/* Read Vtoc */
dos33_read_vtoc(fd);
for(i=0;i<TRACKS_PER_DISK;i++) {
bitmap[0]=sector_buffer[VTOC_FREE_BITMAPS+(i*4)];
bitmap[1]=sector_buffer[VTOC_FREE_BITMAPS+(i*4)+1];
sectors_free+=ones_lookup[bitmap[0]&0xf];
sectors_free+=ones_lookup[(bitmap[0]>>4)&0xf];
sectors_free+=ones_lookup[bitmap[1]&0xf];
sectors_free+=ones_lookup[(bitmap[1]>>4)&0xf];
}
return sectors_free*BYTES_PER_SECTOR;
}
/* Get a T/S value from a Catalog Sector */
static int dos33_get_catalog_ts(int fd) {
dos33_read_vtoc(fd);
return TS_TO_INT(sector_buffer[VTOC_CATALOG_T],
sector_buffer[VTOC_CATALOG_S]);
}
/* returns the next valid catalog entry */
/* after the one passed in */
static int dos33_find_next_file(int fd,int catalog_tsf) {
int catalog_track,catalog_sector,catalog_file;
int file_track,i;
int result;
catalog_file=catalog_tsf>>16;
catalog_track=(catalog_tsf>>8)&0xff;
catalog_sector=(catalog_tsf&0xff);
catalog_loop:
/* Read in Catalog Sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
i=catalog_file;
while(i<7) {
file_track=sector_buffer[CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE)];
/* 0xff means file deleted */
/* 0x0 means empty */
if ((file_track!=0xff) && (file_track!=0x0)){
return ((i<<16)+(catalog_track<<8)+catalog_sector);
}
i++;
}
catalog_track=sector_buffer[CATALOG_NEXT_T];
catalog_sector=sector_buffer[CATALOG_NEXT_S];
if (catalog_sector!=0) {
catalog_file=0;
goto catalog_loop;
}
if (result<0) fprintf(stderr,"Error on I/O\n");
return -1;
}
static int dos33_print_file_info(int fd,int catalog_tsf) {
int catalog_track,catalog_sector,catalog_file,i;
char temp_string[BUFSIZ];
int result;
catalog_file=catalog_tsf>>16;
catalog_track=(catalog_tsf>>8)&0xff;
catalog_sector=(catalog_tsf&0xff);
/* Read in Catalog Sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
if (sector_buffer[CATALOG_FILE_LIST+(catalog_file*CATALOG_ENTRY_SIZE)+FILE_TYPE]>0x7f) {
printf("*");
}
else printf(" ");
printf("%c",dos33_file_type(sector_buffer[CATALOG_FILE_LIST+(catalog_file*CATALOG_ENTRY_SIZE)+FILE_TYPE]));
printf(" ");
printf("%.3i ",sector_buffer[CATALOG_FILE_LIST+(catalog_file*CATALOG_ENTRY_SIZE+FILE_SIZE_L)]+
(sector_buffer[CATALOG_FILE_LIST+(catalog_file*CATALOG_ENTRY_SIZE+FILE_SIZE_H)]<<8));
strncpy(temp_string,
dos33_filename_to_ascii(temp_string,sector_buffer+(CATALOG_FILE_LIST+
(catalog_file*CATALOG_ENTRY_SIZE+FILE_NAME)),30),
BUFSIZ);
for(i=0;i<strlen(temp_string);i++) {
if (temp_string[i]<0x20) printf("^%c",temp_string[i]+0x40);
else printf("%c",temp_string[i]);
}
printf("\n");
if (result<0) fprintf(stderr,"Error on I/O\n");
return 0;
}
/* Checks if "filename" exists */
/* returns entry/track/sector */
static int dos33_check_file_exists(int fd,
char *filename,
int file_deleted) {
int catalog_track,catalog_sector;
int i,file_track;
char file_name[31];
int result;
/* read the VTOC into buffer */
dos33_read_vtoc(fd);
/* get the catalog track and sector from the VTOC */
catalog_track=sector_buffer[VTOC_CATALOG_T];
catalog_sector=sector_buffer[VTOC_CATALOG_S];
repeat_catalog:
/* Read in Catalog Sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
/* scan all file entries in catalog sector */
for(i=0;i<7;i++) {
file_track=sector_buffer[CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE)];
/* 0xff means file deleted */
/* 0x0 means empty */
if (file_track!=0x0) {
if (file_track==0xff) {
dos33_filename_to_ascii(file_name,
sector_buffer+(CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE+FILE_NAME)),29);
if (file_deleted) {
/* return if we found the file */
if (!strncmp(filename,file_name,29)) {
return ((i<<16)+(catalog_track<<8)+catalog_sector);
}
}
}
else {
dos33_filename_to_ascii(file_name,
sector_buffer+(CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE+FILE_NAME)),30);
/* return if we found the file */
if (!strncmp(filename,file_name,30)) {
return ((i<<16)+(catalog_track<<8)+catalog_sector);
}
}
}
}
/* point to next catalog track/sector */
catalog_track=sector_buffer[CATALOG_NEXT_T];
catalog_sector=sector_buffer[CATALOG_NEXT_S];
if (catalog_sector!=0) goto repeat_catalog;
if (result<0) fprintf(stderr,"Error on I/O\n");
return -1;
}
/* could be replaced by "find leading 1" instruction */
/* if available */
int find_first_one(unsigned char byte) {
int i=0;
if (byte==0) return -1;
while((byte& (0x1<<i))==0) {
i++;
}
return i;
}
static int dos33_free_sector(int fd,int track,int sector) {
unsigned char vtoc[BYTES_PER_SECTOR];
int result;
/* Seek to VTOC */
lseek(fd,DISK_OFFSET(VTOC_TRACK,VTOC_SECTOR),SEEK_SET);
/* read in VTOC */
result=read(fd,&vtoc,BYTES_PER_SECTOR);
/* each bitmap is 32 bits. With 16-sector tracks only first 16 used */
/* 1 indicates free, 0 indicates used */
if (sector<8) {
vtoc[VTOC_FREE_BITMAPS+(track*4)+1]|=(0x1<<sector);
}
else {
vtoc[VTOC_FREE_BITMAPS+(track*4)]|=(0x1<<(sector-8));
}
/* write modified VTOC back out */
lseek(fd,DISK_OFFSET(VTOC_TRACK,VTOC_SECTOR),SEEK_SET);
result=write(fd,&vtoc,BYTES_PER_SECTOR);
if (result<0) fprintf(stderr,"Error on I/O\n");
return 0;
}
static int dos33_allocate_sector(int fd) {
int found_track=0,found_sector=0;
unsigned char bitmap[4];
int i,start_track,track_dir,byte;
int result;
dos33_read_vtoc(fd);
/* Originally used to keep things near center of disk for speed */
/* We can use to avoid fragmentation possibly */
start_track=sector_buffer[VTOC_LAST_ALLOC_T]%TRACKS_PER_DISK;
track_dir=sector_buffer[VTOC_ALLOC_DIRECT];
if (track_dir==255) track_dir=-1;
if ((track_dir!=1) && (track_dir!=-1)) {
fprintf(stderr,"ERROR! Invalid track dir %i\n",track_dir);
}
if (((start_track>VTOC_TRACK) && (track_dir!=1)) ||
((start_track<VTOC_TRACK) && (track_dir!=-1))) {
fprintf(stderr,"Warning! Non-optimal values for track dir t=%i d=%i!\n",
start_track,track_dir);
}
i=start_track;
do {
for(byte=1;byte>-1;byte--) {
bitmap[byte]=sector_buffer[VTOC_FREE_BITMAPS+(i*4)+byte];
if (bitmap[byte]!=0x00) {
found_sector=find_first_one(bitmap[byte]);
found_track=i;
/* clear bit indicating in use */
sector_buffer[VTOC_FREE_BITMAPS+(i*4)+byte]&=~(0x1<<found_sector);
found_sector+=(8*(1-byte));
goto found_one;
}
}
/* Move to next track, handling overflows */
i+=track_dir;
if (i<0) {
i=VTOC_TRACK;
track_dir=1;
}
if (i>=TRACKS_PER_DISK) {
i=VTOC_TRACK;
track_dir=-1;
}
} while (i!=start_track);
fprintf(stderr,"No room left!\n");
return -1;
found_one:
/* store new track/direction info */
sector_buffer[VTOC_LAST_ALLOC_T]=found_track;
if (found_track>VTOC_TRACK) sector_buffer[VTOC_ALLOC_DIRECT]=1;
else sector_buffer[VTOC_ALLOC_DIRECT]=-1;
/* Seek to VTOC */
lseek(fd,DISK_OFFSET(VTOC_TRACK,VTOC_SECTOR),SEEK_SET);
/* Write out VTOC */
result=write(fd,§or_buffer,BYTES_PER_SECTOR);
if (result<0) fprintf(stderr,"Error on I/O\n");
return ((found_track<<8)+found_sector);
}
/* creates file apple_filename on the image from local file filename */
/* returns ?? */
static int dos33_add_file(int fd,char type,char *filename,
char *apple_filename) {
int free_space,file_size,needed_sectors;
struct stat file_info;
int size_in_sectors=0;
int initial_ts_list=0,ts_list=0,i,data_ts,x,bytes_read=0,old_ts_list;
int catalog_track,catalog_sector,sectors_used=0;
int input_fd;
int result;
if (apple_filename[0]<64) {
fprintf(stderr,"Error! First char of filename must be ASCII 64 or above!\n");
exit(3);
}
{
int i;
for(i=0;i<strlen(apple_filename);i++) {
if (apple_filename[i]==',') {
fprintf(stderr,"Error! Cannot have , in a filename!\n");
exit(3);
}
}
}
/* FIXME */
/* check type */
/* and sanity check a/b filesize is set properly */
/* Determine size of file to upload */
if (stat(filename,&file_info)<0) {
fprintf(stderr,"Error! %s not found!\n",filename);
exit(3);
}
file_size=(int)file_info.st_size;
/* We need to round up to nearest sector size */
/* Add an extra sector for the T/S list */
/* Then add extra sector for a T/S list every 122*256 bytes (~31k) */
needed_sectors=(file_size/BYTES_PER_SECTOR)+ /* round sectors */
((file_size%BYTES_PER_SECTOR)!=0)+/* tail if needed */
1+/* first T/S list */
(file_size/(122*BYTES_PER_SECTOR)); /* extra t/s lists */
/* Get free space on device */
free_space=dos33_free_space(fd);
/* Check for free space */
if (needed_sectors*BYTES_PER_SECTOR>free_space) {
fprintf(stderr,"Error! Not enough free space on disk image (need %d have %d)\n",
needed_sectors*BYTES_PER_SECTOR,free_space);
exit(4);
}
/* plus one because we need a sector for the tail */
size_in_sectors=(file_size/BYTES_PER_SECTOR)+
((file_size%BYTES_PER_SECTOR)!=0);
// printf("Need to allocate %i data sectors\n",size_in_sectors);
// printf("Need to allocate %i total sectors\n",needed_sectors);
/* Open the local file */
input_fd=open(filename,O_RDONLY);
if (input_fd<0) {
fprintf(stderr,"Error! could not open %s\n",filename);
return -1;
}
i=0;
while (i<size_in_sectors) {
/* Create new T/S list if necessary */
if (i%TSL_MAX_NUMBER==0) {
old_ts_list=ts_list;
/* allocate a sector for the new list */
ts_list=dos33_allocate_sector(fd);
sectors_used++;
if (ts_list<0) return -1;
/* clear the t/s sector */
for(x=0;x<BYTES_PER_SECTOR;x++) sector_buffer[x]=0;
lseek(fd,DISK_OFFSET((ts_list>>8)&0xff,ts_list&0xff),SEEK_SET);
result=write(fd,sector_buffer,BYTES_PER_SECTOR);
if (i==0) initial_ts_list=ts_list;
else {
/* we aren't the first t/s list so do special stuff */
/* load in the old t/s list */
lseek(fd,
DISK_OFFSET(get_high_byte(old_ts_list),
get_low_byte(old_ts_list)),
SEEK_SET);
result=read(fd,§or_buffer,BYTES_PER_SECTOR);
/* point from old ts list to new one we just made */
sector_buffer[TSL_NEXT_TRACK]=get_high_byte(ts_list);
sector_buffer[TSL_NEXT_SECTOR]=get_low_byte(ts_list);
/* set offset into file */
sector_buffer[TSL_OFFSET_H]=get_high_byte((i-122)*256);
sector_buffer[TSL_OFFSET_L]=get_low_byte((i-122)*256);
/* write out the old t/s list with updated info */
lseek(fd,
DISK_OFFSET(get_high_byte(old_ts_list),
get_low_byte(old_ts_list)),
SEEK_SET);
result=write(fd,sector_buffer,BYTES_PER_SECTOR);
}
}
/* allocate a sector */
data_ts=dos33_allocate_sector(fd);
sectors_used++;
if (data_ts<0) return -1;
/* clear sector */
for(x=0;x<BYTES_PER_SECTOR;x++) sector_buffer[x]=0;
/* read from input */
bytes_read=read(input_fd,sector_buffer,BYTES_PER_SECTOR);
if (bytes_read<0) fprintf(stderr,"Error reading bytes!\n");
/* write to disk image */
lseek(fd,DISK_OFFSET((data_ts>>8)&0xff,data_ts&0xff),SEEK_SET);
result=write(fd,sector_buffer,BYTES_PER_SECTOR);
// printf("Writing %i bytes to %i/%i\n",bytes_read,(data_ts>>8)&0xff,
// data_ts&0xff);
/* add to T/s table */
/* read in t/s list */
lseek(fd,DISK_OFFSET((ts_list>>8)&0xff,ts_list&0xff),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
/* point to new data sector */
sector_buffer[((i%TSL_MAX_NUMBER)*2)+TSL_LIST]=(data_ts>>8)&0xff;
sector_buffer[((i%TSL_MAX_NUMBER)*2)+TSL_LIST+1]=(data_ts&0xff);
/* write t/s list back out */
lseek(fd,DISK_OFFSET((ts_list>>8)&0xff,ts_list&0xff),SEEK_SET);
result=write(fd,sector_buffer,BYTES_PER_SECTOR);
i++;
}
/* Add new file to Catalog */
/* read in vtoc */
dos33_read_vtoc(fd);
catalog_track=sector_buffer[VTOC_CATALOG_T];
catalog_sector=sector_buffer[VTOC_CATALOG_S];
continue_parsing_catalog:
/* Read in Catalog Sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
/* Find empty directory entry */
i=0;
while(i<7) {
if ((sector_buffer[CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE)]==0xff) ||
(sector_buffer[CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE)]==0x00))
goto got_a_dentry;
i++;
}
if ((catalog_track=0x11) && (catalog_sector==1)) {
/* in theory can only have 105 files */
/* if full, we have no recourse! */
/* can we allocate new catalog sectors and point to them?? */
fprintf(stderr,"Error! No more room for files!\n");
return -1;
}
catalog_track=sector_buffer[CATALOG_NEXT_T];
catalog_sector=sector_buffer[CATALOG_NEXT_S];
goto continue_parsing_catalog;
got_a_dentry:
// printf("Adding file at entry %i of catalog 0x%x:0x%x\n",
// i,catalog_track,catalog_sector);
/* Point entry to initial t/s list */
sector_buffer[CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE)]=(initial_ts_list>>8)&0xff;
sector_buffer[CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE)+1]=(initial_ts_list&0xff);
/* set file type */
sector_buffer[CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE)+FILE_TYPE]=
dos33_char_to_type(type,0);
// printf("Pointing T/S to %x/%x\n",(initial_ts_list>>8)&0xff,initial_ts_list&0xff);
/* copy over filename */
for(x=0;x<strlen(apple_filename);x++) {
sector_buffer[CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE)+FILE_NAME+x]=
apple_filename[x]^0x80;
}
/* pad out the filename with spaces */
for(x=strlen(apple_filename);x<FILE_NAME_SIZE;x++) {
sector_buffer[CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE)+FILE_NAME+x]=' '^0x80;
}
/* fill in filesize in sectors */
sector_buffer[CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE)+FILE_SIZE_L]=
sectors_used&0xff;
sector_buffer[CATALOG_FILE_LIST+(i*CATALOG_ENTRY_SIZE)+FILE_SIZE_H]=
(sectors_used>>8)&0xff;
/* write out catalog sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=write(fd,sector_buffer,BYTES_PER_SECTOR);
if (result<0) fprintf(stderr,"Error on I/O\n");
return 0;
}
/* load a file. fts=entry/track/sector */
static int dos33_load_file(int fd,int fts,char *filename) {
int output_fd;
int catalog_file,catalog_track,catalog_sector;
int file_type,file_size=-1,tsl_track,tsl_sector,data_t,data_s;
unsigned char data_sector[BYTES_PER_SECTOR];
int tsl_pointer=0,output_pointer=0;
int result;
/* Fix me! Warn if overwriting file! */
output_fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,0666);
if (output_fd<0) {
fprintf(stderr,"Error! could not open %s for local save\n",filename);
return -1;
}
catalog_file=fts>>16;
catalog_track=(fts>>8)&0xff;
catalog_sector=(fts&0xff);
/* Read in Catalog Sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
tsl_track=sector_buffer[CATALOG_FILE_LIST+
(catalog_file*CATALOG_ENTRY_SIZE)+FILE_TS_LIST_T];
tsl_sector=sector_buffer[CATALOG_FILE_LIST+
(catalog_file*CATALOG_ENTRY_SIZE)+FILE_TS_LIST_S];
file_type=dos33_file_type(sector_buffer[CATALOG_FILE_LIST+
(catalog_file*CATALOG_ENTRY_SIZE)
+FILE_TYPE]);
// printf("file_type: %c\n",file_type);
keep_saving:
/* Read in TSL Sector */
lseek(fd,DISK_OFFSET(tsl_track,tsl_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
tsl_pointer=0;
/* check each track/sector pair in the list */
while(tsl_pointer<TSL_MAX_NUMBER) {
/* get the t/s value */
data_t=sector_buffer[TSL_LIST+(tsl_pointer*TSL_ENTRY_SIZE)];
data_s=sector_buffer[TSL_LIST+(tsl_pointer*TSL_ENTRY_SIZE)+1];
if ((data_s==0) && (data_t==0)) {
/* empty */
}
else {
lseek(fd,DISK_OFFSET(data_t,data_s),SEEK_SET);
result=read(fd,&data_sector,BYTES_PER_SECTOR);
/* some file formats have the size in the first sector */
/* so cheat and get real file size from file itself */
if (output_pointer==0) {
switch(file_type) {
case 'A':
case 'I':
file_size=data_sector[0]+(data_sector[1]<<8)+2;
break;
case 'B':
file_size=data_sector[2]+(data_sector[3]<<8)+4;
break;
default:
file_size=-1;
}
}
/* write the block read in out to the output file */
lseek(output_fd,output_pointer*BYTES_PER_SECTOR,SEEK_SET);
result=write(output_fd,&data_sector,BYTES_PER_SECTOR);
}
output_pointer++;
tsl_pointer++;
}
/* finished with TSL sector, see if we have another */
tsl_track=sector_buffer[TSL_NEXT_TRACK];
tsl_sector=sector_buffer[TSL_NEXT_SECTOR];
// printf("Next track/sector=%d/%d op=%d\n",tsl_track,tsl_sector,
// output_pointer*BYTES_PER_SECTOR);
if ((tsl_track==0) && (tsl_sector==0)) {
}
else goto keep_saving;
/* Correct the file size */
if (file_size>=0) {
// printf("Truncating file size to %d\n",file_size);
result=ftruncate(output_fd,file_size);
}
if (result<0) fprintf(stderr,"Error on I/O\n");
return 0;
}
/* lock a file. fts=entry/track/sector */
static int dos33_lock_file(int fd,int fts,int lock) {
int catalog_file,catalog_track,catalog_sector;
int file_type,result;
catalog_file=fts>>16;
catalog_track=(fts>>8)&0xff;
catalog_sector=(fts&0xff);
/* Read in Catalog Sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
file_type=sector_buffer[CATALOG_FILE_LIST+
(catalog_file*CATALOG_ENTRY_SIZE)
+FILE_TYPE];
if (lock) file_type|=0x80;
else file_type&=0x7f;
sector_buffer[CATALOG_FILE_LIST+
(catalog_file*CATALOG_ENTRY_SIZE)
+FILE_TYPE]=file_type;
/* write back modified catalog sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=write(fd,sector_buffer,BYTES_PER_SECTOR);
if (result<0) fprintf(stderr,"Error on I/O\n");
return 0;
}
/* rename a file. fts=entry/track/sector */
/* FIXME: can we rename a locked file? */
/* FIXME: validate the new filename is valid */
static int dos33_rename_file(int fd,int fts,char *new_name) {
int catalog_file,catalog_track,catalog_sector;
int x,result;
catalog_file=fts>>16;
catalog_track=(fts>>8)&0xff;
catalog_sector=(fts&0xff);
/* Read in Catalog Sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
/* copy over filename */
for(x=0;x<strlen(new_name);x++)
sector_buffer[CATALOG_FILE_LIST+(catalog_file*CATALOG_ENTRY_SIZE)+
FILE_NAME+x]=new_name[x]^0x80;
/* pad out the filename with spaces */
for(x=strlen(new_name);x<FILE_NAME_SIZE;x++)
sector_buffer[CATALOG_FILE_LIST+(catalog_file*CATALOG_ENTRY_SIZE)+
FILE_NAME+x]=' '^0x80;
/* write back modified catalog sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=write(fd,sector_buffer,BYTES_PER_SECTOR);
if (result<0) fprintf(stderr,"Error on I/O\n");
return 0;
}
/* undelete a file. fts=entry/track/sector */
/* FIXME: can we rename a locked file? */
/* FIXME: validate the new filename is valid */
static int dos33_undelete_file(int fd,int fts,char *new_name) {
int catalog_file,catalog_track,catalog_sector;
char replacement_char;
int result;
catalog_file=fts>>16;
catalog_track=(fts>>8)&0xff;
catalog_sector=(fts&0xff);
/* Read in Catalog Sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
/* get the stored track value, and put it back */
/* FIXME: should walk file to see if T/s valild */
/* by setting the track value to FF which indicates deleted file */
sector_buffer[CATALOG_FILE_LIST+(catalog_file*CATALOG_ENTRY_SIZE)]=
sector_buffer[CATALOG_FILE_LIST+(catalog_file*CATALOG_ENTRY_SIZE)+
FILE_NAME+29];
/* restore file name if possible */
replacement_char=0xa0;
if (strlen(new_name)>29) replacement_char=new_name[29]^0x80;
sector_buffer[CATALOG_FILE_LIST+(catalog_file*CATALOG_ENTRY_SIZE)+
FILE_NAME+29]=replacement_char;
/* write back modified catalog sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=write(fd,sector_buffer,BYTES_PER_SECTOR);
if (result<0) fprintf(stderr,"Error on I/O\n");
return 0;
}
static int dos33_delete_file(int fd,int fsl) {
int i;
int catalog_track,catalog_sector,catalog_entry;
int ts_track,ts_sector;
char file_type;
int result;
/* unpack file/track/sector info */
catalog_entry=fsl>>16;
catalog_track=(fsl>>8)&0xff;
catalog_sector=(fsl&0xff);
/* Load in the catalog table for the file */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
file_type=sector_buffer[CATALOG_FILE_LIST+
(catalog_entry*CATALOG_ENTRY_SIZE)
+FILE_TYPE];
if (file_type&0x80) {
fprintf(stderr,"File is locked! Unlock before deleting!\n");
exit(1);
}
/* get pointer to t/s list */
ts_track=sector_buffer[CATALOG_FILE_LIST+catalog_entry*CATALOG_ENTRY_SIZE+
FILE_TS_LIST_T];
ts_sector=sector_buffer[CATALOG_FILE_LIST+catalog_entry*CATALOG_ENTRY_SIZE+
FILE_TS_LIST_S];
keep_deleting:
/* load in the t/s list info */
lseek(fd,DISK_OFFSET(ts_track,ts_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
/* Free each sector listed by t/s list */
for(i=0;i<TSL_MAX_NUMBER;i++) {
/* If t/s = 0/0 then no need to clear */
if ((sector_buffer[TSL_LIST+2*i]==0) && (sector_buffer[TSL_LIST+2*i+1]==0)) {
}
else {
dos33_free_sector(fd,sector_buffer[TSL_LIST+2*i],sector_buffer[TSL_LIST+2*i+1]);
}
}
/* free the t/s list */
dos33_free_sector(fd,ts_track,ts_sector);
/* Point to next t/s list */
ts_track=sector_buffer[TSL_NEXT_TRACK];
ts_sector=sector_buffer[TSL_NEXT_SECTOR];
/* If more tsl lists, keep looping */
if ((ts_track==0x0) && (ts_sector==0x0)) {
}
else {
goto keep_deleting;
}
/* Erase file from catalog entry */
/* First reload proper catalog sector */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
/* save track as last char of name, for undelete purposes */
sector_buffer[CATALOG_FILE_LIST+(catalog_entry*CATALOG_ENTRY_SIZE)+
(FILE_NAME+FILE_NAME_SIZE-1)]=
sector_buffer[CATALOG_FILE_LIST+(catalog_entry*CATALOG_ENTRY_SIZE)];
/* Actually delete the file */
/* by setting the track value to FF which indicates deleted file */
sector_buffer[CATALOG_FILE_LIST+(catalog_entry*CATALOG_ENTRY_SIZE)]=0xff;
/* re seek to catalog position and write out changes */
lseek(fd,DISK_OFFSET(catalog_track,catalog_sector),SEEK_SET);
result=write(fd,sector_buffer,BYTES_PER_SECTOR);
if (result<0) fprintf(stderr,"Error on I/O\n");
return 0;
}
static int dump_sector(void) {
int i,j;
for(i=0;i<16;i++) {
printf("$%02X : ",i*16);
for(j=0;j<16;j++)
printf("%02X ",sector_buffer[i*16+j]);
printf("\n");
}
return 0;
}
static int dos33_dump(int fd) {
int num_tracks,catalog_t,catalog_s,file,ts_t,ts_s,ts_total,track,sector;
int i,j;
int deleted=0;
char temp_string[BUFSIZ];
unsigned char tslist[BYTES_PER_SECTOR];
int result;
/* Read Track 1 Sector 9 */
lseek(fd,DISK_OFFSET(1,9),SEEK_SET);
result=read(fd,sector_buffer,BYTES_PER_SECTOR);
printf("Finding name of startup file, Track 1 Sector 9 offset $75\n");
dump_sector();
printf("Startup Filename: ");
for(i=0;i<30;i++) printf("%c",sector_buffer[0x75+i]&0x7f);
printf("\n");
dos33_read_vtoc(fd);
printf("\nVTOC Sector:\n");
dump_sector();