summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-11-29 17:40:32 -0500
committerJune McEnroe <june@causal.agency>2020-11-29 17:40:32 -0500
commit635cf1b47bb2dcd26cdca15e51d4ecfe4a8d04e0 (patch)
tree168a42ad486468cc1ad3082b4758de24dbf41ebc
parentFactor out uint32_t stringify (diff)
downloadbubger-635cf1b47bb2dcd26cdca15e51d4ecfe4a8d04e0.tar.gz
bubger-635cf1b47bb2dcd26cdca15e51d4ecfe4a8d04e0.zip
Use open_memstream for section specs
Diffstat (limited to '')
-rw-r--r--export.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/export.c b/export.c
index d4039e2..1b6d298 100644
--- a/export.c
+++ b/export.c
@@ -160,17 +160,19 @@ static void exportAtom(
 	free(path);
 }
 
-static const char *sectionName(struct List section) {
-	static char buf[1024];
-	char str[32];
-	buf[0] = '\0';
+static char *sectionSpec(struct List section) {
+	char *buf;
+	size_t len;
+	FILE *file = open_memstream(&buf, &len);
+	if (!file) err(EX_OSERR, "open_memstream");
 	for (size_t i = 0; i < section.len; ++i) {
-		snprintf(
-			str, sizeof(str), "%s%" PRIu32,
+		fprintf(
+			file, "%s%" PRIu32,
 			(i ? "." : ""), dataCheck(section.ptr[i], Number).number
 		);
-		strlcat(buf, str, sizeof(buf));
 	}
+	int error = fclose(file);
+	if (error) err(EX_OSERR, "open_memstream");
 	return buf;
 }
 
@@ -184,9 +186,10 @@ static int exportHTMLAttachment(
 	const char *disposition = part->disposition.type;
 	if (!disposition) disposition = "INLINE";
 
+	char *spec = sectionSpec(section);
 	struct Variable vars[] = {
 		{ "messageID", envelope->messageID },
-		{ "section", sectionName(section) },
+		{ "section", spec },
 		{ "name", (name ? name : "") },
 		{ "disposition", (name ? "" : disposition) },
 		{ ".", (name ? "" : ".") },
@@ -211,7 +214,9 @@ static int exportHTMLAttachment(
 	if (error) err(EX_IOERR, "%s", path);
 	free(path);
 
-	return htmlAttachment(file, part, vars);
+	error = htmlAttachment(file, part, vars);
+	free(spec);
+	return error;
 }
 
 static int exportHTMLBody(
@@ -310,10 +315,12 @@ static void fetchParts(
 	) {
 		fetchParts(imap, section, structure->message.structure);
 	} else {
+		char *spec = sectionSpec(*section);
 		fprintf(
 			imap, " BODY[%s%s]",
-			sectionName(*section), (structure->message.structure ? ".TEXT" : "")
+			spec, (structure->message.structure ? ".TEXT" : "")
 		);
+		free(spec);
 	}
 }