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