summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-02-11 18:01:50 -0500
committerJune McEnroe <june@causal.agency>2020-02-11 18:01:50 -0500
commitc9590bab061fcaa3e11a94eea8c34026a6c7a880 (patch)
tree49f34bd8ee14183d099dd8cca987618bfdc2c085
parentSet self.nick to * initially (diff)
downloadcatgirl-c9590bab061fcaa3e11a94eea8c34026a6c7a880.tar.gz
catgirl-c9590bab061fcaa3e11a94eea8c34026a6c7a880.zip
Use time_t for save signature
It's actually more likely to be 64-bit than size_t anyway, and it
eliminates some helper functions.

Also don't error when reading an empty save file.
-rw-r--r--ui.c26
1 files changed, 11 insertions, 15 deletions
diff --git a/ui.c b/ui.c
index cb4115b..29d5583 100644
--- a/ui.c
+++ b/ui.c
@@ -885,20 +885,17 @@ void uiRead(void) {
 	inputUpdate();
 }
 
-static const size_t Signatures[] = {
+static const time_t Signatures[] = {
 	0x6C72696774616301,
 };
 
-static size_t signatureVersion(size_t signature) {
+static size_t signatureVersion(time_t signature) {
 	for (size_t i = 0; i < ARRAY_LEN(Signatures); ++i) {
 		if (signature == Signatures[i]) return i;
 	}
-	err(EX_DATAERR, "unknown file signature %zX", signature);
+	err(EX_DATAERR, "unknown file signature %jX", (uintmax_t)signature);
 }
 
-static int writeSize(FILE *file, size_t value) {
-	return (fwrite(&value, sizeof(value), 1, file) ? 0 : -1);
-}
 static int writeTime(FILE *file, time_t time) {
 	return (fwrite(&time, sizeof(time), 1, file) ? 0 : -1);
 }
@@ -910,7 +907,7 @@ int uiSave(const char *name) {
 	FILE *file = dataOpen(name, "w");
 	if (!file) return -1;
 
-	if (writeSize(file, Signatures[0])) return -1;
+	if (writeTime(file, Signatures[0])) return -1;
 	const struct Window *window;
 	for (window = windows.head; window; window = window->next) {
 		if (writeString(file, idNames[window->id])) return -1;
@@ -926,13 +923,6 @@ int uiSave(const char *name) {
 	return fclose(file);
 }
 
-static size_t readSize(FILE *file) {
-	size_t value;
-	fread(&value, sizeof(value), 1, file);
-	if (ferror(file)) err(EX_IOERR, "fread");
-	if (feof(file)) errx(EX_DATAERR, "unexpected eof");
-	return value;
-}
 static time_t readTime(FILE *file) {
 	time_t time;
 	fread(&time, sizeof(time), 1, file);
@@ -956,7 +946,13 @@ void uiLoad(const char *name) {
 		return;
 	}
 
-	size_t signature = readSize(file);
+	time_t signature;
+	fread(&signature, sizeof(signature), 1, file);
+	if (ferror(file)) err(EX_IOERR, "fread");
+	if (feof(file)) {
+		fclose(file);
+		return;
+	}
 	signatureVersion(signature);
 
 	char *buf = NULL;
span title='2017-08-19 14:13:59 -0400'>2017-08-19Move nethack options to envJune McEnroe 2017-08-17Remove scala syntax fileJune McEnroe 2017-08-06Add wakeJune McEnroe That payload can appear anywhere within an ethernet frame. Wake-on-LAN is funny. 2017-08-04Use $() in install.shJune McEnroe Something gave me the impression that sh did not like this, but it's specified by POSIX. 2017-08-02Create Code Tarmak 3 layoutJune McEnroe 2017-07-31Add tupJune McEnroe 2017-07-31Use designated initializer for hnel tableJune McEnroe I did not know this syntax worked! 2017-07-30Add juneJune McEnroe 2017-07-30Play nethack as ValkyrieJune McEnroe 2017-07-28Add toggle to hnelJune McEnroe 2017-07-28Install slJune McEnroe 2017-07-25Add up, supJune McEnroe 2017-07-24Autopickup ringsJune McEnroe 2017-07-24Name dogJune McEnroe 2017-07-23Add nethackrcJune McEnroe 2017-07-23Remove useless setuid in briJune McEnroe Don't you think it would be better if the setuid bit only gave you permission to do it and didn't do it for you? 2017-07-23Clean up hnel a tiny bitJune McEnroe 2017-07-21Set window size in hnelJune McEnroe 2017-07-21Add hnelJune McEnroe 2017-07-19chmod 600 in dtchJune McEnroe