X-Git-Url: https://gitweb.ps.run/ps-cgit/blobdiff_plain/f5c83d7b5ddceb03e1c6bda2e43c48500c7da9f5..94182d6031df0d956a94ecd7ece233e345468961:/shared.c diff --git a/shared.c b/shared.c index fb4e8ca..3d91a76 100644 --- a/shared.c +++ b/shared.c @@ -561,42 +561,41 @@ char *expand_macros(const char *txt) return result; } -char *get_mimetype_from_file(const char *filename, const char *ext) +char *get_mimetype_for_filename(const char *filename) { - static const char *delimiters; - char *result; - FILE *fd; - char line[1024]; - char *mimetype; - char *token; + char *ext, *mimetype, *token, line[1024]; + FILE *file; + struct string_list_item *mime; if (!filename) return NULL; - fd = fopen(filename, "r"); - if (!fd) + 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); - delimiters = " \t\r\n"; - result = NULL; - - /* loop over all lines in the file */ - while (!result && fgets(line, sizeof(line), fd)) { - mimetype = strtok(line, delimiters); - - /* skip empty lines and comment lines */ - if (!mimetype || (mimetype[0] == '#')) + 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; - - /* loop over all extensions of mimetype */ - while ((token = strtok(NULL, delimiters))) { + mimetype = strtok(line, " \t\r\n"); + while ((token = strtok(NULL, " \t\r\n"))) { if (!strcasecmp(ext, token)) { - result = xstrdup(mimetype); - break; + fclose(file); + return xstrdup(mimetype); } } } - fclose(fd); - - return result; + fclose(file); + return NULL; }