Update
[glibc.git] / locale / loadlocale.c
blobcc454abae42e619d0c9ac8e497c24ac1c0437dc0
1 /* Functions to read locale data files.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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 <errno.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #ifdef _POSIX_MAPPED_FILES
27 # include <sys/mman.h>
28 #endif
29 #include <sys/stat.h>
31 #include "localeinfo.h"
34 static const size_t _nl_category_num_items[] =
36 #define DEFINE_CATEGORY(category, category_name, items, a) \
37 [category] = _NL_ITEM_INDEX (_NL_NUM_##category),
38 #include "categories.def"
39 #undef DEFINE_CATEGORY
43 #define NO_PAREN(arg, rest...) arg, ##rest
45 #define DEFINE_CATEGORY(category, category_name, items, a) \
46 static const enum value_type _nl_value_type_##category[] = { NO_PAREN items };
47 #define DEFINE_ELEMENT(element, element_name, optstd, type, rest...) \
48 [_NL_ITEM_INDEX (element)] = type,
49 #include "categories.def"
50 #undef DEFINE_CATEGORY
52 static const enum value_type *_nl_value_types[] =
54 #define DEFINE_CATEGORY(category, category_name, items, a) \
55 [category] = _nl_value_type_##category,
56 #include "categories.def"
57 #undef DEFINE_CATEGORY
61 void
62 _nl_load_locale (struct loaded_l10nfile *file, int category)
64 int fd;
65 struct
67 unsigned int magic;
68 unsigned int nstrings;
69 unsigned int strindex[0];
70 } *filedata;
71 struct stat st;
72 struct locale_data *newdata;
73 int save_err;
74 int mmaped = 1;
75 size_t cnt;
77 file->decided = 1;
78 file->data = NULL;
80 fd = __open (file->filename, O_RDONLY);
81 if (__builtin_expect (fd, 0) < 0)
82 /* Cannot open the file. */
83 return;
85 if (__builtin_expect (__fstat (fd, &st), 0) < 0)
86 goto puntfd;
87 if (__builtin_expect (S_ISDIR (st.st_mode), 0))
89 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
90 instead. */
91 char *newp;
92 size_t filenamelen;
94 __close (fd);
96 filenamelen = strlen (file->filename);
97 newp = (char *) alloca (filenamelen
98 + 5 + _nl_category_name_sizes[category] + 1);
99 __mempcpy (__mempcpy (__mempcpy (newp, file->filename, filenamelen),
100 "/SYS_", 5),
101 _nl_category_names[category],
102 _nl_category_name_sizes[category] + 1);
104 fd = __open (newp, O_RDONLY);
105 if (__builtin_expect (fd, 0) < 0)
106 return;
108 if (__builtin_expect (__fstat (fd, &st), 0) < 0)
109 goto puntfd;
112 /* Map in the file's data. */
113 save_err = errno;
114 #ifdef _POSIX_MAPPED_FILES
115 # ifndef MAP_COPY
116 /* Linux seems to lack read-only copy-on-write. */
117 # define MAP_COPY MAP_PRIVATE
118 # endif
119 # ifndef MAP_FILE
120 /* Some systems do not have this flag; it is superfluous. */
121 # define MAP_FILE 0
122 # endif
123 # ifndef MAP_INHERIT
124 /* Some systems might lack this; they lose. */
125 # define MAP_INHERIT 0
126 # endif
127 filedata = (void *) __mmap ((caddr_t) 0, st.st_size, PROT_READ,
128 MAP_FILE|MAP_COPY|MAP_INHERIT, fd, 0);
129 if (__builtin_expect ((void *) filedata != MAP_FAILED, 1))
131 if (__builtin_expect (st.st_size < sizeof (*filedata), 0))
132 /* This cannot be a locale data file since it's too small. */
133 goto puntfd;
135 else
137 if (__builtin_expect (errno, ENOSYS) == ENOSYS)
139 #endif /* _POSIX_MAPPED_FILES */
140 /* No mmap; allocate a buffer and read from the file. */
141 mmaped = 0;
142 filedata = malloc (st.st_size);
143 if (filedata != NULL)
145 off_t to_read = st.st_size;
146 ssize_t nread;
147 char *p = (char *) filedata;
148 while (to_read > 0)
150 nread = __read (fd, p, to_read);
151 if (__builtin_expect (nread, 1) <= 0)
153 free (filedata);
154 if (nread == 0)
155 __set_errno (EINVAL); /* Bizarreness going on. */
156 goto puntfd;
158 p += nread;
159 to_read -= nread;
162 else
163 goto puntfd;
164 __set_errno (save_err);
165 #ifdef _POSIX_MAPPED_FILES
167 else
168 goto puntfd;
170 #endif /* _POSIX_MAPPED_FILES */
172 if (__builtin_expect (filedata->magic != LIMAGIC (category), 0))
173 /* Bad data file in either byte order. */
175 puntmap:
176 #ifdef _POSIX_MAPPED_FILES
177 __munmap ((caddr_t) filedata, st.st_size);
178 #endif
179 puntfd:
180 __close (fd);
181 return;
184 if (__builtin_expect (filedata->nstrings < _nl_category_num_items[category],
186 || (__builtin_expect (sizeof *filedata
187 + filedata->nstrings * sizeof (unsigned int)
188 >= (size_t) st.st_size, 0)))
190 /* Insufficient data. */
191 __set_errno (EINVAL);
192 goto puntmap;
195 newdata = malloc (sizeof *newdata
196 + filedata->nstrings * sizeof (union locale_data_value));
197 if (newdata == NULL)
198 goto puntmap;
200 newdata->name = NULL; /* This will be filled if necessary in findlocale.c. */
201 newdata->filedata = (void *) filedata;
202 newdata->filesize = st.st_size;
203 newdata->mmaped = mmaped;
204 newdata->usage_count = 0;
205 newdata->nstrings = filedata->nstrings;
206 for (cnt = 0; cnt < newdata->nstrings; ++cnt)
208 off_t idx = filedata->strindex[cnt];
209 if (__builtin_expect (idx > newdata->filesize, 0))
211 free (newdata);
212 __set_errno (EINVAL);
213 goto puntmap;
215 if (__builtin_expect (_nl_value_types[category][cnt] == word, 0))
216 newdata->values[cnt].word = *((u_int32_t *) (newdata->filedata + idx));
217 else
218 newdata->values[cnt].string = newdata->filedata + idx;
221 __close (fd);
222 file->data = newdata;
225 void
226 _nl_unload_locale (struct locale_data *locale)
228 #ifdef _POSIX_MAPPED_FILES
229 if (__builtin_expect (locale->mmaped, 1))
230 __munmap ((caddr_t) locale->filedata, locale->filesize);
231 else
232 #endif
233 free ((void *) locale->filedata);
235 free (locale);