1 /* Functions to read locale data files.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library 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.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
28 #include "localeinfo.h"
30 const size_t _nl_category_num_items[] =
32 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
33 [category] = _NL_ITEM_INDEX (_NL_NUM_##category),
34 #include "categories.def"
35 #undef DEFINE_CATEGORY
39 #define NO_PAREN(arg, rest...) arg, ##rest
41 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
42 static const enum value_type _nl_value_type_##category[] = { NO_PAREN items };
43 #define DEFINE_ELEMENT(element, element_name, optstd, type, rest...) \
44 [_NL_ITEM_INDEX (element)] = type,
45 #include "categories.def"
46 #undef DEFINE_CATEGORY
48 static const enum value_type *_nl_value_types[] =
50 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
51 [category] = _nl_value_type_##category,
52 #include "categories.def"
53 #undef DEFINE_CATEGORY
58 _nl_load_locale (int category, char **name)
64 unsigned int nstrings;
65 unsigned int strindex[0];
68 struct locale_data *newdata;
70 inline unsigned int SWAP (const unsigned int *inw)
72 const unsigned char *inc = (const unsigned char *) inw;
75 return (inc[3] << 24) | (inc[2] << 16) | (inc[1] << 8) | inc[0];
79 if ((*name)[0] == '\0')
81 *name = getenv ("LC_ALL");
82 if (! *name || (*name)[0] == '\0')
83 *name = getenv (_nl_category_names[category]);
84 if (! *name || (*name)[0] == '\0')
85 *name = getenv ("LANG");
86 if (! *name || (*name)[0] == '\0')
87 *name = (char *) "local";
91 const char *catname = _nl_category_names[category];
92 size_t namelen = strlen (*name);
93 size_t catlen = strlen (catname);
94 char file[sizeof LOCALE_PATH + 1 + namelen + catlen * 2 + 4];
95 if (strchr (*name, '/') != NULL)
96 sprintf (file, "%s/%s", *name, catname);
98 sprintf (file, "%s/%s/%s", LOCALE_PATH, *name, catname);
99 fd = __open (file, O_RDONLY);
102 if (__fstat (fd, &st) < 0)
104 if (S_ISDIR (st.st_mode))
106 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
109 memcpy (stpcpy (strchr (file, '\0'), "SYS_"), catname, catlen);
110 fd = __open (file, O_RDONLY);
113 if (__fstat (fd, &st) < 0)
119 /* Map in the file's data. */
122 /* Linux seems to lack read-only copy-on-write. */
123 #define MAP_COPY MAP_PRIVATE
126 /* Some systems do not have this flag; it is superfluous. */
130 /* Some systems might lack this; they lose. */
131 #define MAP_INHERIT 0
133 filedata = (void *) __mmap ((caddr_t) 0, st.st_size,
134 PROT_READ, MAP_FILE|MAP_COPY|MAP_INHERIT,
136 if (filedata == (void *) -1)
140 /* No mmap; allocate a buffer and read from the file. */
141 filedata = malloc (st.st_size);
144 off_t to_read = st.st_size;
146 char *p = (char *) filedata;
149 nread = __read (fd, p, to_read);
154 errno = EINVAL; /* Bizarreness going on. */
170 if (filedata->magic == LIMAGIC (category))
171 /* Good data file in our byte order. */
175 /* Try the other byte order. */
177 if (SWAP (&filedata->magic) != LIMAGIC (category))
178 /* Bad data file in either byte order. */
181 __munmap ((caddr_t) filedata, st.st_size);
188 #define W(word) SWAP (&(word))
190 if (W (filedata->nstrings) < _nl_category_num_items[category] ||
191 (sizeof *filedata + W (filedata->nstrings) * sizeof (unsigned int)
194 /* Insufficient data. */
199 newdata = malloc (sizeof *newdata +
200 W (filedata->nstrings) * sizeof (union locale_data_value));
204 newdata->filedata = (void *) filedata;
205 newdata->filesize = st.st_size;
206 newdata->nstrings = W (filedata->nstrings);
207 for (i = 0; i < newdata->nstrings; ++i)
209 unsigned int idx = W (filedata->strindex[i]);
210 if (idx >= newdata->filesize)
216 if (_nl_value_types[category][i] == word)
217 newdata->values[i].word = W (*((u32_t *) (newdata->filedata + idx)));
219 newdata->values[i].string = newdata->filedata + idx;
227 _nl_free_locale (struct locale_data *data)
231 /* Ignore a null pointer, like free does. */
233 if (__munmap ((caddr_t) data->filedata, data->filesize) < 0)
236 free ((void *) data->filedata);