-
Notifications
You must be signed in to change notification settings - Fork 27
/
cpmtool.c
992 lines (897 loc) · 33.1 KB
/
cpmtool.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
/* CP/M disk */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SECTOR_SIZE 128
/* Layout is:
* Zero or more reserved tracks (off)
* One or more data blocks (dsm+1), power of 2 at least 1K
* Directory starts with block 0
* Directory has (drm+1) entries
* Spare sectors (ignored by CP/M)
*
* CP/M 1.4 on 8 inch 250.25K disk:
* 77 tracks
* 26 128-byte sectors per track, software skewed
* skew table [1,7,13,19,25,5,11,17,23,3,9,15,21,2,8,14,20,26,6,12,18,24,4,10,16,22]
* 2 reserved tracks
* 2 1K directory blocks, giving 64 directory entries
* 243 1K data blocks numbered 2 - 242
* 6 extra sectors
*/
struct cpm_dpb {
unsigned short spt; /* 128 byte sectors per track (26) */
unsigned char bsh; /* Block shift: 3 = 1K, 4 = 2K, etc. (3) */
unsigned char blm; /* Block mask: 0x7 = 1K, 0xF = 2K, etc. (7) */
unsigned char exm; /* Extent mask: full record count = 128 * (EX & exm) + rc */
unsigned short dsm; /* Number of blocks on the disk - 1 */
unsigned short drm; /* Number of directory entries - 1 */
unsigned char al0; /* Directory allocation bitmap, first byte */
unsigned char al1; /* Directory allocation bitmap, second byte */
unsigned char cks; /* Checksum vector size: 0 for fixed disk */
unsigned short off; /* Offset, number of reserved tracks */
unsigned char skew[64]; /* Skew table */
};
struct cpm_dpb dpb_fd = {
/* SPT */ 26,
/* BSH */ 3,
/* BLM */ 7,
/* EXM */ 0,
/* DSM */ 242,
/* DRM */ 63,
/* AL0 */ 192,
/* AL1 */ 0,
/* CKS */ 16,
/* OFF */ 2,
/* Skew */ { 0, 6, 12, 18, 24, 4, 10, 16, 22, 2, 8, 14, 20,
1, 7, 13, 19, 25, 5, 11, 17, 23, 3, 9, 15, 21 }
};
struct cpm_dpb dpb_hd = {
/* SPT */ 64,
/* BSH */ 4,
/* BLM */ 15,
/* EXM */ 0,
/* DSM */ 2441,
/* DRM */ 1023,
/* AL0 */ 255,
/* AL1 */ 255,
/* CKS */ 0,
/* OFF */ 2,
/* Skew */ { 0, 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 }
};
/* Pointer to drive parameter block */
struct cpm_dpb *dpb = &dpb_fd;
/* Starting sector of directory */
#define SECTOR_DIR 0
/* Number of directory sectors */
#define SECTOR_DIR_SIZE ((dpb->drm + 1) * 32 / SECTOR_SIZE)
/* Sectors per block */
#define SECTORS_PER_BLOCK (1 << dpb->bsh)
/* True for a big disk: use two bytes for allocation map entries */
#define BIG_DISK (dpb->dsm > 255)
/* Max sectors for one extent */
#define SECTORS_PER_EXTENT ((BIG_DISK ? 8 : 16) * SECTORS_PER_BLOCK)
/* Blocks per extent */
#define BLOCKS_PER_EXTENT (BIG_DISK ? 8 : 16)
/* Compute extent number of a directory entry */
#define EXTENT_NO(d) (((0x1f & (d)->ex) + 32 * (d)->s2) / (dpb->exm + 1))
/* Compute record count of a directory entry */
#define RC(d) (128 * ((d)->ex & dpb->exm) + (d)->rc)
/* Directory entry size */
#define ENTRY_SIZE 32
/* CP/M 2.2 disk parameter block offsets */
#define CPM_WORD_DPB_SPT 0
#define CPM_BYTE_DPB_BSH 2
#define CPM_BYTE_DPB_BLM 3
#define CPM_BYTE_DPB_EXM 4
#define CPM_WORD_DPB_DSM 5
#define CPM_WORD_DPB_DRM 7
#define CPM_BYTE_DPB_AL0 9
#define CPM_BYTE_DPB_AL1 10
#define CPM_WORD_DPB_CKS 11
#define CPM_WORD_DPB_OFF 13
/* CP/M 2.2 directory entry offsets */
#define CPM_BYTE_DIR_UU 0
#define CPM_BYTE8_DIR_F 1
#define CPM_BYTE3_DIR_T 9
#define CPM_BYTE_DIR_EX 12
#define CPM_BYTE_DIR_S1 13
#define CPM_BYTE_DIR_S2 14
#define CPM_BYTE_DIR_RC 15
#define CPM_BYTE16_DIR_AL 16
struct dirent {
unsigned char uu; /* User number: 0xE5 means deleted */
unsigned char f[8]; /* File name */
unsigned char t[3]; /* File type t[0].7=readonly, t[1].7=hidden */
unsigned char ex; /* Extent counter: 0-31 */
unsigned char s1; /* Reserved, set to 0 */
unsigned char s2; /* Extent counter high byte */
unsigned char rc; /* Number of records in this extent (0x80 for full extent) */
unsigned char al[16]; /* Allocation map: 0 means free, otherwise block number */
};
/* Extent number is (32*s2 + ex) / (exm+1)
* Record count is (ex & exm)*128 + rc
* Basically non-zero exm means extra rc bits are stuffed into the extent number
* This could be needed to handle large block sizes, where there could be more than
* 128 records in a single extent.
*/
FILE *disk;
/* Get sector. Skip reserved tracks. Deskew. */
void getsect(unsigned char *buf, int sect)
{
int track = sect / dpb->spt;
int sector = sect % dpb->spt;
sector = dpb->skew[sector]; /* De-interleave sectors */
track += dpb->off; /* Skip over reserved tracks */
fseek(disk, (track * dpb->spt + sector) * SECTOR_SIZE, SEEK_SET);
fread((char *)buf, SECTOR_SIZE, 1, disk);
}
void putsect(unsigned char *buf, int sect)
{
int track = sect / dpb->spt;
int sector = sect % dpb->spt;
sector = dpb->skew[sector]; /* De-interleave sectors */
track += dpb->off; /* Skip over reserved tracks */
fseek(disk, (track * dpb->spt + sector) * SECTOR_SIZE, SEEK_SET);
fwrite((char *)buf, SECTOR_SIZE, 1, disk);
}
int lower(int c)
{
if (c >= 'A' && c <= 'Z')
return c - 'A' + 'a';
else
return c;
}
/* Convert file name from directory into UNIX zero-terminated C string name */
char *getname(struct dirent *d)
{
static char s[50];
int p = 0;
int r;
int i;
/* Get name */
for (i = 0; i != sizeof(d->f); i++) {
s[p++] = lower(d->f[i]);
}
/* Zap trailing spaces */
while (p && s[p - 1] == ' ') --p;
/* Append '.' */
s[p++] = '.';
r = p;
/* Get extension */
for (i = 0; i != sizeof(d->t); i++) {
s[p++] = lower(d->t[i]);
}
/* Zap tailing spaces */
while (p && s[p - 1] == ' ') --p;
/* Zap '.' if no extension */
if (p == r) --p;
/* Terminate */
s[p] = 0;
return s;
}
/* Write UNIX name into Atari directory entry */
void putname(struct dirent *d, char *name)
{
int x;
/* Copy file name into directory entry */
x = 0;
while (*name && *name != '.' && x < 8) {
if (*name >= 'a' && *name <= 'z')
d->f[x++] = *name++ - 'a' + 'A';
else
d->f[x++] = *name++;
}
while (x < 8) {
d->f[x++] = ' ';
}
x = 0;
while (*name && *name != '.')
++name;
if (*name == '.') {
++name;
while (*name && x < 3) {
if (*name >= 'a' && *name <= 'z')
d->t[x++] = *name++ - 'a' + 'A';
else
d->t[x++] = *name++;
}
}
while (x < 3) {
d->t[x++] = ' ';
}
}
/* Internal version of name for nice listing */
struct name
{
char *name;
/* From directory entry */
int locked; /* Set if write-protected */
int sector; /* Starting sector of file */
int sects; /* Sector count */
int is_sys; /* Set if it's a .SYS file */
int is_cm; /* Set if it's a .COM file */
/* From file itself */
int load_start;
int load_size;
int init;
int run;
int size;
};
struct name *names[1024];
int name_n;
int comp(struct name **l, struct name **r)
{
return strcmp((*l)->name, (*r)->name);
}
/* Return list of free directory entries (extents) for a file */
int alloc_extents(int *list, int extents)
{
unsigned char buf[SECTOR_SIZE];
int x;
for (x = SECTOR_DIR; x != SECTOR_DIR + SECTOR_DIR_SIZE; ++x) {
int y;
getsect(buf, x);
for (y = 0; y != SECTOR_SIZE; y += ENTRY_SIZE) {
struct dirent *d = (struct dirent *)(buf + y);
if (d->uu == 0xE5) {
*list++ = x * (SECTOR_SIZE / ENTRY_SIZE) + (y / ENTRY_SIZE);
if (!--extents)
return 0;
}
}
}
return -1;
}
/* get allocation map (index by block number) */
unsigned short *alloc_map; /* 0xFFFF is free, 0xFFFE allocated for directory, otherwise entry no. */
void get_map()
{
unsigned char buf[SECTOR_SIZE];
int x;
int entry_no;
if (!alloc_map) {
alloc_map = malloc(sizeof(alloc_map[0]) * (dpb->dsm + 1));
/* Initialize to all free */
for (x = 0; x != dpb->dsm + 1; ++x)
alloc_map[x] = 0xFFFF;
/* Reserve space for directory */
for (x = 0; x != (dpb->drm + 1) / ((SECTOR_SIZE << dpb->bsh) / ENTRY_SIZE); ++x)
alloc_map[x] = 0xFFFE;
}
entry_no = 0;
for (x = 0; x != SECTOR_DIR_SIZE; ++x) {
int y;
/* fprintf(stderr, "sector %d\n", x); */
getsect(buf, x + SECTOR_DIR);
for (y = 0; y != SECTOR_SIZE; y += ENTRY_SIZE) {
struct dirent *d = (struct dirent *)(buf + y);
if (d->uu < 0x20) { /* d->uu != 0xe5 (date stamp is 0x21) */
int z;
/*
char *s = getname(d);
printf("%d %s\n", entry_no, s); */
for (z = 0; z != 16; ++z) {
int blk;
if (BIG_DISK) {
blk = d->al[z] + (256 * d->al[z + 1]);
++z;
} else {
blk = d->al[z];
}
if (blk) {
if (blk >= dpb->dsm + 1) {
fprintf(stderr, "Entry %d: Found block number (%d) exceeding device size\n", entry_no, blk);
} else if (alloc_map[blk] != 0xFFFF) {
fprintf(stderr, "Entry %d: Found doubly allocated block number (%d) by entry %d\n", entry_no, blk, alloc_map[blk]);
} else {
/* Record directory entry number */
/* printf("setting %d\n", d->al[z]); */
alloc_map[blk] = entry_no;
}
} else {
break;
}
}
}
++entry_no;
}
}
}
/* Allocate a block. Returns block number or -1 for out of space. */
int alloc_block(int entry_no)
{
int x;
if (!alloc_map)
get_map();
for (x = 0; x != (dpb->dsm + 1); ++x)
if (alloc_map[x] == 0xFFFF) {
alloc_map[x] = entry_no;
return x;
}
return -1;
}
/* Count free blocks */
int amount_free(void)
{
int count = 0;
int x;
if (!alloc_map)
get_map();
for (x = 0; x != (dpb->dsm + 1); ++x)
if (alloc_map[x] == 0xFFFF) {
++count;
}
return count;
}
/* Get directory entry with specific name and extent number
* (or delete all entries with specified name if del is set)
* Returns 0 if found, -1 if not found.
*/
int find_file(struct dirent *dir, char *filename, int ex, int del)
{
unsigned char buf[SECTOR_SIZE];
int x;
int flg = -1;
for (x = 0; x != SECTOR_DIR_SIZE; ++x) {
int y;
getsect(buf, x + SECTOR_DIR);
for (y = 0; y != SECTOR_SIZE; y += ENTRY_SIZE) {
struct dirent *d = (struct dirent *)(buf + y);
if (d->uu < 0x20 && (del || ex == EXTENT_NO(d))) {
char *s = getname(d);
if (!strcmp(s, filename)) {
if (del) {
d->uu = 0xe5;
flg = 0;
putsect(buf, x + SECTOR_DIR);
} else {
memcpy(dir, d, ENTRY_SIZE);
flg = 0;
return 0;
}
}
}
}
}
return flg;
}
/* Read a file: provide with first extent */
int read_file(char *filename, struct dirent *dir, FILE *f)
{
int rtn = 0;
unsigned char buf[SECTOR_SIZE];
int exno = 0; /* Extent number */
for (;;) {
int recno; /* Record number within extent */
for (recno = 0; recno != RC(dir) && recno != SECTORS_PER_EXTENT; ++recno) {
int blkno; /* Block number within extent */
int recblk; /* Record number within block */
int blk; /* Current block */
blkno = (recno >> dpb->bsh);
recblk = (recno & dpb->blm);
if (BIG_DISK)
blk = dir->al[blkno * 2] + 256 * dir->al[blkno * 2 + 1];
else
blk = dir->al[blkno];
if (blk) {
getsect(buf, (blk << dpb->bsh) + recblk);
fwrite(buf, SECTOR_SIZE, 1, f);
} else {
fprintf(stderr, "allocation map ran out before cr count reached!\n");
rtn = -1;
break;
}
}
if (RC(dir) != SECTORS_PER_EXTENT) {
break;
} else {
++exno;
if (find_file(dir, filename, exno, 0)) {
/* fprintf(stderr, "can't find next extent!\n");
rtn = -1; */
/* This is normal for case where file is maximum extent size! */
break;
}
}
}
return rtn;
}
/* cat a file */
void cat(char *name)
{
struct dirent dir[1];
if (find_file(dir, name, 0, 0)) {
printf("File '%s' not found\n", name);
exit(-1);
} else {
/* printf("Found file. Sector of rib is %d\n", sector); */
read_file(name, dir, stdout);
}
}
/* get a file from the disk */
int get_file(char *atari_name, char *local_name)
{
struct dirent dir[1];
if (find_file(dir, atari_name, 0, 0)) {
printf("File '%s' not found\n", atari_name);
return -1;
} else {
FILE *f = fopen(local_name, "w");
if (!f) {
printf("Couldn't open local file '%s'\n", local_name);
return -1;
}
/* printf("Found file. Sector of rib is %d\n", sector); */
read_file(atari_name, dir, f);
if (fclose(f)) {
printf("Couldn't close local file '%s'\n", local_name);
return -1;
}
return 0;
}
}
/* Delete file name */
int rm(char *name, int ignore)
{
struct dirent dir[1];
if (!find_file(dir, name, 0, 1)) {
return 0;
} else {
if (!ignore)
printf("File '%s' not found\n", name);
return -1;
}
}
/* Free command */
int do_free(void)
{
int amount = amount_free() * SECTORS_PER_BLOCK;
printf("%d free sectors, %d free bytes\n", amount, amount * SECTOR_SIZE);
return 0;
}
/* Pre-allocate space for file */
int alloc_space(int *list, int blocks)
{
while (blocks) {
int x = alloc_block(0xFFFD);
if (x == -1) {
printf("Not enough space\n");
return -1;
} else {
*list++ = x;
}
--blocks;
}
return 0;
}
/* Write a directory entry */
int write_dir(char *name, int extentno, int extent, int rc, int *al)
{
int z, i;
struct dirent d[1];
unsigned char buf[SECTOR_SIZE];
int sect = extent / (SECTOR_SIZE / ENTRY_SIZE);
int ofst = extent % (SECTOR_SIZE / ENTRY_SIZE);
getsect(buf, sect);
putname(d, name);
extentno *= (dpb->exm + 1);
while (rc > 128) {
rc -= 128;
++extentno;
}
d->uu = 0;
d->s1 = 0;
d->ex = (extentno % 32);
d->s2 = (extentno / 32);
d->rc = rc;
for (i = z = 0; z != 16; ++z) {
if (BIG_DISK) {
d->al[z++] = al[i];
d->al[z] = (al[i] >> 8);
} else {
d->al[z] = al[i++];
}
}
/* printf("%s uu=%d s1=%d s2=%d ex=%d rc=%d\n", getname(d), d->uu, d->s1, d->s2, d->ex, d->rc); */
memcpy(buf + ofst * ENTRY_SIZE, d, ENTRY_SIZE);
putsect(buf, sect);
return 0;
}
/* Write a file */
int write_file(char *name, unsigned char *buf, long sects)
{
int x, z;
int *blk_list;
int *extent_list;
long blks; /* Number of blocks needed for this file */
long extents; /* Number of extents needed for this file */
int rc; /* Counter for current extent */
int secno; /* Sector number within extent */
int extent_blkno; /* Block number within extent */
int extentno; /* Extent number of file */
int blkno; /* Block number */
int al[16]; /* Allocation map for current extent */
/* Compute number of blocks needed for file */
blks = (sects + SECTORS_PER_BLOCK - 1) / SECTORS_PER_BLOCK;
/* Compute number of extents needed for file */
extents = ((blks * SECTORS_PER_BLOCK) + SECTORS_PER_EXTENT - 1) / SECTORS_PER_EXTENT;
/* Force at least one extent for case of empty file */
if (!extents)
extents = 1;
/* Allocate space for file */
blk_list = (int *)malloc(sizeof(int) * blks);
if (alloc_space(blk_list, blks))
return -1;
/* Allocate extents for file */
extent_list = (int *)malloc(sizeof(int) * extents);
if (alloc_extents(extent_list, extents))
return -1;
/* Write file */
for (z = 0; z != 16; ++z)
al[z] = 0;
blkno = 0;
extent_blkno = 0;
extentno = 0;
secno = 0;
rc = 0;
for (x = 0; x != sects; ++x) {
al[extent_blkno] = blk_list[blkno];
putsect(buf + x * SECTOR_SIZE, (blk_list[blkno] << dpb->bsh) + secno);
++rc;
if (++secno == SECTORS_PER_BLOCK) {
secno = 0;
++blkno;
if (++extent_blkno == BLOCKS_PER_EXTENT) {
/* Write current extent */
write_dir(name, extentno, extent_list[extentno], rc, al);
++extentno;
rc = 0;
for (z = 0; z != 16; ++z)
al[z] = 0;
extent_blkno = 0;
}
}
}
if (rc || !extentno) {
/* Write final extent */
write_dir(name, extentno, extent_list[extentno], rc, al);
}
return 0;
}
/* Put a file on the disk */
int put_file(char *local_name, char *atari_name)
{
FILE *f = fopen(local_name, "r");
long x, size; /* File size in bytes */
long up; /* Size in bytes rounded up to sectors */
unsigned char *buf;
int rtn;
if (!f) {
printf("Couldn't open '%s'\n", local_name);
return -1;
}
if (fseek(f, 0, SEEK_END)) {
printf("Couldn't get file size of '%s'\n", local_name);
fclose(f);
return -1;
}
size = ftell(f);
if (size < 0) {
printf("Couldn't get file size of '%s'\n", local_name);
fclose(f);
return -1;
}
rewind(f);
/* Round up to a multiple of (SECTOR_SIZE) */
up = size + (SECTOR_SIZE) - 1;
up -= up % (SECTOR_SIZE);
buf = (unsigned char *)malloc(up);
if (fread(buf, 1, size, f) != (size_t)size) {
printf("Couldn't read file '%s'\n", local_name);
fclose(f);
free(buf);
return -1;
}
fclose(f);
/* Fill with ^Zs to end of sector */
for (x = size; x != up; ++x)
buf[x] = 0x1a;
/* Delete existing file */
rm(atari_name, 1);
/* Allocate space and write file */
rtn = write_file(atari_name, buf, up / SECTOR_SIZE);
if (rtn) {
printf("Couldn't write file\n");
return -1;
}
return 0;
}
/* Get file size in sectors */
int get_info(struct dirent *dir, char *name)
{
int rtn = 0;
int count = 0;
int exno = 0; /* Extent number */
/* fprintf(stderr, "info for %s\n", name); */
for (;;) {
int recno; /* Record number within extent */
int cnt = 0;
/* fprintf(stderr, "extent %d rc=%d\n", exno, dir->rc); */
for (recno = 0; recno != RC(dir) && recno != SECTORS_PER_EXTENT; ++recno) {
int blkno; /* Block number within extent */
int blk; /* Current block */
blkno = (recno >> dpb->bsh);
if (BIG_DISK)
blk = dir->al[blkno * 2] + 256 * dir->al[blkno * 2 + 1];
else
blk = dir->al[blkno];
if (blk) {
++count;
++cnt;
} else {
fprintf(stderr,"allocation map ran out before rc count reached! %d\n", recno);
rtn = -1;
break;
}
}
/* fprintf(stderr, " count=%d\n", cnt); */
if (RC(dir) != SECTORS_PER_EXTENT) {
/* Must be the end */
break;
} else {
++exno;
if (find_file(dir, name, exno, 0)) {
/* fprintf(stderr, "%s: can't find next extent (%d)!\n", name, exno);
rtn = -1; */
break;
}
}
}
if (rtn)
return -1;
else
return count;
}
void atari_dir(int all, int full, int single)
{
unsigned char buf[SECTOR_SIZE];
struct dirent dir[1];
int x, y;
int rows;
int cols = (80 / 13);
for (x = 0; x != SECTOR_DIR_SIZE; ++x) {
int y;
getsect(buf, x + SECTOR_DIR);
for (y = 0; y != SECTOR_SIZE; y += ENTRY_SIZE) {
struct dirent *d = (struct dirent *)(buf + y);
if (d->uu < 0x20 && EXTENT_NO(d) == 0) {
struct name *nam;
char *s = getname(d);
nam = (struct name *)malloc(sizeof(struct name));
nam->name = strdup(s);
if (d->t[0] & 0x80)
nam->locked = 1;
else
nam->locked = 0;
if (d->t[1] & 0x80)
nam->is_sys = 1;
else
nam->is_sys = 0;
nam->sector = 0;
nam->sects = 0;
nam->load_start = -1;
nam->load_size = -1;
nam->init = -1;
nam->run = -1;
nam->size = -1;
memcpy(dir, d, ENTRY_SIZE);
nam->sects = get_info(dir, nam->name);
nam->size = nam->sects * SECTOR_SIZE;
if ((all || !nam->is_sys))
names[name_n++] = nam;
}
}
}
qsort(names, name_n, sizeof(struct name *), (int (*)(const void *, const void *))comp);
if (full) {
int totals = 0;
int total_bytes = 0;
printf("\n");
for (x = 0; x != name_n; ++x) {
if (names[x]->load_start != -1)
printf("-r%c%c%c %6d (%3d) %-13s (load_start=$%x load_end=$%x)\n",
(names[x]->locked ? '-' : 'w'),
(names[x]->is_cm ? 'x' : '-'),
(names[x]->is_sys ? 's' : '-'),
names[x]->size, names[x]->sects, names[x]->name, names[x]->load_start, names[x]->load_start + names[x]->load_size - 1);
else
printf("-r%c%c%c %6d (%3d) %-13s\n",
(names[x]->locked ? '-' : 'w'),
(names[x]->is_cm ? 'x' : '-'),
(names[x]->is_sys ? 's' : '-'),
names[x]->size, names[x]->sects, names[x]->name);
totals += names[x]->sects;
total_bytes += names[x]->size;
}
printf("\n%d entries\n", name_n);
printf("\n%d sectors, %d bytes\n", totals, total_bytes);
printf("\n");
do_free();
printf("\n");
} else if (single) {
int x;
for (x = 0; x != name_n; ++x) {
printf("%s\n", names[x]->name);
}
} else {
/* Rows of 12 names each ordered like ls */
rows = (name_n + cols - 1) / cols;
for (y = 0; y != rows; ++y) {
for (x = 0; x != cols; ++x) {
int n = y + x * rows;
/* printf("%11d ", n); */
if (n < name_n)
printf("%-12s ", names[n]->name);
else
printf(" ");
}
printf("\n");
}
}
}
int mkfs()
{
unsigned char buf[SECTOR_SIZE];
int tracks, x, n;
for (x = 0; x != SECTOR_SIZE; ++x)
buf[x] = 0xe5;
tracks = dpb->off + (((dpb->dsm + 1) << dpb->bsh) + dpb->spt - 1) / dpb->spt;
n = tracks * dpb->spt;
printf("%d tracks\n", tracks);
printf("%d sectors\n", n);
for (x = 0; x != n; ++x)
fwrite((char *)buf, SECTOR_SIZE, 1, disk);
return 0;
}
int main(int argc, char *argv[])
{
int all = 0;
int full = 0;
int single = 0;
int x;
long size;
char *disk_name;
dpb = &dpb_fd;
x = 1;
if (x == argc || !strcmp(argv[x], "--help") || !strcmp(argv[x], "-h")) {
printf("\nCP/M disk image tool\n");
printf("\n");
printf("Syntax: cpmtool path-to-disk-image [command] [args]\n");
printf("\n");
printf(" Commands: (default is ls)\n\n");
printf(" ls [-la1] Directory listing\n");
printf(" -l for long\n");
printf(" -a to show system files\n");
printf(" -1 to show a single name per line\n\n");
printf(" cat cpm-name Type file to console\n\n");
printf(" get cpm-name [local-name] Copy file from diskette to local-name\n\n");
printf(" put local-name [cpm-name] Copy file from local-name to diskette\n\n");
printf(" free Print amount of free space\n\n");
printf(" rm cpm-name Delete a file\n\n");
printf(" mkfs Format disk\n\n");
return -1;
}
disk_name = argv[x++];
if (x != argc && !strcmp(argv[x], "mkfs")) {
disk = fopen(disk_name, "w");
if (!disk) {
printf("Couldn't open '%s'\n", disk_name);
return -1;
}
mkfs();
fclose(disk);
return 0;
}
disk = fopen(disk_name, "r+");
if (!disk) {
printf("Couldn't open '%s'\n", disk_name);
return -1;
}
if (fseek(disk, 0, SEEK_END)) {
printf("Couldn't seek disk?\n");
return -1;
}
size = ftell(disk);
if (size == 128 * 77 * 26) { /* Single sided floppy */
dpb = &dpb_fd;
} else {
fprintf(stderr, "assuming hard drive\n");
dpb = &dpb_hd;
}
/* Directory options */
dir:
while (x != argc && argv[x][0] == '-') {
int y;
for (y = 1;argv[x][y];++y) {
int opt = argv[x][y];
switch (opt) {
case 'l': full = 1; break;
case 'a': all = 1; break;
case '1': single = 1; break;
default: printf("Unknown option '%c'\n", opt); return -1;
}
}
++x;
}
if (x == argc) {
/* Just print a directory listing */
atari_dir(all, full, single);
return 0;
} else if (!strcmp(argv[x], "ls")) {
++x;
goto dir;
} else if (!strcmp(argv[x], "free")) {
return do_free();
/* } else if (!strcmp(argv[x], "check")) {
return do_check(); */
} else if (!strcmp(argv[x], "cat")) {
++x;
if (x == argc) {
printf("Missing file name to cat\n");
return -1;
} else {
cat(argv[x++]);
return 0;
}
} else if (!strcmp(argv[x], "get")) {
char *local_name;
char *atari_name;
++x;
if (x == argc) {
printf("Missing file name to get\n");
return -1;
}
atari_name = argv[x];
local_name = atari_name;
if (x + 1 != argc)
local_name = argv[++x];
return get_file(atari_name, local_name);
} else if (!strcmp(argv[x], "put")) {
char *local_name;
char *atari_name;
++x;
if (x == argc) {
printf("Missing file name to put\n");
return -1;
}
local_name = argv[x];
if (strrchr(local_name, '/'))
atari_name = strrchr(local_name, '/') + 1;
else
atari_name = local_name;
printf("%s\n", atari_name);
if (x + 1 != argc)
atari_name = argv[++x];
return put_file(local_name, atari_name);
} else if (!strcmp(argv[x], "rm")) {
char *name;
++x;
if (x == argc) {
printf("Missing name to delete\n");
return -1;
} else {
name = argv[x];
}
return rm(name, 0);
} else {
printf("Unknown command '%s'\n", argv[x]);
return -1;
}
return 0;
}