mime: rewrite detection function
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Этот коммит содержится в:
родитель
790d2498cb
Коммит
73f199be3f
58
shared.c
58
shared.c
@ -563,49 +563,39 @@ char *expand_macros(const char *txt)
|
|||||||
|
|
||||||
char *get_mimetype_for_filename(const char *filename)
|
char *get_mimetype_for_filename(const char *filename)
|
||||||
{
|
{
|
||||||
static const char *delimiters;
|
char *ext, *mimetype, *token, line[1024];
|
||||||
char *ext = NULL, *iterate, *mimetype = NULL, *token;
|
FILE *file;
|
||||||
char line[1024];
|
|
||||||
FILE *fd;
|
|
||||||
struct string_list_item *mime;
|
struct string_list_item *mime;
|
||||||
|
|
||||||
if (filename == NULL)
|
if (!filename)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
ext = strrchr(filename, '.');
|
ext = strrchr(filename, '.');
|
||||||
|
if (!ext)
|
||||||
if (ext && *(++ext)) {
|
|
||||||
mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
|
|
||||||
if (mime) {
|
|
||||||
/* We could just pass the pointer here, but would have to care
|
|
||||||
* whether or not to free the memory. Instead just dup. */
|
|
||||||
mimetype = xstrdup(mime->util);
|
|
||||||
} else if (ctx.cfg.mimetype_file != NULL) {
|
|
||||||
fd = fopen(ctx.cfg.mimetype_file, "r");
|
|
||||||
if (fd == NULL)
|
|
||||||
return NULL;
|
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";
|
if (!ctx.cfg.mimetype_file)
|
||||||
|
return NULL;
|
||||||
/* loop over all lines in the file */
|
file = fopen(ctx.cfg.mimetype_file, "r");
|
||||||
while (mimetype == NULL && fgets(line, sizeof(line), fd)) {
|
if (!file)
|
||||||
iterate = strtok(line, delimiters);
|
return NULL;
|
||||||
|
while (fgets(line, sizeof(line), file)) {
|
||||||
/* skip empty lines and comment lines */
|
if (!line[0] || line[0] == '#')
|
||||||
if (iterate == NULL || (iterate[0] == '#'))
|
|
||||||
continue;
|
continue;
|
||||||
|
mimetype = strtok(line, " \t\r\n");
|
||||||
/* loop over all extensions of mimetype */
|
while ((token = strtok(NULL, " \t\r\n"))) {
|
||||||
while ((token = strtok(NULL, delimiters))) {
|
if (!strcasecmp(ext, token)) {
|
||||||
if (strcasecmp(ext, token) == 0) {
|
fclose(file);
|
||||||
mimetype = xstrdup(iterate);
|
return xstrdup(mimetype);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fd);
|
fclose(file);
|
||||||
}
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
return mimetype;
|
|
||||||
}
|
}
|
||||||
|
Загрузка…
Ссылка в новой задаче
Block a user