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:
parent
519de783b6
commit
bfd1f66350
3 changed files with 17 additions and 18 deletions
10
main.c
10
main.c
|
@ -97,13 +97,13 @@ status(const int code, const char *file_mime, const char *lang)
|
|||
void
|
||||
display_file(const char *path, const char *lang)
|
||||
{
|
||||
FILE *fd;
|
||||
struct stat sb;
|
||||
ssize_t nread;
|
||||
size_t buflen = BUFF_LEN_1;
|
||||
char *buffer[BUFF_LEN_1];
|
||||
char extension[10];
|
||||
char file_mime[50] = "";
|
||||
ssize_t nread;
|
||||
struct stat sb;
|
||||
FILE *fd;
|
||||
const char *file_mime;
|
||||
|
||||
/* this is to check if path is a directory */
|
||||
if (stat(path, &sb) == -1)
|
||||
|
@ -117,7 +117,7 @@ display_file(const char *path, const char *lang)
|
|||
if (S_ISDIR(sb.st_mode) == 1)
|
||||
goto err;
|
||||
|
||||
get_file_mime(path, file_mime, sizeof(file_mime));
|
||||
file_mime = get_file_mime(path);
|
||||
|
||||
status(20, file_mime, lang);
|
||||
|
||||
|
|
17
mimes.c
17
mimes.c
|
@ -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");
|
||||
}
|
||||
|
|
2
mimes.h
2
mimes.h
|
@ -1 +1 @@
|
|||
void get_file_mime(const char *, char *, const ssize_t);
|
||||
const char *get_file_mime(const char *);
|
||||
|
|
Loading…
Reference in a new issue