new file, from Bruno
[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 #include <stddef.h>
27 #include <stdio.h>
28 #include <string.h>
29 #if HAVE_STDLIB_H
30 # include <stdlib.h>
31 #endif
32
33 #if HAVE_LANGINFO_CODESET
34 # include <langinfo.h>
35 #else
36 # if HAVE_SETLOCALE
37 #  include <locale.h>
38 # endif
39 #endif
40
41 char *xmalloc ();
42 char *xrealloc ();
43
44 /* Pointer to the contents of the charset.alias file, if it has already been
45    read, else NULL.  Its format is:
46    ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0'  */
47 static char * volatile charset_aliases;
48
49 /* Return a pointer to the contents of the charset.alias file.  */
50 static const char *
51 get_charset_aliases ()
52 {
53   char *cp;
54
55   cp = charset_aliases;
56   if (cp == NULL)
57     {
58       FILE *fp;
59
60       fp = fopen (LIBDIR "/" "charset.alias", "r");
61       if (fp == NULL)
62         /* File not found, treat it as empty.  */
63         cp = "";
64       else
65         {
66           /* Parse the file's contents.  */
67           int c;
68           char buf1[50+1];
69           char buf2[50+1];
70           char *res_ptr = NULL;
71           size_t res_size = 0;
72           size_t l1, l2;
73
74           for (;;)
75             {
76               c = getc (fp);
77               if (c == EOF)
78                 break;
79               if (c == '\n' || c == ' ' || c == '\t')
80                 continue;
81               if (c == '#')
82                 {
83                   /* Skip comment, to end of line.  */
84                   do
85                     c = getc (fp);
86                   while (!(c == EOF || c == '\n'));
87                   if (c == EOF)
88                     break;
89                   continue;
90                 }
91               ungetc (c, fp);
92               if (fscanf(fp, "%50s %50s", buf1, buf2) < 2)
93                 break;
94               l1 = strlen (buf1);
95               l2 = strlen (buf2);
96               if (res_size == 0)
97                 {
98                   res_size = l1 + 1 + l2 + 1;
99                   res_ptr = xmalloc (res_size + 1);
100                 }
101               else
102                 {
103                   res_size += l1 + 1 + l2 + 1;
104                   res_ptr = xrealloc (res_ptr, res_size + 1);
105                 }
106               strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
107               strcpy (res_ptr + res_size - (l2 + 1), buf2);
108             }
109           fclose (fp);
110           if (res_size == 0)
111             cp = "";
112           else
113             {
114               *(res_ptr + res_size) = '\0';
115               cp = res_ptr;
116             }
117         }
118
119       charset_aliases = cp;
120     }
121
122   return cp;
123 }
124
125 /* Determine the current locale's character encoding, and canonicalize it
126    into one of the canonical names listed in config.charset.
127    The result must not be freed; it is statically allocated.
128    If the canonical name cannot be determined, the result is a non-canonical
129    name or NULL.  */
130
131 #ifdef STATIC
132 STATIC
133 #endif
134 const char *
135 locale_charset ()
136 {
137   const char *codeset;
138   const char *aliases;
139
140 #if HAVE_LANGINFO_CODESET
141
142   /* Most systems support nl_langinfo (CODESET) nowadays.  */
143   codeset = nl_langinfo (CODESET);
144
145 #else
146
147   /* On old systems which lack it, use setlocale and getenv.  */
148   const char *locale = NULL;
149
150 # if HAVE_SETLOCALE
151   locale = setlocale (LC_CTYPE, NULL);
152 # endif
153   if (locale == NULL)
154     {
155       locale = getenv ("LC_ALL");
156       if (locale == NULL)
157         {
158           locale = getenv ("LC_CTYPE");
159           if (locale == NULL)
160             locale = getenv ("LANG");
161         }
162     }
163
164   /* On some old systems, one used to set locale = "iso8859_1". On others,
165      you set it to "language_COUNTRY.charset". In any case, we resolve it
166      through the charset.alias file.  */
167   codeset = locale;
168
169 #endif
170
171   if (codeset != NULL)
172     {
173       /* Resolve alias. */
174       for (aliases = get_charset_aliases ();
175            *aliases != '\0';
176            aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
177         if (!strcmp (codeset, aliases))
178           {
179             codeset = aliases + strlen (aliases) + 1;
180             break;
181           }
182     }
183
184   return codeset;
185 }