about summary refs log tree commit diff
path: root/mexport.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2017-06-12 15:49:08 +0200
committerLeah Neukirchen <leah@vuxu.org>2017-06-12 15:49:08 +0200
commit86793fb26296b59b4ba333e587b6a2ebc718e380 (patch)
tree82ab910577cd0a1f52e5ef994eebb457a97a3994 /mexport.c
parent938b2c2d31617ce7ae1d674f82f520c070153198 (diff)
downloadmblaze-86793fb26296b59b4ba333e587b6a2ebc718e380.tar.gz
mblaze-86793fb26296b59b4ba333e587b6a2ebc718e380.tar.xz
mblaze-86793fb26296b59b4ba333e587b6a2ebc718e380.zip
mexport: support reading from pipes, better error reporting
Diffstat (limited to 'mexport.c')
-rw-r--r--mexport.c55
1 files changed, 31 insertions, 24 deletions
diff --git a/mexport.c b/mexport.c
index 60a986d..ee9f126 100644
--- 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