summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-10-24 07:36:04 -0400
committerJune McEnroe <june@causal.agency>2020-10-24 07:36:04 -0400
commit457e4661d8c96d14382aebf6943294b5c1a83ffd (patch)
tree7c2195671b4026b00379a07774a174f97262277a
parentHandle signals before the main loop (diff)
downloadpounce-457e4661d8c96d14382aebf6943294b5c1a83ffd.tar.gz
pounce-457e4661d8c96d14382aebf6943294b5c1a83ffd.zip
contrib/palaver: Use open_memstream instead of fmemopen
Somehow I never knew about this function. Much better than fmemopen with
mode "w".
-rw-r--r--contrib/palaver/notify.c22
1 files changed, 11 insertions, 11 deletions
diff --git a/contrib/palaver/notify.c b/contrib/palaver/notify.c
index a8ca220..e41f708 100644
--- a/contrib/palaver/notify.c
+++ b/contrib/palaver/notify.c
@@ -533,16 +533,15 @@ static void handleReplyUnaway(struct Message *msg) {
 static bool noPreview;
 static bool noPrivatePreview;
 
-static void jsonBody(
-	char *buf, size_t cap,
-	struct Message *msg, const char *network, bool preview
-) {
+static char *jsonBody(struct Message *msg, const char *network, bool preview) {
 	bool private = (msg->params[0][0] != '#');
 	if (private && noPrivatePreview) preview = false;
 	if (noPreview) preview = false;
 
-	FILE *file = fmemopen(buf, cap, "w");
-	if (!file) err(EX_OSERR, "fmemopen");
+	char *buf;
+	size_t len;
+	FILE *file = open_memstream(&buf, &len);
+	if (!file) err(EX_OSERR, "open_memstream");
 
 	fprintf(file, "{\"badge\":%d", badge);
 	fprintf(file, ",\"sender\":");
@@ -570,9 +569,10 @@ static void jsonBody(
 	}
 	fprintf(file, "}");
 
-	// XXX: fmemopen only null-terminates if there is room.
-	fclose(file);
-	buf[cap - 1] = '\0';
+	int error = fclose(file);
+	if (error) err(EX_IOERR, "fclose");
+
+	return buf;
 }
 
 static void handlePrivmsg(struct Message *msg) {
@@ -600,13 +600,13 @@ static void handlePrivmsg(struct Message *msg) {
 		const char *preview = sqlite3_column_text(stmts[Notify], i++);
 		const char *network = sqlite3_column_text(stmts[Notify], i++);
 
-		char body[4096];
 		if (!badged) {
 			badge++;
 			badged = true;
 		}
-		jsonBody(body, sizeof(body), msg, network, !strcmp(preview, "true"));
+		char *body = jsonBody(msg, network, !strcmp(preview, "true"));
 		pushNotify(endpoint, token, body);
+		free(body);
 	}
 	if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db));
 	sqlite3_reset(stmts[Notify]);
=7d9fcc56afeb3fff34c11fd19b2a28d0253b4add&follow=1'>Add BintiJune McEnroe 2021-02-07Finish configuring xtermJune McEnroe 2021-02-06Enable tapping, reverse scroll, set scaling in wsconsctlJune McEnroe 2021-02-06Set root window to black on purple snowJune McEnroe 2021-02-06Add xmodmap configurationJune McEnroe 2021-02-06Add initial OpenBSD X configurationJune McEnroe 2021-02-06Add xterm output to schemeJune McEnroe