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>
57 #include "un-namespace.h"
59 #include "../locale/xlocale_private.h"
61 #define _DEFAULT_NLS_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L:/usr/local/share/nls/%L/%N.cat:/usr/local/share/nls/%N/%L"
63 #define RLOCK(fail) { int ret; \
65 ((ret = _pthread_rwlock_rdlock(&rwlock)) != 0)) { \
69 #define WLOCK(fail) { int ret; \
71 ((ret = _pthread_rwlock_wrlock(&rwlock)) != 0)) { \
75 #define UNLOCK { if (__isthreaded) \
76 _pthread_rwlock_unlock(&rwlock); }
78 #define NLERR ((nl_catd) -1)
79 #define NLRETERR(errc) { errno = errc; return (NLERR); }
80 #define SAVEFAIL(n, l, e) { np = calloc(1, sizeof(struct catentry)); \
82 np->name = strdup(n); \
84 np->lang = (l == NULL) ? NULL : \
87 if (np->name == NULL || \
88 (l != NULL && np->lang == NULL)) { \
94 SLIST_INSERT_HEAD(&cache, np, \
102 static nl_catd load_msgcat(const char *, const char *, const char *);
104 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
107 SLIST_ENTRY(catentry) list;
116 SLIST_HEAD(listhead, catentry) cache =
117 SLIST_HEAD_INITIALIZER(cache);
120 catopen(const char *name, int type)
124 char *base, *cptr, *cptr1, *nlspath, *pathP, *pcode;
127 const char *lang, *tmpptr;
130 /* sanity checking */
131 if (name == NULL || *name == '\0')
134 if (strchr(name, '/') != NULL)
135 /* have a pathname */
138 if (type == NL_CAT_LOCALE)
139 lang = querylocale(LC_MESSAGES_MASK, __get_locale());
141 lang = getenv("LANG");
143 if (lang == NULL || *lang == '\0' || strlen(lang) > ENCODING_LEN ||
145 (lang[1] == '\0' || (lang[1] == '.' && lang[2] == '\0'))) ||
146 strchr(lang, '/') != NULL)
150 /* Try to get it from the cache first */
152 SLIST_FOREACH(np, &cache, list) {
153 if ((strcmp(np->name, name) == 0) &&
154 ((lang != NULL && np->lang != NULL &&
155 strcmp(np->lang, lang) == 0) || (np->lang == lang))) {
156 if (np->caterrno != 0) {
157 /* Found cached failing entry */
159 NLRETERR(np->caterrno);
161 /* Found cached successful entry */
162 atomic_add_int(&np->refcount, 1);
170 /* is it absolute path ? if yes, load immediately */
171 if (strchr(name, '/') != NULL)
172 return (load_msgcat(name, name, lang));
174 /* sanity checking */
175 if ((plang = cptr1 = strdup(lang)) == NULL)
177 if ((cptr = strchr(cptr1, '@')) != NULL)
180 if ((cptr = strchr(cptr1, '_')) != NULL) {
184 if ((cptr = strchr(cptr1, '.')) != NULL) {
189 if ((nlspath = getenv("NLSPATH")) == NULL || issetugid())
190 nlspath = _DEFAULT_NLS_PATH;
192 if ((base = cptr = strdup(nlspath)) == NULL) {
199 while ((nlspath = strsep(&cptr, ":")) != NULL) {
202 for (; *nlspath; ++nlspath) {
203 if (*nlspath == '%') {
204 switch (*(nlspath + 1)) {
218 tmpptr = (char *)name;
227 *(pathP++) = *nlspath;
232 spcleft = sizeof(path) -
234 if (strlcpy(pathP, tmpptr, spcleft) >=
239 SAVEFAIL(name, lang, ENAMETOOLONG);
240 NLRETERR(ENAMETOOLONG);
242 pathP += strlen(tmpptr);
244 if (pathP - path >= sizeof(path) - 1)
246 *(pathP++) = *nlspath;
250 if (stat(path, &sbuf) == 0) {
253 return (load_msgcat(path, name, lang));
256 tmpptr = (char *)name;
263 SAVEFAIL(name, lang, ENOENT);
268 catgets(nl_catd catd, int set_id, int msg_id, const char *s)
270 struct _nls_cat_hdr *cat_hdr;
271 struct _nls_msg_hdr *msg_hdr;
272 struct _nls_set_hdr *set_hdr;
275 if (catd == NULL || catd == NLERR) {
277 /* LINTED interface problem */
281 cat_hdr = (struct _nls_cat_hdr *)catd->__data;
282 set_hdr = (struct _nls_set_hdr *)(void *)((char *)catd->__data +
283 sizeof(struct _nls_cat_hdr));
285 /* binary search, see knuth algorithm b */
287 u = ntohl((u_int32_t)cat_hdr->__nsets) - 1;
290 r = set_id - ntohl((u_int32_t)set_hdr[i].__setno);
293 msg_hdr = (struct _nls_msg_hdr *)
294 (void *)((char *)catd->__data +
295 sizeof(struct _nls_cat_hdr) +
296 ntohl((u_int32_t)cat_hdr->__msg_hdr_offset));
298 l = ntohl((u_int32_t)set_hdr[i].__index);
299 u = l + ntohl((u_int32_t)set_hdr[i].__nmsgs) - 1;
303 ntohl((u_int32_t)msg_hdr[i].__msgno);
305 return ((char *) catd->__data +
306 sizeof(struct _nls_cat_hdr) +
308 cat_hdr->__msg_txt_offset) +
310 msg_hdr[i].__offset));
331 /* LINTED interface problem */
336 catfree(struct catentry *np)
339 if (np->catd != NULL && np->catd != NLERR) {
340 munmap(np->catd->__data, (size_t)np->catd->__size);
343 SLIST_REMOVE(&cache, np, catentry, list);
351 catclose(nl_catd catd)
355 /* sanity checking */
356 if (catd == NULL || catd == NLERR) {
361 /* Remove from cache if not referenced any more */
363 SLIST_FOREACH(np, &cache, list) {
364 if (catd == np->catd) {
365 if (atomic_fetchadd_int(&np->refcount, -1) == 1)
375 * Internal support functions
379 load_msgcat(const char *path, const char *name, const char *lang)
385 char *copy_path, *copy_name, *copy_lang;
388 /* path/name will never be NULL here */
391 * One more try in cache; if it was not found by name,
392 * it might still be found by absolute path.
395 SLIST_FOREACH(np, &cache, list) {
396 if ((np->path != NULL) && (strcmp(np->path, path) == 0)) {
397 atomic_add_int(&np->refcount, 1);
404 if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1) {
405 SAVEFAIL(name, lang, errno);
409 if (_fstat(fd, &st) != 0) {
411 SAVEFAIL(name, lang, EFTYPE);
416 * If the file size cannot be held in size_t we cannot mmap()
417 * it to the memory. Probably, this will not be a problem given
418 * that catalog files are usually small.
420 if (st.st_size > SIZE_T_MAX) {
422 SAVEFAIL(name, lang, EFBIG);
426 if ((data = mmap(0, (size_t)st.st_size, PROT_READ,
427 MAP_FILE|MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) {
428 int saved_errno = errno;
430 SAVEFAIL(name, lang, saved_errno);
431 NLRETERR(saved_errno);
435 if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
437 munmap(data, (size_t)st.st_size);
438 SAVEFAIL(name, lang, EFTYPE);
442 copy_name = strdup(name);
443 copy_path = strdup(path);
444 copy_lang = (lang == NULL) ? NULL : strdup(lang);
445 catd = malloc(sizeof (*catd));
446 np = calloc(1, sizeof(struct catentry));
448 if (copy_name == NULL || copy_path == NULL ||
449 (lang != NULL && copy_lang == NULL) ||
450 catd == NULL || np == NULL) {
456 munmap(data, (size_t)st.st_size);
457 SAVEFAIL(name, lang, ENOMEM);
462 catd->__size = (int)st.st_size;
464 /* Caching opened catalog */
465 np->name = copy_name;
466 np->path = copy_path;
468 np->lang = copy_lang;
469 atomic_store_int(&np->refcount, 1);
471 SLIST_INSERT_HEAD(&cache, np, list);