~ubuntu-branches/ubuntu/raring/findutils/raring

« back to all changes in this revision

Viewing changes to intl/localcharset.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2005-07-04 11:37:37 UTC
  • mto: (11.1.1 lenny) (1.1.10 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050704113737-oxfumqxsqgfz5gay
Tags: upstream-4.2.22
ImportĀ upstreamĀ versionĀ 4.2.22

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Determine a canonical name for the current locale's character encoding.
2
 
 
3
 
   Copyright (C) 2000-2001 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 <gnulib/config.h>
24
 
# undef VERSION
25
 
# undef PACKAGE_VERSION
26
 
# undef PACKAGE_TARNAME
27
 
# undef PACKAGE_STRING
28
 
# include <config.h>
29
 
#endif
30
 
 
31
 
#if HAVE_STDDEF_H
32
 
# include <stddef.h>
33
 
#endif
34
 
 
35
 
#include <stdio.h>
36
 
#if HAVE_STRING_H
37
 
# include <string.h>
38
 
#else
39
 
# include <strings.h>
40
 
#endif
41
 
#if HAVE_STDLIB_H
42
 
# include <stdlib.h>
43
 
#endif
44
 
 
45
 
#if defined _WIN32 || defined __WIN32__
46
 
# undef WIN32   /* avoid warning on mingw32 */
47
 
# define WIN32
48
 
#endif
49
 
 
50
 
#ifndef WIN32
51
 
# if HAVE_LANGINFO_CODESET
52
 
#  include <langinfo.h>
53
 
# else
54
 
#  if HAVE_SETLOCALE
55
 
#   include <locale.h>
56
 
#  endif
57
 
# endif
58
 
#else /* WIN32 */
59
 
# define WIN32_LEAN_AND_MEAN
60
 
# include <windows.h>
61
 
#endif
62
 
 
63
 
#ifndef DIRECTORY_SEPARATOR
64
 
# define DIRECTORY_SEPARATOR '/'
65
 
#endif
66
 
 
67
 
#ifndef ISSLASH
68
 
# define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
69
 
#endif
70
 
 
71
 
/* The following static variable is declared 'volatile' to avoid a
72
 
   possible multithread problem in the function get_charset_aliases. If we
73
 
   are running in a threaded environment, and if two threads initialize
74
 
   'charset_aliases' simultaneously, both will produce the same value,
75
 
   and everything will be ok if the two assignments to 'charset_aliases'
76
 
   are atomic. But I don't know what will happen if the two assignments mix.  */
77
 
#if __STDC__ != 1
78
 
# define volatile /* empty */
79
 
#endif
80
 
/* Pointer to the contents of the charset.alias file, if it has already been
81
 
   read, else NULL.  Its format is:
82
 
   ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0'  */
83
 
static char * volatile charset_aliases;
84
 
 
85
 
/* Return a pointer to the contents of the charset.alias file.  */
86
 
static const char *
87
 
get_charset_aliases ()
88
 
{
89
 
  char *cp;
90
 
 
91
 
  cp = charset_aliases;
92
 
  if (cp == NULL)
93
 
    {
94
 
#ifndef WIN32
95
 
      FILE *fp;
96
 
      const char *dir = LIBDIR;
97
 
      const char *base = "charset.alias";
98
 
      char *file_name;
99
 
 
100
 
      /* Concatenate dir and base into freshly allocated file_name.  */
101
 
      {
102
 
        size_t dir_len = strlen (dir);
103
 
        size_t base_len = strlen (base);
104
 
        int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
105
 
        file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
106
 
        if (file_name != NULL)
107
 
          {
108
 
            memcpy (file_name, dir, dir_len);
109
 
            if (add_slash)
110
 
              file_name[dir_len] = DIRECTORY_SEPARATOR;
111
 
            memcpy (file_name + dir_len + add_slash, base, base_len + 1);
112
 
          }
113
 
      }
114
 
 
115
 
      if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
116
 
        /* Out of memory or file not found, treat it as empty.  */
117
 
        cp = "";
118
 
      else
119
 
        {
120
 
          /* Parse the file's contents.  */
121
 
          int c;
122
 
          char buf1[50+1];
123
 
          char buf2[50+1];
124
 
          char *res_ptr = NULL;
125
 
          size_t res_size = 0;
126
 
          size_t l1, l2;
127
 
 
128
 
          for (;;)
129
 
            {
130
 
              c = getc (fp);
131
 
              if (c == EOF)
132
 
                break;
133
 
              if (c == '\n' || c == ' ' || c == '\t')
134
 
                continue;
135
 
              if (c == '#')
136
 
                {
137
 
                  /* Skip comment, to end of line.  */
138
 
                  do
139
 
                    c = getc (fp);
140
 
                  while (!(c == EOF || c == '\n'));
141
 
                  if (c == EOF)
142
 
                    break;
143
 
                  continue;
144
 
                }
145
 
              ungetc (c, fp);
146
 
              if (fscanf(fp, "%50s %50s", buf1, buf2) < 2)
147
 
                break;
148
 
              l1 = strlen (buf1);
149
 
              l2 = strlen (buf2);
150
 
              if (res_size == 0)
151
 
                {
152
 
                  res_size = l1 + 1 + l2 + 1;
153
 
                  res_ptr = malloc (res_size + 1);
154
 
                }
155
 
              else
156
 
                {
157
 
                  res_size += l1 + 1 + l2 + 1;
158
 
                  res_ptr = realloc (res_ptr, res_size + 1);
159
 
                }
160
 
              if (res_ptr == NULL)
161
 
                {
162
 
                  /* Out of memory. */
163
 
                  res_size = 0;
164
 
                  break;
165
 
                }
166
 
              strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
167
 
              strcpy (res_ptr + res_size - (l2 + 1), buf2);
168
 
            }
169
 
          fclose (fp);
170
 
          if (res_size == 0)
171
 
            cp = "";
172
 
          else
173
 
            {
174
 
              *(res_ptr + res_size) = '\0';
175
 
              cp = res_ptr;
176
 
            }
177
 
        }
178
 
 
179
 
      if (file_name != NULL)
180
 
        free (file_name);
181
 
 
182
 
#else /* WIN32 */
183
 
 
184
 
      /* To avoid the troubles of installing a separate file in the same
185
 
         directory as the DLL and of retrieving the DLL's directory at
186
 
         runtime, simply inline the aliases here.  */
187
 
 
188
 
      cp = "CP936" "\0" "GBK" "\0"
189
 
           "CP1361" "\0" "JOHAB" "\0";
190
 
#endif
191
 
 
192
 
      charset_aliases = cp;
193
 
    }
194
 
 
195
 
  return cp;
196
 
}
197
 
 
198
 
/* Determine the current locale's character encoding, and canonicalize it
199
 
   into one of the canonical names listed in config.charset.
200
 
   The result must not be freed; it is statically allocated.
201
 
   If the canonical name cannot be determined, the result is a non-canonical
202
 
   name.  */
203
 
 
204
 
#ifdef STATIC
205
 
STATIC
206
 
#endif
207
 
const char *
208
 
locale_charset ()
209
 
{
210
 
  const char *codeset;
211
 
  const char *aliases;
212
 
 
213
 
#ifndef WIN32
214
 
 
215
 
# if HAVE_LANGINFO_CODESET
216
 
 
217
 
  /* Most systems support nl_langinfo (CODESET) nowadays.  */
218
 
  codeset = nl_langinfo (CODESET);
219
 
 
220
 
# else
221
 
 
222
 
  /* On old systems which lack it, use setlocale or getenv.  */
223
 
  const char *locale = NULL;
224
 
 
225
 
  /* But most old systems don't have a complete set of locales.  Some
226
 
     (like SunOS 4 or DJGPP) have only the C locale.  Therefore we don't
227
 
     use setlocale here; it would return "C" when it doesn't support the
228
 
     locale name the user has set.  */
229
 
#  if HAVE_SETLOCALE && 0
230
 
  locale = setlocale (LC_CTYPE, NULL);
231
 
#  endif
232
 
  if (locale == NULL || locale[0] == '\0')
233
 
    {
234
 
      locale = getenv ("LC_ALL");
235
 
      if (locale == NULL || locale[0] == '\0')
236
 
        {
237
 
          locale = getenv ("LC_CTYPE");
238
 
          if (locale == NULL || locale[0] == '\0')
239
 
            locale = getenv ("LANG");
240
 
        }
241
 
    }
242
 
 
243
 
  /* On some old systems, one used to set locale = "iso8859_1". On others,
244
 
     you set it to "language_COUNTRY.charset". In any case, we resolve it
245
 
     through the charset.alias file.  */
246
 
  codeset = locale;
247
 
 
248
 
# endif
249
 
 
250
 
#else /* WIN32 */
251
 
 
252
 
  static char buf[2 + 10 + 1];
253
 
 
254
 
  /* Win32 has a function returning the locale's codepage as a number.  */
255
 
  sprintf (buf, "CP%u", GetACP ());
256
 
  codeset = buf;
257
 
 
258
 
#endif
259
 
 
260
 
  if (codeset == NULL)
261
 
    /* The canonical name cannot be determined.  */
262
 
    codeset = "";
263
 
 
264
 
  /* Resolve alias. */
265
 
  for (aliases = get_charset_aliases ();
266
 
       *aliases != '\0';
267
 
       aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
268
 
    if (strcmp (codeset, aliases) == 0
269
 
        || (aliases[0] == '*' && aliases[1] == '\0'))
270
 
      {
271
 
        codeset = aliases + strlen (aliases) + 1;
272
 
        break;
273
 
      }
274
 
 
275
 
  return codeset;
276
 
}