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

int wrfile ( const struct comm comm,
const char *  filename,
FILE *  file,
char *  buf 
)

copy a file

Parameters:
comm the communication primitives
filename file name (including drive unit number)
file input file
buf at least 258-byte transfer buffer
Returns:
zero on success, nonzero on error

Definition at line 90 of file wrfile.c.

References comm::comm_read, comm::comm_rs, comm::comm_sr, and comm::comm_write.

Referenced by main().

{
  unsigned i;
  for (i = 0; filename[i]; i++);
  if (i > 127)
    return fputs ("wrfile: too long file name\n", stderr), 2;
  memcpy (buf + 1, filename, i + 1);
  *buf = i;
  if ((*comm->comm_write) (buf, i + 2))
    return 3;
  (*comm->comm_sr) ();
  if ((*comm->comm_read) (buf, 1))
    return 3;
  else if (*buf) {
  wrfile_fail:
    fprintf (stderr, "wrfile `%s': failed with %u\n",
           filename, (unsigned char) *buf);
    (*comm->comm_rs) ();
    return *buf || 0;
  }

  while ((i = fread (buf + 2, 1, 255, file))) {
    /* compute the checksum */
    {
      unsigned check = i;
      register char* b = buf + i + 2;
      *b = 0, *buf = i;
      while (--b > buf + 1)
      check += (unsigned char) *b;
      buf[1] = check;
    }

    for (;;) {
      if ((*comm->comm_write) (buf, i + 3))
      return 3;
      else {
      unsigned char c;
      (*comm->comm_sr) ();
      if ((*comm->comm_read) (&c, 1))
        return 3;
      (*comm->comm_rs) ();
      if (c == 0x80)
        break;
      else if (c == 0x81)
        continue;
      *buf = c;
      goto wrfile_fail;
      }
    }
  }

  *buf = 0;
  return ((*comm->comm_write) (buf, 1)) ? 3 : 0;
}


Generated by  Doxygen 1.6.0   Back to index