about summary refs log tree commit diff
path: root/mshow.c
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-07-29 15:08:16 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2016-07-29 15:08:16 +0200
commit5d1266b38f35916d3ce48cc07052c459c802a7d3 (patch)
treeba934be080d12a79999c9bcb680c338040a82960 /mshow.c
parenta1ed8d7b6c1d140351aa9781e7d41655a38e6681 (diff)
downloadmblaze-5d1266b38f35916d3ce48cc07052c459c802a7d3.tar.gz
mblaze-5d1266b38f35916d3ce48cc07052c459c802a7d3.tar.xz
mblaze-5d1266b38f35916d3ce48cc07052c459c802a7d3.zip
mshow: normalize CRLF for plain text parts
Diffstat (limited to 'mshow.c')
-rw-r--r--mshow.c28
1 files changed, 24 insertions, 4 deletions
diff --git a/mshow.c b/mshow.c
index e41f0fb..9c1be33 100644
--- a/mshow.c
+++ b/mshow.c
@@ -47,6 +47,26 @@ printhdr(char *hdr)
 	}
 }
 
+int
+print_ascii(char *body, size_t bodylen)
+{
+	if (!memchr(body, '\r', bodylen))
+		return fwrite(body, 1, bodylen, stdout);
+
+	// crlf normalization required
+	size_t i;
+	for (i = 0; i < bodylen; i++) {
+		if (body[i] == '\r') {
+			if (!(i+1 < bodylen && body[i+1] == '\n'))
+				putc_unlocked('\n', stdout);
+			continue;
+		}
+		putc_unlocked(body[i], stdout);
+	}
+
+	return bodylen;
+}
+
 void
 print_u8recode(char *body, size_t bodylen, char *srcenc)
 {
@@ -67,7 +87,7 @@ print_u8recode(char *body, size_t bodylen, char *srcenc)
 		size_t r = iconv(ic, &body, &bodylen, &bufptr, &buflen);
 
 		if (bufptr != buf) {
-			fwrite(buf, 1, bufptr-buf, stdout);
+			print_ascii(buf, bufptr-buf);
 			final_char = bufptr[-1];
 		}
 
@@ -76,7 +96,7 @@ print_u8recode(char *body, size_t bodylen, char *srcenc)
 			buflen = sizeof buf;
 			r = iconv(ic, 0, 0, &bufptr, &buflen);
 			if (bufptr != buf) {
-				fwrite(buf, 1, bufptr-buf, stdout);
+				print_ascii(buf, bufptr-buf);
 				final_char = bufptr[-1];
 			}
 			if (r != (size_t)-1)
@@ -205,7 +225,7 @@ nofilter:
 			    strcasecmp(charset, "utf-8") == 0 ||
 			    strcasecmp(charset, "utf8") == 0 ||
 			    strcasecmp(charset, "us-ascii") == 0)
-				fwrite(body, 1, bodylen, stdout);
+				print_ascii(body, bodylen);
 			else
 				print_u8recode(body, bodylen, charset);
 			free(charset);
@@ -248,7 +268,7 @@ reply_mime(int depth, struct message *msg, char *body, size_t bodylen)
 		    strcasecmp(charset, "utf-8") == 0 ||
 		    strcasecmp(charset, "utf8") == 0 ||
 		    strcasecmp(charset, "us-ascii") == 0)
-			fwrite(body, 1, bodylen, stdout);
+			print_ascii(body, bodylen);
 		else
 			print_u8recode(body, bodylen, charset);
 		free(charset);