mexport: support reading from pipes, better error reporting

pull/37/head
Leah Neukirchen 7 years ago
parent 938b2c2d31
commit 86793fb262

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

Loading…
Cancel
Save