]> granicus.if.org Git - file/commitdiff
Cache old LC_CTYPE locale for restoring it later.
authorKimmo Suominen <kimmo@suominen.com>
Tue, 18 Feb 2014 11:09:31 +0000 (11:09 +0000)
committerKimmo Suominen <kimmo@suominen.com>
Tue, 18 Feb 2014 11:09:31 +0000 (11:09 +0000)
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
src/funcs.c
src/readcdf.c
src/softmagic.c

index 24b146591b363353871a0a6400f0150538af4490..1806ca3845f56236202e047b357de0667c1f68b6 100644 (file)
--- 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 <christos@zoulas.com>
 
        * Count recursion levels through indirect magic
index df5c11fbbaa02d33ef4f863d5e6913b3d5eacf6a..0c788160d6ae60c7c8cee593c8afedfddb5097e3 100644 (file)
 #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 <assert.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
@@ -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;
 }
index 144136de59304ed622d1a1298623bf6a4d77dd88..4251c4ab0ccd8bf34bf4dea79c1037805651cb2d 100644 (file)
 #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 <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
@@ -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;
 }
 
index 5ed347e2468b6eb7f186b676677c8e4f9e2e500b..2e41e124a5549db2cf4a52a100f60a125fa43f55 100644 (file)
@@ -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 <assert.h>
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
@@ -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;
 }