summary refs log tree commit diff
diff options
context:
space:
mode:
-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);
 	}
 }