* sysdeps/m68k/fpu/bits/mathinline.h: Don't define log2 as inline.
[glibc.git] / locale / loadlocale.c
blob55c9a587f4c4899da8fd8866623c3381b9243f98
1 /* Functions to read locale data files.
2 Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <assert.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #ifdef _POSIX_MAPPED_FILES
29 # include <sys/mman.h>
30 #endif
31 #include <sys/stat.h>
33 #include "localeinfo.h"
36 static const size_t _nl_category_num_items[] =
38 #define DEFINE_CATEGORY(category, category_name, items, a) \
39 [category] = _NL_ITEM_INDEX (_NL_NUM_##category),
40 #include "categories.def"
41 #undef DEFINE_CATEGORY
45 #define NO_PAREN(arg, rest...) arg, ##rest
47 #define DEFINE_CATEGORY(category, category_name, items, a) \
48 static const enum value_type _nl_value_type_##category[] = { NO_PAREN items };
49 #define DEFINE_ELEMENT(element, element_name, optstd, type, rest...) \
50 [_NL_ITEM_INDEX (element)] = type,
51 #include "categories.def"
52 #undef DEFINE_CATEGORY
54 static const enum value_type *_nl_value_types[] =
56 #define DEFINE_CATEGORY(category, category_name, items, a) \
57 [category] = _nl_value_type_##category,
58 #include "categories.def"
59 #undef DEFINE_CATEGORY
63 void
64 _nl_load_locale (struct loaded_l10nfile *file, int category)
66 int fd;
67 struct
69 unsigned int magic;
70 unsigned int nstrings;
71 unsigned int strindex[0];
72 } *filedata;
73 struct stat64 st;
74 struct locale_data *newdata;
75 int save_err;
76 int mmaped = 1;
77 size_t cnt;
79 file->decided = 1;
80 file->data = NULL;
82 fd = __open (file->filename, O_RDONLY);
83 if (__builtin_expect (fd, 0) < 0)
84 /* Cannot open the file. */
85 return;
87 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
88 goto puntfd;
89 if (__builtin_expect (S_ISDIR (st.st_mode), 0))
91 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
92 instead. */
93 char *newp;
94 size_t filenamelen;
96 __close (fd);
98 filenamelen = strlen (file->filename);
99 newp = (char *) alloca (filenamelen
100 + 5 + _nl_category_name_sizes[category] + 1);
101 __mempcpy (__mempcpy (__mempcpy (newp, file->filename, filenamelen),
102 "/SYS_", 5),
103 _nl_category_names[category],
104 _nl_category_name_sizes[category] + 1);
106 fd = __open (newp, O_RDONLY);
107 if (__builtin_expect (fd, 0) < 0)
108 return;
110 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
111 goto puntfd;
114 /* Map in the file's data. */
115 save_err = errno;
116 #ifdef _POSIX_MAPPED_FILES
117 # ifndef MAP_COPY
118 /* Linux seems to lack read-only copy-on-write. */
119 # define MAP_COPY MAP_PRIVATE
120 # endif
121 # ifndef MAP_FILE
122 /* Some systems do not have this flag; it is superfluous. */
123 # define MAP_FILE 0
124 # endif
125 # ifndef MAP_INHERIT
126 /* Some systems might lack this; they lose. */
127 # define MAP_INHERIT 0
128 # endif
129 filedata = (void *) __mmap ((caddr_t) 0, st.st_size, PROT_READ,
130 MAP_FILE|MAP_COPY|MAP_INHERIT, fd, 0);
131 if (__builtin_expect ((void *) filedata != MAP_FAILED, 1))
133 if (__builtin_expect (st.st_size < sizeof (*filedata), 0))
134 /* This cannot be a locale data file since it's too small. */
135 goto puntfd;
137 else
139 if (__builtin_expect (errno, ENOSYS) == ENOSYS)
141 #endif /* _POSIX_MAPPED_FILES */
142 /* No mmap; allocate a buffer and read from the file. */
143 mmaped = 0;
144 filedata = malloc (st.st_size);
145 if (filedata != NULL)
147 off_t to_read = st.st_size;
148 ssize_t nread;
149 char *p = (char *) filedata;
150 while (to_read > 0)
152 nread = __read (fd, p, to_read);
153 if (__builtin_expect (nread, 1) <= 0)
155 free (filedata);
156 if (nread == 0)
157 __set_errno (EINVAL); /* Bizarreness going on. */
158 goto puntfd;
160 p += nread;
161 to_read -= nread;
164 else
165 goto puntfd;
166 __set_errno (save_err);
167 #ifdef _POSIX_MAPPED_FILES
169 else
170 goto puntfd;
172 #endif /* _POSIX_MAPPED_FILES */
174 if (__builtin_expect (filedata->magic != LIMAGIC (category), 0))
175 /* Bad data file in either byte order. */
177 puntmap:
178 #ifdef _POSIX_MAPPED_FILES
179 __munmap ((caddr_t) filedata, st.st_size);
180 #endif
181 puntfd:
182 __close (fd);
183 return;
186 if (__builtin_expect (filedata->nstrings < _nl_category_num_items[category],
188 || (__builtin_expect (sizeof *filedata
189 + filedata->nstrings * sizeof (unsigned int)
190 >= (size_t) st.st_size, 0)))
192 /* Insufficient data. */
193 __set_errno (EINVAL);
194 goto puntmap;
197 newdata = malloc (sizeof *newdata
198 + filedata->nstrings * sizeof (union locale_data_value));
199 if (newdata == NULL)
200 goto puntmap;
202 newdata->name = NULL; /* This will be filled if necessary in findlocale.c. */
203 newdata->filedata = (void *) filedata;
204 newdata->filesize = st.st_size;
205 newdata->mmaped = mmaped;
206 newdata->usage_count = 0;
207 newdata->use_translit = 0;
208 newdata->options = NULL;
209 newdata->nstrings = filedata->nstrings;
210 for (cnt = 0; cnt < newdata->nstrings; ++cnt)
212 off_t idx = filedata->strindex[cnt];
213 if (__builtin_expect (idx > newdata->filesize, 0))
215 free (newdata);
216 __set_errno (EINVAL);
217 goto puntmap;
219 if (__builtin_expect (_nl_value_types[category][cnt] == word, 0))
221 assert (idx % __alignof__ (u_int32_t) == 0);
222 newdata->values[cnt].word =
223 *((u_int32_t *) (newdata->filedata + idx));
225 else
226 newdata->values[cnt].string = newdata->filedata + idx;
229 __close (fd);
230 file->data = newdata;
233 void
234 _nl_unload_locale (struct locale_data *locale)
236 #ifdef _POSIX_MAPPED_FILES
237 if (__builtin_expect (locale->mmaped, 1))
238 __munmap ((caddr_t) locale->filedata, locale->filesize);
239 else
240 #endif
241 free ((void *) locale->filedata);
243 free ((char *) locale->options);
244 free ((char *) locale->name);
245 free (locale);