summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ring.c39
1 files changed, 27 insertions, 12 deletions
diff --git a/ring.c b/ring.c
index 3bd177d..2d41666 100644
--- a/ring.c
+++ b/ring.c
@@ -118,15 +118,23 @@ void ringInfo(void) {
 	}
 }
 
-static const size_t FileVersion[] = {
+static const size_t Signatures[] = {
 	0x0165636E756F70, // no ring size
 	0x0265636E756F70, // time_t only
+	0x0365636E756F70,
 };
 
+static size_t signatureVersion(size_t signature) {
+	for (size_t i = 0; i < ARRAY_LEN(Signatures); ++i) {
+		if (signature == Signatures[i]) return i;
+	}
+	errx(EX_DATAERR, "unknown file signature %zX", 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) {
+static int writeTime(FILE *file, struct timeval time) {
 	return (fwrite(&time, sizeof(time), 1, file) ? 0 : -1);
 }
 static int writeString(FILE *file, const char *str) {
@@ -134,7 +142,7 @@ static int writeString(FILE *file, const char *str) {
 }
 
 int ringSave(FILE *file) {
-	if (writeSize(file, FileVersion[1])) return -1;
+	if (writeSize(file, Signatures[2])) return -1;
 	if (writeSize(file, ring.len)) return -1;
 	if (writeSize(file, producer)) return -1;
 	if (writeSize(file, consumers.len)) return -1;
@@ -143,7 +151,7 @@ int ringSave(FILE *file) {
 		if (writeSize(file, consumers.ptr[i].pos)) return -1;
 	}
 	for (size_t i = 0; i < ring.len; ++i) {
-		if (writeTime(file, ring.times[i].tv_sec)) return -1;
+		if (writeTime(file, ring.times[i])) return -1;
 	}
 	for (size_t i = 0; i < ring.len; ++i) {
 		if (!ring.lines[i]) break;
@@ -157,7 +165,12 @@ static void readSize(FILE *file, size_t *value) {
 	if (ferror(file)) err(EX_IOERR, "fread");
 	if (feof(file)) err(EX_DATAERR, "unexpected eof");
 }
-static void readTime(FILE *file, time_t *time) {
+static void readTime(FILE *file, struct timeval *time) {
+	fread(time, sizeof(*time), 1, file);
+	if (ferror(file)) err(EX_IOERR, "fread");
+	if (feof(file)) err(EX_DATAERR, "unexpected eof");
+}
+static void readTimeT(FILE *file, time_t *time) {
 	fread(time, sizeof(*time), 1, file);
 	if (ferror(file)) err(EX_IOERR, "fread");
 	if (feof(file)) err(EX_DATAERR, "unexpected eof");
@@ -168,16 +181,14 @@ static void readString(FILE *file, char **buf, size_t *cap) {
 }
 
 void ringLoad(FILE *file) {
-	size_t version;
-	fread(&version, sizeof(version), 1, file);
+	size_t signature;
+	fread(&signature, sizeof(signature), 1, file);
 	if (ferror(file)) err(EX_IOERR, "fread");
 	if (feof(file)) return;
-	if (version != FileVersion[0] && version != FileVersion[1]) {
-		errx(EX_DATAERR, "unknown file version %zX", version);
-	}
+	size_t version = signatureVersion(signature);
 
 	size_t saveLen = 4096;
-	if (version == FileVersion[1]) readSize(file, &saveLen);
+	if (version > 0) readSize(file, &saveLen);
 	if (saveLen > ring.len) {
 		errx(EX_DATAERR, "cannot load save with larger ring");
 	}
@@ -196,7 +207,11 @@ void ringLoad(FILE *file) {
 	}
 
 	for (size_t i = 0; i < saveLen; ++i) {
-		readTime(file, &ring.times[i].tv_sec);
+		if (version < 2) {
+			readTimeT(file, &ring.times[i].tv_sec);
+		} else {
+			readTime(file, &ring.times[i]);
+		}
 	}
 	for (size_t i = 0; i < saveLen; ++i) {
 		readString(file, &buf, &cap);
b45dbe2cd833995a87cb9c1cdcd5dead3d4ad17f&follow=1'>Add "Come On Petunia"June McEnroe 2020-11-13Add x4 to LESSJune McEnroe 2020-11-04Remove modified sensitivity settingsJune McEnroe 2020-10-29Remove editJune McEnroe 2020-10-27Switch gr alias back to git rebaseJune McEnroe I always type out git reset and sometimes still expect gr to be rebase... Never got used to it I guess. 2020-10-27Allow cd host: to cd to same path over sshJune McEnroe 2020-10-27Use SendEnv for cd host:pathJune McEnroe Works properly for weird paths, etc. 2020-10-27Allow cd host:path over sshJune McEnroe Requires AcceptEnv SSH_CD in the remote sshd_config. 2020-10-07Use mandoc -T utf8 for text.June McEnroe Don't depend on LANG being set. 2020-09-20Add The Awakened KingdomJune McEnroe A cute extra novella. Finally finished this series. 2020-09-12Move /opt/local back, cheat port select to use system manJune McEnroe This is not really how you're supposed to use the select system, I don't think, since the mandoc package actually creates those files, but it does work. This lets me actually use the git installed by MacPorts. 2020-09-12Move /opt/local behind /usr againJune McEnroe The reason I did this with pkgsrc was because I actually don't want the man(1) from mandoc, since it won't follow MANSECT. Same applies to MacPorts. I wish I could disable its man(1) with a variant or whatever. 2020-09-12Enable toc in cgit renderings of man pagesJune McEnroe But keep it disabled for READMEs since they always use non-standard sections and the TOC is just distracting there, I think. Also add the style so its h1 is the same size as the ones inside sections... 2020-09-11Install mandoc on macOSJune McEnroe 2020-09-11Rewrite install script yet againJune McEnroe 2020-09-11Remove NetBSD from install scriptJune McEnroe I never use it. 2020-09-11Use MacPorts rather than pkgsrcJune McEnroe My system is probably such a mess now... 2020-09-11Add debian VM name to sshJune McEnroe 2020-09-11Add influencer tweetJune McEnroe 2020-09-10Add The Kingdom of GodsJune McEnroe Reading has really slowed down :( 2020-09-07Add SunglassesJune McEnroe An IRC find. 2020-09-06Add Between the BreathsJune McEnroe One of those good songs from a soundtrack of a film that probably isn't? The summary sounds a lot more interesting than the title implies, at least. 2020-09-04Open /dev/tty in nudgeJune McEnroe This makes it work even when it's run connected to a pipe, i.e. as the notify command of catgirl... 2020-09-04Add nudgeJune McEnroe 2020-09-03Build fbclock with -lzJune McEnroe I guess this got lost somewhere, long ago... 2020-08-29Add tweets from retweetsJune McEnroe