summaryrefslogtreecommitdiffstats
path: root/abcwrite.c
blob: 310879985e3ce4a39f3a7d917ea92b5bfc99ef5d (plain)
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
/*
 * abcwrite.c
 *
 * Program to build a populated ABC-DOS hard disk image
 */
#include <ctype.h>
#include <dirent.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "util.h"

/*
 * Find a free cluster in the bitmap; return the cluster number
 */
static int get_cluster(struct disk *disk, unsigned int cluster_hint)
{
  int nclust, i, x, nc, best, bnc;

  best = -1;
  bnc = 0;

  nclust = disk->fmt->sectors << disk->fmt->cluster_shift;

  for (i = 0; i < nclust; i++) {
    if (!is_used(disk, i)) {
      /* How long is this? */
      for (x = i, nc = 0; i < nclust && nc < cluster_hint; i++) {
	if (is_used(disk, i))
	  break;
	nc++;
      }
      if (nc == cluster_hint) {
	/* Found as big as we wanted */
	mark_used(disk, x);
	return x;
      } else if (nc > bnc) {
	best = x;
	bnc = nc;
      }
    }
  }

  if (bnc) {
    /* We found *something* at least... */
    mark_used(disk, best);
    return best;
  }

  fprintf(stderr, "%s: output disk full\n", program);
  exit(1);
}

/*
 * Returns 0 for OK, -1 for failure
 */
int mangle_name(char *dst, const char *src)
{
  static const wchar_t srcset[] =
    L"0123456789_."
    L"ÉABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÅÜÆØ"
    L"éabcdefghijklmnopqrstuvwxyzäöåüæø";
  static const char dstset[] =
    "0123456789_."
    "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^[\\"
    "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^[\\";
  char *d = dst;
  const char *s, *p;
  wchar_t sc;
  const wchar_t *scp;
  char dc;
  int n;

  if ( (p = strrchr(src,'/')) )
    s = p+1;
  else
    s = src;

  memset(dst, ' ', 11);
  dst[11] = '\0';

  mbtowc(NULL, NULL, 0);	/* Reset the shift state */

  while (d < dst+11 && (n = mbtowc(&sc, s, (size_t)~0)) > 0) {
    s += n;

    if ( (scp = wcschr(srcset, sc)) ) {
      dc = dstset[scp - srcset];
    } else {
      dc = '_';
    }

    if ( dc == '.' )
      d = dst+8;
    else
      *d++ = dc;
  }

  return 0;
}

/*
 * Get the contents of a file, and guess whether or not it's a text file
 */

unsigned char *read_file(const char *filename, int *sizep)
{
  FILE *f = fopen(filename, "rb");
  unsigned char buffer[256];
  int i, n;
  int size = 0;
  int binary = 0;
  unsigned char *data, *dp;
  int blocks;

  if ( !f )
    return NULL;

  while ( (n = fread(buffer, 1, sizeof buffer, f)) ) {
    if ( !binary )
      for ( i = 0 ; i < n ; i++ ) {
	if ( (buffer[i] & 0x80) || buffer[i] == 0x00 || buffer[i] == 0x03 ) {
	  binary = 1;
	  break;
	}
      }

    size += n;
  }

  rewind(f);

  if ( binary ) {
    blocks = (size+252)/253;
    data = xmalloc(blocks*253);
    fread(data,size,1,f);
    memset(data+size,0,blocks*253-size);
    size = blocks*253;
  } else {
    int blocks = (size+251)/252 + 1;
    data = xmalloc(blocks*253);

    dp = data;
    while ( memset(buffer, 0, 253),
	    (n = fread(buffer, 1, 252, f)) ) {
      for ( i = 0 ; i < n ; i++ ) {
	if ( buffer[i] == '\n' )
	  buffer[i] = '\r';
      }
      buffer[n] = '\x03';

      memcpy(dp, buffer, 253);
      dp += 253;
    }

    /* Ending block */
    memset(buffer, 0, 6);
    buffer[6] = '\x03';
    memcpy(dp, buffer, 253);
    dp += 253;

    size = dp - data;
  }

  fclose(f);

  *sizep = size;

  return data;
}

/*
 * Create a file object and return a pointer to the header block.
 */
static unsigned char *
create_file(struct directory *dir, const char *mangled_name,
	    unsigned char *data, int size, int ufd)
{
  struct disk *disk = dir->disk;
  int nblocks, cluster, secperclust, clusters;
  int dirs, dirp;		/* Directory (sector,position) */
  unsigned char *dirent;
  int blockno, cleft;
  unsigned char *header, *hdata, *dp;

  nblocks = (size+252)/253 + 1;
  secperclust = 1 << disk->fmt->cluster_shift;
  clusters = (nblocks + secperclust - 1) >> disk->fmt->cluster_shift;

  /* Find and create directory entry */
  dirp = -1;
  for ( dirs = 0 ; dirs < disk->fmt->dirsecs ; dirs++ ) {
    if ( dir->bitmap[0xEF+dirs] < 0x10 ) {
      dirp = dir->bitmap[0xEF+dirs]++;
      break;
    }
  }

  if ( dirs >= disk->fmt->dirsecs ) {
    /* Directory full */
    fprintf(stderr, "%s: directory full\n", program);
    return NULL;
  }

  dirent = dir->dirsec + dirs*256 + dirp*16;

  memcpy(dirent+4, mangled_name, 11);

  /* Allocate first cluster */
  cluster = get_cluster(disk, clusters--);
  if (cluster < 0) {
    fprintf(stderr, "get_cluster = %d\n", cluster);
    return NULL;
  }

  dirent[0] = cluster >> 3;
  dirent[1] = cluster << 5;
  dirent[2] = nblocks & 0xFF;
  dirent[3] = nblocks >> 8;

  /* Create "file header" */
  dp = header = disk->image + (cluster << (disk->fmt->cluster_shift+8));
  cleft = secperclust - 1;	/* Left in the first cluster */

  memset(hdata = header, 0, 256);

  *hdata++ = (dirp << 4) + dirs;
  *hdata++ = 0;		/* Header = block 0 */
  *hdata++ = 0;		/* Header = block 0 */
  *hdata++ = 0xFF;
  /* Starting extent has one cluster in it */
  hdata[0] = cluster >> 3;
  hdata[1] = (cluster << 5) + 0; /* Bits[4:0] = extend size minus one */

  /* Actually copy the file */
  blockno = 0;
  while ( size ) {
    int nbytes = (size > 253) ? 253 : size;

    blockno++;

    if (cleft) {
      dp += 256;		/* Subsequent sector */
      cleft--;
    } else {
      cluster = get_cluster(disk, clusters--);
      if (cluster < 0) {
	fprintf(stderr, "get_cluster = %d\n", cluster);
	return NULL;
      }
      dp = disk->image + (cluster << (disk->fmt->cluster_shift+8));
      cleft = secperclust - 1;
      if (cluster == ((hdata[0] << 3) + (hdata[1] >> 5) + 1) &&
	  (hdata[1] & 31) < 31) {
	hdata[1]++;
      } else {
	hdata += 2;
	hdata[0] = cluster >> 3;
	hdata[1] = (cluster << 5) + 0;
      }
    }

    if (ufd && blockno > 1) {
      memset(dp, 0xff, 256);
    } else {
      dp[0] = (dirp << 4) + dirs;
      dp[1] = blockno & 0xFF;
      dp[2] = blockno >> 8;
      memcpy(dp+3, data, 253);
      data += 253;
    }

    size -= nbytes;
  }

  /* Terminate the hdata */
  hdata += 2;
  hdata[0] = 0xFF;
  hdata[1] = 0xFF;

  return header;
}

/*
 * Copy a file
 */
int copy_file(struct directory *dir, const char *filename)
{
  int size;
  char mangled_name[12];
  unsigned char *data;
  unsigned char *header;

  data = read_file(filename, &size);

  mangle_name(mangled_name, filename);

  header = create_file(dir, mangled_name, data, size, 0);
  free(data);
  return header ? 0 : -1;
}

/*
 * Create a UFD
 */
struct directory *make_ufd(struct directory *dir, const char *filename)
{
  char mangled_name[12];
  unsigned char bitmap[253];
  unsigned char *header;
  struct directory *ufd;
  int dirs_block = dir->block ? dir->block+2 : 0;

  ufd = xmalloc(sizeof(struct directory));
  memset(ufd, 0, sizeof *ufd);
  ufd->disk = dir->disk;

  mangle_name(mangled_name, filename);
  memcpy(mangled_name+8, "Ufd", 4);

  memset(bitmap, 0, sizeof bitmap);
  bitmap[0] = dirs_block & 0xFF;
  bitmap[1] = dirs_block >> 8;

  header = create_file(dir, mangled_name, bitmap,
		       (dir->disk->fmt->dirsecs+1)*253, 1);
  if (!header) {
    fprintf(stderr, "%s: header returns NULL\n", program);
    return NULL;
  }

  if ((header[6] & header[7]) != 0xff) {
    fprintf(stderr, "%s: discontiguous UFD (disk too fragmented)\n", program);
    return NULL;
  }
  
  ufd->bitmap = header + 256;
  ufd->dirsec = header + 512;
  ufd->block = (header - dir->disk->image) >> 8;

  /* The UFD bitmap contains two copies of the self-pointer... */
  ufd->bitmap[5] = ufd->bitmap[0];

  return ufd;
}

/* Copy files as given by tree on stdin */
int get_files(struct directory *ufd, const char *dirpath)
{
  struct stat st;
  DIR *d;
  struct dirent *de;
  char filename[4096];
  struct directory *subdir;

  if ( !(d = opendir(dirpath)) ) {
    perror(dirpath);
    return -1;
  }

  while ( (de = readdir(d)) ) {
    if ( !strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") )
      continue;

    sprintf(filename, "%s/%s", dirpath, de->d_name);
    if ( stat(filename, &st) ) {
      perror(filename);
      continue;
    }
    if ( S_ISDIR(st.st_mode) ) {
      subdir = make_ufd(ufd, filename);
      if (subdir)
	get_files(subdir, filename);
      else
	fprintf(stderr, "%s: make_ufd(%s) failed\n", program, filename);
    } else {
      copy_file(ufd, filename);
    }
  }
  closedir(d);

  return 0;
}

int main(int argc, char *argv[])
{
  struct disk *disk;
  const struct diskfmt *fmt;
  FILE *f;

  setlocale(LC_ALL, "");

  program = argv[0];

  if (argc != 4 || !(fmt = get_format(argv[1]))) {
    fprintf(stderr, "Usage: %s format indir outfile\n", argv[0]);
    show_formats(stderr);
    return 1;
  }

  disk = allocate_disk(fmt);

  /* Install files */
  get_files(&disk->mfd, argv[2]);

  /* Create backup MFD if it exists */
  if (fmt->dirsec[1]) {
    memcpy(disk->image + (fmt->dirsec[1] << 8),
	   disk->image + (fmt->dirsec[0] << 8),
	   fmt->dirsecs << 8);
  }

  /* Finally, write out image */
  f = !strcmp(argv[3], "-") ? stdout : fopen(argv[3], "wb");
  if (!f) {
    perror(argv[3]);
    return 1;
  }
  fwrite(disk->image, 1, fmt->sectors << 8, f);
  if (f != stdout)
    fclose(f);

  return 0;
}