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. */
26 #ifdef _POSIX_MAPPED_FILES
27 # include <sys/mman.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
62 _nl_load_locale (struct loaded_l10nfile
*file
, int category
)
68 unsigned int nstrings
;
69 unsigned int strindex
[0];
72 struct locale_data
*newdata
;
80 fd
= __open (file
->filename
, O_RDONLY
);
81 if (__builtin_expect (fd
, 0) < 0)
82 /* Cannot open the file. */
85 if (__builtin_expect (__fstat (fd
, &st
), 0) < 0)
87 if (__builtin_expect (S_ISDIR (st
.st_mode
), 0))
89 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
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
),
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)
108 if (__builtin_expect (__fstat (fd
, &st
), 0) < 0)
112 /* Map in the file's data. */
114 #ifdef _POSIX_MAPPED_FILES
116 /* Linux seems to lack read-only copy-on-write. */
117 # define MAP_COPY MAP_PRIVATE
120 /* Some systems do not have this flag; it is superfluous. */
124 /* Some systems might lack this; they lose. */
125 # define MAP_INHERIT 0
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. */
137 if (__builtin_expect (errno
, ENOSYS
) == ENOSYS
)
139 #endif /* _POSIX_MAPPED_FILES */
140 /* No mmap; allocate a buffer and read from the file. */
142 filedata
= malloc (st
.st_size
);
143 if (filedata
!= NULL
)
145 off_t to_read
= st
.st_size
;
147 char *p
= (char *) filedata
;
150 nread
= __read (fd
, p
, to_read
);
151 if (__builtin_expect (nread
, 1) <= 0)
155 __set_errno (EINVAL
); /* Bizarreness going on. */
164 __set_errno (save_err
);
165 #ifdef _POSIX_MAPPED_FILES
170 #endif /* _POSIX_MAPPED_FILES */
172 if (__builtin_expect (filedata
->magic
!= LIMAGIC (category
), 0))
173 /* Bad data file in either byte order. */
176 #ifdef _POSIX_MAPPED_FILES
177 __munmap ((caddr_t
) filedata
, st
.st_size
);
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
);
195 newdata
= malloc (sizeof *newdata
196 + filedata
->nstrings
* sizeof (union locale_data_value
));
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))
212 __set_errno (EINVAL
);
215 if (__builtin_expect (_nl_value_types
[category
][cnt
] == word
, 0))
216 newdata
->values
[cnt
].word
= *((u_int32_t
*) (newdata
->filedata
+ idx
));
218 newdata
->values
[cnt
].string
= newdata
->filedata
+ idx
;
222 file
->data
= newdata
;
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
);
233 free ((void *) locale
->filedata
);