- char cmd[2 * PATH_MAX + 2]; /* file command buffer */
- char buf[BUFSIZ >= 2048 ? BUFSIZ : 2048];
- FILE *fp; /* content and pipe file pointer */
- char mimetypeproc[] = MIMETYPEPROC " '%s'";
-
- if ((int) snprintf (cmd, sizeof cmd, mimetypeproc, file_name) <
- (int) sizeof cmd) {
- if ((fp = popen (cmd, "r")) != NULL) {
- /* Make sure that buf has space for one additional
- character, the semicolon that might be added below. */
- if (fgets (buf, sizeof buf - 1, fp)) {
- char *cp, *space;
+ char *mimetype;
+
+ if ((mimetype = get_file_info(MIMETYPEPROC, file_name))) {
+#ifdef MIMEENCODINGPROC
+ /* Try to append charset for text content. */
+ char *mimeencoding;
+
+ if (strncasecmp(mimetype, "text", 4) == 0) {
+ if ((mimeencoding = get_file_info(MIMEENCODINGPROC, file_name))) {
+ content_type = concat(mimetype, "; charset=", mimeencoding,
+ NULL);
+ } else {
+ content_type = strdup(mimetype);
+ }
+ } else {
+ content_type = strdup(mimetype);
+ }
+#else /* MIMEENCODINGPROC */
+ content_type = strdup(mimetype);
+#endif /* MIMEENCODINGPROC */
+ }
+#else /* MIMETYPEPROC */
+ NMH_UNUSED(file_name);
+#endif /* MIMETYPEPROC */
+
+ return content_type;
+}
+
+
+#ifdef MIMETYPEPROC
+/*
+ * Get information using proc about a file.
+ */
+static char *
+get_file_info(const char *proc, const char *file_name) {
+ char *cmd, *cp;
+ char *quotec = "'";
+
+ if ((cp = strchr(file_name, '\''))) {
+ /* file_name contains a single quote. */
+ if (strchr(file_name, '"')) {
+ advise(NULL, "filenames containing both single and double quotes "
+ "are unsupported for attachment");
+ return NULL;
+ } else {
+ quotec = "\"";
+ }
+ }
+
+ cmd = concat(proc, " ", quotec, file_name, quotec, NULL);
+ if ((cmd = concat(proc, " ", quotec, file_name, quotec, NULL))) {
+ FILE *fp;
+
+ if ((fp = popen(cmd, "r")) != NULL) {
+ char buf[BUFSIZ >= 2048 ? BUFSIZ : 2048];
+
+ buf[0] = '\0';
+ if (fgets(buf, sizeof buf, fp)) {
+ char *eol;