about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-02-01 11:39:25 -0500
committerJune McEnroe <june@causal.agency>2021-02-01 11:39:25 -0500
commitcc8a88c059fffe5e7834cb2bfc2bb7c932a67c77 (patch)
treec8e2d14240c6edb4d35a5f11a89ffcac7f6392c4
parentCheck signals first in the loop (diff)
downloadcatsit-cc8a88c059fffe5e7834cb2bfc2bb7c932a67c77.tar.gz
catsit-cc8a88c059fffe5e7834cb2bfc2bb7c932a67c77.zip
Remove use of "%n"
https://cvsweb.openbsd.org/src/lib/libc/stdio/vfprintf.c?rev=1.79&content-type=text/x-cvsweb-markup

I think this is silly, as I've said elsewhere, and it's a shame
because that was clearly the best way to write this. Oh well.
Diffstat (limited to '')
-rw-r--r--service.c26
1 files changed, 14 insertions, 12 deletions
diff --git a/service.c b/service.c
index 6718b06..4ea9126 100644
--- a/service.c
+++ b/service.c
@@ -133,21 +133,23 @@ static const char *humanize(struct timespec interval) {
 		snprintf(buf, sizeof(buf), "%dms", (int)(interval.tv_nsec / 1000000));
 		return buf;
 	}
-	int days = interval.tv_sec / (24 * 60 * 60);
+	int d = interval.tv_sec / (24 * 60 * 60);
 	interval.tv_sec %= (24 * 60 * 60);
-	int hours = interval.tv_sec / (60 * 60);
+	int h = interval.tv_sec / (60 * 60);
 	interval.tv_sec %= (60 * 60);
-	int mins = interval.tv_sec / 60;
+	int m = interval.tv_sec / 60;
 	interval.tv_sec %= 60;
-	int d, h, m, s;
-	snprintf(
-		buf, sizeof(buf), "%n%dd %n%dh %n%dm %n%ds",
-		&d, days, &h, hours, &m, mins, &s, (int)interval.tv_sec
-	);
-	if (days) return &buf[d];
-	if (hours) return &buf[h];
-	if (mins) return &buf[m];
-	return &buf[s];
+	int s = interval.tv_sec;
+	if (d) {
+		snprintf(buf, sizeof(buf), "%dd %dh %dm %ds", d, h, m, s);
+	} else if (h) {
+		snprintf(buf, sizeof(buf), "%dh %dm %ds", h, m, s);
+	} else if (m) {
+		snprintf(buf, sizeof(buf), "%dm %ds", m, s);
+	} else {
+		snprintf(buf, sizeof(buf), "%ds", s);
+	}
+	return buf;
 }
 
 void serviceStatus(struct Service *service) {