]>
diplodocus.org Git - nmh/blob - uip/attach.c
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 */
28 if ((mimetype
= get_file_info(MIMETYPEPROC
, file_name
))) {
29 #ifdef MIMEENCODINGPROC
30 /* Try to append charset for text content. */
33 if (strncasecmp(mimetype
, "text", 4) == 0) {
34 if ((mimeencoding
= get_file_info(MIMEENCODINGPROC
, file_name
))) {
35 content_type
= concat(mimetype
, "; charset=", mimeencoding
,
38 content_type
= strdup(mimetype
);
41 content_type
= strdup(mimetype
);
43 #else /* MIMEENCODINGPROC */
44 content_type
= strdup(mimetype
);
45 #endif /* MIMEENCODINGPROC */
47 #else /* MIMETYPEPROC */
48 NMH_UNUSED(file_name
);
49 #endif /* MIMETYPEPROC */
57 * Get information using proc about a file.
60 get_file_info(const char *proc
, const char *file_name
) {
64 if ((cp
= strchr(file_name
, '\''))) {
65 /* file_name contains a single quote. */
66 if (strchr(file_name
, '"')) {
67 advise(NULL
, "filenames containing both single and double quotes "
68 "are unsupported for attachment");
75 cmd
= concat(proc
, " ", quotec
, file_name
, quotec
, NULL
);
76 if ((cmd
= concat(proc
, " ", quotec
, file_name
, quotec
, NULL
))) {
79 if ((fp
= popen(cmd
, "r")) != NULL
) {
80 char buf
[BUFSIZ
>= 2048 ? BUFSIZ
: 2048];
83 if (fgets(buf
, sizeof buf
, fp
)) {
86 /* Skip leading <filename>:<whitespace>, if present. */
87 if ((cp
= strchr(buf
, ':')) != NULL
) {
89 while (*cp
&& isblank((unsigned char) *cp
)) {
96 /* Truncate at newline (LF or CR), if present. */
97 if ((eol
= strpbrk(cp
, "\n\r")) != NULL
) {
100 } else if (buf
[0] == '\0') {
101 /* This can happen on Cygwin if the popen()
102 mysteriously fails. Return NULL so that the caller
103 will use another method to determine the info. */
110 advise(NULL
, "no output from %s", cmd
);
115 advise(NULL
, "concat with \"%s\" failed, out of memory?", proc
);
118 return cp
? strdup(cp
) : NULL
;
120 #endif /* MIMETYPEPROC */