aboutsummaryrefslogtreecommitdiff
path: root/mime.c
blob: 35ff72e47d9e280b831795a9d008ae877ae0a5c3 (plain)
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
/*
 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "gmid.h"

void
init_mime(void)
{
	conf.mimes.len = 0;
	conf.mimes.cap = 2;

	conf.mimes.t = calloc(conf.mimes.cap, sizeof(struct etm));
	if (conf.mimes.t == NULL)
		fatal("calloc: %s", strerror(errno));

	conf.mimes.def = strdup("application/octet-stream");
	if (conf.mimes.def == NULL)
		fatal("strdup: %s", strerror(errno));

}

void
set_default_mime(const char *m)
{
	free(conf.mimes.def);
	if ((conf.mimes.def = strdup(m)) == NULL)
		fatal("strdup: %s", strerror(errno));
}

/* register mime for the given extension */
void
add_mime(const char *mime, const char *ext)
{
	if (conf.mimes.len == conf.mimes.cap) {
		conf.mimes.cap *= 1.5;
		conf.mimes.t = realloc(conf.mimes.t,
		    conf.mimes.cap * sizeof(struct etm));
		if (conf.mimes.t == NULL)
			fatal("realloc: %s", strerror(errno));
	}

	conf.mimes.t[conf.mimes.len].mime = mime;
	conf.mimes.t[conf.mimes.len].ext  = ext;
	conf.mimes.len++;
}

/* load a default set of common mime-extension associations */
void
load_default_mime()
{
	struct etm *i, m[] = {
		{"application/pdf",	"pdf"},
		{"image/gif",		"gif"},
		{"image/jpeg",		"jpg"},
		{"image/jpeg",		"jpeg"},
		{"image/png",		"png"},
		{"image/svg+xml",	"svg"},
		{"text/gemini",		"gemini"},
		{"text/gemini",		"gmi"},
		{"text/markdown",	"markdown"},
		{"text/markdown",	"md"},
		{"text/plain",		"txt"},
		{"text/xml",		"xml"},
		{NULL, NULL}
	};

	for (i = m; i->mime != NULL; ++i)
		add_mime(i->mime, i->ext);
}

static const char *
path_ext(const char *path)
{
	const char *end;

	end = path + strlen(path)-1;
	for (; end != path; --end) {
		if (*end == '.')
			return end+1;
		if (*end == '/')
			break;
	}

	return NULL;
}

const char *
mime(const char *path)
{
	const char *ext;
	struct etm *t;

	if ((ext = path_ext(path)) == NULL)
		return conf.mimes.def;

	for (t = conf.mimes.t; t->mime != NULL; ++t)
		if (!strcmp(ext, t->ext))
			return t->mime;

	return conf.mimes.def;
}