1 /*	$NetBSD: localencoding.c,v 1.4 2014/12/10 04:37:55 christos Exp $	*/
2 
3 #ifndef lint
4 static char *rcsid = "Id: localencoding.c,v 1.1 2003/06/04 00:25:53 marka Exp ";
5 #endif
6 
7 /*
8  * Copyright (c) 2000 Japan Network Information Center.  All rights reserved.
9  *
10  * By using this file, you agree to the terms and conditions set forth bellow.
11  *
12  * 			LICENSE TERMS AND CONDITIONS
13  *
14  * The following License Terms and Conditions apply, unless a different
15  * license is obtained from Japan Network Information Center ("JPNIC"),
16  * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
17  * Chiyoda-ku, Tokyo 101-0047, Japan.
18  *
19  * 1. Use, Modification and Redistribution (including distribution of any
20  *    modified or derived work) in source and/or binary forms is permitted
21  *    under this License Terms and Conditions.
22  *
23  * 2. Redistribution of source code must retain the copyright notices as they
24  *    appear in each source code file, this License Terms and Conditions.
25  *
26  * 3. Redistribution in binary form must reproduce the Copyright Notice,
27  *    this License Terms and Conditions, in the documentation and/or other
28  *    materials provided with the distribution.  For the purposes of binary
29  *    distribution the "Copyright Notice" refers to the following language:
30  *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
31  *
32  * 4. The name of JPNIC may not be used to endorse or promote products
33  *    derived from this Software without specific prior written approval of
34  *    JPNIC.
35  *
36  * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
37  *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38  *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
39  *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
40  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
45  *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
46  *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
47  */
48 
49 #include <config.h>
50 
51 #ifdef WIN32
52 #include <windows.h>
53 #endif
54 #include <stddef.h>
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <string.h>
58 
59 #ifdef HAVE_LOCALE_H
60 #include <locale.h>
61 #endif
62 #ifdef HAVE_LANGINFO_H
63 #include <langinfo.h>
64 #endif
65 
66 #include <idn/logmacro.h>
67 #include <idn/localencoding.h>
68 #include <idn/debug.h>
69 
70 #ifdef ENABLE_MDNKIT_COMPAT
71 #include <mdn/localencoding.h>
72 #endif
73 
74 const char *
75 idn_localencoding_name(void) {
76 	char *name;
77 
78 	TRACE(("idn_localencoding_name()\n"));
79 
80 	if ((name = getenv(IDN_LOCALCS_ENV)) != NULL) {
81 		TRACE(("local encoding=\"%-.30s\"\n",
82 		      name == NULL ? "<null>" : name));
83 		return (name);
84 	}
85 #ifdef ENABLE_MDNKIT_COMPAT
86 	if ((name = getenv(MDN_LOCALCS_ENV)) != NULL) {
87 		TRACE(("local encoding=\"%-.30s\"\n",
88 		      name == NULL ? "<null>" : name));
89 		return (name);
90 	}
91 #endif
92 
93 #ifdef WIN32
94 	{
95 		static char cp_str[40];	/* enough */
96 		(void)sprintf(cp_str, "CP%u", GetACP());
97 		TRACE(("local encoding(codepage)=\"%-.30s\"\n", cp_str));
98 		return (cp_str);
99 	}
100 #else /* WIN32 */
101 #ifdef HAVE_LIBCHARSET
102 	name = locale_charset();
103 	TRACE(("local encoding=\"%-.30s\"\n",
104 	       name == NULL ? "<null>" : name));
105 	return (name);
106 #endif
107 
108 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
109 	if ((name = nl_langinfo(CODESET)) != NULL) {
110 		TRACE(("local encoding=\"%-.30s\"\n",
111 		       name == NULL ? "<null>" : name));
112 		return (name);
113 	}
114 #endif
115 	(void)(
116 #ifdef HAVE_SETLOCALE
117 		(name = setlocale(LC_CTYPE, NULL)) ||
118 #endif
119 		(name = getenv("LC_ALL")) ||
120 		(name = getenv("LC_CTYPE")) ||
121 		(name = getenv("LANG")));
122 	TRACE(("local encoding=\"%-.30s\"\n", name == NULL ? "<null>" : name));
123 	return (name);
124 #endif /* WIN32 */
125 }
126