]> gitweb.ps.run Git - ps-cgit/blobdiff - shared.c
cgit.c: remove useless null check
[ps-cgit] / shared.c
index fb4e8ca642d4b3c3f988b30f5c1487ad952edb44..3d91a76ec09c8a88aefa2d1148f3542aa6850565 100644 (file)
--- a/shared.c
+++ b/shared.c
@@ -561,42 +561,41 @@ char *expand_macros(const char *txt)
        return result;
 }
 
-char *get_mimetype_from_file(const char *filename, const char *ext)
+char *get_mimetype_for_filename(const char *filename)
 {
-       static const char *delimiters;
-       char *result;
-       FILE *fd;
-       char line[1024];
-       char *mimetype;
-       char *token;
+       char *ext, *mimetype, *token, line[1024];
+       FILE *file;
+       struct string_list_item *mime;
 
        if (!filename)
                return NULL;
 
-       fd = fopen(filename, "r");
-       if (!fd)
+       ext = strrchr(filename, '.');
+       if (!ext)
                return NULL;
+       ++ext;
+       if (!ext[0])
+               return NULL;
+       mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
+       if (mime)
+               return xstrdup(mime->util);
 
-       delimiters = " \t\r\n";
-       result = NULL;
-
-       /* loop over all lines in the file */
-       while (!result && fgets(line, sizeof(line), fd)) {
-               mimetype = strtok(line, delimiters);
-
-               /* skip empty lines and comment lines */
-               if (!mimetype || (mimetype[0] == '#'))
+       if (!ctx.cfg.mimetype_file)
+               return NULL;
+       file = fopen(ctx.cfg.mimetype_file, "r");
+       if (!file)
+               return NULL;
+       while (fgets(line, sizeof(line), file)) {
+               if (!line[0] || line[0] == '#')
                        continue;
-
-               /* loop over all extensions of mimetype */
-               while ((token = strtok(NULL, delimiters))) {
+               mimetype = strtok(line, " \t\r\n");
+               while ((token = strtok(NULL, " \t\r\n"))) {
                        if (!strcasecmp(ext, token)) {
-                               result = xstrdup(mimetype);
-                               break;
+                               fclose(file);
+                               return xstrdup(mimetype);
                        }
                }
        }
-       fclose(fd);
-
-       return result;
+       fclose(file);
+       return NULL;
 }