1*84d9c625SLionel Sambuc /* $NetBSD: newlocale.c,v 1.3 2013/09/13 13:13:32 joerg Exp $ */
2*84d9c625SLionel Sambuc
3*84d9c625SLionel Sambuc /*-
4*84d9c625SLionel Sambuc * Copyright (c)2008, 2011 Citrus Project,
5*84d9c625SLionel Sambuc * All rights reserved.
6*84d9c625SLionel Sambuc *
7*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
9*84d9c625SLionel Sambuc * are met:
10*84d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*84d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*84d9c625SLionel Sambuc *
16*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*84d9c625SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*84d9c625SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*84d9c625SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*84d9c625SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*84d9c625SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*84d9c625SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*84d9c625SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*84d9c625SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*84d9c625SLionel Sambuc * SUCH DAMAGE.
27*84d9c625SLionel Sambuc */
28*84d9c625SLionel Sambuc
29*84d9c625SLionel Sambuc #include <sys/cdefs.h>
30*84d9c625SLionel Sambuc __RCSID("$NetBSD: newlocale.c,v 1.3 2013/09/13 13:13:32 joerg Exp $");
31*84d9c625SLionel Sambuc
32*84d9c625SLionel Sambuc #include "namespace.h"
33*84d9c625SLionel Sambuc #include <assert.h>
34*84d9c625SLionel Sambuc #include <errno.h>
35*84d9c625SLionel Sambuc #include <locale.h>
36*84d9c625SLionel Sambuc #include <stdlib.h>
37*84d9c625SLionel Sambuc #include <string.h>
38*84d9c625SLionel Sambuc
39*84d9c625SLionel Sambuc #include "setlocale_local.h"
40*84d9c625SLionel Sambuc
__weak_alias(newlocale,_newlocale)41*84d9c625SLionel Sambuc __weak_alias(newlocale, _newlocale)
42*84d9c625SLionel Sambuc
43*84d9c625SLionel Sambuc locale_t
44*84d9c625SLionel Sambuc newlocale(int mask, const char *name, locale_t src)
45*84d9c625SLionel Sambuc {
46*84d9c625SLionel Sambuc struct _locale *dst;
47*84d9c625SLionel Sambuc char head[_LOCALENAME_LEN_MAX * (_LC_LAST - 1)], *tail;
48*84d9c625SLionel Sambuc const char *tokens[_LC_LAST - 1];
49*84d9c625SLionel Sambuc _locale_set_t l;
50*84d9c625SLionel Sambuc int i, howmany, categories[_LC_LAST - 1];
51*84d9c625SLionel Sambuc
52*84d9c625SLionel Sambuc if (name == NULL)
53*84d9c625SLionel Sambuc name = _C_LOCALE;
54*84d9c625SLionel Sambuc dst = malloc(sizeof(*dst));
55*84d9c625SLionel Sambuc if (dst == NULL)
56*84d9c625SLionel Sambuc return (locale_t)NULL;
57*84d9c625SLionel Sambuc if (src == NULL)
58*84d9c625SLionel Sambuc src = _current_locale();
59*84d9c625SLionel Sambuc memcpy(dst, src, sizeof(*src));
60*84d9c625SLionel Sambuc strlcpy(&head[0], name, sizeof(head));
61*84d9c625SLionel Sambuc tokens[0] = (const char *)&head[0];
62*84d9c625SLionel Sambuc tail = strchr(tokens[0], '/');
63*84d9c625SLionel Sambuc if (tail == NULL) {
64*84d9c625SLionel Sambuc for (i = 1; i < _LC_LAST; ++i) {
65*84d9c625SLionel Sambuc if (mask & (1 << i)) {
66*84d9c625SLionel Sambuc l = _find_category(i);
67*84d9c625SLionel Sambuc _DIAGASSERT(l != NULL);
68*84d9c625SLionel Sambuc (*l)(tokens[0], dst);
69*84d9c625SLionel Sambuc }
70*84d9c625SLionel Sambuc }
71*84d9c625SLionel Sambuc } else {
72*84d9c625SLionel Sambuc *tail++ = '\0';
73*84d9c625SLionel Sambuc howmany = 0;
74*84d9c625SLionel Sambuc for (i = 1; i < _LC_LAST; ++i) {
75*84d9c625SLionel Sambuc if (mask & (1 << i))
76*84d9c625SLionel Sambuc categories[howmany++] = i;
77*84d9c625SLionel Sambuc }
78*84d9c625SLionel Sambuc if (howmany-- > 0) {
79*84d9c625SLionel Sambuc for (i = 1; i < howmany; ++i) {
80*84d9c625SLionel Sambuc tokens[i] = (const char *)tail;
81*84d9c625SLionel Sambuc tail = strchr(tokens[i], '/');
82*84d9c625SLionel Sambuc if (tail == NULL) {
83*84d9c625SLionel Sambuc free(dst);
84*84d9c625SLionel Sambuc return NULL;
85*84d9c625SLionel Sambuc }
86*84d9c625SLionel Sambuc }
87*84d9c625SLionel Sambuc tokens[howmany] = tail;
88*84d9c625SLionel Sambuc tail = strchr(tokens[howmany], '/');
89*84d9c625SLionel Sambuc if (tail != NULL) {
90*84d9c625SLionel Sambuc free(dst);
91*84d9c625SLionel Sambuc return NULL;
92*84d9c625SLionel Sambuc }
93*84d9c625SLionel Sambuc for (i = 0; i <= howmany; ++i) {
94*84d9c625SLionel Sambuc l = _find_category(categories[i]);
95*84d9c625SLionel Sambuc _DIAGASSERT(l != NULL);
96*84d9c625SLionel Sambuc (*l)(tokens[i], dst);
97*84d9c625SLionel Sambuc }
98*84d9c625SLionel Sambuc }
99*84d9c625SLionel Sambuc }
100*84d9c625SLionel Sambuc if (_setlocale_cache(dst, NULL)) {
101*84d9c625SLionel Sambuc free(dst);
102*84d9c625SLionel Sambuc return NULL;
103*84d9c625SLionel Sambuc }
104*84d9c625SLionel Sambuc return (locale_t)dst;
105*84d9c625SLionel Sambuc }
106