]>
diplodocus.org Git - nmh/blob - sbr/mime_type.c
1 /* mime_type.c -- routine to determine the MIME Content-Type of a file
3 * This code is Copyright (c) 2014, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
11 #include "mime_type.h"
14 static char *get_file_info(const char *, const char *);
15 #endif /* MIMETYPEPROC */
18 * Try to use external command to determine mime type, and possibly
19 * encoding. If that fails try using the filename extension. Caller
20 * is responsible for free'ing returned memory.
23 mime_type(const char *file_name
) {
24 char *content_type
= NULL
; /* mime content type */
26 static int loaded_defaults
= 0;
31 if ((mimetype
= get_file_info(MIMETYPEPROC
, file_name
))) {
32 #ifdef MIMEENCODINGPROC
33 /* Try to append charset for text content. */
36 if (!strncasecmp(mimetype
, "text", 4) &&
37 (mimeencoding
= get_file_info(MIMEENCODINGPROC
, file_name
))) {
38 content_type
= concat(mimetype
, "; charset=", mimeencoding
, NULL
);
42 content_type
= mimetype
;
43 #else /* MIMEENCODINGPROC */
44 content_type
= mimetype
;
45 #endif /* MIMEENCODINGPROC */
47 #endif /* MIMETYPEPROC */
50 * If we didn't get the MIME type from the contents (or we don't support
51 * the necessary command) then use the mhshow suffix.
54 if (content_type
== NULL
) {
55 struct node
*np
; /* Content scan node pointer */
56 FILE *fp
; /* File pointer for mhn.defaults */
58 if (! loaded_defaults
&&
59 (fp
= fopen(p
= etcpath("mhn.defaults"), "r"))) {
61 readconfig(NULL
, fp
, p
, 0);
65 if ((p
= strrchr(file_name
, '.')) != NULL
) {
66 for (np
= m_defs
; np
; np
= np
->n_next
) {
67 if (strncasecmp(np
->n_name
, "mhshow-suffix-", 14) == 0 &&
68 strcasecmp(p
, FENDNULL(np
->n_field
)) == 0) {
69 content_type
= strdup(np
->n_name
+ 14);
76 * If we didn't match any filename extension, try to infer the
77 * content type. If we have binary, assume application/octet-stream;
78 * otherwise, assume text/plain.
81 if (content_type
== NULL
) {
85 if (!(fp
= fopen(file_name
, "r"))) {
86 inform("unable to access file \"%s\"", file_name
);
90 while ((c
= getc(fp
)) != EOF
) {
91 if (! isascii(c
) || c
== 0) {
100 strdup(binary
? "application/octet-stream" : "text/plain");
110 * Get information using proc about a file.
111 * Non-null return value must be free(3)'d. */
113 get_file_info(const char *proc
, const char *file_name
)
119 char buf
[max(BUFSIZ
, 2048)];
123 if (strchr(file_name
, '\'')) {
124 if (strchr(file_name
, '"')) {
125 inform("filenames containing both single and double quotes "
126 "are unsupported for attachment");
133 cmd
= concat(proc
, " ", quotec
, file_name
, quotec
, NULL
);
135 inform("concat with \"%s\" failed, out of memory?", proc
);
139 if ((fp
= popen(cmd
, "r")) == NULL
) {
140 inform("no output from %s", cmd
);
145 ok
= fgets(buf
, sizeof buf
, fp
);
151 /* s#^.*:[ \t]*##. */
153 if ((needle
= strchr(info
, ':'))) {
155 while (isblank((unsigned char)*info
))
160 if ((needle
= strpbrk(info
, "\n\r")))
165 #endif /* MIMETYPEPROC */