aboutsummaryrefslogtreecommitdiffstats
path: root/shared.c
diff options
context:
space:
mode:
Diffstat (limited to 'shared.c')
-rw-r--r--shared.c65
1 files changed, 37 insertions, 28 deletions
diff --git a/shared.c b/shared.c
index fb4e8ca..225ce15 100644
--- a/shared.c
+++ b/shared.c
@@ -561,42 +561,51 @@ char *expand_macros(const char *txt)
561 return result; 561 return result;
562} 562}
563 563
564char *get_mimetype_from_file(const char *filename, const char *ext) 564char *get_mimetype_for_filename(const char *filename)
565{ 565{
566 static const char *delimiters; 566 static const char *delimiters;
567 char *result; 567 char *ext = NULL, *iterate, *mimetype = NULL, *token;
568 FILE *fd;
569 char line[1024]; 568 char line[1024];
570 char *mimetype; 569 FILE *fd;
571 char *token; 570 struct string_list_item *mime;
572
573 if (!filename)
574 return NULL;
575 571
576 fd = fopen(filename, "r"); 572 if (filename == NULL)
577 if (!fd)
578 return NULL; 573 return NULL;
579 574
580 delimiters = " \t\r\n"; 575 ext = strrchr(filename, '.');
581 result = NULL; 576
582 577 if (ext && *(++ext)) {
583 /* loop over all lines in the file */ 578 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
584 while (!result && fgets(line, sizeof(line), fd)) { 579 if (mime) {
585 mimetype = strtok(line, delimiters); 580 /* We could just pass the pointer here, but would have to care
586 581 * whether or not to free the memory. Instead just dup. */
587 /* skip empty lines and comment lines */ 582 mimetype = xstrdup(mime->util);
588 if (!mimetype || (mimetype[0] == '#')) 583 } else if (ctx.cfg.mimetype_file != NULL) {
589 continue; 584 fd = fopen(ctx.cfg.mimetype_file, "r");
590 585 if (fd == NULL)
591 /* loop over all extensions of mimetype */ 586 return NULL;
592 while ((token = strtok(NULL, delimiters))) { 587
593 if (!strcasecmp(ext, token)) { 588 delimiters = " \t\r\n";
594 result = xstrdup(mimetype); 589
595 break; 590 /* loop over all lines in the file */
591 while (mimetype == NULL && fgets(line, sizeof(line), fd)) {
592 iterate = strtok(line, delimiters);
593
594 /* skip empty lines and comment lines */
595 if (iterate == NULL || (iterate[0] == '#'))
596 continue;
597
598 /* loop over all extensions of mimetype */
599 while ((token = strtok(NULL, delimiters))) {
600 if (strcasecmp(ext, token) == 0) {
601 mimetype = xstrdup(iterate);
602 break;
603 }
604 }
596 } 605 }
606 fclose(fd);
597 } 607 }
598 } 608 }
599 fclose(fd);
600 609
601 return result; 610 return mimetype;
602} 611}