tin  2.6.1
About: TIN is a threaded NNTP and spool based UseNet newsreader.
  Fossies Dox: tin-2.6.1.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

mimetypes.c
Go to the documentation of this file.
1/*
2 * Project : tin - a Usenet reader
3 * Module : mimetypes.c
4 * Author : J. Faultless
5 * Created : 2000-03-31
6 * Updated : 2019-02-18
7 * Notes : mime.types handling
8 *
9 * Copyright (c) 2000-2022 Jason Faultless <jason@altarstone.com>
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * 3. Neither the name of the copyright holder nor the names of its
24 * contributors may be used to endorse or promote products derived from
25 * this software without specific prior written permission.
26 *
27 * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#ifndef TIN_H
41# include "tin.h"
42#endif /* !TIN_H */
43
44
45/*
46 * local prototypes
47 */
48static t_bool _lookup_mimetype(const char *file, const char *ext, t_part *part);
49static t_bool _lookup_extension(char *extension, size_t ext_len, const char *file, const char *type);
50
51
52/*
53 * Match a filename extension to a content-type / subtype pair in mime.types
54 * Update the passed-in attachment structure with type/subtype if found
55 * Return TRUE if found
56 */
57static t_bool
59 const char *file,
60 const char *ext,
61 t_part *part)
62{
63 FILE *fp;
64 char *exts;
65 char *ptr;
66 char buf[PATH_LEN];
67 int i;
68
69 if ((fp = fopen(file, "r")) == NULL)
70 return FALSE;
71
72 while ((fgets(buf, sizeof(buf), fp)) != NULL) {
73 if (buf[0] == '#' || buf[0] == '\n') /* Skip comments & blank lines */
74 continue;
75
76 if (strtok(buf, " \t\n")) {
77 while ((exts = strtok(NULL, " \t\n")) != NULL) { /* Work through list of extensions */
78 if (strcasecmp(ext, exts) == 0) {
79 if ((i = content_type(strtok(buf, "/"))) != -1) {
80 if ((ptr = strtok(NULL, "\n")) != NULL) {
81 part->type = i;
83 part->subtype = my_strdup(ptr);
84 fclose(fp);
85 return TRUE;
86 }
87 }
88 }
89 }
90 }
91 }
92
93 fclose(fp);
94 return FALSE;
95}
96
97
98/*
99 * Check:
100 * $HOME/.mime.types
101 * /etc/mime.types
102 * TIN_DEFAULTS_DIR/mime.types
103 */
104void
106 const char *ext,
107 t_part *part)
108{
109 char buf[PATH_LEN];
110
111 joinpath(buf, sizeof(buf), homedir, ".mime.types");
112 if (_lookup_mimetype(buf, ext, part))
113 return;
114
115 if (_lookup_mimetype("/etc/mime.types", ext, part))
116 return;
117
118#ifdef TIN_DEFAULTS_DIR
119 joinpath(buf, sizeof(buf), TIN_DEFAULTS_DIR, "mime.types");
121#endif /* TIN_DEFAULTS_DIR */
122}
123
124
125/*
126 * look for a filename extension in file for the specified
127 * type ("major/minor"), the result is stored in extension
128 */
129static t_bool
131 char *extension,
132 size_t ext_len,
133 const char *file,
134 const char *type)
135{
136 FILE *fp;
137 char *p;
138 char buf[8192];
139
140 if ((fp = fopen(file, "r")) == NULL)
141 return FALSE;
142 while ((fgets(buf, sizeof(buf), fp)) != NULL) {
143 if (buf[0] == '#' || buf[0] == '\n' || strncmp(buf, type, strlen(type)))
144 continue;
145 if ((p = strtok(buf, " \t\n"))) {
146 if (strlen(p) != strlen(type))
147 continue;
148 if ((p = strtok(NULL, " \t\n")) != NULL) {
149 my_strncpy(extension, p, ext_len - 1);
150 fclose(fp);
151 return TRUE;
152 }
153 }
154 }
155 fclose(fp);
156 return FALSE;
157}
158
159
160/*
161 * look for a filename extension for the specified
162 * major minor, the result is stored in extension
163 * files checked are:
164 * $HOME/.mime.types
165 * /etc/mime.types
166 * TIN_DEFAULTS_DIR/mime.types
167 */
168t_bool
170 char *extension,
171 size_t ext_len,
172 const char *major,
173 const char *minor)
174{
175 char *type;
176 char buf[PATH_LEN];
177
178 if (!major || !minor) {
179 *extension = '\0';
180 return FALSE;
181 }
182
183 type = my_malloc(strlen(major) + 1 + strlen(minor) + 1);
184 strcpy(type, major);
185 strcat(type, "/");
186 strcat(type, minor);
187
188 joinpath(buf, sizeof(buf), homedir, ".mime.types");
189 if (_lookup_extension(extension, ext_len, buf, type)) {
190 free(type);
191 return TRUE;
192 }
193
194 if (_lookup_extension(extension, ext_len, "/etc/mime.types", type)) {
195 free(type);
196 return TRUE;
197 }
198
199#ifdef TIN_DEFAULTS_DIR
200 joinpath(buf, sizeof(buf), TIN_DEFAULTS_DIR, "mime.types");
201 if (_lookup_extension(extension, ext_len, buf, type)) {
202 free(type);
203 return TRUE;
204 }
205#endif /* TIN_DEFAULTS_DIR */
206 free(type);
207 my_strncpy(extension, minor, ext_len - 1);
208 return FALSE;
209}
unsigned t_bool
Definition: bool.h:77
#define TRUE
Definition: bool.h:74
#define FALSE
Definition: bool.h:70
char homedir[PATH_LEN]
Definition: init.c:78
static char buf[16]
Definition: langinfo.c:50
t_bool lookup_extension(char *extension, size_t ext_len, const char *major, const char *minor)
Definition: mimetypes.c:169
void lookup_mimetype(const char *ext, t_part *part)
Definition: mimetypes.c:105
static t_bool _lookup_extension(char *extension, size_t ext_len, const char *file, const char *type)
Definition: mimetypes.c:130
static t_bool _lookup_mimetype(const char *file, const char *ext, t_part *part)
Definition: mimetypes.c:58
int content_type(char *type)
Definition: rfc2046.c:115
void joinpath(char *result, size_t result_size, const char *dir, const char *file)
Definition: joinpath.c:50
char * my_strdup(const char *str)
Definition: string.c:139
void my_strncpy(char *p, const char *q, size_t n)
Definition: string.c:196
int strcasecmp(const char *p, const char *q)
Definition: string.c:475
Definition: rfc2046.h:93
unsigned type
Definition: rfc2046.h:94
char * subtype
Definition: rfc2046.h:100
#define my_malloc(size)
Definition: tin.h:2245
#define FreeIfNeeded(p)
Definition: tin.h:2252
#define PATH_LEN
Definition: tin.h:843