"Fossies" - the Fresh Open Source Software Archive 
Member "tin-2.6.2/src/mimetypes.c" (9 Dec 2022, 5322 Bytes) of package /linux/misc/tin-2.6.2.tar.xz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "mimetypes.c" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
2.6.1_vs_2.6.2.
1 /*
2 * Project : tin - a Usenet reader
3 * Module : mimetypes.c
4 * Author : J. Faultless
5 * Created : 2000-03-31
6 * Updated : 2022-04-09
7 * Notes : mime.types handling
8 *
9 * Copyright (c) 2000-2023 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 */
48 static t_bool _lookup_mimetype(const char *file, const char *ext, t_part *part);
49 static 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 */
57 static t_bool
58 _lookup_mimetype(
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 = (unsigned int) i;
82 FreeIfNeeded(part->subtype);
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 */
104 void
105 lookup_mimetype(
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");
120 _lookup_mimetype(buf, ext, part);
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 */
129 static t_bool
130 _lookup_extension(
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 */
168 t_bool
169 lookup_extension(
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 }