Index: ChangeLog
===================================================================
RCS file: /p/file/cvsroot/file/ChangeLog,v
retrieving revision 1.219
retrieving revision 1.220
diff -u -r1.219 -r1.220
--- ChangeLog	12 Feb 2014 23:21:46 -0000	1.219
+++ ChangeLog	18 Feb 2014 11:09:31 -0000	1.220
@@ -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: src/funcs.c
===================================================================
RCS file: /p/file/cvsroot/file/src/funcs.c,v
retrieving revision 1.67
retrieving revision 1.68
diff -u -r1.67 -r1.68
--- src/funcs.c	12 Feb 2014 23:20:53 -0000	1.67
+++ src/funcs.c	18 Feb 2014 11:09:31 -0000	1.68
@@ -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 <assert.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
@@ -442,7 +443,12 @@
 {
 	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 @@
 		rv = nm;
 	}
 out:
-	(void)setlocale(LC_CTYPE, "");
+	(void)setlocale(LC_CTYPE, old_lc_ctype);
+	free(old_lc_ctype);
 	return rv;
 }
Index: src/readcdf.c
===================================================================
RCS file: /p/file/cvsroot/file/src/readcdf.c,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -r1.37 -r1.38
--- src/readcdf.c	6 Jan 2014 13:41:18 -0000	1.37
+++ src/readcdf.c	18 Feb 2014 11:09:31 -0000	1.38
@@ -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 <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
@@ -74,14 +75,20 @@
 {
 	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: src/softmagic.c
===================================================================
RCS file: /p/file/cvsroot/file/src/softmagic.c,v
retrieving revision 1.174
retrieving revision 1.176
diff -u -r1.174 -r1.176
--- src/softmagic.c	12 Feb 2014 23:20:53 -0000	1.174
+++ src/softmagic.c	18 Feb 2014 17:59:21 -0000	1.176
@@ -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.176 2014/02/18 17:59:21 kim Exp $")
 #endif	/* lint */
 
 #include "magic.h"
@@ -42,6 +42,7 @@
 #else
 #define F(a, b) (a)
 #endif
+#include <assert.h>
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
@@ -352,10 +353,15 @@
 {
 	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 @@
 		regfree(&rx);
 		rv = !rc;
 	}
-	(void)setlocale(LC_CTYPE, "");
+	(void)setlocale(LC_CTYPE, old_lc_ctype);
+	free(old_lc_ctype);
 	return rv;
 }
 
@@ -1884,6 +1891,7 @@
 	double dl, dv;
 	int matched;
 	union VALUETYPE *p = &ms->ms_value;
+	char *old_lc_ctype;
 
 	switch (m->type) {
 	case FILE_BYTE:
@@ -2042,6 +2050,11 @@
 		if (ms->search.s == 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");
 		l = 0;
 		rc = regcomp(&rx, m->value.s,
 		    REG_EXTENDED|REG_NEWLINE|
@@ -2090,6 +2103,8 @@
 			}
 			regfree(&rx);
 		}
+		(void)setlocale(LC_CTYPE, old_lc_ctype);
+		free(old_lc_ctype);
 		if (v == (uint64_t)-1)
 			return -1;
 		break;
