Add Bruno's comment justifying use of volatile.
[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 /* The following static variable is declared 'volatile' to avoid a
52    possible multithread problem in the function get_charset_aliases. If we
53    are running in a threaded environment, and if two threads initialize
54    'charset_aliases' simultaneously, both will produce the same value,
55    and everything will be ok if the two assignments to 'charset_aliases'
56    are atomic. But I don't know what will happen if the two assignments mix.  */
57 /* Pointer to the contents of the charset.alias file, if it has already been
58    read, else NULL.  Its format is:
59    ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0'  */
60 static char * volatile charset_aliases;
61
62 /* Return a pointer to the contents of the charset.alias file.  */
63 static const char *
64 get_charset_aliases ()
65 {
66   char *cp;
67
68   cp = charset_aliases;
69   if (cp == NULL)
70     {
71       FILE *fp;
72
73       fp = fopen (LIBDIR "/" "charset.alias", "r");
74       if (fp == NULL)
75         /* File not found, treat it as empty.  */
76         cp = "";
77       else
78         {
79           /* Parse the file's contents.  */
80           int c;
81           char buf1[50+1];
82           char buf2[50+1];
83           char *res_ptr = NULL;
84           size_t res_size = 0;
85           size_t l1, l2;
86
87           for (;;)
88             {
89               c = getc (fp);
90               if (c == EOF)
91                 break;
92               if (c == '\n' || c == ' ' || c == '\t')
93                 continue;
94               if (c == '#')
95                 {
96                   /* Skip comment, to end of line.  */
97                   do
98                     c = getc (fp);
99                   while (!(c == EOF || c == '\n'));
100                   if (c == EOF)
101                     break;
102                   continue;
103                 }
104               ungetc (c, fp);
105               if (fscanf(fp, "%50s %50s", buf1, buf2) < 2)
106                 break;
107               l1 = strlen (buf1);
108               l2 = strlen (buf2);
109               if (res_size == 0)
110                 {
111                   res_size = l1 + 1 + l2 + 1;
112                   res_ptr = xmalloc (res_size + 1);
113                 }
114               else
115                 {
116                   res_size += l1 + 1 + l2 + 1;
117                   res_ptr = xrealloc (res_ptr, res_size + 1);
118                 }
119               strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
120               strcpy (res_ptr + res_size - (l2 + 1), buf2);
121             }
122           fclose (fp);
123           if (res_size == 0)
124             cp = "";
125           else
126             {
127               *(res_ptr + res_size) = '\0';
128               cp = res_ptr;
129             }
130         }
131
132       charset_aliases = cp;
133     }
134
135   return cp;
136 }
137
138 /* Determine the current locale's character encoding, and canonicalize it
139    into one of the canonical names listed in config.charset.
140    The result must not be freed; it is statically allocated.
141    If the canonical name cannot be determined, the result is a non-canonical
142    name or NULL.  */
143
144 #ifdef STATIC
145 STATIC
146 #endif
147 const char *
148 locale_charset ()
149 {
150   const char *codeset;
151   const char *aliases;
152
153 #if HAVE_LANGINFO_CODESET
154
155   /* Most systems support nl_langinfo (CODESET) nowadays.  */
156   codeset = nl_langinfo (CODESET);
157
158 #else
159
160   /* On old systems which lack it, use setlocale and getenv.  */
161   const char *locale = NULL;
162
163 # if HAVE_SETLOCALE
164   locale = setlocale (LC_CTYPE, NULL);
165 # endif
166   if (locale == NULL)
167     {
168       locale = getenv ("LC_ALL");
169       if (locale == NULL)
170         {
171           locale = getenv ("LC_CTYPE");
172           if (locale == NULL)
173             locale = getenv ("LANG");
174         }
175     }
176
177   /* On some old systems, one used to set locale = "iso8859_1". On others,
178      you set it to "language_COUNTRY.charset". In any case, we resolve it
179      through the charset.alias file.  */
180   codeset = locale;
181
182 #endif
183
184   if (codeset != NULL)
185     {
186       /* Resolve alias. */
187       for (aliases = get_charset_aliases ();
188            *aliases != '\0';
189            aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
190         if (!strcmp (codeset, aliases))
191           {
192             codeset = aliases + strlen (aliases) + 1;
193             break;
194           }
195     }
196
197   return codeset;
198 }