From 669af4ffcae6d6515f8de7a53c407ca26d1263c6 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Sun, 9 May 2021 14:43:00 +0200 Subject: mdeliver: ignore last empty line of mbox entries https://www.loc.gov/preservation/digital/formats/fdd/fdd000383.shtml > Each message is immediately prefaced by a separation line and > terminated by an empty line. Bug discovered by skarnet. Fixes #207. --- mdeliver.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'mdeliver.c') diff --git a/mdeliver.c b/mdeliver.c index 27c4b9c..c9e00c5 100644 --- a/mdeliver.c +++ b/mdeliver.c @@ -147,6 +147,8 @@ try_again: int in_header = 1; int is_old = 0; + int prev_line_empty = 0; + int this_line_empty = 0; // only for mbox parsing while (1) { errno = 0; ssize_t rd = getdelim(&line, &linelen, '\n', infile); @@ -158,8 +160,12 @@ try_again: char *line_start = line; if (line[0] == '\n' && (!line[1] || - (line[1] == '\r' && !line[2]))) + (line[1] == '\r' && !line[2]))) { + this_line_empty = Mflag ? 1 : 0; in_header = 0; + } else { + this_line_empty = 0; + } if (Mflag && strncmp("From ", line, 5) == 0) break; @@ -189,8 +195,15 @@ try_again: } } - if (fwrite(line_start, 1, rd, outfile) != (size_t)rd) - goto fail; + // print delayed empty line + if (prev_line_empty) + if (fputc('\n', outfile) == EOF) + goto fail; + if (!this_line_empty) + if (fwrite(line_start, 1, rd, outfile) != (size_t)rd) + goto fail; + + prev_line_empty = this_line_empty; } if (fflush(outfile) == EOF) goto fail; -- cgit 1.4.1