about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cgit.h2
-rw-r--r--shared.c40
-rw-r--r--ui-plain.c40
3 files changed, 42 insertions, 40 deletions
diff --git a/cgit.h b/cgit.h index f327627..0f1e186 100644 --- a/cgit.h +++ b/cgit.h
@@ -391,4 +391,6 @@ extern int readfile(const char *path, char **buf, size_t *size);
391 391
392extern char *expand_macros(const char *txt); 392extern char *expand_macros(const char *txt);
393 393
394extern char *get_mimetype_from_file(const char *filename, const char *ext);
395
394#endif /* CGIT_H */ 396#endif /* CGIT_H */
diff --git a/shared.c b/shared.c index 3bd2a9e..fb4e8ca 100644 --- a/shared.c +++ b/shared.c
@@ -560,3 +560,43 @@ char *expand_macros(const char *txt)
560 } 560 }
561 return result; 561 return result;
562} 562}
563
564char *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}
diff --git a/ui-plain.c b/ui-plain.c index 0daa7bf..d68518e 100644 --- a/ui-plain.c +++ b/ui-plain.c
@@ -16,46 +16,6 @@ struct walk_tree_context {
16 int match; 16 int match;
17}; 17};
18 18
19static char *get_mimetype_from_file(const char *filename, const char *ext)
20{
21 static const char *delimiters;
22 char *result;
23 FILE *fd;
24 char line[1024];
25 char *mimetype;
26 char *token;
27
28 if (!filename)
29 return NULL;
30
31 fd = fopen(filename, "r");
32 if (!fd)
33 return NULL;
34
35 delimiters = " \t\r\n";
36 result = NULL;
37
38 /* loop over all lines in the file */
39 while (!result && fgets(line, sizeof(line), fd)) {
40 mimetype = strtok(line, delimiters);
41
42 /* skip empty lines and comment lines */
43 if (!mimetype || (mimetype[0] == '#'))
44 continue;
45
46 /* loop over all extensions of mimetype */
47 while ((token = strtok(NULL, delimiters))) {
48 if (!strcasecmp(ext, token)) {
49 result = xstrdup(mimetype);
50 break;
51 }
52 }
53 }
54 fclose(fd);
55
56 return result;
57}
58
59static int print_object(const unsigned char *sha1, const char *path) 19static int print_object(const unsigned char *sha1, const char *path)
60{ 20{
61 enum object_type type; 21 enum object_type type;