]>
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.
10 #include "readconfig.h"
14 #include "mime_type.h"
17 static char *get_file_info(const char *, const char *);
18 #endif /* MIMETYPEPROC */
21 * Try to use external command to determine mime type, and possibly
22 * encoding. If that fails try using the filename extension. Caller
23 * is responsible for free'ing returned memory.
26 mime_type(const char *file_name
)
28 char *content_type
= NULL
; /* mime content type */
34 if ((mimetype
= get_file_info(MIMETYPEPROC
, file_name
))) {
35 #ifdef MIMEENCODINGPROC
36 /* Try to append charset for text content. */
39 if (!strncasecmp(mimetype
, "text", 4) &&
40 (mimeencoding
= get_file_info(MIMEENCODINGPROC
, file_name
))) {
41 content_type
= concat(mimetype
, "; charset=", mimeencoding
, NULL
);
45 content_type
= mimetype
;
46 #else /* MIMEENCODINGPROC */
47 content_type
= mimetype
;
48 #endif /* MIMEENCODINGPROC */
50 #endif /* MIMETYPEPROC */
53 * If we didn't get the MIME type from the contents (or we don't support
54 * the necessary command) then use the mhshow suffix.
57 if (content_type
== NULL
) {
58 struct node
*np
; /* Content scan node pointer */
59 FILE *fp
; /* File pointer for mhn.defaults */
61 static bool loaded_defaults
;
62 if (! loaded_defaults
&&
63 (fp
= fopen(p
= etcpath("mhn.defaults"), "r"))) {
64 loaded_defaults
= true;
65 readconfig(NULL
, fp
, p
, 0);
69 if ((p
= strrchr(file_name
, '.')) != NULL
) {
70 for (np
= m_defs
; np
; np
= np
->n_next
) {
71 if (strncasecmp(np
->n_name
, "mhshow-suffix-", 14) == 0 &&
72 strcasecmp(p
, FENDNULL(np
->n_field
)) == 0) {
73 content_type
= strdup(np
->n_name
+ 14);
80 * If we didn't match any filename extension, try to infer the
81 * content type. If we have binary, assume application/octet-stream;
82 * otherwise, assume text/plain.
85 if (content_type
== NULL
) {
89 if (!(fp
= fopen(file_name
, "r"))) {
90 inform("unable to access file \"%s\"", file_name
);
95 while ((c
= getc(fp
)) != EOF
) {
96 if (! isascii(c
) || c
== 0) {
105 strdup(binary
? "application/octet-stream" : "text/plain");
115 * Get information using proc about a file.
116 * Non-null return value must be free(3)'d. */
118 get_file_info(const char *proc
, const char *file_name
)
124 char buf
[max(BUFSIZ
, 2048)];
128 if (strchr(file_name
, '\'')) {
129 if (strchr(file_name
, '"')) {
130 inform("filenames containing both single and double quotes "
131 "are unsupported for attachment");
138 cmd
= concat(proc
, " ", quotec
, file_name
, quotec
, NULL
);
140 inform("concat with \"%s\" failed, out of memory?", proc
);
144 if ((fp
= popen(cmd
, "r")) == NULL
) {
145 inform("no output from %s", cmd
);
150 ok
= fgets(buf
, sizeof buf
, fp
);
156 /* s#^[^:]*:[ \t]*##. */
158 if ((needle
= strchr(info
, ':'))) {
160 while (isblank((unsigned char)*info
))
165 if ((needle
= strpbrk(info
, "\n\r")))
170 #endif /* MIMETYPEPROC */