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
)
45 unsigned int nstrings
;
46 unsigned int strindex
[0];
49 struct locale_data
*newdata
;
51 inline unsigned int SWAP (const unsigned int *inw
)
53 const unsigned char *inc
= (const unsigned char *) inw
;
56 return (inc
[3] << 24) | (inc
[2] << 16) | (inc
[1] << 8) | inc
[0];
60 if ((*name
)[0] == '\0')
62 *name
= getenv ("LC_ALL");
63 if (! *name
|| (*name
)[0] == '\0')
64 *name
= getenv (_nl_category_names
[category
]);
65 if (! *name
|| (*name
)[0] == '\0')
66 *name
= getenv ("LANG");
67 if (! *name
|| (*name
)[0] == '\0')
68 *name
= (char *) "local";
72 const char *catname
= _nl_category_names
[category
];
73 size_t namelen
= strlen (*name
);
74 size_t catlen
= strlen (catname
);
75 char file
[sizeof LOCALE_PATH
+ 1 + namelen
+ catlen
* 2 + 4];
76 if (strchr (*name
, '/') != NULL
)
77 sprintf (file
, "%s/%s", *name
, catname
);
79 sprintf (file
, "%s/%s/%s", LOCALE_PATH
, *name
, catname
);
80 fd
= __open (file
, O_RDONLY
);
83 if (__fstat (fd
, &st
) < 0)
85 if (S_ISDIR (st
.st_mode
))
87 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
90 memcpy (stpcpy (strchr (file
, '\0'), "SYS_"), catname
, catlen
);
91 fd
= __open (file
, O_RDONLY
);
94 if (__fstat (fd
, &st
) < 0)
100 /* Map in the file's data. */
103 /* Linux seems to lack read-only copy-on-write. */
104 #define MAP_COPY MAP_PRIVATE
107 /* Some systems do not have this flag; it is superfluous. */
111 /* Some systems might lack this; they lose. */
112 #define MAP_INHERIT 0
114 filedata
= (void *) __mmap ((caddr_t
) 0, st
.st_size
,
115 PROT_READ
, MAP_FILE
|MAP_COPY
|MAP_INHERIT
,
117 if (filedata
== (void *) -1)
121 /* No mmap; allocate a buffer and read from the file. */
122 filedata
= malloc (st
.st_size
);
125 off_t to_read
= st
.st_size
;
127 char *p
= (char *) filedata
;
130 nread
= __read (fd
, p
, to_read
);
135 errno
= EINVAL
; /* Bizarreness going on. */
151 if (filedata
->magic
== LIMAGIC (category
))
152 /* Good data file in our byte order. */
156 /* Try the other byte order. */
158 if (SWAP (&filedata
->magic
) != LIMAGIC (category
))
159 /* Bad data file in either byte order. */
162 __munmap ((caddr_t
) filedata
, st
.st_size
);
169 #define W(word) SWAP (&(word))
171 if (W (filedata
->nstrings
) < _nl_category_num_items
[category
] ||
172 (sizeof *filedata
+ W (filedata
->nstrings
) * sizeof (unsigned int)
175 /* Insufficient data. */
180 newdata
= malloc (sizeof *newdata
+
181 W (filedata
->nstrings
) * sizeof (char *));
185 newdata
->filedata
= (void *) filedata
;
186 newdata
->filesize
= st
.st_size
;
187 newdata
->nstrings
= W (filedata
->nstrings
);
188 for (i
= 0; i
< newdata
->nstrings
; ++i
)
190 unsigned int idx
= W (filedata
->strindex
[i
]);
191 if (idx
>= newdata
->filesize
)
197 newdata
->strings
[i
] = newdata
->filedata
+ idx
;
205 _nl_free_locale (struct locale_data
*data
)
209 /* Ignore a null pointer, like free does. */
211 if (__munmap ((caddr_t
) data
->filedata
, data
->filesize
) < 0)
214 free ((void *) data
->filedata
);