X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/e35fb433456859d250d2e075350d11d4e173713b..080568bfb0056607359204678f84d23c74f02832:/sbr/mime_type.c diff --git a/sbr/mime_type.c b/sbr/mime_type.c index a640110a..4e3ebbb7 100644 --- a/sbr/mime_type.c +++ b/sbr/mime_type.c @@ -20,10 +20,10 @@ static char *get_file_info(const char *, const char *); * is responsible for free'ing returned memory. */ char * -mime_type(const char *file_name) { +mime_type(const char *file_name) +{ char *content_type = NULL; /* mime content type */ char *p; - static int loaded_defaults = 0; #ifdef MIMETYPEPROC char *mimetype; @@ -33,17 +33,13 @@ mime_type(const char *file_name) { /* Try to append charset for text content. */ char *mimeencoding; - if (strncasecmp(mimetype, "text", 4) == 0) { - if ((mimeencoding = get_file_info(MIMEENCODINGPROC, file_name))) { - content_type = concat(mimetype, "; charset=", mimeencoding, - NULL); - free (mimetype); - } else { - content_type = mimetype; - } - } else { + if (!strncasecmp(mimetype, "text", 4) && + (mimeencoding = get_file_info(MIMEENCODINGPROC, file_name))) { + content_type = concat(mimetype, "; charset=", mimeencoding, NULL); + free(mimeencoding); + free(mimetype); + } else content_type = mimetype; - } #else /* MIMEENCODINGPROC */ content_type = mimetype; #endif /* MIMEENCODINGPROC */ @@ -59,9 +55,10 @@ mime_type(const char *file_name) { struct node *np; /* Content scan node pointer */ FILE *fp; /* File pointer for mhn.defaults */ + static bool loaded_defaults; if (! loaded_defaults && (fp = fopen(p = etcpath("mhn.defaults"), "r"))) { - loaded_defaults = 1; + loaded_defaults = true; readconfig(NULL, fp, p, 0); fclose(fp); } @@ -84,16 +81,17 @@ mime_type(const char *file_name) { if (content_type == NULL) { FILE *fp; - int binary = 0, c; + int c; if (!(fp = fopen(file_name, "r"))) { inform("unable to access file \"%s\"", file_name); return NULL; } + bool binary = false; while ((c = getc(fp)) != EOF) { if (! isascii(c) || c == 0) { - binary = 1; + binary = true; break; } } @@ -112,7 +110,7 @@ mime_type(const char *file_name) { #ifdef MIMETYPEPROC /* * Get information using proc about a file. - */ + * Non-null return value must be free(3)'d. */ static char * get_file_info(const char *proc, const char *file_name) { @@ -152,7 +150,7 @@ get_file_info(const char *proc, const char *file_name) if (!ok) return NULL; - /* s#^.*:[ \t]*##. */ + /* s#^[^:]*:[ \t]*##. */ info = buf; if ((needle = strchr(info, ':'))) { info = needle + 1;