From 6b0688752a11d4958da6f675a8f29688dc5e1194 Mon Sep 17 00:00:00 2001 From: Carsten Haitzler Date: Sun, 12 Aug 2007 10:00:02 +0000 Subject: [PATCH] and for the picky - there's less copies for tolowoer. i also saw some BAD BAD leaks. bad! alloc pointer but not freeing them! also fixed up #include of alloca.h if needed. SVN revision: 31260 --- legacy/efreet/configure.in | 1 + legacy/efreet/src/lib/efreet_mime.c | 118 +++++++++++-------------- legacy/efreet/src/lib/efreet_private.h | 6 +- 3 files changed, 59 insertions(+), 66 deletions(-) diff --git a/legacy/efreet/configure.in b/legacy/efreet/configure.in index c3d19934bc..dbb89cb804 100644 --- a/legacy/efreet/configure.in +++ b/legacy/efreet/configure.in @@ -11,6 +11,7 @@ AC_HEADER_STDC AC_C_CONST AM_ENABLE_SHARED AM_PROG_LIBTOOL +AC_FUNC_ALLOCA AC_C___ATTRIBUTE__ dnl Set PACKAGE_DATA_DIR in config.h. diff --git a/legacy/efreet/src/lib/efreet_mime.c b/legacy/efreet/src/lib/efreet_mime.c index 69415ee7fb..33c4c15b01 100644 --- a/legacy/efreet/src/lib/efreet_mime.c +++ b/legacy/efreet/src/lib/efreet_mime.c @@ -257,47 +257,45 @@ const char *efreet_mime_magic_type_get(const char *file) */ const char *efreet_mime_globs_type_get(const char *file) { - Efreet_Mime_Glob *g; - char *s, *sl; - const char *ext, *mime; - + Efreet_Mime_Glob *g; + char *s, *sl, *p; + const char *ext, *mime; + /* * Check in the extension hash for the type */ - sl = alloca(strlen(file) + 1); - strcpy(sl, file); - for (s = sl; *s; s++) *s = tolower(*s); - ext = strchr(sl,'.'); - while(ext) + ext = strchr(file, '.'); + sl = alloca(strlen(ext) + 1); + for (s = ext, p = sl; *s; s++, p++) *p = tolower(*s); + *p = 0; + p = sl; + while (p) { - ++ext; - - if (ext && (mime = ecore_hash_get(wild, ext))) - return mime; - - ext = strchr(ext,'.'); + p++; + if (p && (mime = ecore_hash_get(wild, p))) return mime; + p = strchr(p, '.'); } - /* - * Fallback to the other globs if not found - */ - ecore_list_first_goto(globs); - while ((g = ecore_list_next(globs))) - { + /* + * Fallback to the other globs if not found + */ + ecore_list_first_goto(globs); + while ((g = ecore_list_next(globs))) + { if (efreet_mime_glob_match(file, g->glob)) - return g->mime; - } - - s = strdup(file); - ecore_list_first_goto(globs); - while ((g = ecore_list_next(globs))) - { - if (efreet_mime_glob_case_match(s, g->glob)) - return g->mime; - } - FREE(s); - - return NULL; + return g->mime; + } + + ext = alloca(strlen(file) + 1); + for (s = file, p = ext; *s; s++, p++) *p = tolower(*s); + *p = 0; + ecore_list_first_goto(globs); + while ((g = ecore_list_next(globs))) + { + if (efreet_mime_glob_case_match(ext, g->glob)) + return g->mime; + } + return NULL; } /** @@ -1196,18 +1194,14 @@ efreet_mime_magic_entry_free(void *data) static int efreet_mime_glob_match(const char *str, const char *glob) { - if (!str || !glob) + if (!str || !glob) return 0; + if (glob[0] == 0) + { + if (str[0] == 0) return 1; return 0; - - if (glob[0] == '\0') - { - if (str[0] == '\0') return 1; - return 0; - } - - if (!fnmatch(glob, str, 0)) return 1; - - return 0; + } + if (!fnmatch(glob, str, 0)) return 1; + return 0; } /** @@ -1215,30 +1209,24 @@ efreet_mime_glob_match(const char *str, const char *glob) * @param str: String (filename) to match * @param glob: Glob to match str to * @return Returns 1 on success, 0 on failure - * @brief Compares str to glob, case insensitive + * @brief Compares str to glob, case insensitive (expects str already in lower case) */ static int efreet_mime_glob_case_match(char *str, const char *glob) { - const char *p; - char *tglob, *tp; - - if (glob[0] == '\0') - { - if (str[0] == '\0') return 1; + const char *p; + char *tglob, *tp; + + if (!str || !glob) return 0; + if (glob[0] == 0) + { + if (str[0] == 0) return 1; return 0; - } - - for (tp = str; *tp != '\0'; tp++) *tp = tolower(*tp); - - tglob = NEW(1, strlen(glob) + 1); - for (tp = tglob, p = glob; *p != 0; p++, tp++) *tp = tolower(*p); - *tp = 0; - - if (!fnmatch(str, p, 0)) return 1; - - IF_FREE(tglob); - - return 0; + } + tglob = alloca(strlen(glob) + 1); + for (tp = tglob, p = glob; *p; p++, tp++) *tp = tolower(*p); + *tp = 0; + if (!fnmatch(str, tglob, 0)) return 1; + return 0; } diff --git a/legacy/efreet/src/lib/efreet_private.h b/legacy/efreet/src/lib/efreet_private.h index 20a1d2f867..88c5026dc9 100644 --- a/legacy/efreet/src/lib/efreet_private.h +++ b/legacy/efreet/src/lib/efreet_private.h @@ -11,6 +11,7 @@ * @{ */ +#include "config.h" #include #include #include @@ -24,11 +25,14 @@ #include #include +#ifdef HAVE_ALLOCA_H +#include +#endif + #include #include #include -#include "config.h" #include "efreet_xml.h" #include "efreet_ini.h"