1 /***********************************************************
2 Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts.
3 Copyright 2010, Gabor Kovesdan <gabor@FreeBSD.org>
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that Alfalfa's name not be used in
12 advertising or publicity pertaining to distribution of the software
13 without specific, written prior permission.
15 ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
16 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
17 ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
18 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
19 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
20 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 If you make any modifications, bugfixes or other changes to this software
24 we'd appreciate it if you could send a copy to us so we can keep things
25 up-to-date. Many thanks.
27 Alfalfa Software, Inc.
29 Cambridge, MA 02139 USA
32 ******************************************************************/
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
39 #include "namespace.h"
40 #include <sys/types.h>
43 #include <sys/queue.h>
45 #include <arpa/inet.h> /* for ntohl() */
46 #include <machine/atomic.h>
58 #include "un-namespace.h"
60 #include "../locale/xlocale_private.h"
61 #include "libc_private.h"
63 #define _DEFAULT_NLS_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L:" \
64 _PATH_LOCALBASE "/share/nls/%L/%N.cat:" \
65 _PATH_LOCALBASE "/share/nls/%N/%L"
67 #define RLOCK(fail) { int ret; \
69 ((ret = _pthread_rwlock_rdlock(&rwlock)) != 0)) { \
73 #define WLOCK(fail) { int ret; \
75 ((ret = _pthread_rwlock_wrlock(&rwlock)) != 0)) { \
79 #define UNLOCK { if (__isthreaded) \
80 _pthread_rwlock_unlock(&rwlock); }
82 #define NLERR ((nl_catd) -1)
83 #define NLRETERR(errc) { errno = errc; return (NLERR); }
84 #define SAVEFAIL(n, l, e) { np = calloc(1, sizeof(struct catentry)); \
86 np->name = strdup(n); \
88 np->lang = (l == NULL) ? NULL : \
91 if (np->name == NULL || \
92 (l != NULL && np->lang == NULL)) { \
98 SLIST_INSERT_HEAD(&cache, np, \
106 static nl_catd load_msgcat(const char *, const char *, const char *);
108 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
111 SLIST_ENTRY(catentry) list;
120 SLIST_HEAD(listhead, catentry) cache =
121 SLIST_HEAD_INITIALIZER(cache);
124 catopen(const char *name, int type)
126 return (__catopen_l(name, type, __get_locale()));
130 __catopen_l(const char *name, int type, locale_t locale)
134 char *base, *cptr, *cptr1, *nlspath, *pathP, *pcode;
137 const char *lang, *tmpptr;
140 /* sanity checking */
141 if (name == NULL || *name == '\0')
144 if (strchr(name, '/') != NULL)
145 /* have a pathname */
148 if (type == NL_CAT_LOCALE)
149 lang = querylocale(LC_MESSAGES_MASK, locale);
151 lang = getenv("LANG");
153 if (lang == NULL || *lang == '\0' || strlen(lang) > ENCODING_LEN ||
155 (lang[1] == '\0' || (lang[1] == '.' && lang[2] == '\0'))) ||
156 strchr(lang, '/') != NULL)
160 /* Try to get it from the cache first */
162 SLIST_FOREACH(np, &cache, list) {
163 if ((strcmp(np->name, name) == 0) &&
164 ((lang != NULL && np->lang != NULL &&
165 strcmp(np->lang, lang) == 0) || (np->lang == lang))) {
166 if (np->caterrno != 0) {
167 /* Found cached failing entry */
169 NLRETERR(np->caterrno);
171 /* Found cached successful entry */
172 atomic_add_int(&np->refcount, 1);
180 /* is it absolute path ? if yes, load immediately */
181 if (strchr(name, '/') != NULL)
182 return (load_msgcat(name, name, lang));
184 /* sanity checking */
185 if ((plang = cptr1 = strdup(lang)) == NULL)
187 if ((cptr = strchr(cptr1, '@')) != NULL)
190 if ((cptr = strchr(cptr1, '_')) != NULL) {
194 if ((cptr = strchr(cptr1, '.')) != NULL) {
199 if ((nlspath = getenv("NLSPATH")) == NULL || issetugid())
200 nlspath = _DEFAULT_NLS_PATH;
202 if ((base = cptr = strdup(nlspath)) == NULL) {
209 while ((nlspath = strsep(&cptr, ":")) != NULL) {
212 for (; *nlspath; ++nlspath) {
213 if (*nlspath == '%') {
214 switch (*(nlspath + 1)) {
228 tmpptr = (char *)name;
237 *(pathP++) = *nlspath;
242 spcleft = sizeof(path) -
244 if (strlcpy(pathP, tmpptr, spcleft) >=
249 SAVEFAIL(name, lang, ENAMETOOLONG);
250 NLRETERR(ENAMETOOLONG);
252 pathP += strlen(tmpptr);
254 if (pathP - path >= sizeof(path) - 1)
256 *(pathP++) = *nlspath;
260 if (stat(path, &sbuf) == 0) {
263 return (load_msgcat(path, name, lang));
266 tmpptr = (char *)name;
273 SAVEFAIL(name, lang, ENOENT);
278 catgets(nl_catd catd, int set_id, int msg_id, const char *s)
280 struct _nls_cat_hdr *cat_hdr;
281 struct _nls_msg_hdr *msg_hdr;
282 struct _nls_set_hdr *set_hdr;
285 if (catd == NULL || catd == NLERR) {
287 /* LINTED interface problem */
291 cat_hdr = (struct _nls_cat_hdr *)catd->__data;
292 set_hdr = (struct _nls_set_hdr *)(void *)((char *)catd->__data +
293 sizeof(struct _nls_cat_hdr));
295 /* binary search, see knuth algorithm b */
297 u = ntohl((u_int32_t)cat_hdr->__nsets) - 1;
300 r = set_id - ntohl((u_int32_t)set_hdr[i].__setno);
303 msg_hdr = (struct _nls_msg_hdr *)
304 (void *)((char *)catd->__data +
305 sizeof(struct _nls_cat_hdr) +
306 ntohl((u_int32_t)cat_hdr->__msg_hdr_offset));
308 l = ntohl((u_int32_t)set_hdr[i].__index);
309 u = l + ntohl((u_int32_t)set_hdr[i].__nmsgs) - 1;
313 ntohl((u_int32_t)msg_hdr[i].__msgno);
315 return ((char *) catd->__data +
316 sizeof(struct _nls_cat_hdr) +
318 cat_hdr->__msg_txt_offset) +
320 msg_hdr[i].__offset));
341 /* LINTED interface problem */
346 catfree(struct catentry *np)
349 if (np->catd != NULL && np->catd != NLERR) {
350 munmap(np->catd->__data, (size_t)np->catd->__size);
353 SLIST_REMOVE(&cache, np, catentry, list);
361 catclose(nl_catd catd)
365 /* sanity checking */
366 if (catd == NULL || catd == NLERR) {
371 /* Remove from cache if not referenced any more */
373 SLIST_FOREACH(np, &cache, list) {
374 if (catd == np->catd) {
375 if (atomic_fetchadd_int(&np->refcount, -1) == 1)
385 * Internal support functions
389 load_msgcat(const char *path, const char *name, const char *lang)
395 char *copy_path, *copy_name, *copy_lang;
398 /* path/name will never be NULL here */
401 * One more try in cache; if it was not found by name,
402 * it might still be found by absolute path.
405 SLIST_FOREACH(np, &cache, list) {
406 if ((np->path != NULL) && (strcmp(np->path, path) == 0)) {
407 atomic_add_int(&np->refcount, 1);
414 if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1) {
415 SAVEFAIL(name, lang, errno);
419 if (_fstat(fd, &st) != 0) {
421 SAVEFAIL(name, lang, EFTYPE);
426 * If the file size cannot be held in size_t we cannot mmap()
427 * it to the memory. Probably, this will not be a problem given
428 * that catalog files are usually small.
430 if (st.st_size > SIZE_T_MAX) {
432 SAVEFAIL(name, lang, EFBIG);
436 if ((data = mmap(0, (size_t)st.st_size, PROT_READ,
437 MAP_FILE|MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) {
438 int saved_errno = errno;
440 SAVEFAIL(name, lang, saved_errno);
441 NLRETERR(saved_errno);
445 if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
447 munmap(data, (size_t)st.st_size);
448 SAVEFAIL(name, lang, EFTYPE);
452 copy_name = strdup(name);
453 copy_path = strdup(path);
454 copy_lang = (lang == NULL) ? NULL : strdup(lang);
455 catd = malloc(sizeof (*catd));
456 np = calloc(1, sizeof(struct catentry));
458 if (copy_name == NULL || copy_path == NULL ||
459 (lang != NULL && copy_lang == NULL) ||
460 catd == NULL || np == NULL) {
466 munmap(data, (size_t)st.st_size);
467 SAVEFAIL(name, lang, ENOMEM);
472 catd->__size = (int)st.st_size;
474 /* Caching opened catalog */
475 np->name = copy_name;
476 np->path = copy_path;
478 np->lang = copy_lang;
479 atomic_store_int(&np->refcount, 1);
481 SLIST_INSERT_HEAD(&cache, np, list);