-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpmfs.c
1916 lines (1801 loc) · 53.3 KB
/
cpmfs.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
/* #includes */ /*{{{C}}}*//*{{{*/
#include "config.h"
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "cpmdir.h"
#include "cpmfs.h"
#ifdef USE_DMALLOC
#include <dmalloc.h>
#endif
/*}}}*/
/* #defines */ /*{{{*/
#undef CPMFS_DEBUG
/* Number of _used_ bits per int */
#define INTBITS ((int)(sizeof(int)*8))
/* Convert BCD datestamp digits to binary */
#define BCD2BIN(x) ((((x)>>4)&0xf)*10 + ((x)&0xf))
#define BIN2BCD(x) (((((x)/10)&0xf)<<4) + (((x)%10)&0xf))
/* There are four reserved directory entries: ., .., [passwd] and [label].
The first two of them refer to the same inode. */
#define RESERVED_ENTRIES 4
/* CP/M does not support any kind of inodes, so they are simulated.
Inode 0-(maxdir-1) correlate to the lowest extent number (not the first
extent of the file in the directory) of a file. Inode maxdir is the
root directory, inode maxdir+1 is the optional passwd file and inode
maxdir+2 the optional disk label. */
#define RESERVED_INODES 3
#define PASSWD_RECLEN 24
/*}}}*/
extern char **environ;
const char *boo;
static mode_t s_ifdir=1;
static mode_t s_ifreg=1;
/* memcpy7 -- Copy string, leaving 8th bit alone */ /*{{{*/
static void memcpy7(char *dest, const char *src, int count)
{
while (count--)
{
*dest = ((*dest) & 0x80) | ((*src) & 0x7F);
++dest;
++src;
}
}
/*}}}*/
/* file name conversions */
/* splitFilename -- split file name into name and extension */ /*{{{*/
static int splitFilename(const char *fullname, int type, char *name, char *ext, int *user)
{
int i,j;
assert(fullname!=(const char*)0);
assert(name!=(char*)0);
assert(ext!=(char*)0);
assert(user!=(int*)0);
memset(name,' ',8);
memset(ext,' ',3);
if (!isdigit(fullname[0]) || !isdigit(fullname[1]))
{
boo="illegal CP/M filename";
return -1;
}
*user=10*(fullname[0]-'0')+(fullname[1]-'0');
fullname+=2;
if ((fullname[0]=='\0') || *user>=((type&CPMFS_HI_USER) ? 32 : 16))
{
boo="illegal CP/M filename";
return -1;
}
for (i=0; i<8 && fullname[i] && fullname[i]!='.'; ++i) if (!ISFILECHAR(i,fullname[i]))
{
boo="illegal CP/M filename";
return -1;
}
else name[i]=toupper(fullname[i]);
if (fullname[i]=='.')
{
++i;
for (j=0; j<3 && fullname[i]; ++i,++j) if (!ISFILECHAR(1,fullname[i]))
{
boo="illegal CP/M filename";
return -1;
}
else ext[j]=toupper(fullname[i]);
if (i==1 && j==0)
{
boo="illegal CP/M filename";
return -1;
}
}
return 0;
}
/*}}}*/
/* isMatching -- do two file names match? */ /*{{{*/
static int isMatching(int user1, const char *name1, const char *ext1, int user2, const char *name2, const char *ext2)
{
int i;
assert(name1!=(const char*)0);
assert(ext1!=(const char*)0);
assert(name2!=(const char*)0);
assert(ext2!=(const char*)0);
if (user1!=user2) return 0;
for (i=0; i<8; ++i) if ((name1[i]&0x7f)!=(name2[i]&0x7f)) return 0;
for (i=0; i<3; ++i) if ((ext1[i]&0x7f)!=(ext2[i]&0x7f)) return 0;
return 1;
}
/*}}}*/
/* time conversions */
/* cpm2unix_time -- convert CP/M time to UTC */ /*{{{*/
static time_t cpm2unix_time(int days, int hour, int min)
{
/* CP/M stores timestamps in local time. We don't know which */
/* timezone was used and if DST was in effect. Assuming it was */
/* the current offset from UTC is most sensible, but not perfect. */
int year,days_per_year;
static int days_per_month[]={31,0,31,30,31,30,31,31,30,31,30,31};
char **old_environ;
static char gmt0[]="TZ=GMT0";
static char *gmt_env[]={ gmt0, (char*)0 };
struct tm tms;
time_t lt,t;
time(<);
t=lt;
tms=*localtime(<);
old_environ=environ;
environ=gmt_env;
lt=mktime(&tms);
lt-=t;
tms.tm_sec=0;
tms.tm_min=((min>>4)&0xf)*10+(min&0xf);
tms.tm_hour=((hour>>4)&0xf)*10+(hour&0xf);
tms.tm_mday=1;
tms.tm_mon=0;
tms.tm_year=78;
tms.tm_isdst=-1;
for (;;)
{
year=tms.tm_year+1900;
days_per_year=((year%4)==0 && ((year%100) || (year%400)==0)) ? 366 : 365;
if (days>days_per_year)
{
days-=days_per_year;
++tms.tm_year;
}
else break;
}
for (;;)
{
days_per_month[1]=(days_per_year==366) ? 29 : 28;
if (days>days_per_month[tms.tm_mon])
{
days-=days_per_month[tms.tm_mon];
++tms.tm_mon;
}
else break;
}
t=mktime(&tms)+(days-1)*24*3600;
environ=old_environ;
t-=lt;
return t;
}
/*}}}*/
/* unix2cpm_time -- convert UTC to CP/M time */ /*{{{*/
static void unix2cpm_time(time_t now, int *days, int *hour, int *min)
{
struct tm *tms;
int i;
tms=localtime(&now);
*min=((tms->tm_min/10)<<4)|(tms->tm_min%10);
*hour=((tms->tm_hour/10)<<4)|(tms->tm_hour%10);
for (i=1978,*days=0; i<1900+tms->tm_year; ++i)
{
*days+=365;
if (i%4==0 && (i%100!=0 || i%400==0)) ++*days;
}
*days += tms->tm_yday+1;
}
/*}}}*/
/* ds2unix_time -- convert DS to Unix time */ /*{{{*/
static time_t ds2unix_time(const struct dsEntry *entry)
{
struct tm tms;
int yr;
if (entry->minute==0 &&
entry->hour==0 &&
entry->day==0 &&
entry->month==0 &&
entry->year==0) return 0;
tms.tm_isdst = -1;
tms.tm_sec = 0;
tms.tm_min = BCD2BIN( entry->minute );
tms.tm_hour = BCD2BIN( entry->hour );
tms.tm_mday = BCD2BIN( entry->day );
tms.tm_mon = BCD2BIN( entry->month ) - 1;
yr = BCD2BIN(entry->year);
if (yr<70) yr+=100;
tms.tm_year = yr;
return mktime(&tms);
}
/*}}}*/
/* unix2ds_time -- convert Unix to DS time */ /*{{{*/
static void unix2ds_time(time_t now, struct dsEntry *entry)
{
struct tm *tms;
int yr;
if ( now==0 )
{
entry->minute=entry->hour=entry->day=entry->month=entry->year = 0;
}
else
{
tms=localtime(&now);
entry->minute = BIN2BCD( tms->tm_min );
entry->hour = BIN2BCD( tms->tm_hour );
entry->day = BIN2BCD( tms->tm_mday );
entry->month = BIN2BCD( tms->tm_mon + 1 );
yr = tms->tm_year;
if ( yr>100 ) yr -= 100;
entry->year = BIN2BCD( yr );
}
}
/*}}}*/
/* allocation vector bitmap functions */
/* alvInit -- init allocation vector */ /*{{{*/
static void alvInit(const struct cpmSuperBlock *d)
{
int i,j,offset,block;
assert(d!=(const struct cpmSuperBlock*)0);
/* clean bitmap */ /*{{{*/
memset(d->alv,0,d->alvSize*sizeof(int));
/*}}}*/
/* mark directory blocks as used */ /*{{{*/
*d->alv=(1<<((d->maxdir*32+d->blksiz-1)/d->blksiz))-1;
/*}}}*/
for (i=0; i<d->maxdir; ++i) /* mark file blocks as used */ /*{{{*/
{
if (d->dir[i].status>=0 && d->dir[i].status<=(d->type&CPMFS_HI_USER ? 31 : 15))
{
#ifdef CPMFS_DEBUG
fprintf(stderr,"alvInit: allocate extent %d\n",i);
#endif
for (j=0; j<16; ++j)
{
block=(unsigned char)d->dir[i].pointers[j];
if (d->size>=256) block+=(((unsigned char)d->dir[i].pointers[++j])<<8);
if (block && block<d->size)
{
#ifdef CPMFS_DEBUG
fprintf(stderr,"alvInit: allocate block %d\n",block);
#endif
offset=block/INTBITS;
d->alv[offset]|=(1<<block%INTBITS);
}
}
}
}
/*}}}*/
}
/*}}}*/
/* allocBlock -- allocate a new disk block */ /*{{{*/
static int allocBlock(const struct cpmSuperBlock *drive)
{
int i,j,bits,block;
assert(drive!=(const struct cpmSuperBlock*)0);
for (i=0; i<drive->alvSize; ++i)
{
for (j=0,bits=drive->alv[i]; j<INTBITS; ++j)
{
if ((bits&1)==0)
{
block=i*INTBITS+j;
if (block>=drive->size)
{
boo="device full";
return -1;
}
drive->alv[i] |= (1<<j);
return block;
}
bits >>= 1;
}
}
boo="device full";
return -1;
}
/*}}}*/
/* logical block I/O */
/* readBlock -- read a (partial) block */ /*{{{*/
static int readBlock(const struct cpmSuperBlock *d, int blockno, char *buffer, int start, int end)
{
int sect, track, counter;
assert(d);
assert(blockno>=0);
assert(buffer);
if (blockno>=d->size)
{
boo="Attempting to access block beyond end of disk";
return -1;
}
if (end<0) end=d->blksiz/d->secLength-1;
sect=(blockno*(d->blksiz/d->secLength)+ d->sectrk*d->boottrk)%d->sectrk;
track=(blockno*(d->blksiz/d->secLength)+ d->sectrk*d->boottrk)/d->sectrk;
for (counter=0; counter<=end; ++counter)
{
const char *err;
assert(d->skewtab[sect]>=0);
assert(d->skewtab[sect]<d->sectrk);
if (counter>=start && (err=Device_readSector(&d->dev,track,d->skewtab[sect],buffer+(d->secLength*counter))))
{
boo=err;
return -1;
}
++sect;
if (sect>=d->sectrk)
{
sect = 0;
++track;
}
}
return 0;
}
/*}}}*/
/* writeBlock -- write a (partial) block */ /*{{{*/
static int writeBlock(const struct cpmSuperBlock *d, int blockno, const char *buffer, int start, int end)
{
int sect, track, counter;
assert(blockno>=0);
assert(blockno<d->size);
assert(buffer!=(const char*)0);
if (end < 0) end=d->blksiz/d->secLength-1;
sect = (blockno*(d->blksiz/d->secLength))%d->sectrk;
track = (blockno*(d->blksiz/d->secLength))/d->sectrk+d->boottrk;
for (counter = 0; counter<=end; ++counter)
{
const char *err;
if (counter>=start && (err=Device_writeSector(&d->dev,track,d->skewtab[sect],buffer+(d->secLength*counter))))
{
boo=err;
return -1;
}
++sect;
if (sect>=d->sectrk)
{
sect=0;
++track;
}
}
return 0;
}
/*}}}*/
/* directory management */
/* findFileExtent -- find first/next extent for a file */ /*{{{*/
static int findFileExtent(const struct cpmSuperBlock *sb, int user, const char *name, const char *ext, int start, int extno)
{
boo="file already exists";
for (; start<sb->maxdir; ++start)
{
if
(
((unsigned char)sb->dir[start].status)<=(sb->type&CPMFS_HI_USER ? 31 : 15)
&& (extno==-1 || (EXTENT(sb->dir[start].extnol,sb->dir[start].extnoh)/sb->extents)==(extno/sb->extents))
&& isMatching(user,name,ext,sb->dir[start].status,sb->dir[start].name,sb->dir[start].ext)
) return start;
}
boo="file not found";
return -1;
}
/*}}}*/
/* findFreeExtent -- find first free extent */ /*{{{*/
static int findFreeExtent(const struct cpmSuperBlock *drive)
{
int i;
for (i=0; i<drive->maxdir; ++i) if (drive->dir[i].status==(char)0xe5) return (i);
boo="directory full";
return -1;
}
/*}}}*/
/* updateTimeStamps -- convert time stamps to CP/M format */ /*{{{*/
static void updateTimeStamps(const struct cpmInode *ino, int extent)
{
struct PhysDirectoryEntry *date;
int i;
int ca_min,ca_hour,ca_days,u_min,u_hour,u_days;
if (!S_ISREG(ino->mode)) return;
#ifdef CPMFS_DEBUG
fprintf(stderr,"CPMFS: updating time stamps for inode %d (%d)\n",extent,extent&3);
#endif
unix2cpm_time(ino->sb->cnotatime ? ino->ctime : ino->atime,&ca_days,&ca_hour,&ca_min);
unix2cpm_time(ino->mtime,&u_days,&u_hour,&u_min);
if ((ino->sb->type&CPMFS_CPM3_DATES) && (date=ino->sb->dir+(extent|3))->status==0x21)
{
ino->sb->dirtyDirectory=1;
switch (extent&3)
{
case 0: /* first entry */ /*{{{*/
{
date->name[0]=ca_days&0xff; date->name[1]=ca_days>>8;
date->name[2]=ca_hour;
date->name[3]=ca_min;
date->name[4]=u_days&0xff; date->name[5]=u_days>>8;
date->name[6]=u_hour;
date->name[7]=u_min;
break;
}
/*}}}*/
case 1: /* second entry */ /*{{{*/
{
date->ext[2]=ca_days&0xff; date->extnol=ca_days>>8;
date->lrc=ca_hour;
date->extnoh=ca_min;
date->blkcnt=u_days&0xff; date->pointers[0]=u_days>>8;
date->pointers[1]=u_hour;
date->pointers[2]=u_min;
break;
}
/*}}}*/
case 2: /* third entry */ /*{{{*/
{
date->pointers[5]=ca_days&0xff; date->pointers[6]=ca_days>>8;
date->pointers[7]=ca_hour;
date->pointers[8]=ca_min;
date->pointers[9]=u_days&0xff; date->pointers[10]=u_days>>8;
date->pointers[11]=u_hour;
date->pointers[12]=u_min;
break;
}
/*}}}*/
}
}
}
/*}}}*/
/* updateDsStamps -- set time in datestamper file */ /*{{{*/
static void updateDsStamps(const struct cpmInode *ino, int extent)
{
int yr;
struct tm *cpm_time;
struct dsDate *stamp;
if (!S_ISREG(ino->mode)) return;
if ( !(ino->sb->type&CPMFS_DS_DATES) ) return;
#ifdef CPMFS_DEBUG
fprintf(stderr,"CPMFS: updating ds stamps for inode %d (%d)\n",extent,extent&3);
#endif
/* Get datestamp struct */
stamp = ino->sb->ds+extent;
unix2ds_time( ino->mtime, &stamp->modify );
unix2ds_time( ino->ctime, &stamp->create );
unix2ds_time( ino->atime, &stamp->access );
ino->sb->dirtyDs = 1;
}
/*}}}*/
/* readTimeStamps -- read CP/M time stamp */ /*{{{*/
static int readTimeStamps(struct cpmInode *i, int lowestExt)
{
/* variables */ /*{{{*/
struct PhysDirectoryEntry *date;
int u_days=0,u_hour=0,u_min=0;
int ca_days=0,ca_hour=0,ca_min=0;
int protectMode=0;
/*}}}*/
if ( (i->sb->type&CPMFS_CPM3_DATES) && (date=i->sb->dir+(lowestExt|3))->status==0x21 )
{
switch (lowestExt&3)
{
case 0: /* first entry of the four */ /*{{{*/
{
ca_days=((unsigned char)date->name[0])+(((unsigned char)date->name[1])<<8);
ca_hour=(unsigned char)date->name[2];
ca_min=(unsigned char)date->name[3];
u_days=((unsigned char)date->name[4])+(((unsigned char)date->name[5])<<8);
u_hour=(unsigned char)date->name[6];
u_min=(unsigned char)date->name[7];
protectMode=(unsigned char)date->ext[0];
break;
}
/*}}}*/
case 1: /* second entry */ /*{{{*/
{
ca_days=((unsigned char)date->ext[2])+(((unsigned char)date->extnol)<<8);
ca_hour=(unsigned char)date->lrc;
ca_min=(unsigned char)date->extnoh;
u_days=((unsigned char)date->blkcnt)+(((unsigned char)date->pointers[0])<<8);
u_hour=(unsigned char)date->pointers[1];
u_min=(unsigned char)date->pointers[2];
protectMode=(unsigned char)date->pointers[3];
break;
}
/*}}}*/
case 2: /* third one */ /*{{{*/
{
ca_days=((unsigned char)date->pointers[5])+(((unsigned char)date->pointers[6])<<8);
ca_hour=(unsigned char)date->pointers[7];
ca_min=(unsigned char)date->pointers[8];
u_days=((unsigned char)date->pointers[9])+(((unsigned char)date->pointers[10])<<8);
u_hour=(unsigned char)date->pointers[11];
u_min=(unsigned char)date->pointers[12];
protectMode=(unsigned char)date->pointers[13];
break;
}
/*}}}*/
}
if (i->sb->cnotatime)
{
i->ctime=cpm2unix_time(ca_days,ca_hour,ca_min);
i->atime=0;
}
else
{
i->ctime=0;
i->atime=cpm2unix_time(ca_days,ca_hour,ca_min);
}
i->mtime=cpm2unix_time(u_days,u_hour,u_min);
}
else
{
i->atime=i->mtime=i->ctime=0;
protectMode=0;
}
return protectMode;
}
/*}}}*/
/* readDsStamps -- read datestamper time stamp */ /*{{{*/
static void readDsStamps(struct cpmInode *i, int lowestExt)
{
struct dsDate *stamp;
if ( !(i->sb->type&CPMFS_DS_DATES) ) return;
/* Get datestamp */
stamp = i->sb->ds+lowestExt;
i->mtime = ds2unix_time(&stamp->modify);
i->ctime = ds2unix_time(&stamp->create);
i->atime = ds2unix_time(&stamp->access);
}
/*}}}*/
/* match -- match filename against a pattern */ /*{{{*/
static int recmatch(const char *a, const char *pattern)
{
int first=1;
assert(a);
assert(pattern);
while (*pattern)
{
switch (*pattern)
{
case '*':
{
if (*a=='.' && first) return 1;
++pattern;
while (*a) if (recmatch(a,pattern)) return 1; else ++a;
break;
}
case '?':
{
if (*a) { ++a; ++pattern; } else return 0;
break;
}
default: if (tolower(*a)==tolower(*pattern)) { ++a; ++pattern; } else return 0;
}
first=0;
}
return (*pattern=='\0' && *a=='\0');
}
int match(const char *a, const char *pattern)
{
int user;
char pat[255];
assert(a);
assert(pattern);
assert(strlen(pattern)<255);
if (isdigit(*pattern) && *(pattern+1)==':') { user=(*pattern-'0'); pattern+=2; }
else if (isdigit(*pattern) && isdigit(*(pattern+1)) && *(pattern+2)==':') { user=(10*(*pattern-'0')+(*(pattern+1)-'0')); pattern+=3; }
else user=-1;
if (user==-1) sprintf(pat,"??%s",pattern);
else sprintf(pat,"%02d%s",user,pattern);
return recmatch(a,pat);
}
/*}}}*/
/* cpmglob -- expand CP/M style wildcards */ /*{{{*/
void cpmglob(int optin, int argc, char * const argv[], struct cpmInode *root, int *gargc, char ***gargv)
{
struct cpmFile dir;
int entries,dirsize=0;
struct cpmDirent *dirent=(struct cpmDirent*)0;
int gargcap=0,i,j;
*gargv=(char**)0;
*gargc=0;
cpmOpendir(root,&dir);
entries=0;
dirsize=8;
dirent=malloc(sizeof(struct cpmDirent)*dirsize);
while (cpmReaddir(&dir,&dirent[entries]))
{
++entries;
if (entries==dirsize) dirent=realloc(dirent,sizeof(struct cpmDirent)*(dirsize*=2));
}
for (i=optin; i<argc; ++i)
{
int found;
for (j=0,found=0; j<entries; ++j)
{
if (match(dirent[j].name,argv[i]))
{
if (*gargc==gargcap) *gargv=realloc(*gargv,sizeof(char*)*(gargcap ? (gargcap*=2) : (gargcap=16)));
(*gargv)[*gargc]=strcpy(malloc(strlen(dirent[j].name)+1),dirent[j].name);
++*gargc;
++found;
}
}
}
free(dirent);
}
/*}}}*/
/* superblock management */
/* diskdefReadSuper -- read super block from diskdefs file */ /*{{{*/
static int diskdefReadSuper(struct cpmSuperBlock *d, const char *format)
{
char line[256];
FILE *fp;
int insideDef=0,found=0;
d->libdskGeometry[0] = '\0';
d->type=0;
if ((fp=fopen("diskdefs","r"))==(FILE*)0 && (fp=fopen(DISKDEFS,"r"))==(FILE*)0)
{
fprintf(stderr,"%s: Neither `diskdefs' nor `" DISKDEFS "' could be opened.\n",cmd);
exit(1);
}
while (fgets(line,sizeof(line),fp)!=(char*)0)
{
int argc;
char *argv[2];
char *s;
/* Allow inline comments preceded by ; or # */
s = strchr(line, '#');
if (s) strcpy(s, "\n");
s = strchr(line, ';');
if (s) strcpy(s, "\n");
for (argc=0; argc<1 && (argv[argc]=strtok(argc ? (char*)0 : line," \t\n")); ++argc);
if ((argv[argc]=strtok((char*)0,"\n"))!=(char*)0) ++argc;
if (insideDef)
{
if (argc==1 && strcmp(argv[0],"end")==0)
{
insideDef=0;
d->size=(d->secLength*d->sectrk*(d->tracks-d->boottrk))/d->blksiz;
if (d->extents==0) d->extents=((d->size>=256 ? 8 : 16)*d->blksiz)/16384;
if (d->extents==0) d->extents=1;
if (found) break;
}
else if (argc==2)
{
if (strcmp(argv[0],"seclen")==0) d->secLength=strtol(argv[1],(char**)0,0);
else if (strcmp(argv[0],"tracks")==0) d->tracks=strtol(argv[1],(char**)0,0);
else if (strcmp(argv[0],"sectrk")==0) d->sectrk=strtol(argv[1],(char**)0,0);
else if (strcmp(argv[0],"blocksize")==0) d->blksiz=strtol(argv[1],(char**)0,0);
else if (strcmp(argv[0],"maxdir")==0) d->maxdir=strtol(argv[1],(char**)0,0);
else if (strcmp(argv[0],"skew")==0) d->skew=strtol(argv[1],(char**)0,0);
else if (strcmp(argv[0],"skewtab")==0)
{
int pass,sectors;
for (pass=0; pass<2; ++pass)
{
sectors=0;
for (s=argv[1]; *s; )
{
int phys;
char *end;
phys=strtol(s,&end,10);
if (pass==1) d->skewtab[sectors]=phys;
if (end==s)
{
fprintf(stderr,"%s: invalid skewtab `%s' at `%s'\n",cmd,argv[1],s);
exit(1);
}
s=end;
++sectors;
if (*s==',') ++s;
}
if (pass==0) d->skewtab=malloc(sizeof(int)*sectors);
}
}
else if (strcmp(argv[0],"boottrk")==0) d->boottrk=strtol(argv[1],(char**)0,0);
else if (strcmp(argv[0],"offset")==0)
{
off_t val;
unsigned int multiplier;
char *endptr;
errno=0;
multiplier=1;
val = strtol(argv[1],&endptr,10);
if ((errno==ERANGE && val==LONG_MAX)||(errno!=0 && val<=0))
{
fprintf(stderr,"%s: invalid offset value \"%s\" - %s\n",cmd,argv[1],strerror(errno));
exit(1);
}
if (endptr==argv[1])
{
fprintf(stderr,"%s: offset value \"%s\" is not a number\n",cmd,argv[1]);
exit(1);
}
if (*endptr!='\0')
{
/* Have a unit specifier */
switch (toupper(*endptr))
{
case 'K':
multiplier=1024;
break;
case 'M':
multiplier=1024*1024;
break;
case 'T':
if (d->sectrk<0||d->tracks<0||d->secLength<0)
{
fprintf(stderr,"%s: offset must be specified after sectrk, tracks and secLength\n",cmd);
exit(1);
}
multiplier=d->sectrk*d->secLength;
break;
case 'S':
if (d->sectrk<0||d->tracks<0||d->secLength<0)
{
fprintf(stderr,"%s: offset must be specified after sectrk, tracks and secLength\n",cmd);
exit(1);
}
multiplier=d->secLength;
break;
default:
fprintf(stderr,"%s: unknown unit specifier \"%c\"\n",cmd,*endptr);
exit(1);
}
}
if (val*multiplier>INT_MAX)
{
fprintf(stderr,"%s: effective offset is out of range\n",cmd);
exit(1);
}
d->offset=val*multiplier;
}
else if (strcmp(argv[0],"logicalextents")==0) d->extents=strtol(argv[1],(char**)0,0);
else if (strcmp(argv[0],"os")==0)
{
if (strcmp(argv[1],"2.2" )==0) d->type|=CPMFS_DR22;
else if (strcmp(argv[1],"3" )==0) d->type|=CPMFS_DR3;
else if (strcmp(argv[1],"isx" )==0) d->type|=CPMFS_ISX;
else if (strcmp(argv[1],"p2dos")==0) d->type|=CPMFS_P2DOS;
else if (strcmp(argv[1],"zsys" )==0) d->type|=CPMFS_ZSYS;
else
{
fprintf(stderr, "%s: invalid OS type `%s'\n", cmd, argv[1]);
exit(1);
}
}
else if (strcmp(argv[0], "libdsk:format")==0)
{
strncpy(d->libdskGeometry, argv[1], sizeof(d->libdskGeometry) - 1);
d->libdskGeometry[sizeof(d->libdskGeometry) - 1] = 0;
}
}
else if (argc>0 && argv[0][0]!='#' && argv[0][0]!=';')
{
fprintf(stderr,"%s: invalid keyword `%s'\n",cmd,argv[0]);
exit(1);
}
}
else if (argc==2 && strcmp(argv[0],"diskdef")==0)
{
insideDef=1;
d->skew=1;
d->extents=0;
d->type=CPMFS_DR22;
d->skewtab=(int*)0;
d->offset=0;
d->boottrk=d->secLength=d->sectrk=d->tracks=-1;
d->libdskGeometry[0] = 0;
if (strcmp(argv[1],format)==0) found=1;
}
}
fclose(fp);
if (!found)
{
fprintf(stderr,"%s: unknown format %s\n",cmd,format);
exit(1);
}
if (d->boottrk<0)
{
fprintf(stderr, "%s: boottrk parameter invalid or missing from diskdef\n",cmd);
exit(1);
}
if (d->secLength<0)
{
fprintf(stderr, "%s: secLength parameter invalid or missing from diskdef\n",cmd);
exit(1);
}
if (d->sectrk<0)
{
fprintf(stderr, "%s: sectrk parameter invalid or missing from diskdef\n",cmd);
exit(1);
}
if (d->tracks<0)
{
fprintf(stderr, "%s: tracks parameter invalid or missing from diskdef\n",cmd);
exit(1);
}
return 0;
}
/*}}}*/
/* amsReadSuper -- read super block from amstrad disk */ /*{{{*/
static int amsReadSuper(struct cpmSuperBlock *d, const char *format)
{
unsigned char boot_sector[512], *boot_spec;
const char *err;
Device_setGeometry(&d->dev,512,9,40,0,"pcw180");
if ((err=Device_readSector(&d->dev, 0, 0, (char *)boot_sector)))
{
fprintf(stderr,"%s: Failed to read Amstrad superblock (%s)\n",cmd,err);
exit(1);
}
boot_spec=(boot_sector[0] == 0 || boot_sector[0] == 3)?boot_sector:(unsigned char*)0;
/* Check for JCE's extension to allow Amstrad and MSDOS superblocks
* in the same sector (for the PCW16)
*/
if
(
(boot_sector[0] == 0xE9 || boot_sector[0] == 0xEB)
&& !memcmp(boot_sector + 0x2B, "CP/M", 4)
&& !memcmp(boot_sector + 0x33, "DSK", 3)
&& !memcmp(boot_sector + 0x7C, "CP/M", 4)
) boot_spec = boot_sector + 128;
if (boot_spec==(unsigned char*)0)
{
fprintf(stderr,"%s: Amstrad superblock not present\n",cmd);
exit(1);
}
/* boot_spec[0] = format number: 0 for SS SD, 3 for DS DD
[1] = single/double sided and density flags
[2] = cylinders per side
[3] = sectors per cylinder
[4] = Physical sector shift, 2 => 512
[5] = Reserved track count
[6] = Block shift
[7] = No. of directory blocks
*/
d->type = 0;
d->type |= CPMFS_DR3; /* Amstrads are CP/M 3 systems */
d->secLength = 128 << boot_spec[4];
d->tracks = boot_spec[2];
if (boot_spec[1] & 3) d->tracks *= 2;
d->sectrk = boot_spec[3];
d->blksiz = 128 << boot_spec[6];
d->maxdir = (d->blksiz / 32) * boot_spec[7];
d->skew = 1; /* Amstrads skew at the controller level */
d->skewtab = (int*)0;
d->boottrk = boot_spec[5];
d->offset = 0;
d->size = (d->secLength*d->sectrk*(d->tracks-d->boottrk))/d->blksiz;
d->extents = ((d->size>=256 ? 8 : 16)*d->blksiz)/16384;
d->libdskGeometry[0] = 0; /* LibDsk can recognise an Amstrad superblock
* and autodect */
return 0;
}
/*}}}*/
/* cpmCheckDs -- read all datestamper timestamps */ /*{{{*/
int cpmCheckDs(struct cpmSuperBlock *sb)
{
int dsoffset, dsblks, dsrecs, off, i;
unsigned char *buf;
if (!isMatching(0,"!!!TIME&","DAT",sb->dir->status,sb->dir->name,sb->dir->ext)) return -1;
/* Offset to ds file in alloc blocks */
dsoffset=(sb->maxdir*32+(sb->blksiz-1))/sb->blksiz;
dsrecs=(sb->maxdir+7)/8;
dsblks=(dsrecs*128+(sb->blksiz-1))/sb->blksiz;
/* Allocate buffer */
sb->ds=malloc(dsblks*sb->blksiz);
/* Read ds file in its entirety */
off=0;
for (i=dsoffset; i<dsoffset+dsblks; i++)
{
if (readBlock(sb,i,((char*)sb->ds)+off,0,-1)==-1) return -1;
off+=sb->blksiz;
}
/* Verify checksums */
buf = (unsigned char *)sb->ds;
for (i=0; i<dsrecs; i++)
{
unsigned cksum, j;
cksum=0;
for (j=0; j<127; j++) cksum += buf[j];
if (buf[j]!=(cksum&0xff))
{
#ifdef CPMFS_DEBUG
fprintf( stderr, "!!!TIME&.DAT file failed cksum at record %i\n", i );
#endif
free(sb->ds);
sb->ds = (struct dsDate *)0;
return -1;
}
buf += 128;
}
return 0;
}
/*}}}*/
/* cpmReadSuper -- get DPB and init in-core data for drive */ /*{{{*/
int cpmReadSuper(struct cpmSuperBlock *d, struct cpmInode *root, const char *format)
{
while (s_ifdir && !S_ISDIR(s_ifdir)) s_ifdir<<=1;
assert(s_ifdir);
while (s_ifreg && !S_ISREG(s_ifreg)) s_ifreg<<=1;
assert(s_ifreg);
if (strcmp(format,"amstrad")==0) amsReadSuper(d,format);
else diskdefReadSuper(d,format);
boo = Device_setGeometry(&d->dev,d->secLength,d->sectrk,d->tracks,d->offset,d->libdskGeometry);
if (boo) return -1;
if (d->skewtab==(int*)0) /* generate skew table */ /*{{{*/
{
int i,j,k;
if (( d->skewtab = malloc(d->sectrk*sizeof(int))) == (int*)0)
{
boo=strerror(errno);
return -1;
}
memset(d->skewtab,0,d->sectrk*sizeof(int));
for (i=j=0; i<d->sectrk; ++i,j=(j+d->skew)%d->sectrk)
{
while (1)
{
assert(i<d->sectrk);