+
+char *get_mimetype_for_filename(const char *filename)
+{
+ char *ext, *mimetype, line[1024];
+ struct string_list list = STRING_LIST_INIT_NODUP;
+ int i;
+ FILE *file;
+ struct string_list_item *mime;
+
+ if (!filename)
+ return NULL;
+
+ ext = strrchr(filename, '.');
+ if (!ext)
+ return NULL;
+ ++ext;
+ if (!ext[0])
+ return NULL;
+ mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
+ if (mime)
+ return xstrdup(mime->util);
+
+ if (!ctx.cfg.mimetype_file)
+ return NULL;
+ file = fopen(ctx.cfg.mimetype_file, "r");
+ if (!file)
+ return NULL;
+ while (fgets(line, sizeof(line), file)) {
+ if (!line[0] || line[0] == '#')
+ continue;
+ string_list_split_in_place(&list, line, " \t\r\n", -1);
+ string_list_remove_empty_items(&list, 0);
+ mimetype = list.items[0].string;
+ for (i = 1; i < list.nr; i++) {
+ if (!strcasecmp(ext, list.items[i].string)) {
+ fclose(file);
+ return xstrdup(mimetype);
+ }
+ }
+ string_list_clear(&list, 0);
+ }
+ fclose(file);
+ return NULL;
+}