about summary refs log tree commit diff
path: root/ring.c
diff options
context:
space:
mode:
Diffstat (limited to 'ring.c')
-rw-r--r--ring.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/ring.c b/ring.c
index e675526..07b66d8 100644
--- a/ring.c
+++ b/ring.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
+/* Copyright (C) 2019  June McEnroe <june@causal.agency>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -27,6 +27,8 @@
 
 #include <assert.h>
 #include <err.h>
+#include <inttypes.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/time.h>
@@ -42,7 +44,7 @@ static struct {
 
 void ringAlloc(size_t len) {
 	if (len & (len - 1)) {
-		errx(EX_CONFIG, "ring length must be power of two: %zu", len);
+		errx(EX_USAGE, "ring length must be power of two: %zu", len);
 	}
 	ring.lines = calloc(len, sizeof(*ring.lines));
 	if (!ring.lines) err(EX_OSERR, "calloc");
@@ -51,7 +53,7 @@ void ringAlloc(size_t len) {
 	ring.len = len;
 }
 
-size_t producer;
+static size_t producer;
 
 void ringProduce(const char *line) {
 	size_t i = producer++ & (ring.len - 1);
@@ -110,10 +112,12 @@ size_t ringDiff(size_t consumer) {
 const char *ringPeek(struct timeval *time, size_t consumer) {
 	if (!ringDiff(consumer)) return NULL;
 	if (ringDiff(consumer) > ring.len) {
-		warnx(
-			"consumer %s dropped %zu messages",
-			consumers.ptr[consumer].name, ringDiff(consumer) - ring.len
-		);
+		if (consumers.ptr[consumer].pos) {
+			warnx(
+				"consumer %s dropped %zu messages",
+				consumers.ptr[consumer].name, ringDiff(consumer) - ring.len
+			);
+		}
 		consumers.ptr[consumer].pos = producer - ring.len;
 	}
 	size_t i = consumers.ptr[consumer].pos & (ring.len - 1);
@@ -139,17 +143,17 @@ void ringInfo(void) {
 	}
 }
 
-static const size_t Signatures[] = {
+static const uint64_t Signatures[] = {
 	0x0165636E756F70, // no ring size
 	0x0265636E756F70, // time_t only
 	0x0365636E756F70,
 };
 
-static size_t signatureVersion(size_t signature) {
+static size_t signatureVersion(uint64_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);
+	errx(EX_DATAERR, "unknown file signature %" PRIX64, signature);
 }
 
 static int writeSize(FILE *file, size_t value) {
@@ -163,7 +167,7 @@ static int writeString(FILE *file, const char *str) {
 }
 
 int ringSave(FILE *file) {
-	if (writeSize(file, Signatures[2])) return -1;
+	if (!fwrite(&Signatures[2], sizeof(*Signatures), 1, file)) return -1;
 	if (writeSize(file, ring.len)) return -1;
 	if (writeSize(file, producer)) return -1;
 	if (writeSize(file, consumers.len)) return -1;
@@ -202,7 +206,7 @@ static void readString(FILE *file, char **buf, size_t *cap) {
 }
 
 void ringLoad(FILE *file) {
-	size_t signature;
+	uint64_t signature;
 	fread(&signature, sizeof(signature), 1, file);
 	if (ferror(file)) err(EX_IOERR, "fread");
 	if (feof(file)) return;