-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ddir.c
434 lines (340 loc) · 9.04 KB
/
ddir.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
/*
* ddir.c
*
* Directory of Dragon DOS disk
*
* Usage: 'ddir input_file.vdk', 'ddir input_file.vdk *.bin'
*
* Graham E. Kinns <gekinns@iee.org> Apr '97
* Ported to Linux by Adrien Destugues <pulkomandy.tk> 2021
*
* $Log: ddir.c $
* Revision 2.1 2021/12/12
* Minor tidying
* Revision 2.0 2021/11/16
* Linux port
* Revision 1.0 1997/04/15 20:56:54 Graham
* Initial revision
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "hookdbt.h"
#include "diskbios.h"
#include "dmabound.h"
#include "wildcard.h"
#if defined (__BORLANDC__) && (__BORLANDC__ >= 0x0450 && __BORLANDC__ <= 0x0452)
void
_ExceptInit (void)
{
}
#endif /* BC++ v4.0x */
#ifdef RCS
static const char rcs_id[] = "$Id: ddir.c 2.1 2021/12/12 Linux Exp $";
#endif
#define PROG_NAME "ddir"
#define VERSION "2.1"
#define MAX_DIR_ENTS 160
static Byte directory [MAX_DIR_ENTS][25];
/* Local function prototypes */
static Byte get_drive_num (const char * const path);
static Byte read_dragon_sector (Byte drive_num, Byte track, Byte side, Byte sector, Byte *buf);
static void read_sector (Byte drive_num, Byte track, Byte side, Byte sector, Byte *buf);
static const char * const printable_filename (uint dir_entry);
static const char * const dos_filename (uint dir_entry);
static ulong calc_filesize (uint dir_entry_idx);
static Byte
get_drive_num (const char * const path)
{
extern const char* FILENAME;
FILENAME = path;
return 0;
}
static Byte
read_dragon_sector (Byte drive_num, Byte track, Byte side, Byte sector, Byte *buf)
{
Disk_Base_Table dbt;
Byte result;
uint count = 0;
hook_disk_base_table (&dbt);
dbt.table[3] = 0x01;
dbt.table[4] = 18;
update_disk_base_table (&dbt);
while (count++ < 3)
{
result = (bios_read_sectors (drive_num, track, side, sector, 1, buf, 256) & 0xff00) >> 8;
if (result == 0x00)
break;
bios_reset_drive (drive_num);
}
unhook_disk_base_table (&dbt);
return (result);
}
/* Wrapper for read_dragon_sector that handles fatal errors (may not return) */
static void
read_sector (Byte drive_num, Byte track, Byte side, Byte sector, Byte *buf)
{
Byte result;
result = read_dragon_sector (drive_num, track, side, sector, buf);
if (result != 0x00)
{
fprintf (stderr, "Error: BIOS error 0x%02x: %s\n",
result, bios_error_description (result));
exit (1);
}
}
static const char * const
printable_filename (uint dir_entry)
{
static char filename[13];
uint i;
Byte b;
for (i = 0; i < 8; i++)
{
b = directory[dir_entry][i+1];
if (b == 0x00)
break;
if (b < 0x20 || b > 0x7f)
filename[i] = '_';
else
filename[i] = (char) b;
}
for ( ; i < 8; i++)
filename[i] = ' ';
filename[8] = '.';
for (i = 0; i < 3; i++)
{
b = directory[dir_entry][i+9];
if (b == 0x00)
break;
if (b < 0x20 || b > 0x7f)
filename[i+9] = '_';
else
filename[i+9] = (char) b;
}
for ( ; i < 3; i++)
filename[i+9] = ' ';
filename[12] = '\0';
return (filename);
}
static const char * const
dos_filename (uint dir_entry)
{
static char filename[13];
static const char invalid_dos_chars[] = ":;,.\"|<>*?\\/+=[]";
uint i;
Byte b;
char *p;
p = filename;
for (i = 0; i < 8; i++)
{
b = directory[dir_entry][i+1];
if (b == 0x00)
break;
if (b <= 0x20 || b > 0x7f || strchr (invalid_dos_chars, (char) b) != NULL)
*p++ = '_';
else
*p++ = (char) b;
}
*p++ = '.';
for (i = 0; i < 3; i++)
{
b = directory[dir_entry][i+9];
if (b == 0x00)
break;
if (b <= 0x20 || b > 0x7f || strchr (invalid_dos_chars, (char) b) != NULL)
*p++ = '_';
else
*p++ = (char) b;
}
*p = '\0';
strlwr (filename);
return (filename);
}
static ulong
calc_filesize (uint dir_entry_idx)
{
uint entry;
uint byte_idx; /* Ptr to LSN + count */
Word start_lsn;
Byte sector_count;
ulong file_size = 0;
entry = dir_entry_idx;
byte_idx = 12;
for (;;)
{
start_lsn = (((Word) directory[entry][byte_idx]) << 8)
| directory[entry][byte_idx+1];
sector_count = directory[entry][byte_idx+2];
/* Assume a sector count of 0 marks the end of used entries */
if (start_lsn == 0x0000 && sector_count == 0x00)
break;
DPRINTF (("Entry %3u offset %2u LSN %03x Count %u", entry, byte_idx, start_lsn, sector_count));
file_size += (ulong) sector_count * 256;
byte_idx += 3;
/* End of usable entries ? */
if (byte_idx > 21)
{
Byte new_entry;
/* Is this entry continued ? */
if ((directory[entry][0] & 0x20) == 0x00)
break; /* for () */
/* Offset 24 has ptr to next entry */
new_entry = directory[entry][24];
/* Sanity check for continuation block */
if ((directory[new_entry][0] & 0x01) == 0x00)
{
fprintf (stderr, "Warning: corrupt directory entry %u points to non-continuation entry %u\n", entry, new_entry);
break;
}
else
{
entry = new_entry;
}
byte_idx = 1;
}
}
/* Byte offset 24 is bytes used in last sector (0x00 == 256) */
if (directory[entry][24] != 0x00)
file_size -= 256 - directory[entry][24];
DPRINTF (("Entry %3u offset %2u byte 24 = %02x %3u", entry, byte_idx, directory[entry][24], directory[entry][24]));
return ((ulong) file_size);
}
int
main (int argc, char *argv[])
{
Byte drive_num;
Byte *buf;
Bool is_bootable;
uint num_tracks, num_sectors, num_sides;
uint sector, entry;
ulong bytes_free;
char *wildcard;
uint file_count;
fputs (PROG_NAME " v" VERSION " -- Dragon DOS disk directory\n"
"Original by Graham E. Kinns <gekinns@iee.org>\nLinux port by Adrien Destugues <pulkomandy.tk>\nCompiled on " __DATE__ "\n"
"\n", stderr);
if (argc < 2
|| argv[1][0] == '-' || argv[1][0] == '/')
{
fprintf (stderr, "Usage: %s input_file.vdk [filename_wildcard]\n\n", argv[0]);
return (1);
}
drive_num = get_drive_num (argv[1]);
if (drive_num == 0xff)
{
fprintf (stderr, "Error: invalid VDK image specifier in '%s'\n", argv[1]);
return (1);
}
if (argc > 2)
{
wildcard = malloc (strlen (argv[2]) + 3);
if (wildcard == NULL)
{
fprintf (stderr, "Error: malloc failed\n");
return (1);
}
strcpy (wildcard, argv[2]);
strlwr (wildcard);
if (strchr (wildcard, '.') == NULL)
strcat (wildcard, ".*");
}
else
{
wildcard = strdup ("*.*");
if (wildcard == NULL)
{
fprintf (stderr, "Error: malloc failed\n");
return (1);
}
}
buf = malloc_dma_page_aligned_block (256);
if (buf == NULL)
{
fprintf (stderr, "Error: malloc failed\n");
return (1);
}
bios_reset_drive (drive_num);
/* Check for boot sector while head is over Track 0 */
read_sector (drive_num, 0, 0, 3, buf);
is_bootable = (buf[0] == 'O' || buf[1] == 'S') ? TRUE : FALSE;
read_sector (drive_num, 20, 0, 1, buf);
num_tracks = buf[252];
num_sectors = buf[253];
if (num_tracks != (Byte) ~buf[254]
|| num_sectors != (Byte) ~buf[255])
{
fprintf (stderr, "Not a Dragon DOS VDK image\n");
return (1);
}
num_sides = (num_sectors > 18 ? 2 : 1);
printf ("Disk in drive %u: %02u tracks, %u side%s, %02u sectors, %sbootable\n",
drive_num,
num_tracks,
num_sides,
num_sides == 1 ? "" : "s",
num_sectors,
is_bootable ? "" : "not ");
for (sector = 0; sector < 16; sector++)
{
read_sector (drive_num, 20, 0, sector+3, buf);
/* This is a crude, lazy fudge ... ;-) */
memcpy (directory[sector * 10], buf, 250);
}
#ifdef notdef
printf ("Files matching '%s':\n", wildcard);
#endif
printf ("\n");
file_count = 0;
for (entry = 0; entry < MAX_DIR_ENTS && (directory[entry][0] & 0x08) == 0x00; entry++)
{
const char *filename;
#ifdef DEBUG
{
uint i;
printf ("%02x:", entry);
for (i = 0; i < 25; i++)
printf (" %02x", directory[entry][i]);
printf ("\n");
}
#endif
/* Skip deleted and continuation entries */
if (directory[entry][0] & (0x80 | 0x01))
continue;
filename = dos_filename (entry);
if (wildcard_match (wildcard, filename))
{
printf ("%-13s %6lu %c %s\n",
printable_filename (entry),
calc_filesize (entry),
directory[entry][0] & 0x02 ? 'P' : ' ',
filename);
file_count++;
}
}
bytes_free = 0;
for (sector = 1; sector < 3; sector++)
{
uint sector_count = 0;
uint byte;
read_sector (drive_num, 20, 0, sector, buf);
for (byte = 0; byte < 180; byte++)
{
Byte mask = 0x80;
uint bit;
for (bit = 8; bit; bit--, mask >>= 1)
if (buf[byte] & mask)
sector_count++;
}
bytes_free += (ulong) sector_count * 256;
}
printf ("\n");
printf ("Files matching '%s': %u\n", wildcard, file_count);
printf ("Free space: %6lu bytes\n", bytes_free);
bios_reset_drive (drive_num);
free_dma_page_aligned_block (buf);
free (wildcard);
return (0);
}