From b1570c18a997b40490a7f8aa4da7b4383d4a34a6 Mon Sep 17 00:00:00 2001 From: Kimmo Suominen Date: Tue, 18 Feb 2014 11:09:31 +0000 Subject: [PATCH] Cache old LC_CTYPE locale for restoring it later. Cache old LC_CTYPE locale before setting it to "C", so we can use it to restore LC_CTYPE instead of asking setlocale() to scan the environment variables. --- ChangeLog | 6 ++++++ src/funcs.c | 11 +++++++++-- src/readcdf.c | 11 +++++++++-- src/softmagic.c | 11 +++++++++-- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 24b14659..1806ca38 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2014-02-18 13:04 Kimmo Suominen (kimmo@suominen.com) + + * Cache old LC_CTYPE locale before setting it to "C", so + we can use it to restore LC_CTYPE instead of asking + setlocale() to scan the environment variables. + 2014-02-12 18:21 Christos Zoulas * Count recursion levels through indirect magic diff --git a/src/funcs.c b/src/funcs.c index df5c11fb..0c788160 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -27,10 +27,11 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: funcs.c,v 1.67 2014/02/12 23:20:53 christos Exp $") +FILE_RCSID("@(#)$File: funcs.c,v 1.68 2014/02/18 11:09:31 kim Exp $") #endif /* lint */ #include "magic.h" +#include #include #include #include @@ -442,7 +443,12 @@ file_replace(struct magic_set *ms, const char *pat, const char *rep) { regex_t rx; int rc, rv = -1; + char *old_lc_ctype; + old_lc_ctype = setlocale(LC_CTYPE, NULL); + assert(old_lc_ctype != NULL); + old_lc_ctype = strdup(old_lc_ctype); + assert(old_lc_ctype != NULL); (void)setlocale(LC_CTYPE, "C"); rc = regcomp(&rx, pat, REG_EXTENDED); if (rc) { @@ -463,6 +469,7 @@ file_replace(struct magic_set *ms, const char *pat, const char *rep) rv = nm; } out: - (void)setlocale(LC_CTYPE, ""); + (void)setlocale(LC_CTYPE, old_lc_ctype); + free(old_lc_ctype); return rv; } diff --git a/src/readcdf.c b/src/readcdf.c index 144136de..4251c4ab 100644 --- a/src/readcdf.c +++ b/src/readcdf.c @@ -26,9 +26,10 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: readcdf.c,v 1.37 2014/01/06 13:41:18 rrt Exp $") +FILE_RCSID("@(#)$File: readcdf.c,v 1.38 2014/02/18 11:09:31 kim Exp $") #endif +#include #include #include #include @@ -74,14 +75,20 @@ cdf_app_to_mime(const char *vbuf, const struct nv *nv) { size_t i; const char *rv = NULL; + char *old_lc_ctype; + old_lc_ctype = setlocale(LC_CTYPE, NULL); + assert(old_lc_ctype != NULL); + old_lc_ctype = strdup(old_lc_ctype); + assert(old_lc_ctype != NULL); (void)setlocale(LC_CTYPE, "C"); for (i = 0; nv[i].pattern != NULL; i++) if (strcasestr(vbuf, nv[i].pattern) != NULL) { rv = nv[i].mime; break; } - (void)setlocale(LC_CTYPE, ""); + (void)setlocale(LC_CTYPE, old_lc_ctype); + free(old_lc_ctype); return rv; } diff --git a/src/softmagic.c b/src/softmagic.c index 5ed347e2..2e41e124 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: softmagic.c,v 1.174 2014/02/12 23:20:53 christos Exp $") +FILE_RCSID("@(#)$File: softmagic.c,v 1.175 2014/02/18 11:09:31 kim Exp $") #endif /* lint */ #include "magic.h" @@ -42,6 +42,7 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.174 2014/02/12 23:20:53 christos Exp $") #else #define F(a, b) (a) #endif +#include #include #include #include @@ -352,10 +353,15 @@ check_fmt(struct magic_set *ms, struct magic *m) { regex_t rx; int rc, rv = -1; + char *old_lc_ctype; if (strchr(m->desc, '%') == NULL) return 0; + old_lc_ctype = setlocale(LC_CTYPE, NULL); + assert(old_lc_ctype != NULL); + old_lc_ctype = strdup(old_lc_ctype); + assert(old_lc_ctype != NULL); (void)setlocale(LC_CTYPE, "C"); rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB); if (rc) { @@ -367,7 +373,8 @@ check_fmt(struct magic_set *ms, struct magic *m) regfree(&rx); rv = !rc; } - (void)setlocale(LC_CTYPE, ""); + (void)setlocale(LC_CTYPE, old_lc_ctype); + free(old_lc_ctype); return rv; } -- 2.40.0