Logo Search packages:      
Sourcecode: cbmlink version File versions  Download package

static int load_file ( const char *  cmd,
const char *  filename 
) [static]

load a file to the memory of the remote host

Parameters:
cmd the -l command
filename name of the file to be loaded, "-" for stdin
Returns:
zero on success, nonzero on error

converted start address

length of transferred block

input file

address specifier string (NULL=none)

cursor to command switch

Definition at line 228 of file cbmlink.c.

References bank, hostinfo::basic, buffer, load(), and resolve_addr().

Referenced by main().

{
  /** converted start address */
  unsigned addr = 0;
  /** length of transferred block */
  unsigned length;
  /** input file */
  FILE* f;
  /** address specifier string (NULL=none) */
  const char* addrspec = 0;
  /** cursor to command switch */
  const char* cmdc = cmd + 3;

  for (;;) {
    switch (cmdc[-1]) {
    case 0: /* load program to default address */
      break;
    case 'p':
      if (cmdc != cmd + 3)
      return -1;
      cmdc++;
      continue;
    case 'b': /* load program to the start of BASIC */
      if (*cmdc)
      return -1;
      break;
    case 'r': /* load program to relocated address */
    case 'o': /* load binary object to specified address */
      if (*cmdc == ',' && cmdc[1]) {
      addrspec = cmdc + 1;
      break;
      }
    default:
      return -1;
    }
    break;
  }

  f = *filename == '-' && !filename[1] ? stdin : fopen (filename, "rb");
  if (!f) {
    fputs (filename, stderr), perror (": fopen(reading)");
    return -2;
  }
  if (cmdc != cmd + 3) {
    /* skip the P00 header */
    if (26 != (length = fread (buffer, 1, 26, f)))
      goto loadEOF;
    if (memcmp (buffer, "C64File", 8)) {
      fprintf (stderr, "load %s: not a P00 file\n", filename);
      goto loadError;
    }
  }
  if (*cmdc != 'o') {
    /* read the start address from the file */
    addr = (unsigned) fgetc (f);
    addr |= (unsigned) (unsigned char) fgetc (f) << 8;
  }
  if (feof (f) || ferror (f)) {
  loadEOF:
    fprintf (stderr, "load %s: unexpected end of file\n", filename);
  loadError:
    if (f != stdin)
      fclose (f);
    return -2;
  }

  if (*cmdc == 'b')
    addr = hostinfo->basic;
  else if (addrspec) { /* override the load address */
    char* e;
    addr = resolve_addr (addrspec, &e);
    if (*e) {
      fprintf (stderr, "load %s: invalid address %s\n", filename, addrspec);
      goto loadError;
    }
  }
  length = fread (buffer, 1, sizeof buffer, f);
  if (!length || ferror (f))
    goto loadEOF;
#ifdef SMALLBUF
  else {
    int st = load (comm, bank, addr, (addr + length) & 0xffff, buffer);
    if (st) {
    loadDone:
      if (f != stdin) fclose (f);
      return st;
    }
    addr += length, addr &= 0xffff;
    length = fread (buffer, 1, sizeof buffer, f);
    if (ferror (f))
      goto loadEOF;
    if (!length)
      goto loadDone;
  }
#endif /* SMALLBUF */
  if (!feof (f)) {
    fprintf (stderr, "load %s: more than 64 kilobytes\n", filename);
    goto loadError;
  }
  if (f != stdin)
    fclose (f);
  return load (comm, bank, addr, (addr + length) & 0xffff, buffer);
}


Generated by  Doxygen 1.6.0   Back to index