summary refs log tree commit diff
path: root/mbox.c
diff options
context:
space:
mode:
Diffstat (limited to 'mbox.c')
-rw-r--r--mbox.c33
1 files changed, 17 insertions, 16 deletions
diff --git a/mbox.c b/mbox.c
index f8684de..3412129 100644
--- a/mbox.c
+++ b/mbox.c
@@ -27,28 +27,29 @@ int mboxFrom(FILE *file) {
 	return (n < 0 ? n : 0);
 }
 
-int mboxHeader(FILE *file, char *header) {
-	for (char *crlf; (crlf = strstr(header, "\r\n")); header = &crlf[2]) {
-		*crlf = '\0';
-		int n = fprintf(file, "%s\n", header);
-		if (n < 0) return n;
+int mboxHeader(FILE *file, const char *header) {
+	for (const char *crlf; (crlf = strstr(header, "\r\n")); header = &crlf[2]) {
+		if (crlf - header) {
+			size_t n = fwrite(header, crlf - header, 1, file);
+			if (!n) return -1;
+		}
+		if (fprintf(file, "\n") < 0) return -1;
 	}
 	return 0;
 }
 
-int mboxBody(FILE *file, char *body) {
-	int n;
-	for (char *crlf; (crlf = strstr(body, "\r\n")); body = &crlf[2]) {
-		*crlf = '\0';
-		char *from = body;
+int mboxBody(FILE *file, const char *body) {
+	for (const char *crlf; (crlf = strstr(body, "\r\n")); body = &crlf[2]) {
+		const char *from = body;
 		while (*from == '>') from++;
 		if (!strncmp(from, "From ", 5)) {
-			n = fprintf(file, ">%s\n", body);
-		} else {
-			n = fprintf(file, "%s\n", body);
+			if (fprintf(file, ">") < 0) return -1;
+		}
+		if (crlf - body) {
+			size_t n = fwrite(body, crlf - body, 1, file);
+			if (!n) return -1;
 		}
-		if (n < 0) return n;
+		if (fprintf(file, "\n") < 0) return -1;
 	}
-	n = fprintf(file, "\n");
-	return (n < 0 ? n : 0);
+	return (fprintf(file, "\n" ) < 0 ? -1 : 0);
 }
cEnroe 2020-02-11Move base64 out of chat.hJune McEnroe 2020-02-11Move XDG_SUBDIR out of chat.hJune McEnroe 2020-02-11Fix whois idle unit calculationJune McEnroe 2020-02-11Cast towupper to wchar_tJune McEnroe 2020-02-11Cast set but unused variables to voidJune McEnroe 2020-02-11Declare strlcatJune McEnroe 2020-02-11Check if VDSUSP existsJune McEnroe 2020-02-11Fix completeReplace iterationJune McEnroe 2020-02-11Use pkg(8) to configure on FreeBSDJune McEnroe 2020-02-11Remove legacy codeJune McEnroe 2020-02-11Add INSTALLING section to READMEJune McEnroe