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

enum WrStatus WritePC64 ( const struct Filename name,
const byte_t data,
size_t  length,
char **  newname,
log_t  log 
)

Write a file in PC64 format (.P00, .S00 etc.)

Parameters:
name native (PETSCII) name of the file
data the contents of the file
length length of the file contents
newname (output) the converted file name
log Call-back function for diagnostic output
Returns:
status of the operation

Definition at line 300 of file write.c.

References Errors, filename2char(), filesuffix(), Filename::name, Filename::recordLength, REL, TruncateName(), Filename::type, WrFail, WrNoSpace, and WrOK.

{
  char* filename,* c;
  int i;
  struct stat statbuf;

  if (!filename2char (name, newname))
    return WrFail;

  if (!(filename = malloc (TruncateName ((unsigned char*)*newname) + 4 + 1)))
    return WrFail;

  i = sprintf (filename, "%s%.4s", *newname, filesuffix (name));

  if (name->type == REL) /* Fix the suffix for relative files */
    filename[i - 3] = 'r';

  c = &filename[i - 2];

  for (i = 0; i < 100; i++) {
    sprintf (c, "%02d", i);
    if (stat (filename, &statbuf)) { /* found an available file name */
      FILE* f;

      free (*newname);
      *newname = filename;

      if (!(f = fopen (*newname, "wb"))) {
      (*log) (Errors, name, "fopen: %s", strerror (errno));
      return errno == ENOSPC ? WrNoSpace : WrFail;
      }

      if (1 != fwrite ("C64File", 8, 1, f) ||
        1 != fwrite (name->name, 16, 1, f) ||
        EOF == fputc (0, f) ||
        EOF == fputc (name->recordLength, f) ||
        length != fwrite (data, 1, length, f)) {
      (*log) (Errors, name, "fwrite: %s", strerror (errno));
      fclose (f);
      return errno == ENOSPC ? WrNoSpace : WrFail;
      }

      fclose (f);

      return WrOK;
    }
  }

  (*log) (Errors, name, "out of file name space");
  return WrFail;
}


Generated by  Doxygen 1.6.0   Back to index