diff options
author | Benjamin Chausse <benjamin@chausse.xyz> | 2024-08-09 14:11:50 -0400 |
---|---|---|
committer | Benjamin Chausse <benjamin@chausse.xyz> | 2024-08-09 14:11:50 -0400 |
commit | 5857d82e8e596d6fda406a0c4d8d68ca7a03c124 (patch) | |
tree | 553916894dee907825360580c5d9a05c82c5af16 /sfeed_mbox.c | |
parent | 3574e3cbf9d99546e868aeb995ce2c171cdc36a6 (diff) | |
parent | 19957bc272e745af7b56b79fa648e8b6b77113b1 (diff) |
Diffstat (limited to 'sfeed_mbox.c')
-rw-r--r-- | sfeed_mbox.c | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/sfeed_mbox.c b/sfeed_mbox.c index cd6e65d..c00971f 100644 --- a/sfeed_mbox.c +++ b/sfeed_mbox.c @@ -22,33 +22,35 @@ djb2(unsigned char *s, unsigned long long hash) } /* Unescape / decode fields printed by string_print_encoded() - * "\\" to "\", "\t", to TAB, "\n" to newline. Unrecognised escape sequences - * are ignored: "\z" etc. Mangle "From " in mboxrd style (always prefix >). */ + * "\\" to "\", "\t", to TAB, "\n" to newline. Other escape sequences are + * ignored: "\z" etc. Mangle "From " in mboxrd style (always prefix >). */ static void printcontent(const char *s, FILE *fp) { escapefrom: for (; *s == '>'; s++) - fputc('>', fp); + putc('>', fp); /* escape "From ", mboxrd-style. */ if (!strncmp(s, "From ", 5)) - fputc('>', fp); + putc('>', fp); for (; *s; s++) { switch (*s) { case '\\': + if (*(s + 1) == '\0') + break; s++; switch (*s) { case 'n': - fputc('\n', fp); + putc('\n', fp); s++; goto escapefrom; - case '\\': fputc('\\', fp); break; - case 't': fputc('\t', fp); break; + case '\\': putc('\\', fp); break; + case 't': putc('\t', fp); break; } break; default: - fputc(*s, fp); break; + putc(*s, fp); break; } } } @@ -63,7 +65,8 @@ printfeed(FILE *fp, const char *feedname) ssize_t linelen; int ishtml; - while ((linelen = getline(&line, &linesize, fp)) > 0) { + while ((linelen = getline(&line, &linesize, fp)) > 0 && + !ferror(stdout)) { if (line[linelen - 1] == '\n') line[--linelen] = '\0'; hash = djb2((unsigned char *)line, 5381ULL); @@ -81,7 +84,7 @@ printfeed(FILE *fp, const char *feedname) printf("Date: %s\n", dtimebuf); /* invalid/missing: use current time */ } - printf("From: %s <sfeed@>\n", fields[FieldAuthor][0] ? fields[FieldAuthor] : feedname); + printf("From: %s <anonymous@>\n", fields[FieldAuthor][0] ? fields[FieldAuthor] : feedname); printf("To: %s <%s@%s>\n", user, user, host); printf("Subject: %s\n", fields[FieldTitle]); printf("Message-ID: <%s%s%llu@%s>\n", @@ -152,8 +155,8 @@ main(int argc, char *argv[]) user = "you"; if (gethostname(host, sizeof(host)) == -1) err(1, "gethostname"); - if ((now = time(NULL)) == -1) - err(1, "time"); + if ((now = time(NULL)) == (time_t)-1) + errx(1, "time"); if (!gmtime_r(&now, &tmnow)) err(1, "gmtime_r: can't get current time"); if (!strftime(mtimebuf, sizeof(mtimebuf), "%a %b %d %H:%M:%S %Y", &tmnow)) @@ -163,17 +166,20 @@ main(int argc, char *argv[]) if (argc == 1) { printfeed(stdin, ""); + checkfileerror(stdin, "<stdin>", 'r'); } else { for (i = 1; i < argc; i++) { if (!(fp = fopen(argv[i], "r"))) err(1, "fopen: %s", argv[i]); name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i]; printfeed(fp, name); - if (ferror(fp)) - err(1, "ferror: %s", argv[i]); + checkfileerror(fp, argv[i], 'r'); + checkfileerror(stdout, "<stdout>", 'w'); fclose(fp); } } + checkfileerror(stdout, "<stdout>", 'w'); + return 0; } |