-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfs.c
1933 lines (1619 loc) · 43.2 KB
/
fs.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
/* Time-stamp: <98/01/10 14:08:41 derik>
------------------------------------------------------------------------------
=====
CPCFS -- f s . c -- Manageing the Filesystem
=====
Version 0.85 (c) February '96 by Derik van Zuetphen
------------------------------------------------------------------------------
*/
#include <sys/stat.h>
#include "cpcfs.h"
/****** Variables ******/
int cur_trk = -1; /* flag for no track loaded */
int cur_hd = 0;
int cur_blk = -1;
bool tag_ok () {
/* ^^^^^^ */
return (strncmp("MV - CPC",(signed char*)disk_header.tag,8)==0);
}
void alloc_block(int blk,int file) {
/* ^^^^^^^^^^^
<file> is only informational, real CP/M uses bitmaps */
if (file >= 0xFF) file = 0xFE; /* FF marks a free block */
if (blk > dpb->DSM)
printm(1,"Warning: Directory entry %d currupt!\n",file);
else
blk_alloc[blk]=file;
}
void free_block(int blk) {
/* ^^^^^^^^^^ */
if (blk <= dpb->DSM)
blk_alloc[blk]=0xFF;
}
bool is_free_block(int blk) {
/* ^^^^^^^^^^^^^ */
return blk_alloc[blk]==0xFF;
}
void calc_allocation () {
/* ^^^^^^^^^^^^^^^ */
int i;
allocated_blks = 0;
free_blks = 0;
for (i=dpb->DBL; i<=dpb->DSM; i++)
if (is_free_block(i)) free_blks++;
else allocated_blks++;
percentage=100*allocated_blks/(float)(allocated_blks+free_blks);
total_blks = allocated_blks + free_blks;
}
bool inactive () {
/* ^^^^^^^^ */
if (*disk_header.tag==0) {
printm(1,"No image loaded!\n");
return TRUE;
} else
return FALSE;
}
/* Calculate track, sector, and head number of the first sector of block
<blk>.
A quick picture:
secs
.------^-------.
_______________
/ -2- - - - - >/| } heads
/______________/ | }
/ | -1- - - - - > | |
: | | |
: | | |
: | | |
trks = | -4- - - - - -| |
: | | |
: | -3- - - - - > | |
: | | |
\ |_______________|/
numbering scheme:
first secs, then heads, then tracks
%s /s%h /s/h%t has to be applied on running sec number
s = sec/trk, h = heads, t = tracks (here infinite)
*/
int trk_calc(int blk) {
/* ^^^^^^^^ */
return ((long)blk*dpb->BLS/dpb->BPS + dpb->OFS*dpb->SECS)
/ (dpb->SECS*dpb->HDS);
}
int sec_calc(int blk) {
/* ^^^^^^^^
The returned number is not shifted by <sec_offset>! */
return ((long)blk*dpb->BLS/dpb->BPS + dpb->OFS*dpb->SECS) % dpb->SECS;
}
int hd_calc(int blk) {
/* ^^^^^^^ */
return ((long)blk*dpb->BLS/dpb->BPS + dpb->OFS*dpb->SECS)
/ dpb->SECS % dpb->HDS;
}
int blk_calc(int hd, int trk, int sec) {
/* ^^^^^^^^
Return the blocknumber of position <hd>, <trk>, <sec> or -1, if it is a
reserved position
*/
if (trk*dpb->HDS+hd < dpb->OFS) return -1;
return ((long)sec + hd*dpb->SECS
+ trk*dpb->SECS*dpb->HDS
- dpb->OFS*dpb->SECS)
/ (dpb->BLS/dpb->BPS);
}
void abandonimage() {
/* ^^^^^^^^^^^^ */
*disk_header.tag = 0;
cur_trk = -1;
if (track) {free(track); track=NULL;}
if (blk_alloc) {free(blk_alloc); blk_alloc=NULL;}
if (directory) {free(directory); directory=(DirEntry*)NULL;}
if (block_buffer){free(block_buffer); block_buffer=(uchar*)NULL;}
errorf(FALSE,"Image \"%s\" abandoned!",imagename);
}
/********
Tracks
********/
int read_track (int hd, int trk) {
/* ^^^^^^^^^^
WARNING: the DPB does not nessecarily exist here! */
long n, pos;
if (trk == cur_trk && hd == cur_hd) {
return 0;
}
printm(11,"[rt(%d,%d)] ",hd,trk);
pos = (long)(trk*disk_header.nbof_heads + hd)
* (long)disk_header.tracksize;
n = lseek(imagefile,0x100L+pos,SEEK_SET);
if (n == -1L) {
errorf(TRUE,"Image currupt! I cannot position on track %d!",
trk);
abandonimage();
return -1;
}
n = read(imagefile,track,disk_header.tracksize);
if (n != disk_header.tracksize) {
errorf(TRUE,"Image currupt! I can only read %ld bytes "
"of track %d (instead of %d bytes)!",
n,trk,disk_header.tracksize);
abandonimage();
return -1;
}
cur_trk = trk;
cur_hd = hd;
return 0;
}
int write_track() {
/* ^^^^^^^^^^^ */
long n,pos;
if (cur_trk == -1) return 0;
printm(11,"[wt(%d,%d)] ",cur_hd,cur_trk);
pos = (long)(cur_trk*dpb->HDS + cur_hd) * (long)disk_header.tracksize;
n = lseek(imagefile,0x100L+pos,SEEK_SET);
if (n == -1L) {
errorf(TRUE,"Image currupt! I cannot position on track %d!",
cur_trk);
abandonimage();
return -1;
}
n = write(imagefile,track,disk_header.tracksize);
if (n != disk_header.tracksize) {
errorf(TRUE,"Something wrong! I cannot write %d bytes "
"to track %d (only %d bytes written)!",
disk_header.tracksize,cur_trk,n);
abandonimage();
return -1;
}
return 0;
}
bool next_sector (int *hd, int *trk, int *sec) {
/* ^^^^^^^^^^^
Assumes <sec> without offset. Answer TRUE if the <trk> or <hd> is changed. */
(*sec)++;
if (*sec >= dpb->SECS) {
*sec -= dpb->SECS;
(*hd)++;
if (*hd >= dpb->HDS) {
*hd = 0;
(*trk)++;
}
return TRUE;
}
return FALSE;
}
int interleave(uchar *track,int sec) {
/* Searches the sector numbers of <track> for the sectorid <sec> and answers
its potition */
int i;
for (i=0;i<dpb->SECS;i++) {
if (((struct t_header*)track)->sector[i].sector
== sec+dpb->SEC1) {
return i;
}
}
errorf(FALSE,"Image uses invalid interleave! Sector 0x%X in Track %d "
"and Head %d",sec,cur_trk,cur_hd);
abandonimage();
return 0;
}
uchar *read_block (int blk) {
/* ^^^^^^^^^^
Read block <blk> into a buffer and return its address, or NULL on error */
int trk, sec, hd;
int filled = 0;
if (blk==cur_blk) return block_buffer;
printm(11,"[rb(%d)] ",blk);
trk = trk_calc (blk);
sec = sec_calc (blk);
hd = hd_calc (blk);
while (filled < dpb->BLS) {
if (read_track(hd,trk)) return NULL;
memcpy(block_buffer+filled,
track+0x100+interleave(track,sec)*dpb->BPS,dpb->BPS);
filled += dpb->BPS;
next_sector (&hd,&trk,&sec);
}
cur_blk = blk;
return block_buffer;
}
uchar *write_block (int blk, char *buf) {
/* ^^^^^^^^^^^
Return the written buffer or NULL on error */
int trk, sec, hd;
int filled = 0;
printm(11,"[wb(%d)] ",blk);
trk = trk_calc (blk);
sec = sec_calc (blk);
hd = hd_calc (blk);
while (filled < dpb->BLS) {
if (read_track(hd,trk)) return NULL;
memcpy(track+0x100+interleave(track,sec)*dpb->BPS,
buf+filled,dpb->BPS);
filled += dpb->BPS;
if (next_sector (&hd,&trk,&sec)) write_track();
}
write_track();
return buf;
}
/***********
Directory
***********/
/* local definitions for glob_cpm_* */
#define GLOB_ENV_MAX 3
int glob_env = 0;
/* determines which global variable-set should <glob_cpm_*> use, */
/* necessary for nested globs */
uchar pattern[GLOB_ENV_MAX][INPUTLEN];
int last_entry[GLOB_ENV_MAX];
int glob_cpm_next() {
/* ^^^^^^^^^^^^^
Answer the next entry (or -1) with the same pattern as before */
int i;
uchar name[20];
for (i=last_entry[glob_env]+1;i<=dpb->DRM;i++) {
if (!directory[i].first) continue;
build_cpm_name_32((signed char*)name, directory[i].user,
(signed char*)directory[i].root,
(signed char*)directory[i].ext);
if (match((signed char*)pattern[glob_env],(signed char*)name)) {
last_entry[glob_env] = i;
return i;
}
}
return -1;
}
int glob_cpm_file(char *pat) {
/* ^^^^^^^^^^^^^
Scan the entries for the first file that matches <pattern> and answer its
first (according to <first> flag, see DirEntry) entry number,
If the pattern contains no filename part, *.* is assumed.
Errorcode is -1, if pattern not found.
The work is mostly deferred to <glob_cpm_next>. */
int user;
uchar root[INPUTLEN], ext[INPUTLEN];
const char errmsg[] = "Illegal filename \"%s\"";
if (parse_cpm_filename(pat,&user,root,ext))
return errorf(FALSE,errmsg,pat);
upper(root);
upper(ext);
if (*root==0) {
if (user >= 0) {
strcpy(root,"*");
strcpy(ext,"*");
} else {
return errorf(FALSE,errmsg,pat);
}
}
if (user==-1) user = cur_user;
build_cpm_name((signed char*)pattern[glob_env], user,
(signed char*)root, (signed char*)ext);
last_entry[glob_env] = -1; /* thus start with 0 */
return glob_cpm_next();
}
/* local definitions for update_directory */
struct pair {uchar en; uchar ex;};
int cmp_pair(struct pair *x, struct pair *y) {
if (x->ex < y->ex) return -1;
if (x->ex > y->ex) return 1;
return 0;
}
void update_directory() {
/* ^^^^^^^^^^^^^^^^
(Re-)Organizes the directory structure (Packing the name, making a linked
list) */
int i, j;
/* a dynamic array of (entry,extent) pairs */
struct pair *map;
printm(10,"[ud] ");
map = (struct pair*)Malloc(sizeof(struct pair)*(dpb->DRM+1));
/****** packing a name of kind "FOO BAR" to "FOO.BAR\0" ******/
for (i=0;i<=dpb->DRM;i++) {
if (directory[i].user == 0xE5) continue;
build_cpm_name_32((signed char*)directory[i].name, -1,
(signed char*)directory[i].root,
(signed char*)directory[i].ext);
}
/****** organizing the directory structure as linked list ******/
/* set entries in the directory to "not visited"
<size> = 0 : never visit; <size> = -1 : not yet visited */
for (i=0;i<=dpb->DRM;i++) {
if (directory[i].user == 0xE5) /*NOT <filler>, E5 is required*/
directory[i].size = 0; /* never visit empty entries */
else
directory[i].size = -1; /* not visited */
directory[i].first = FALSE;
directory[i].next = -1;
};
/* scan the entries */
for (i=0;i<=dpb->DRM;i++) {
if (directory[i].size > -1) continue;
/* reset the map */
for (j=0;j<=dpb->DRM;j++) {map[j].en=j;map[j].ex=0xFF;}
/* fill the map with <extent> from the directory */
map[i].ex = directory[i].extent;
for (j=0;j<=dpb->DRM;j++) {
if ((directory[j].size == -1) &&
(directory[j].user == directory[i].user) &&
(i!=j) &&
(strcmp((signed char*)directory[i].name,
(signed char*)directory[j].name)==0)) {
map[j].ex = directory[j].extent;
directory[j].size = 0;
}
}
/* sort the map according to map[].ex, not necessary in most cases */
qsort(map,dpb->DRM+1,sizeof(struct pair),
(int(*)(const void*,const void*))cmp_pair);
/* fill <first>, <size> and <next> from the map */
directory[map[0].en].first = TRUE;
j=1;
while (map[j].ex < 0xFF) {
directory[map[j-1].en].next = map[j].en;
j++;
}
directory[map[j-1].en].next = -1;
/* the filesize located in the first fileentry can be calculated from the
<extent> and <rec> fields of the last fileentry */
directory[map[0].en].size =
(long)directory[map[j-1].en].extent * EXTENTSIZE
+ directory[map[j-1].en].rec * RECORDSIZE;
} /* for i */
free(map); map=NULL;
}
void get_directory() {
/* ^^^^^^^^^^^^^ */
int i,j,off;
uchar *buf;
int mask;
printm(10,"[rd] ");
/* reading the directory data */
for (i=0;i<=dpb->DRM;i++) {
buf = read_block((signed)i*32/dpb->BLS);
off = i*32 % dpb->BLS;
/* user, name, ... */
directory[i].user = buf[off+0];
for (j=0;j<8;j++) directory[i].root[j] = buf[off+j+1] & 0x7F;
for (j=0;j<3;j++) directory[i].ext[j] = buf[off+j+9] & 0x7F;
directory[i].name[0] = 0;
directory[i].extent = buf[off+12];
directory[i].unused[0] = buf[off+13];
directory[i].unused[1] = buf[off+14];
directory[i].rec = buf[off+15];
/* attributes */
mask=0x1;
directory[i].attr = 0;
for (j=11;j>0;j--) {
if (buf[off+j]&0x80) directory[i].attr |= mask;
mask <<= 1;
}
/* block pointer */
for (j=0;j<16;j++) directory[i].blk[j] = 0;
if (BLKNR_SIZE==1) {
for (j=0;j<16;j++) {
directory[i].blk[j] = buf[off+16+j];
}
} else if (BLKNR_SIZE==2) {
for (j=0;j<8;j++) {
directory[i].blk[j] = buf[off+16+2*j]
+ 256 * buf[off+17+2*j];
}
}
}
update_directory();
for (i=0;i<dpb->DBL;i++) alloc_block(i,0); /* 0 is not correct! */
/* marking the blocks as allocated */
for (j=0;j<=dpb->DSM;j++) free_block(j);
for (i=0;i<=dpb->DRM;i++) {
for (j=0;j<BLKNR;j++) {
if ((directory[i].user!=0xE5)&&(directory[i].blk[j]))
alloc_block(directory[i].blk[j],i);
}
}
}
void put_directory() {
/* ^^^^^^^^^^^^^ */
int i, j, off;
uchar *buf;
int mask;
int block;
printm(10,"[wd] ");
buf = block_buffer; /* simply a shortcut */
block = 0;
for (i=0;i<=dpb->DRM;i++) {
off=i*32 % dpb->BLS;
buf[off] = directory[i].user;
for (j=0;j<8;j++) buf[off+j+1] = directory[i].root[j];
for (j=0;j<3;j++) buf[off+j+9] = directory[i].ext[j];
buf[off+12] = directory[i].extent;
buf[off+13] = directory[i].unused[0];
buf[off+14] = directory[i].unused[1];
buf[off+15] = directory[i].rec;
mask=0x1;
for (j=11;j>0;j--) {
if (directory[i].attr & mask)
buf[off+j] |= 0x80;
mask <<= 1;
};
if (BLKNR_SIZE==1) {
for (j=0;j<16;j++) {
buf[off+16+j] = directory[i].blk[j];
}
} else if (BLKNR_SIZE==2) {
for (j=0;j<8;j++) {
buf[off+16+2*j] = directory[i].blk[j] % 256;
buf[off+17+2*j] = directory[i].blk[j] / 256;
}
}
/* if next entry is in the next block, then write the current block */
if ((i+1)*32/(signed)dpb->BLS > block) {
write_block(block,buf);
block++;
}
}
}
/*******
Image
*******/
void update_dpb(DPB_type *dpb, uchar *track) {
/* ^^^^^^^^^^
Determine the extended DPB data out of <dpb> and the sample track <track>.
Complete the extension parts of <dpb>. <track> must be read in first!
*/
dpb->BLS = 1 << (dpb->BSH + 7); /* or 2^BSH*128 */
/* an image must exist, do not call form <format>! */
/* dpb->SEC1 = keep from DPB template */
dpb->SECS = ((struct t_header*)track)->SPT;
/* the next two elements should already be set */
dpb->TRKS = disk_header.nbof_tracks;
dpb->HDS = disk_header.nbof_heads;
dpb->SYS = (dpb->OFS>0) && (*(track+0x100)) != filler;
dpb->DBL = 32 * (dpb->DRM+1) / dpb->BLS; /* or often CKS/8 */
dpb->DSM = (dpb->TRKS*dpb->HDS*dpb->SECS) / (dpb->BLS/dpb->BPS) - 1;
/* subtract reserved tracks */
dpb->DSM -= dpb->OFS * dpb->SECS / (dpb->BLS/dpb->BPS);
/* subtract one for a slack block, because neither 18/8 nor 9/2 is integral: */
dpb->DSM--;
if (dpb->DSM>=255) {
/* 2 byte pointer and 8 pointer per entry */
BLKNR_SIZE = 2;
BLKNR = 8;
} else {
/* 1 byte pointer and 16 pointer per entry */
BLKNR_SIZE = 1;
BLKNR = 16;
}
}
void close_image() {
/* ^^^^^^^^^^^ */
if (*disk_header.tag) {
printm(10,"[ci] ");
if (cur_trk > -1) write_track();
put_directory();
free(blk_alloc); blk_alloc=NULL;
free(track); track=NULL;
free(directory); directory=(DirEntry*)NULL;
free(block_buffer); block_buffer=(uchar*)NULL;
*disk_header.tag = 0;
dpb = NULL;
close(imagefile);
}
cur_trk = -1;
cur_blk = -1;
}
int open_image(char *name) {
/* ^^^^^^^^^^
alloc track buffer, blk_alloc, buffer, read directory */
int n;
char dirsep[2] = {DIRSEPARATOR, 0};
char *p;
if (*disk_header.tag) close_image();
/* open file */
printm(10,"[oi] ");
imagefile = open(name,O_RDWR|O_BINARY,0);
imagename = name; /* temporary, for <abandonimage> */
if (imagefile < 0) {
return errorf(TRUE,"Cannot open \"%s\"",name);
}
n = read(imagefile,&disk_header,0x100);
if (n!=0x100) {
errorf(FALSE,"Image corrupt! I cannot read image header "
"(only %d bytes)!",n);
abandonimage();
return -1;
}
if (!tag_ok()) {
errorf(FALSE,"\"%s\" is not a DSK image!",name);
abandonimage();
return -1;
}
if ((disk_header.nbof_heads<1) || (disk_header.nbof_tracks<1)) {
errorf(FALSE,"--==>>> open_image: \"%s\"",name);
abandonimage();
return -1;
}
/* allocate memory */
track = Malloc(disk_header.tracksize);
/* set up varaibles */
filler = 0xE5;
cur_user=0;
p = getwd(full_imagename);
if (p) strcpy(full_imagename,p);
if (full_imagename[strlen(full_imagename)-1]==DIRSEPARATOR)
full_imagename[strlen(full_imagename)-1]=0;
strcat(full_imagename,dirsep);
strcat(full_imagename,name);
#if DOS
lower(full_imagename);
#endif
if((imagename=strrchr(full_imagename,DIRSEPARATOR)))
imagename++;
else imagename=full_imagename;
/* determine system/data-disk */
read_track(0,0);
cur_format = ((struct t_header*)track)->sector[0].sector;
if (cur_format>=SYSTEMFORMAT && cur_format<SYSTEMFORMAT+FORMATDIFF)
cur_format=SYSTEMFORMAT;
if (cur_format>=IBMFORMAT && cur_format<IBMFORMAT+FORMATDIFF)
cur_format=IBMFORMAT;
if (cur_format>=DATAFORMAT && cur_format<DATAFORMAT+FORMATDIFF)
cur_format=DATAFORMAT;
switch (cur_format) {
case SYSTEMFORMAT:
dpb=&DPB_store[SYSTEM_DPB]; update_dpb(dpb,track); break;
case IBMFORMAT: /* or Vortex format */
if (disk_header.nbof_heads==1) { /*sensible condition???*/
dpb=&DPB_store[IBM_DPB];
update_dpb(dpb,track);
} else {
dpb=&DPB_store[VORTEX_DPB];
update_dpb(dpb,track);
cur_format = VORTEXFORMAT;
}
break;
case DATAFORMAT:
dpb=&DPB_store[DATA_DPB]; update_dpb(dpb,track); break;
default:
errorf(FALSE,"Disk format not recognised!");
abandonimage();
return -1;
break;
}
/* calculate number of blocks and allocate memory */
blk_alloc = Malloc(dpb->DSM+1);
directory = (DirEntry*)Malloc(sizeof(DirEntry)*(dpb->DRM+1));
/* allocate block buffer */
block_buffer = (uchar*)Malloc(dpb->BLS);
/* get directory information */
get_directory();
calc_allocation();
return 0;
}
int comment_image(const char *text) {
/* ^^^^^^^^^^^^^
Place <text> in the comment field of the image and save the image
48 bytes tag = 8 bytes required + 40 bytes free */
int i;
memset(disk_header.tag+8,0,40);
i=0;
while (text[i] && i<40) {
*(disk_header.tag+8+i) = text[i];
i++;
}
lseek(imagefile,0L,SEEK_SET);
if (write(imagefile,&disk_header,sizeof(disk_header)) < 0) {
return errorf(TRUE,"--==>>> comment_image");
}
return 0;
}
/********
Blocks
********/
int get_free_block() {
/* ^^^^^^^^^^^^^^ */
static int next = 0;
int i;
if (next > dpb->DSM) next = 0;
/* try to allocate next block, if there was a previos one (next!=0) */
if ((next != 0) && (is_free_block(next))) return next++;
/* try to find the first free block */
for (i=dpb->DBL;i<=dpb->DSM;i++) {
if (is_free_block(i)) return i;
}
return -1;
}
/*****************
FS Maintenance
*****************/
struct {
int flag;
uchar type;
ushort load;
ushort jump;
ushort size;
ushort checksum;
} amsdos_header;
void get_amshead(int ent) {
/* ^^^^^^^^^^^
Read the first 128 bytes from the file and store them in the structure
<amsdos_header>. Set the <flag> to 2, if it's a valid header; to 1, if it's
invalid; and to 0 if the files is empty.
*/
int i;
ushort sum = 0;
uchar *buf;
if (directory[ent].blk[0] == 0) {
amsdos_header.flag = 0;
return;
}
buf = read_block((signed)directory[ent].blk[0]);
amsdos_header.type = buf[18];
amsdos_header.load = buf[21] + 256*buf[22];
amsdos_header.jump = buf[26] + 256*buf[27];
amsdos_header.size = buf[64] + 256*buf[65];
amsdos_header.checksum = buf[67] + 256*buf[68];
for (i=0;i<=66;i++) {
sum += buf[i];
}
if (sum==amsdos_header.checksum)
amsdos_header.flag = 2;
else
amsdos_header.flag = 1;
return;
}
/****** local function for dir() ******/
int cmp_array(int *x, int *y) {
/* ^^^^^^^^^
Compares two filenames, whose entry numbers are <x> and <y> */
int res;
if (directory[*x].user < directory[*y].user) res = -1;
else if (directory[*x].user > directory[*y].user) res = 1;
else {
res = strncmp((signed char*)directory[*x].root,
(signed char*)directory[*y].root, 8);
if (res==0)
res = strncmp((signed char*)directory[*x].ext,
(signed char*)directory[*y].ext, 3);
}
return res;
}
int dir(char *pat, int mask) {
/* ^^^
<mask> is a "bit-or" of the following bits:
DIR_DOUBLE default --00
DIR_WIDE only names and sizes --01
DIR_AMSHEAD incl. AMSDOS Header --10
DIR_LONG incl. entries, records, all attributes --11
DIR_SORT sorted -1--
*/
long total_bytes = 0;
int used_entries = 0;
int i,j;
int ent;
int files;
int mode;
int *array; /* temporary dynamic array */
char upbuffer[INPUTLEN]; /* buffer for upper case conversion*/
char *buf;
int user;
char root[INPUTLEN];
char ext[INPUTLEN];
array = (int*)Malloc(sizeof(int)*max(256,dpb->DRM+1));
parse_cpm_filename(pat,&user,root,ext);
if (user==-1) user = cur_user;
/* calculate active users, entries */
files = 0;
for (i=0;i<256;i++) array[i]=0;
for (i=0;i<=dpb->DRM;i++) {
if (directory[i].user != 0xE5) used_entries++;
if (directory[i].first) {
array[directory[i].user]++;
files++;
}
}
newpage("c");
/*** header ***/
strcpy(upbuffer,imagename);
upper(upbuffer);
printm(0,"Directory of Image: %s, User ", upbuffer);
if (user==-2) printm(0,"ALL\n\n");
else printm(0,"%-d\n\n",user);
nextline(); nextline();
/*** used users ***/
if (files>0) {
printm(0,"Used Users: ");
for (i=0;i<256;i++) {
if (array[i]!=0) {
printm(0,"%d with %d file%s \t",i,array[i],
plural(array[i]));
}
}
printm(0,"\n\n"); nextline(); nextline();
}
/*** files ***/
/* fetch all needed files */
/* fill the array with 64+1 large, but nonnegative values */
for (i=0;i<=dpb->DRM+1;i++) array[i]=0xFFF;
ent=glob_cpm_file(pat);
files=0;
while (ent>=0) {
array[files] = ent;
total_bytes += directory[ent].size;
ent=glob_cpm_next();
files++;
}
if (mask & DIR_SORT) qsort(array,files,sizeof(int),
(int(*)(const void*,const void*))cmp_array);
/* <array> contains now the entry numbers of the requested files in
the requested order */
/* output of the filenames */
if (files==0) {
printm(0,"No files\n");
goto footer;
}
i=0;
switch (mask&0x3) {
case DIR_DOUBLE:
printm(0," U Name Size Attr Ent "
"%c U Name Size Attr Ent\n",vert);
nextline();
printm(0,"%s%c",repstr(hori,39),cross);
printm(0,"%s\n",repstr(hori,37));
nextline();
while (array[i]<0xFFF) {
j=array[i]; ent=1;
while(directory[j].next>-1) {
ent++;
j=directory[j].next;
}
printm(0," %u %-12s %6lu %3s %c %3d ",
directory[array[i]].user,
directory[array[i]].name,
directory[array[i]].size,
show_attr(directory[array[i]].attr,ATTR_R,FALSE),
( directory[array[i]].attr&~ATTR_R? '+' : ' '),
ent);
if (i%2==0) printm(0," %c",vert);
else {putcharm(0,10); nextline();}
i++;
};
break;
case DIR_WIDE:
printm(0,"Name Size %c"
"Name Size %c"
"Name Size %c"
"Name Size\n",vert,vert,vert);
nextline();
printm(0,"%s%c",repstr(hori,19),cross);
printm(0,"%s%c",repstr(hori,19),cross);
printm(0,"%s%c",repstr(hori,19),cross);
printm(0,"%s\n",repstr(hori,19));
nextline();
while (array[i]<0xFFF) {
printm(0,"%-12s%6lu ",
directory[array[i]].name,
directory[array[i]].size);
if (i%4!=3) printm(0,"%c",vert);
else {putcharm(0,10); nextline();}
i++;
};
break;
case DIR_AMSHEAD:
printm(0," U Name Size Attr Amsdos-Header\n");
nextline();
printm(0,"%s\n",repstr(hori,74));
nextline();
while (array[i]<0xFFF) {
printm(0,"%2u %-12s %6lu %3s %c ",
directory[array[i]].user,
directory[array[i]].name,
directory[array[i]].size,
show_attr(directory[array[i]].attr,ATTR_R,FALSE),
( directory[array[i]].attr&~ATTR_R? '+' : ' '));
get_amshead(array[i]);
if (amsdos_header.flag==0) {