73 lines
2.5 KiB
C
73 lines
2.5 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#include <coco.h>
|
|
#include <stdio.h>
|
|
#include <decbfile.h>
|
|
#include <vcc/printer.h>
|
|
#include "mission.h"
|
|
|
|
enum { IO_ERROR, MISSING, OCCUPIED };
|
|
|
|
static int inspect(const char *name)
|
|
{
|
|
FILE *f = fopen(name, "rb");
|
|
if (!f) return vcc_file_error() == DECB_ERR_NOT_FOUND ? MISSING : IO_ERROR;
|
|
return fclose(f) == 0 ? OCCUPIED : IO_ERROR;
|
|
}
|
|
|
|
/* Verify every byte, the exact EOF, and the close before publishing the name. */
|
|
static int matches(const char *name, const char *report)
|
|
{
|
|
FILE *f = fopen(name, "rb");
|
|
if (!f) return 0;
|
|
while (*report && fgetc(f) == *report) ++report;
|
|
int exact = !*report && fgetc(f) == EOF && !ferror(f);
|
|
return fclose(f) == 0 && exact;
|
|
}
|
|
|
|
static int publish(const char *report, char *name)
|
|
{
|
|
char draft[] = "LOG00.TMP:0";
|
|
unsigned i;
|
|
for (i = 1; i <= 99; ++i) {
|
|
name[3] = draft[3] = (char)('0' + i / 10);
|
|
name[4] = draft[4] = (char)('0' + i % 10);
|
|
int live = inspect(name), temp = inspect(draft);
|
|
if (live == IO_ERROR || temp == IO_ERROR) return 0;
|
|
if (live == OCCUPIED || temp == OCCUPIED) continue;
|
|
|
|
/* Old reports and interrupted drafts belong to the disk owner.
|
|
Only an unused pair is eligible. rename also refuses collisions. */
|
|
FILE *f = fopen(draft, "w");
|
|
if (!f) return 0;
|
|
int written = fputs(report, f) != EOF;
|
|
if (fclose(f) != 0 || !written || !matches(draft, report)) return 0;
|
|
return rename(draft, name) == 0;
|
|
}
|
|
printf("ALL REPORT NAMES IN USE\n");
|
|
return 0;
|
|
}
|
|
|
|
int saveReport(unsigned char sector)
|
|
{
|
|
/* One byte string, including the CoCo's CR line endings, goes to both
|
|
devices. A paper copy is requested only after publication succeeds. */
|
|
char report[sizeof("MISSION RECORDER\rSECTOR 00\r")];
|
|
char name[] = "LOG00.TXT:0";
|
|
snprintf(report, sizeof(report), "MISSION RECORDER\rSECTOR %02u\r", (unsigned)sector);
|
|
while (!publish(report, name)) {
|
|
byte key;
|
|
printf("SAVE FAILED\nCHECK DRIVE 0; R RETRY OR Q QUIT\n");
|
|
do { key = waitkey(0); } while (key != 'R' && key != 'r' &&
|
|
key != 'Q' && key != 'q');
|
|
if (key == 'Q' || key == 'q') return 1;
|
|
}
|
|
name[9] = '\0';
|
|
printf("SAVED %s\n", name);
|
|
if (vcc_printer_printf("%s", report) < 0) {
|
|
printf("REPORT DELIVERY INCOMPLETE\n");
|
|
return 1;
|
|
}
|
|
printf("REPORT SENT TO PRINTER PORT\n");
|
|
return 0;
|
|
}
|