1 /* Functions to read locale data files.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
28 #include "localeinfo.h"
30 const size_t _nl_category_num_items
[] =
32 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
33 [category] = _NL_ITEM_INDEX (_NL_NUM_##category),
34 #include "categories.def"
35 #undef DEFINE_CATEGORY
39 _nl_load_locale (int category
, char **name
)
46 unsigned int nstrings
;
47 unsigned int strindex
[0];
50 struct locale_data
*newdata
;
52 inline unsigned int SWAP (const unsigned int *inw
)
54 const unsigned char *inc
= (const unsigned char *) inw
;
57 return (inc
[3] << 24) | (inc
[2] << 16) | (inc
[1] << 8) | inc
[0];
61 if ((*name
)[0] == '\0')
63 *name
= getenv ("LC_ALL");
64 if (! *name
|| (*name
)[0] == '\0')
65 *name
= getenv (_nl_category_names
[category
]);
66 if (! *name
|| (*name
)[0] == '\0')
67 *name
= getenv ("LANG");
68 if (! *name
|| (*name
)[0] == '\0')
69 *name
= (char *) "local";
72 /* XXX can't use asprintf here */
73 if (asprintf (&file
, "%s%s/%s",
74 strchr (*name
, '/') != NULL
? "" : "/share/locale/", /* XXX */
75 *name
, _nl_category_names
[category
]) == -1)
78 fd
= __open (file
, O_RDONLY
);
82 if (__fstat (fd
, &st
) < 0)
86 /* Map in the file's data. */
89 /* Linux seems to lack read-only copy-on-write. */
90 #define MAP_COPY MAP_PRIVATE
92 filedata
= (void *) __mmap ((caddr_t
) 0, st
.st_size
,
93 PROT_READ
, MAP_FILE
|MAP_COPY
, fd
, 0);
94 if (filedata
== (void *) -1)
98 /* No mmap; allocate a buffer and read from the file. */
99 filedata
= malloc (st
.st_size
);
102 off_t to_read
= st
.st_size
;
104 char *p
= (char *) filedata
;
107 nread
= __read (fd
, p
, to_read
);
112 errno
= EINVAL
; /* Bizarreness going on. */
128 if (filedata
->magic
== LIMAGIC (category
))
129 /* Good data file in our byte order. */
133 /* Try the other byte order. */
135 if (SWAP (&filedata
->magic
) != LIMAGIC (category
))
136 /* Bad data file in either byte order. */
139 __munmap ((caddr_t
) filedata
, st
.st_size
);
146 #define W(word) SWAP (&(word))
148 if (W (filedata
->nstrings
) < _nl_category_num_items
[category
] ||
149 (sizeof *filedata
+ W (filedata
->nstrings
) * sizeof (unsigned int)
152 /* Insufficient data. */
157 newdata
= malloc (sizeof *newdata
+
158 W (filedata
->nstrings
) * sizeof (char *));
162 newdata
->filedata
= (void *) filedata
;
163 newdata
->filesize
= st
.st_size
;
164 newdata
->nstrings
= W (filedata
->nstrings
);
165 for (i
= 0; i
< newdata
->nstrings
; ++i
)
167 unsigned int idx
= W (filedata
->strindex
[i
]);
168 if (idx
>= newdata
->filesize
)
174 newdata
->strings
[i
] = newdata
->filedata
+ idx
;
182 _nl_free_locale (struct locale_data
*data
)
185 if (__munmap ((caddr_t
) data
->filedata
, data
->filesize
) < 0)
188 free ((void *) data
->filedata
);