about summary refs log tree commit diff
path: root/mmime.c
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-08-02 14:09:28 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2016-08-02 14:09:28 +0200
commitc4822a983be3726e6a07cabb245680c8b728e940 (patch)
tree59b62479ce34b3e500dc1fa1fc94fe885a09505e /mmime.c
parent2073102d4d2231dd2f341c9dc8bde9ea38274369 (diff)
downloadmblaze-c4822a983be3726e6a07cabb245680c8b728e940.tar.gz
mblaze-c4822a983be3726e6a07cabb245680c8b728e940.tar.xz
mblaze-c4822a983be3726e6a07cabb245680c8b728e940.zip
mmime: add -c to check if MIME-encoding is needed
Diffstat (limited to 'mmime.c')
-rw-r--r--mmime.c43
1 files changed, 40 insertions, 3 deletions
diff --git a/mmime.c b/mmime.c
index 93eb01b..8dc5a59 100644
--- a/mmime.c
+++ b/mmime.c
@@ -17,6 +17,7 @@
 
 #include "blaze822.h"
 
+static int cflag;
 static int rflag;
 
 int gen_b64(uint8_t *s, off_t size)
@@ -322,7 +323,7 @@ gen_build()
 
 		gen_qp((uint8_t *)line, strlen(line), 78, 0);
 	}
-	if (!rflag)
+	if (!rflag && !inheader)
 		printf("--%s--\n", sep);
 
 	free(line);
@@ -330,22 +331,58 @@ gen_build()
 }
 
 int
+check()
+{
+	off_t bithigh = 0;
+	off_t bitlow = 0;
+	off_t linelen = 0;
+	off_t maxlinelen = 0;
+
+	int c;
+	int l = -1;
+
+	while ((c = getchar()) != EOF) {
+		if (c == '\n') {
+			if (maxlinelen < linelen)
+				maxlinelen = linelen;
+			linelen = 0;
+		} else {
+			linelen++;
+		}
+		if (c != '\t' && c != '\n' && c < 32)
+			bitlow++;
+		if (c > 127)
+			bithigh++;
+		l = c;
+	}
+
+	if (bitlow == 0 && bithigh == 0 && maxlinelen <= 72 && l == '\n')
+		return 0;
+	else
+		return 1;
+}
+
+int
 main(int argc, char *argv[])
 {
 	srand48(time(0) ^ getpid());
 
 	int c;
-	while ((c = getopt(argc, argv, "r")) != -1)
+	while ((c = getopt(argc, argv, "cr")) != -1)
 		switch(c) {
 		case 'r': rflag = 1; break;
+		case 'c': cflag = 1; break;
 		default:
 		usage:
-			fprintf(stderr, "Usage: mmime [-r] < message\n");
+			fprintf(stderr, "Usage: mmime [-c|-r] < message\n");
 			exit(1);
 		}
 
 	if (argc != optind)
 		goto usage;
 
+	if (cflag)
+		return check();
+
 	return gen_build();
 }