Guard some #includes with `#if HAVE_...'.
[gnulib.git] / lib / localcharset.c
1 /* Determine a canonical name for the current locale's character encoding.
2
3    Copyright (C) 2000 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify it
6    under the terms of the GNU Library General Public License as published
7    by the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18    USA.  */
19
20 /* Written by Bruno Haible <haible@clisp.cons.org>.  */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #if HAVE_STDDEF_H
27 # include <stddef.h>
28 #endif
29
30 #include <stdio.h>
31 #if HAVE_STRING_H
32 # include <string.h>
33 #else
34 # include <strings.h>
35 #endif
36 #if HAVE_STDLIB_H
37 # include <stdlib.h>
38 #endif
39
40 #if HAVE_LANGINFO_CODESET
41 # include <langinfo.h>
42 #else
43 # if HAVE_SETLOCALE
44 #  include <locale.h>
45 # endif
46 #endif
47
48 char *xmalloc ();
49 char *xrealloc ();
50
51 /* Pointer to the contents of the charset.alias file, if it has already been
52    read, else NULL.  Its format is:
53    ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0'  */
54 static char * volatile charset_aliases;
55
56 /* Return a pointer to the contents of the charset.alias file.  */
57 static const char *
58 get_charset_aliases ()
59 {
60   char *cp;
61
62   cp = charset_aliases;
63   if (cp == NULL)
64     {
65       FILE *fp;
66
67       fp = fopen (LIBDIR "/" "charset.alias", "r");
68       if (fp == NULL)
69         /* File not found, treat it as empty.  */
70         cp = "";
71       else
72         {
73           /* Parse the file's contents.  */
74           int c;
75           char buf1[50+1];
76           char buf2[50+1];
77           char *res_ptr = NULL;
78           size_t res_size = 0;
79           size_t l1, l2;
80
81           for (;;)
82             {
83               c = getc (fp);
84               if (c == EOF)
85                 break;
86               if (c == '\n' || c == ' ' || c == '\t')
87                 continue;
88               if (c == '#')
89                 {
90                   /* Skip comment, to end of line.  */
91                   do
92                     c = getc (fp);
93                   while (!(c == EOF || c == '\n'));
94                   if (c == EOF)
95                     break;
96                   continue;
97                 }
98               ungetc (c, fp);
99               if (fscanf(fp, "%50s %50s", buf1, buf2) < 2)
100                 break;
101               l1 = strlen (buf1);
102               l2 = strlen (buf2);
103               if (res_size == 0)
104                 {
105                   res_size = l1 + 1 + l2 + 1;
106                   res_ptr = xmalloc (res_size + 1);
107                 }
108               else
109                 {
110                   res_size += l1 + 1 + l2 + 1;
111                   res_ptr = xrealloc (res_ptr, res_size + 1);
112                 }
113               strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
114               strcpy (res_ptr + res_size - (l2 + 1), buf2);
115             }
116           fclose (fp);
117           if (res_size == 0)
118             cp = "";
119           else
120             {
121               *(res_ptr + res_size) = '\0';
122               cp = res_ptr;
123             }
124         }
125
126       charset_aliases = cp;
127     }
128
129   return cp;
130 }
131
132 /* Determine the current locale's character encoding, and canonicalize it
133    into one of the canonical names listed in config.charset.
134    The result must not be freed; it is statically allocated.
135    If the canonical name cannot be determined, the result is a non-canonical
136    name or NULL.  */
137
138 #ifdef STATIC
139 STATIC
140 #endif
141 const char *
142 locale_charset ()
143 {
144   const char *codeset;
145   const char *aliases;
146
147 #if HAVE_LANGINFO_CODESET
148
149   /* Most systems support nl_langinfo (CODESET) nowadays.  */
150   codeset = nl_langinfo (CODESET);
151
152 #else
153
154   /* On old systems which lack it, use setlocale and getenv.  */
155   const char *locale = NULL;
156
157 # if HAVE_SETLOCALE
158   locale = setlocale (LC_CTYPE, NULL);
159 # endif
160   if (locale == NULL)
161     {
162       locale = getenv ("LC_ALL");
163       if (locale == NULL)
164         {
165           locale = getenv ("LC_CTYPE");
166           if (locale == NULL)
167             locale = getenv ("LANG");
168         }
169     }
170
171   /* On some old systems, one used to set locale = "iso8859_1". On others,
172      you set it to "language_COUNTRY.charset". In any case, we resolve it
173      through the charset.alias file.  */
174   codeset = locale;
175
176 #endif
177
178   if (codeset != NULL)
179     {
180       /* Resolve alias. */
181       for (aliases = get_charset_aliases ();
182            *aliases != '\0';
183            aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
184         if (!strcmp (codeset, aliases))
185           {
186             codeset = aliases + strlen (aliases) + 1;
187             break;
188           }
189     }
190
191   return codeset;
192 }