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
parent 519de783b6
commit bfd1f66350
3 changed files with 17 additions and 18 deletions

10
main.c
View file

@ -97,13 +97,13 @@ status(const int code, const char *file_mime, const char *lang)
void void
display_file(const char *path, const char *lang) display_file(const char *path, const char *lang)
{ {
FILE *fd;
struct stat sb;
ssize_t nread;
size_t buflen = BUFF_LEN_1; size_t buflen = BUFF_LEN_1;
char *buffer[BUFF_LEN_1]; char *buffer[BUFF_LEN_1];
char extension[10]; char extension[10];
char file_mime[50] = ""; const char *file_mime;
ssize_t nread;
struct stat sb;
FILE *fd;
/* this is to check if path is a directory */ /* this is to check if path is a directory */
if (stat(path, &sb) == -1) if (stat(path, &sb) == -1)
@ -117,7 +117,7 @@ display_file(const char *path, const char *lang)
if (S_ISDIR(sb.st_mode) == 1) if (S_ISDIR(sb.st_mode) == 1)
goto err; goto err;
get_file_mime(path, file_mime, sizeof(file_mime)); file_mime = get_file_mime(path);
status(20, file_mime, lang); status(20, file_mime, lang);

17
mimes.c
View file

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

View file

@ -1 +1 @@
void get_file_mime(const char *, char *, const ssize_t); const char *get_file_mime(const char *);