Return a constant string from the mimes database.

We are not modifying it so there is no need to copy memory around.
This also prevents file_mime getting out of sync as had already
happend. It had a size of 50 while the mime types database type used
to have a size of 70.
This commit is contained in:
Florian Obser 2020-12-05 16:58:26 +01:00 committed by Solene Rapenne
commit bfd1f66350
3 changed files with 17 additions and 18 deletions

17
mimes.c
View file

@ -117,23 +117,22 @@ static const struct {
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif
void
get_file_mime(const char *path, char *type, const ssize_t type_size)
const char *
get_file_mime(const char *path)
{
int i;
char *extension;
extension = strrchr(path, '.');
if ((extension = strrchr(path, '.')) == NULL)
goto out;
/* look for the MIME in the database */
for (i = 0; i < nitems(database); i++) {
if (strcmp(database[i].extension, extension + 1) == 0) {
strlcpy(type, database[i].type, type_size);
break;
}
if (strcmp(database[i].extension, extension + 1) == 0)
return (database[i].type);
}
out:
/* if no MIME have been found, set a default one */
if (strlen(type) == 0)
strlcpy(type, "text/gemini", type_size);
return ("text/gemini");
}