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