summaryrefslogtreecommitdiff
path: root/src/locale/__setlocalecat.c
blob: a947dbfff8a3388f73cd993e42ec92fa5f538f04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <locale.h>
#include <string.h>
#include "locale_impl.h"
#include "libc.h"
#include "atomic.h"

static const char envvars[][12] = {
	"LC_CTYPE",
	"LC_NUMERIC",
	"LC_TIME",
	"LC_COLLATE",
	"LC_MONETARY",
	"LC_MESSAGES",
};

int __setlocalecat(locale_t loc, int cat, const char *val)
{
	if (!*val) {
		(val = getenv("LC_ALL")) && *val ||
		(val = getenv(envvars[cat])) && *val ||
		(val = getenv("LANG")) && *val ||
		(val = "C.UTF-8");
	}

	size_t n = strnlen(val, LOCALE_NAME_MAX);
	int builtin = (val[0]=='C' && !val[1])
		|| !strcmp(val, "C.UTF-8")
		|| !strcmp(val, "POSIX");

	switch (cat) {
	case LC_CTYPE:
		a_store(&loc->ctype_utf8, !builtin || val[1]=='.');
		break;
	case LC_MESSAGES:
		if (builtin) {
			loc->messages_name[0] = 0;
		} else {
			memcpy(loc->messages_name, val, n);
			loc->messages_name[n] = 0;
		}
		/* fall through */
	default:
		break;
	}
	return 0;
}