-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathedos.c
435 lines (395 loc) · 14.7 KB
/
edos.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
/* EDOS diskette access
* Copyright
* (C) 2023 Joseph H. Allen
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 1, or (at your option) any later version.
*
* It is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this software; see the file COPYING. If not, write to the Free Software Foundation,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* EDOS disk access */
#define SECTOR_SIZE 128
#define TRACK_SIZE 26
#define SECTOR_DIR 3 /* Starting directory sector */
#define SECTOR_DIR_SIZE 5 /* Number of directory sectors */
#define ENTRY_SIZE 11 /* Size of directory entry */
#define ENTRIES_PER_SECTOR 11 /* Number of entries per sector */
/* Sector interleave table */
int interleave_table[]=
{
0, 9, 18,
1, 10, 19,
2, 11, 20,
3, 12, 21,
4, 13, 22,
5, 14, 23,
6, 15, 24,
7, 16, 25,
8, 17
};
/* Directory entry */
struct dirent {
unsigned char name[5]; /* 5 '*'s if deleted */
unsigned char mark; /* 0xFF means end of directory, 0x80 means file was deleted */
unsigned char track; /* Starting track number of file */
unsigned char sect; /* Starting sector number of file (1 based) */
unsigned char size_hi; /* Size high byte? */
unsigned char size_lo; /* Size low byte: size is one larger than actual size I think */
unsigned char unknown; /* This is observed to have 0xFF, 0x02 or 0x00 */
};
FILE *disk;
/* Get sector: account for interleave */
void getsect(unsigned char *buf, int lsn)
{
int track = lsn / TRACK_SIZE;
int sect = lsn - track * TRACK_SIZE;
sect = interleave_table[sect];
fseek(disk, (track * TRACK_SIZE + sect) * SECTOR_SIZE, SEEK_SET);
fread((char *)buf, SECTOR_SIZE, 1, disk);
}
int lower(int c)
{
if (c >= 'A' && c <= 'Z')
return c - 'A' + 'a';
else
return c;
}
struct name
{
char *name;
int size;
int lsn;
int mark;
int unknown;
};
struct name *names[SECTOR_DIR_SIZE * ENTRIES_PER_SECTOR];
int name_n;
int comp(struct name **l, struct name **r)
{
return strcmp((*l)->name, (*r)->name);
}
int force_convert;
void read_file(int lsn, int size, FILE *f)
{
unsigned char buf[SECTOR_SIZE];
int x;
int guess_type = 0;
for (x = lsn; x != lsn + size; ++x)
{
getsect(buf, x);
if (x == lsn && buf[0] == 'D')
guess_type = 1; /* Object file- don't delete NULs */
if (guess_type)
{
/* Not ASCII! */
if (x + 1 == lsn + size)
{
int y;
/* Last sector of file- delete trailing NULs */
for (y = SECTOR_SIZE - 1; y && !buf[y]; --y);
if (y)
fwrite(buf, y + 1, 1, f);
}
else
{
fwrite(buf, SECTOR_SIZE, 1, f);
}
}
else
{
/* ASCII */
int ends = SECTOR_SIZE;
int n;
if (x + 1 == lsn + size)
{
/* Last sector, delete trailing NULs */
while (ends && buf[ends-1] == 0) --ends;
}
for (n = 0; n != ends; ++n) {
int c = buf[n];
if (c == 13) {
/* Convert to MS-DOS */
fputc('\r', f);
fputc('\n', f);
} else if (c == 10) {
/* Delete Line Feeds */
} else if (c == 0) {
/* Delete NULs- not sure this is a good idea */
} else {
fputc(c, f);
}
}
}
}
}
/* Find a file, return its starting sector number and size */
int find_file(char *filename, int *size)
{
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 - ENTRY_SIZE; y += ENTRY_SIZE) {
struct dirent *d = (struct dirent *)(buf + y);
/* if (d->mark != 0xff && d->name[0] != '*') { */ /* Not deleted */
if (d->name[0] >= 'A' && d->name[0] <= 'Z') {
char s[50];
int p = 0;
int i;
for (i = 0; i != sizeof(d->name); i++) {
s[p++] = lower(d->name[i]);
}
while (p && s[p - 1] == ' ') --p;
s[p] = 0;
*size = d->size_hi * 256 + d->size_lo - 1;
if (!strcmp(s, filename)) {
return (d->track * TRACK_SIZE) + d->sect - 1;
}
}
}
}
return -1;
}
/* Load directory */
void edos_load_dir()
{
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 - ENTRY_SIZE; y += ENTRY_SIZE) {
struct dirent *d = (struct dirent *)(buf + y);
if (d->mark == 0xFF)
goto done;
/* if (d->mark != 0xff && d->name[0] != '*') { */
/* if (d->name[0] >= 'A' && d->name[0] <= 'Z') { */
if (d->mark != 0x80) {
struct name *nam;
char s[50];
int p = 0;
int i;
for (i = 0; i != sizeof(d->name); i++) {
s[p++] = lower(d->name[i]);
}
while (p && s[p - 1] == ' ') --p;
s[p] = 0;
nam = (struct name *)malloc(sizeof(struct name));
nam->name = strdup(s);
nam->lsn = (d->track * TRACK_SIZE) + d->sect - 1;
nam->size = d->size_hi * 256 + d->size_lo - 1;
nam->mark = d->mark;
nam->unknown = d->unknown;
names[name_n++] = nam;
}
}
}
done:
qsort(names, name_n, sizeof(struct name *), (int (*)(const void *, const void *))comp);
}
/* Directory listing */
void edos_dir(int full, int single)
{
int x, y;
int rows;
int cols = (80 / 13);
edos_load_dir();
if (full) {
int totals = 0;
printf("\n");
for (x = 0; x != name_n; ++x) {
printf("%6d %-13s lsn = %d mark = %x unknown = %x\n",
names[x]->size * SECTOR_SIZE, names[x]->name, names[x]->lsn, names[x]->mark, names[x]->unknown);
totals += names[x]->size;
}
printf("\n%d entries\n", name_n);
printf("\n%d sectors, %d bytes\n", totals, totals * SECTOR_SIZE);
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("%-11s ", names[n]->name);
else
printf(" ");
}
printf("\n");
}
}
}
/* cat a file */
void cat(char *name)
{
int size;
int lsn = find_file(name, &size);
if (lsn == -1) {
printf("File '%s' not found\n", name);
exit(-1);
} else {
read_file(lsn, size, stdout);
}
}
/* get a file from the disk */
int get_file_lsn(int lsn, int size, char *local_name)
{
FILE *f = fopen(local_name, "w");
if (!f) {
printf("Couldn't open local file '%s'\n", local_name);
return -1;
}
read_file(lsn, size, f);
if (fclose(f)) {
printf("Couldn't close local file '%s'\n", local_name);
return -1;
}
return 0;
}
int get_file(char *mdos_name, char *local_name)
{
int size;
int lsn = find_file(mdos_name, &size);
if (lsn == -1) {
fprintf(stderr, "File '%s' not found\n", mdos_name);
return -1;
} else {
return get_file_lsn(lsn, size, local_name);
}
}
int main(int argc, char *argv[])
{
int full = 0;
int single = 0;
int x;
char *disk_name;
x = 1;
if (x == argc || !strcmp(argv[x], "--help") || !strcmp(argv[x], "-h"))
{
help:
printf("\nEXORciser EDOS-II diskette access\n");
printf("\n");
printf("Syntax: edos path-to-diskette command args\n");
printf("\n");
printf(" Commands:\n");
printf(" ls [-la1A] Directory listing\n");
printf(" -l for long\n");
printf(" -1 to show a single name per line\n");
printf(" cat mdos-name Type file to console\n");
printf(" get mdos-name [local-name] Copy file from diskette to local-name\n");
printf(" x Extract all files into current directory\n");
printf("\n");
return -1;
}
disk_name = argv[x++];
disk = fopen(disk_name, "r+");
if (!disk) {
printf("Couldn't open '%s'\n", disk_name);
return -1;
}
if (x == argc || argv[x][0] == '-') {
/* Just print a directory listing */
goto dir;
} else if (!strcmp(argv[x], "help")) {
goto help;
} else if (!strcmp(argv[x], "ls")) {
++x;
dir:
/* Directory options */
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 '1': single = 1; break;
default: printf("Unknown option '%c'\n", opt); return -1;
}
}
++x;
}
edos_dir(full, single);
return 0;
} 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 *mdos_name;
++x;
if (x == argc) {
printf("Missing file name to get\n");
return -1;
}
mdos_name = argv[x];
local_name = mdos_name;
if (x + 1 != argc)
local_name = argv[++x];
return get_file(mdos_name, local_name);
} else if (!strcmp(argv[x], "x")) {
char local_name[80];
edos_load_dir();
int x;
int sta = 0;
for (x = 0; x != name_n; ++x)
{
int n;
printf("Extracting %s\n", names[x]->name);
sprintf(local_name, "%s", names[x]->name);
for (n = 1; n != 10; ++n)
{
FILE *f = fopen(local_name, "r");
if (f)
{
fclose(f);
sprintf(local_name, "%s.%d", names[x]->name, n);
}
else
break;
}
if (n == 10)
{
printf("Couldn't extract '%s' at LSN=%d, too many files with same name\n", names[x]->name, names[x]->lsn);
}
else if (n != 1)
{
printf("File already exists, renamed to %s\n", local_name);
}
if (get_file_lsn(names[x]->lsn, names[x]->size, local_name))
{
sta = -1;
printf(" failed reading file.\n");
}
}
return sta;
} else {
printf("Unknown command '%s'\n", argv[x]);
return -1;
}
return 0;
}