// SPDX-License-Identifier: GPL-3.0-or-later // Run on a practice disk. This exercise recognizes the four Planet char cards[][CARD_SIZE]. #include #include #include #define LIVE "ATLAS.DAT:0" #define DRAFT "ATLAS.NEW:0" #define BACKUP "ATLAS.BAK:0" #define CARD_SIZE 24 static const char original[][CARD_SIZE] = { "MERCURY: CRATERS", "VENUS: CLOUDS", "EARTH: BLUE SEA", "MARS: RED RIDGE" }; static const char newMars[CARD_SIZE] = "MARS: DUST STORM"; static char live[4][CARD_SIZE], draft[4][CARD_SIZE], backup[4][CARD_SIZE]; enum { ABSENT, FIRST, SECOND, UNRECOGNIZED, DISK_ERROR }; /* A valid edition contains four exact cards. Each may also have a visit mark in its final byte. Check file length as well as text before changing a name. */ static int inspect(const char *name, char cards[][CARD_SIZE]) { unsigned i; FILE *f = fopen(name, "rb"); if (!f) return vcc_file_error() == DECB_ERR_NOT_FOUND ? ABSENT : DISK_ERROR; size_t count = fread(cards, 1, sizeof(original), f); int extra = fgetc(f); int failed = ferror(f); if (fclose(f) == EOF || failed) return DISK_ERROR; if (count != sizeof(original) || extra != EOF) return UNRECOGNIZED; for (i = 0; i < 4; ++i) { char mark = cards[i][CARD_SIZE - 1]; if (mark != 0 && mark != 'V') return UNRECOGNIZED; if (memcmp(cards[i], original[i], CARD_SIZE - 1) != 0) { if (i == 3 && memcmp(cards[3], newMars, CARD_SIZE - 1) == 0) return SECOND; return UNRECOGNIZED; } } return FIRST; } /* Write only after the caller has checked that the name is unused. */ static int writeCards(const char *name, const void *cards) { FILE *f = fopen(name, "wb"); if (!f) return 0; int complete = fwrite(cards, 1, sizeof(original), f) == sizeof(original); return fclose(f) == 0 && complete; } static void reviseMars(char cards[][CARD_SIZE]) { // The old visit mark belongs to the traveler. Keep the last byte. memcpy(cards[3], newMars, CARD_SIZE - 1); } static int recover(int draftState) { // The live name was missing. Restore the known old edition first. if (rename(BACKUP, LIVE) != 0) return 0; printf("ORIGINAL ATLAS RESTORED\n%s\n", backup[3]); reviseMars(backup); if (draftState == SECOND && memcmp(backup, draft, sizeof(original)) == 0) return remove(DRAFT) == 0; if (draftState != ABSENT) printf("KEPT UNEXPECTED ATLAS.NEW\n"); return 1; } static int saveAtlas(void) { int current = inspect(LIVE, live); int old = inspect(BACKUP, backup); int next = inspect(DRAFT, draft); if (current == DISK_ERROR || old == DISK_ERROR || next == DISK_ERROR) return 0; if (current == ABSENT && old == FIRST) return recover(next); if (current == SECOND && next == ABSENT) { printf("ATLAS ALREADY UPDATED\n%s\n", live[3]); return 1; } if (current == ABSENT && old == ABSENT && next == ABSENT) { if (!writeCards(LIVE, original) || inspect(LIVE, live) != FIRST || memcmp(live, original, sizeof(original)) != 0) return 0; current = FIRST; } if (current != FIRST || old != ABSENT) { printf("CHECK THE THREE ATLAS FILES\n"); return 0; } reviseMars(live); // Only the copy in RAM has changed. if (next == ABSENT) { if (!writeCards(DRAFT, live)) return 0; next = inspect(DRAFT, draft); } if (next != SECOND || memcmp(live, draft, sizeof(original)) != 0) { printf("CHECK ATLAS.NEW\n"); return 0; } // Both files are closed. At this checkpoint the old edition is complete // under BACKUP, and the new edition is complete under DRAFT. if (rename(LIVE, BACKUP) != 0) return 0; printf("BACKUP IN PLACE\nPRESS A KEY TO INSTALL\n"); waitkey(0); if (rename(DRAFT, LIVE) != 0) return 0; printf("UPDATED ATLAS SAVED\n%s\n", draft[3]); return 1; } int main(void) { cls(1); printf("PLANET ATLAS BACKUP\n"); if (saveAtlas()) return 0; printf("SAVE STOPPED - CHECK DISK\n"); return 1; }