diff options
Diffstat (limited to 'shared.c')
-rw-r--r-- | shared.c | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -560,3 +560,43 @@ char *expand_macros(const char *txt) | |||
560 | } | 560 | } |
561 | return result; | 561 | return result; |
562 | } | 562 | } |
563 | |||
564 | char *get_mimetype_from_file(const char *filename, const char *ext) | ||
565 | { | ||
566 | static const char *delimiters; | ||
567 | char *result; | ||
568 | FILE *fd; | ||
569 | char line[1024]; | ||
570 | char *mimetype; | ||
571 | char *token; | ||
572 | |||
573 | if (!filename) | ||
574 | return NULL; | ||
575 | |||
576 | fd = fopen(filename, "r"); | ||
577 | if (!fd) | ||
578 | return NULL; | ||
579 | |||
580 | delimiters = " \t\r\n"; | ||
581 | result = NULL; | ||
582 | |||
583 | /* loop over all lines in the file */ | ||
584 | while (!result && fgets(line, sizeof(line), fd)) { | ||
585 | mimetype = strtok(line, delimiters); | ||
586 | |||
587 | /* skip empty lines and comment lines */ | ||
588 | if (!mimetype || (mimetype[0] == '#')) | ||
589 | continue; | ||
590 | |||
591 | /* loop over all extensions of mimetype */ | ||
592 | while ((token = strtok(NULL, delimiters))) { | ||
593 | if (!strcasecmp(ext, token)) { | ||
594 | result = xstrdup(mimetype); | ||
595 | break; | ||
596 | } | ||
597 | } | ||
598 | } | ||
599 | fclose(fd); | ||
600 | |||
601 | return result; | ||
602 | } | ||