From 4b08b726f9735b5a6a17e588753a496700ff25f7 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Thu, 6 Apr 2023 19:26:04 -0400 Subject: [PATCH] Simplify the signature of savefile(). --- advent.h | 3 ++- cheat.c | 7 +++---- main.c | 2 +- saveresume.c | 7 ++++--- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/advent.h b/advent.h index f9a6a9a..3cea5f5 100644 --- a/advent.h +++ b/advent.h @@ -248,6 +248,7 @@ typedef struct { struct save_t { char magic[sizeof(ADVENT_MAGIC)]; int32_t version; + int32_t canary; struct game_t game; }; @@ -277,7 +278,7 @@ extern void set_seed(int32_t); extern int32_t randrange(int32_t); extern int score(enum termination); extern void terminate(enum termination) __attribute__((noreturn)); -extern int savefile(FILE *, int32_t); +extern int savefile(FILE *); #if defined ADVENT_AUTOSAVE extern void autosave(void); #endif diff --git a/cheat.c b/cheat.c index ebbc166..1a415b7 100644 --- a/cheat.c +++ b/cheat.c @@ -19,7 +19,6 @@ int main(int argc, char *argv[]) { int ch; char *savefilename = NULL; - int version = 0; FILE *fp = NULL; // Initialize game variables @@ -59,8 +58,8 @@ int main(int argc, char *argv[]) printf("cheat: game.turns = %d\n", game.turns); break; case 'v': - version = atoi(optarg); - printf("cheat: version = %d\n", version); + save.version = atoi(optarg); + printf("cheat: version = %d\n", save.version); break; case 'o': savefilename = optarg; @@ -89,7 +88,7 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - savefile(fp, version); + savefile(fp); fclose(fp); diff --git a/main.c b/main.c index 80689e3..a162463 100644 --- a/main.c +++ b/main.c @@ -24,7 +24,7 @@ void autosave(void) { if (autosave_fp != NULL) { rewind(autosave_fp); - savefile(autosave_fp, /* version (auto): */0); + savefile(autosave_fp); fflush(autosave_fp); } } diff --git a/saveresume.c b/saveresume.c index 0ed8cd3..0b71319 100644 --- a/saveresume.c +++ b/saveresume.c @@ -22,11 +22,12 @@ struct save_t save; #define IGNORE(r) do{if (r){}}while(0) -int savefile(FILE *fp, int32_t version) +int savefile(FILE *fp) /* Save game to file. No input or output from user. */ { memcpy(&save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)); - save.version = (version == 0) ? SAVE_VERSION : version; + if (save.version == 0) + save.version = SAVE_VERSION; save.game = game; IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp)); @@ -84,7 +85,7 @@ int suspend(void) free(name); } - savefile(fp, SAVE_VERSION); + savefile(fp); fclose(fp); rspeak(RESUME_HELP); exit(EXIT_SUCCESS); -- 2.31.1