mblaze

Unix utilities to deal with Maildir - my mirror
git clone https://pi.duncano.de/git/mblaze.git
Log | Files | Refs | README | COPYING

commit 86793fb26296b59b4ba333e587b6a2ebc718e380
parent 938b2c2d31617ce7ae1d674f82f520c070153198
Author: Leah Neukirchen <leah@vuxu.org>
Date:   Mon, 12 Jun 2017 15:49:08 +0200

mexport: support reading from pipes, better error reporting

Diffstat:
mexport.c | 55+++++++++++++++++++++++++++++++------------------------
1 file changed, 31 insertions(+), 24 deletions(-)

diff --git a/mexport.c b/mexport.c @@ -23,37 +23,45 @@ export(char *file) while (*file == ' ' || *file == '\t') file++; - msg = blaze822(file); - if (!msg) + FILE *infile = fopen(file, "r"); + if (!infile) { + status = 1; + fprintf(stderr, "mexport: error opening '%s': %s\n", + file, strerror(errno)); return; + } char from[1024] = "nobody"; + time_t date = -1; - char *v; - if ((v = blaze822_hdr(msg, "return-path")) || - (v = blaze822_hdr(msg, "x-envelope-from"))) { - char *s = strchr(v, '<'); - char *e = strchr(s, '>'); - if (s && e) { - e++; - memcpy(from, s, e-s); - from[e-s] = 0; + if (fseek(infile, 0L, SEEK_SET) && errno == ESPIPE) { + date = time(0); + memcpy(from, "stdin", 6); + } else { + msg = blaze822(file); + if (!msg) + return; + + char *v; + if ((v = blaze822_hdr(msg, "return-path")) || + (v = blaze822_hdr(msg, "x-envelope-from"))) { + char *s = strchr(v, '<'); + char *e = strchr(s, '>'); + if (s && e) { + e++; + memcpy(from, s, e-s); + from[e-s] = 0; + } } - } - time_t date = -1; - if ((v = blaze822_hdr(msg, "date"))) { - date = blaze822_date(v); + if ((v = blaze822_hdr(msg, "date"))) { + date = blaze822_date(v); + } + blaze822_free(msg); } char *line = 0; size_t linelen = 0; - - FILE *infile = fopen(file, "r"); - if (!infile) { - status = 1; - return; - } printf("From %s %s", from, ctime(&date)); @@ -66,7 +74,8 @@ export(char *file) if (rd == -1) { if (errno == 0) break; - // XXX print error? + fprintf(stderr, "mexport: error reading '%s': %s\n", + file, strerror(errno)); status = 1; return; } @@ -112,8 +121,6 @@ export(char *file) putchar('\n'); fclose(infile); - - blaze822_free(msg); } int