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. */
28 #ifdef _POSIX_MAPPED_FILES
29 # include <sys/mman.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
64 _nl_load_locale (struct loaded_l10nfile
*file
, int category
)
70 unsigned int nstrings
;
71 unsigned int strindex
[0];
74 struct locale_data
*newdata
;
82 fd
= __open (file
->filename
, O_RDONLY
);
83 if (__builtin_expect (fd
, 0) < 0)
84 /* Cannot open the file. */
87 if (__builtin_expect (__fxstat64 (_STAT_VER
, fd
, &st
), 0) < 0)
89 if (__builtin_expect (S_ISDIR (st
.st_mode
), 0))
91 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
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
),
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)
110 if (__builtin_expect (__fxstat64 (_STAT_VER
, fd
, &st
), 0) < 0)
114 /* Map in the file's data. */
116 #ifdef _POSIX_MAPPED_FILES
118 /* Linux seems to lack read-only copy-on-write. */
119 # define MAP_COPY MAP_PRIVATE
122 /* Some systems do not have this flag; it is superfluous. */
126 /* Some systems might lack this; they lose. */
127 # define MAP_INHERIT 0
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. */
139 if (__builtin_expect (errno
, ENOSYS
) == ENOSYS
)
141 #endif /* _POSIX_MAPPED_FILES */
142 /* No mmap; allocate a buffer and read from the file. */
144 filedata
= malloc (st
.st_size
);
145 if (filedata
!= NULL
)
147 off_t to_read
= st
.st_size
;
149 char *p
= (char *) filedata
;
152 nread
= __read (fd
, p
, to_read
);
153 if (__builtin_expect (nread
, 1) <= 0)
157 __set_errno (EINVAL
); /* Bizarreness going on. */
166 __set_errno (save_err
);
167 #ifdef _POSIX_MAPPED_FILES
172 #endif /* _POSIX_MAPPED_FILES */
174 if (__builtin_expect (filedata
->magic
!= LIMAGIC (category
), 0))
175 /* Bad data file in either byte order. */
178 #ifdef _POSIX_MAPPED_FILES
179 __munmap ((caddr_t
) filedata
, st
.st_size
);
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
);
197 newdata
= malloc (sizeof *newdata
198 + filedata
->nstrings
* sizeof (union locale_data_value
));
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))
216 __set_errno (EINVAL
);
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
));
226 newdata
->values
[cnt
].string
= newdata
->filedata
+ idx
;
230 file
->data
= newdata
;
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
);
241 free ((void *) locale
->filedata
);
243 free ((char *) locale
->options
);
244 free ((char *) locale
->name
);