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

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

save the memory of the remote host to a file

Parameters:
cmd the -s command
filename name of the file to be saved, "-" for stdout
Returns:
zero on success, nonzero on error

converted start address

converted end address

length of the data

output file

the address specifier string

end-of-address pointer

Definition at line 338 of file cbmlink.c.

References bank, buffer, end, resolve_addr(), and save().

Referenced by main().

{
  /** converted start address */
  unsigned start;
  /** converted end address */
  unsigned end;
  /** length of the data */
  unsigned length;
  /** output file */
  FILE* f;
  /** the address specifier string */
  const char* addrspec = cmd + 2;
  /** end-of-address pointer */
  char* e;

  switch (*addrspec++) {
  case ',': /* save program file */
    break;
  case 'o': /* save object file */
    if (*addrspec++ == ',')
      break;
    /* fall through */
  default:
    return -1;
  }

  f = filename[0] == '-' && !filename[1] ? stdout : fopen (filename, "wb");

  if (!f) {
    fputs (filename, stderr), perror (": fopen(writing)");
    return 2;
  }

  start = resolve_addr (addrspec, &e);
  if (*e != ',') {
  saveAddress:
    fprintf (stderr, "save %s: invalid address %s\n", filename, addrspec);
    if (f != stdout)
      fclose (f);
    return -1;
  }
  else
    addrspec = e + 1;

  end = resolve_addr (addrspec, &e);
  if (*e)
    goto saveAddress;

  length = end == start ? 0x10000 : (end - start) & 0xffff;
#ifdef SMALLBUF
  if (!length || length > sizeof buffer)
    end = (start + sizeof buffer) & 0xffff;
 nextHalf:
#endif /* SMALLBUF */
  if (save (comm, bank, start, end, buffer)) {
  saveError:
    if (f != stdout)
      fclose (f);
    return 2;
  }
  if (cmd[2] == ',') {
    fputc (start & 0xff, f);
    fputc (start >> 8, f);
  }
#ifdef SMALLBUF
  if (!length || length > sizeof buffer) {
    if (sizeof buffer != fwrite (buffer, 1, length, f))
      goto writeError;
    length -= sizeof buffer;
    goto nextHalf;
  }
#endif /* SMALLBUF */
  if (length != fwrite (buffer, 1, length, f)) {
#ifdef SMALLBUF
  writeError:
#endif /* SMALLBUF */
    fputs (filename, stderr), perror (": fwrite");
    goto saveError;
  }
  if (f != stdout)
    fclose (f);
  return 0;
}


Generated by  Doxygen 1.6.0   Back to index