diff options
| author | Christian Hesse | 2015-08-16 14:53:52 +0200 |
|---|---|---|
| committer | Jason A. Donenfeld | 2015-08-17 14:25:08 +0200 |
| commit | aa943bc9a68ccdcc5cbe29f6ac3b5e787d4c22ca (patch) | |
| tree | fc3118e65804d0dcc206e8d39fd55a76f9b3b9d2 /shared.c | |
| parent | f5c83d7b5ddceb03e1c6bda2e43c48500c7da9f5 (diff) | |
| download | cgit-aa943bc9a68ccdcc5cbe29f6ac3b5e787d4c22ca.tar.gz cgit-aa943bc9a68ccdcc5cbe29f6ac3b5e787d4c22ca.tar.bz2 cgit-aa943bc9a68ccdcc5cbe29f6ac3b5e787d4c22ca.zip | |
refactor get_mimetype_from_file() to get_mimetype_for_filename()
* handle mimetype within a single function
* return allocated memory on success
Signed-off-by: Christian Hesse <mail@eworm.de>
Diffstat (limited to 'shared.c')
| -rw-r--r-- | shared.c | 65 |
1 files changed, 37 insertions, 28 deletions
| @@ -561,42 +561,51 @@ char *expand_macros(const char *txt) | |||
| 561 | return result; | 561 | return result; |
| 562 | } | 562 | } |
| 563 | 563 | ||
| 564 | char *get_mimetype_from_file(const char *filename, const char *ext) | 564 | char *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 | } |
