]>
diplodocus.org Git - nmh/blob - uip/attach.c
540ddd9321bbe4f8ccc4060f1efb0049bfeee7c4
2 * attach.c -- routines to help attach files via whatnow
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
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. Caller is responsible for free'ing returned memory.
22 mime_type(const char *file_name
) {
23 char *content_type
= NULL
; /* mime content type */
25 static int loaded_defaults
= 0;
30 if ((mimetype
= get_file_info(MIMETYPEPROC
, file_name
))) {
31 #ifdef MIMEENCODINGPROC
32 /* Try to append charset for text content. */
35 if (strncasecmp(mimetype
, "text", 4) == 0) {
36 if ((mimeencoding
= get_file_info(MIMEENCODINGPROC
, file_name
))) {
37 content_type
= concat(mimetype
, "; charset=", mimeencoding
,
40 content_type
= strdup(mimetype
);
43 content_type
= strdup(mimetype
);
45 #else /* MIMEENCODINGPROC */
46 content_type
= strdup(mimetype
);
47 #endif /* MIMEENCODINGPROC */
49 #endif /* MIMETYPEPROC */
52 * If we didn't get the MIME type from the contents (or we don't support
53 * the necessary command) then use the mhshow suffix.
56 if (content_type
== NULL
) {
57 struct node
*np
; /* Content scan node pointer */
58 FILE *fp
; /* File pointer for mhn.defaults */
60 if (! loaded_defaults
&&
61 (fp
= fopen(p
= etcpath("mhn.defaults"), "r"))) {
63 readconfig(NULL
, fp
, p
, 0);
67 if ((p
= strrchr(file_name
, '.')) != NULL
) {
68 for (np
= m_defs
; np
; np
= np
->n_next
) {
69 if (strncasecmp(np
->n_name
, "mhshow-suffix-", 14) == 0 &&
70 strcasecmp(p
, np
->n_field
? np
->n_field
: "") == 0) {
71 content_type
= strdup(np
->n_name
+ 14);
78 * If we didn't match any filename extension, try to infer the
79 * content type. If we have binary, assume application/octet-stream;
80 * otherwise, assume text/plain.
83 if (content_type
== NULL
) {
87 if (!(fp
= fopen(file_name
, "r"))) {
88 advise (NULL
, "unable to access file \"%s\"", file_name
);
92 while ((c
= getc(fp
)) != EOF
) {
102 strdup(binary
? "application/octet-stream" : "text/plain");
112 * Get information using proc about a file.
115 get_file_info(const char *proc
, const char *file_name
) {
119 if ((cp
= strchr(file_name
, '\''))) {
120 /* file_name contains a single quote. */
121 if (strchr(file_name
, '"')) {
122 advise(NULL
, "filenames containing both single and double quotes "
123 "are unsupported for attachment");
130 cmd
= concat(proc
, " ", quotec
, file_name
, quotec
, NULL
);
131 if ((cmd
= concat(proc
, " ", quotec
, file_name
, quotec
, NULL
))) {
134 if ((fp
= popen(cmd
, "r")) != NULL
) {
135 char buf
[BUFSIZ
>= 2048 ? BUFSIZ
: 2048];
138 if (fgets(buf
, sizeof buf
, fp
)) {
141 /* Skip leading <filename>:<whitespace>, if present. */
142 if ((cp
= strchr(buf
, ':')) != NULL
) {
144 while (*cp
&& isblank((unsigned char) *cp
)) {
151 /* Truncate at newline (LF or CR), if present. */
152 if ((eol
= strpbrk(cp
, "\n\r")) != NULL
) {
155 } else if (buf
[0] == '\0') {
156 /* This can happen on Cygwin if the popen()
157 mysteriously fails. Return NULL so that the caller
158 will use another method to determine the info. */
165 advise(NULL
, "no output from %s", cmd
);
170 advise(NULL
, "concat with \"%s\" failed, out of memory?", proc
);
173 return cp
? strdup(cp
) : NULL
;
175 #endif /* MIMETYPEPROC */