__setlocalecat.c 2.8 KB
Newer Older
R
Rich Felker 已提交
1 2 3 4 5 6
#include <locale.h>
#include <string.h>
#include "locale_impl.h"
#include "libc.h"
#include "atomic.h"

7 8 9 10 11 12 13
const char *__lctrans_impl(const char *msg, const struct __locale_map *lm)
{
	const char *trans = 0;
	if (lm) trans = __mo_lookup(lm->map, lm->map_size, msg);
	return trans ? trans : msg;
}

14 15 16 17
const unsigned char *__map_file(const char *, size_t *);
int __munmap(void *, size_t);
char *__strchrnul(const char *, int);

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
static const char envvars[][12] = {
	"LC_CTYPE",
	"LC_NUMERIC",
	"LC_TIME",
	"LC_COLLATE",
	"LC_MONETARY",
	"LC_MESSAGES",
};

static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };

static const struct __locale_map c_dot_utf8 = {
	.map = empty_mo,
	.map_size = sizeof empty_mo,
	.name = "C.UTF-8"
};

const struct __locale_map *__get_locale(int cat, const char *val)
36
{
37
	static int lock[2];
38
	static void *volatile loc_head;
39 40
	const struct __locale_map *p;
	struct __locale_map *new = 0;
41 42
	const char *path = 0, *z;
	char buf[256];
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	size_t l, n;

	if (!*val) {
		(val = getenv("LC_ALL")) && *val ||
		(val = getenv(envvars[cat])) && *val ||
		(val = getenv("LANG")) && *val ||
		(val = "C.UTF-8");
	}

	/* Limit name length and forbid leading dot or any slashes. */
	for (n=0; n<LOCALE_NAME_MAX && val[n] && val[n]!='/'; n++);
	if (val[0]=='.' || val[n]) val = "C.UTF-8";
	int builtin = (val[0]=='C' && !val[1])
		|| !strcmp(val, "C.UTF-8")
		|| !strcmp(val, "POSIX");

	if (builtin) {
		if (cat == LC_CTYPE && val[1]=='.')
			return (void *)&c_dot_utf8;
		return 0;
	}
64 65

	for (p=loc_head; p; p=p->next)
66
		if (!strcmp(val, p->name)) return p;
67

68 69 70
	LOCK(lock);

	for (p=loc_head; p; p=p->next)
71
		if (!strcmp(val, p->name)) {
72 73 74 75
			UNLOCK(lock);
			return p;
		}

76 77 78
	if (!libc.secure) path = getenv("MUSL_LOCPATH");
	/* FIXME: add a default path? */

79
	if (path) for (; *path; path=z+!!*z) {
80 81 82 83 84
		z = __strchrnul(path, ':');
		l = z - path - !!*z;
		if (l >= sizeof buf - n - 2) continue;
		memcpy(buf, path, l);
		buf[l] = '/';
85
		memcpy(buf+l+1, val, n);
86
		buf[l+1+n] = 0;
87 88
		size_t map_size;
		const void *map = __map_file(buf, &map_size);
89 90 91 92
		if (map) {
			new = malloc(sizeof *new);
			if (!new) {
				__munmap((void *)map, map_size);
93
				break;
94 95 96
			}
			new->map = map;
			new->map_size = map_size;
97
			memcpy(new->name, val, n);
98
			new->name[n] = 0;
99 100 101
			new->next = loc_head;
			loc_head = new;
			break;
102 103
		}
	}
R
Rich Felker 已提交
104

105 106 107 108 109 110 111 112 113 114 115
	/* If no locale definition was found, make a locale map
	 * object anyway to store the name, which is kept for the
	 * sake of being able to do message translations at the
	 * application level. */
	if (!new && (new = malloc(sizeof *new))) {
		new->map = empty_mo;
		new->map_size = sizeof empty_mo;
		memcpy(new->name, val, n);
		new->name[n] = 0;
		new->next = loc_head;
		loc_head = new;
R
Rich Felker 已提交
116 117
	}

118 119 120
	/* For LC_CTYPE, never return a null pointer unless the
	 * requested name was "C" or "POSIX". */
	if (!new && cat == LC_CTYPE) new = (void *)&c_dot_utf8;
R
Rich Felker 已提交
121

122 123
	UNLOCK(lock);
	return new;
R
Rich Felker 已提交
124
}