-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dcopy.c
439 lines (350 loc) · 9.97 KB
/
dcopy.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
/*
* dcopy.c $Id: dcopy.c 2.1 2021/12/12 Linux Rel $
*
* Copies files from Dragon DOS disk
*
* Usage: 'dcopy input_file.vdk', 'dcopy input_file.vdk *.bin', 'dcopy input_file.vdk *.bin *.bi_'
* 'dcopy input_file.vdk *.bin /tmp/'
*
* Graham E. Kinns <gekinns@iee.org> Apr '97
* Ported to Linux by Adrien Destugues <pulkomandy.tk> 2021
*
* $Log: dcopy.c $
* Revision 2.1 2021/12/12
* Minor tidying
* Revision 2.0 2021/11/16
* Linux port
* Revision 1.1 1997/05/08 21:59:08 Graham
* Rename directory in split_filepath() prototype to fix warning.
* Revision 1.0 1997/04/15 21:05:38 Graham
* Initial revision
*/
#ifdef RCS
static const char rcs_id[] = "$Id: dcopy.c 2.1 2021/12/12 Linux Rel $";
#endif
#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 */
#define PROG_NAME "dcopy"
#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 dos_filename (uint dir_entry);
static void split_filepath (const char * const path, char ** dir_name, char ** filename);
static Bool file_copy (Byte drive_num, uint start_entry, const char *filename,
uint num_sectors, Byte *buf, ulong *file_size);
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
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);
}
/*
* Splits supplied path into a directory (ending in \ / or :) and filename
*/
static void
split_filepath (const char * const path, char ** dir_name, char ** filename)
{
char buf[128];
char *filename_start;
char *p;
*buf = '\0';
if (path != NULL)
strcpy (buf, path);
p = filename_start = buf;
while ((p = strpbrk (p, "\\/:")) != NULL)
filename_start = ++p;
if (filename != NULL)
*filename = strdup ((*filename_start != '\0') ? filename_start : "*.*");
/* Terminate dir */
*filename_start = '\0';
if (dir_name != NULL)
*dir_name = strdup (buf);
}
static Bool
file_copy (Byte drive_num, uint start_entry, const char *filename,
uint num_sectors, Byte *buf, ulong *file_size)
{
uint entry;
uint byte_idx;
Word lsn;
Byte sector_count;
FILE *opfile;
Bool data_in_buf;
DPRINTF (("filecopy: entry %u %s\n", entry, filename));
*file_size = 0;
opfile = fopen (filename, "wb");
if (opfile == NULL)
{
fprintf (stderr, "Error: Unable to create '%s'\n", filename);
return (FALSE);
}
entry = start_entry;
byte_idx = 12;
data_in_buf = FALSE;
for (;;)
{
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 (lsn == 0x0000 && sector_count == 0x00)
break;
DPRINTF (("Entry %3u offset %2u LSN %03x Count %u", entry, byte_idx, lsn, sector_count));
/* Read sector_count but only copy sector_count - 1 */
while (sector_count-- > 0)
{
Byte track, sector, side;
if (data_in_buf)
{
fwrite (buf, 1, 256, opfile);
*file_size += 256;
}
track = lsn / num_sectors;
sector = (lsn % num_sectors) + 1;
if (sector > 18)
{
sector -= 18;
side = 1;
}
else
{
side = 0;
}
read_sector (drive_num, track, side, sector, buf);
lsn++;
data_in_buf = TRUE;
}
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;
}
entry = new_entry;
byte_idx = 1; /* Continuation entries start at offset 1 */
}
}
DPRINTF (("Entry %3u offset %2u byte 24 = %02x %3u", entry, byte_idx, directory[entry][24], directory[entry][24]));
/* Byte offset 24 is bytes used in last sector (0x00 == 256) */
if (data_in_buf)
{
fwrite (buf, 1, (directory[entry][24] != 0x00) ? directory[entry][24] : 256, opfile);
*file_size += (directory[entry][24] != 0x00) ? directory[entry][24] : 256;
}
fclose (opfile);
return (TRUE);
}
int
main (int argc, char *argv[])
{
Byte drive_num;
Byte *buf;
uint num_tracks, num_sectors;
uint sector, entry;
char *wildcard;
uint file_count;
ulong file_size;
char *output_directory;
char *output_filemask;
fputs (PROG_NAME " v" VERSION " -- Copies files from Dragon DOS disk\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] [dir\\][output_wildcard]\n\n", argv[0]);
return (1);
}
drive_num = get_drive_num (argv[1]);
if (drive_num == 0xff)
{
fprintf (stderr, "Error: invalid input file 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);
}
}
split_filepath ((argc > 3) ? argv[3] : NULL, &output_directory, &output_filemask);
buf = malloc_dma_page_aligned_block (256);
if (buf == NULL)
{
fprintf (stderr, "Error: malloc failed\n");
return (1);
}
bios_reset_drive (drive_num);
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);
}
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);
}
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))
{
Bool result;
char *output_filename;
char output_filepath[128];
output_filename = make_dest_filename (filename, output_filemask);
if (output_filename == NULL)
{
/* Failed */
fprintf (stderr, "Internal error: Unable to make o/p filename from '%s' '%s'\n", filename, output_filemask);
continue;
}
strcpy (output_filepath, output_directory);
strcat (output_filepath, output_filename);
free (output_filename);
printf ("Copying %-12s to %-*s ... ", filename, (int) strlen (output_directory) + 12, output_filepath);
fflush (stdout);
result = file_copy (drive_num, entry, output_filepath, num_sectors, buf, &file_size);
if (result)
printf ("%6lu bytes\n", file_size);
else
printf ("failed\n");
file_count++;
}
}
printf ("Files copied: %u\n", file_count);
bios_reset_drive (drive_num);
free_dma_page_aligned_block (buf);
free (wildcard);
return (0);
}