about summary refs log tree commit diff
path: root/src/locale/__setlocalecat.c
blob: f1e4bf07939d315bef1a9320ecce8c0dc6ad6d1f (plain) (blame)
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 = getenv(envvars[cat])) ||
		(val = getenv("LANG")) ||
		(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;
}