-
Notifications
You must be signed in to change notification settings - Fork 0
/
mime.c
167 lines (127 loc) · 3.07 KB
/
mime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* mime.c - mime file support for GoFish
* Copyright (C) 2002 Sean MacLennan <seanm@seanm.ca>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this project; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "gofish.h"
struct mime {
char *ext;
char *mime;
};
static char *mime_file = "/etc/mime.types";
static int mime_set = 0;
static struct mime *mimes = NULL;
static int n_mimes = 0;
void set_mime_file(char *fname)
{
mime_file = strdup(fname);
mime_set = 1;
}
char *mime_find(char *fname)
{
char *ext;
struct mime *m;
int i;
if((ext = strrchr(fname, '.'))) {
++ext;
for(m = mimes, i = 0; i < n_mimes; ++i, ++m)
if(strcmp(m->ext, ext) == 0)
return m->mime;
}
return NULL;
}
static int add_mime(char *mime, char *ext)
{
struct mime *m;
int i;
// See if extension already exists
for(m = mimes, i = 0; i < n_mimes; ++i, ++m)
if(strcmp(m->ext, ext) == 0)
return 1;
if(!(mimes = realloc(mimes, (n_mimes + 1) * sizeof(struct mime)))) {
printf("Out of memory\n");
exit(1);
}
m = mimes + n_mimes;
++n_mimes;
m->mime = must_strdup(mime);
m->ext = must_strdup(ext);
return 0;
}
static int read_mime_file(char *fname)
{
FILE *fp;
char line[160], *p, *e;
if((fp = fopen(fname, "r")) == NULL) {
if(mime_set) perror(fname);
return 1;
}
while(fgets(line, sizeof(line), fp)) {
if(*line == '#' || isspace((int)*line)) continue; // comment
if((p = strchr(line, '\n'))) *p = '\0';
for(p = line; *p && !isspace((int)*p); ++p) ;
if(*p == '\0') continue;
*p++ = '\0';
while(isspace((int)*p)) ++p;
for(e = p; *e; p = e) {
while(*e && !isspace((int)*e)) ++e;
if(*e) {
*e++ = '\0';
while(isspace((int)*e)) ++e;
}
add_mime(line, p);
}
}
fclose(fp);
return 0;
}
static struct mime default_mimes[] = {
{ "gif", "image/gif" },
{ "jpg", "image/jpeg" },
{ "png", "image/png" },
{ "jpeg", "image/jpeg" },
{ "pdf", "application/pdf" },
{ "html", "text/html" },
{ "htm", "text/html" },
{ "txt", "text/plain" },
{ "ico", "text/plain" },
{ 0, 0 },
};
void mime_init()
{
struct mime *m;
if(mime_file)
read_mime_file(mime_file);
if(mime_set) free(mime_file);
// Add the defaults if not already set
for(m = default_mimes; m->ext; ++m)
add_mime(m->mime, m->ext);
}
void mime_cleanup()
{
struct mime *m;
int i;
for(m = mimes, i = 0; i < n_mimes; ++i, ++m) {
free(m->mime);
free(m->ext);
}
free(mimes);
}