* sysdeps/m68k/dl-machine.h (RTLD_START): Call pre-init funtions.
[glibc.git] / locale / loadlocale.c
blob24d0c3a67d3a689fdec672b99bbe80b17542b65b
1 /* Functions to read locale data files.
2 Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@gnu.org>, 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, b, c, d) \
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, b, c, d) \
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, b, c, d) \
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 swap = 0;
75 int mmaped = 1;
76 size_t cnt;
77 inline unsigned int SWAP (const unsigned int *inw)
79 const unsigned char *inc = (const unsigned char *) inw;
80 if (!swap)
81 return *inw;
82 return (inc[3] << 24) | (inc[2] << 16) | (inc[1] << 8) | inc[0];
85 file->decided = 1;
86 file->data = NULL;
88 fd = __open (file->filename, O_RDONLY);
89 if (fd < 0)
90 /* Cannot open the file. */
91 return;
93 if (__fstat (fd, &st) < 0)
94 goto puntfd;
95 if (S_ISDIR (st.st_mode))
97 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
98 instead. */
99 char *newp;
100 size_t filenamelen;
102 __close (fd);
104 filenamelen = strlen (file->filename);
105 newp = (char *) alloca (filenamelen
106 + 5 + _nl_category_name_sizes[category] + 1);
107 __mempcpy (__mempcpy (__mempcpy (newp, file->filename, filenamelen),
108 "/SYS_", 5),
109 _nl_category_names[category],
110 _nl_category_name_sizes[category] + 1);
112 fd = __open (newp, O_RDONLY);
113 if (fd < 0)
114 return;
116 if (__fstat (fd, &st) < 0)
117 goto puntfd;
120 /* Map in the file's data. */
121 save_err = errno;
122 #ifdef _POSIX_MAPPED_FILES
123 # ifndef MAP_COPY
124 /* Linux seems to lack read-only copy-on-write. */
125 # define MAP_COPY MAP_PRIVATE
126 # endif
127 # ifndef MAP_FILE
128 /* Some systems do not have this flag; it is superfluous. */
129 # define MAP_FILE 0
130 # endif
131 # ifndef MAP_INHERIT
132 /* Some systems might lack this; they lose. */
133 # define MAP_INHERIT 0
134 # endif
135 filedata = (void *) __mmap ((caddr_t) 0, st.st_size, PROT_READ,
136 MAP_FILE|MAP_COPY|MAP_INHERIT, fd, 0);
137 if ((void *) filedata != MAP_FAILED)
139 if (st.st_size < sizeof (*filedata))
140 /* This cannot be a locale data file since it's too small. */
141 goto puntfd;
143 else
145 if (errno == ENOSYS)
147 #endif /* _POSIX_MAPPED_FILES */
148 /* No mmap; allocate a buffer and read from the file. */
149 mmaped = 0;
150 filedata = malloc (st.st_size);
151 if (filedata != NULL)
153 off_t to_read = st.st_size;
154 ssize_t nread;
155 char *p = (char *) filedata;
156 while (to_read > 0)
158 nread = __read (fd, p, to_read);
159 if (nread <= 0)
161 free (filedata);
162 if (nread == 0)
163 __set_errno (EINVAL); /* Bizarreness going on. */
164 goto puntfd;
166 p += nread;
167 to_read -= nread;
170 else
171 goto puntfd;
172 __set_errno (save_err);
173 #ifdef _POSIX_MAPPED_FILES
175 else
176 goto puntfd;
178 #endif /* _POSIX_MAPPED_FILES */
180 if (filedata->magic == LIMAGIC (category))
181 /* Good data file in our byte order. */
182 swap = 0;
183 else
185 /* Try the other byte order. */
186 swap = 1;
187 if (SWAP (&filedata->magic) != LIMAGIC (category))
188 /* Bad data file in either byte order. */
190 puntmap:
191 #ifdef _POSIX_MAPPED_FILES
192 if (mmaped)
193 __munmap ((caddr_t) filedata, st.st_size);
194 else
195 #endif /* _POSIX_MAPPED_FILES */
196 free (filedata);
197 puntfd:
198 __close (fd);
199 return;
203 #define W(word) SWAP (&(word))
205 if (W (filedata->nstrings) < _nl_category_num_items[category] ||
206 (sizeof *filedata + W (filedata->nstrings) * sizeof (unsigned int)
207 >= (size_t) st.st_size))
209 /* Insufficient data. */
210 __set_errno (EINVAL);
211 goto puntmap;
214 newdata = malloc (sizeof *newdata +
215 (_nl_category_num_items[category]
216 * sizeof (union locale_data_value)));
217 if (! newdata)
218 goto puntmap;
220 newdata->name = NULL; /* This will be filled if necessary in findlocale.c. */
221 newdata->filedata = (void *) filedata;
222 newdata->filesize = st.st_size;
223 newdata->mmaped = mmaped;
224 newdata->usage_count = 0;
225 newdata->nstrings = _nl_category_num_items[category];
226 for (cnt = 0; cnt < newdata->nstrings; ++cnt)
228 off_t idx = W (filedata->strindex[cnt]);
229 if (idx >= newdata->filesize)
231 free (newdata);
232 __set_errno (EINVAL);
233 goto puntmap;
235 if (_nl_value_types[category][cnt] == word)
236 newdata->values[cnt].word = W (*((u_int32_t *) (newdata->filedata
237 + idx)));
238 else
239 newdata->values[cnt].string = newdata->filedata + idx;
242 __close (fd);
243 file->data = newdata;
246 void
247 _nl_unload_locale (struct locale_data *locale)
249 if (locale->name != NULL)
250 free ((void *) locale->name);
252 #ifdef _POSIX_MAPPED_FILES
253 if (locale->mmaped)
254 __munmap ((caddr_t) locale->filedata, locale->filesize);
255 else
256 #endif
257 free ((void *) locale->filedata);
259 free (locale);