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