From: David Levine Date: Sun, 22 Dec 2013 22:42:34 +0000 (-0600) Subject: file --mime on OpenBSD returns strings such as "text/plain X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/ad47ad24fe6707b96237b09e43dfbb4c3e2a2a82?ds=inline;hp=c4bbc2137e04f0be80fa008b97d65656e3e11ec5 file --mime on OpenBSD returns strings such as "text/plain charset=us-ascii", without an intervening semilcolon. Insert one if there isn't one. --- diff --git a/uip/sendsbr.c b/uip/sendsbr.c index 6d76e9cd..bfd65157 100644 --- a/uip/sendsbr.c +++ b/uip/sendsbr.c @@ -613,8 +613,10 @@ mime_type (const char *file_name) { if ((int) snprintf (cmd, sizeof cmd, mimetypeproc, file_name) < (int) sizeof cmd) { if ((fp = popen (cmd, "r")) != NULL) { - if (fgets (buf, sizeof buf, fp)) { - char *cp; + /* 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; /* Skip leading :, if present. */ if ((content_type = strchr (buf, ':')) != NULL) { @@ -630,6 +632,23 @@ mime_type (const char *file_name) { if ((cp = strpbrk (content_type, "\n\n")) != NULL) { *cp = '\0'; } + + /* If necessary, insert semicolon between content type + and charset. Assume that the first space is between + them. */ + if ((space = strchr (content_type, ' ')) != NULL) { + ssize_t len = strlen (content_type); + + if (space - content_type > 0 && + len > space - content_type + 1) { + if (*(space - 1) != ';') { + /* The +1 is for the terminating NULL. */ + memmove (space + 1, space, + len - (space - content_type) + 1); + *space = ';'; + } + } + } } else { advise (NULL, "unable to read mime type"); }