From f5c83d7b5ddceb03e1c6bda2e43c48500c7da9f5 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 14 Aug 2015 16:50:56 +0200 Subject: move get_mimetype_from_file() to shared Signed-off-by: Christian Hesse --- shared.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'shared.c') diff --git a/shared.c b/shared.c index 3bd2a9e..fb4e8ca 100644 --- a/shared.c +++ b/shared.c @@ -560,3 +560,43 @@ char *expand_macros(const char *txt) } return result; } + +char *get_mimetype_from_file(const char *filename, const char *ext) +{ + static const char *delimiters; + char *result; + FILE *fd; + char line[1024]; + char *mimetype; + char *token; + + if (!filename) + return NULL; + + fd = fopen(filename, "r"); + if (!fd) + return NULL; + + 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] == '#')) + continue; + + /* loop over all extensions of mimetype */ + while ((token = strtok(NULL, delimiters))) { + if (!strcasecmp(ext, token)) { + result = xstrdup(mimetype); + break; + } + } + } + fclose(fd); + + return result; +} -- cgit v1.2.3-70-g09d2