xref: /netbsd-src/lib/libc/locale/newlocale.c (revision ace5b9b5feb0e7608bd2da7a617428d2e1cf8aa3)
1*ace5b9b5Schristos /* $NetBSD: newlocale.c,v 1.5 2024/01/20 14:52:48 christos Exp $ */
281d0329eSjoerg 
381d0329eSjoerg /*-
481d0329eSjoerg  * Copyright (c)2008, 2011 Citrus Project,
581d0329eSjoerg  * All rights reserved.
681d0329eSjoerg  *
781d0329eSjoerg  * Redistribution and use in source and binary forms, with or without
881d0329eSjoerg  * modification, are permitted provided that the following conditions
981d0329eSjoerg  * are met:
1081d0329eSjoerg  * 1. Redistributions of source code must retain the above copyright
1181d0329eSjoerg  *    notice, this list of conditions and the following disclaimer.
1281d0329eSjoerg  * 2. Redistributions in binary form must reproduce the above copyright
1381d0329eSjoerg  *    notice, this list of conditions and the following disclaimer in the
1481d0329eSjoerg  *    documentation and/or other materials provided with the distribution.
1581d0329eSjoerg  *
1681d0329eSjoerg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1781d0329eSjoerg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1881d0329eSjoerg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1981d0329eSjoerg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2081d0329eSjoerg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2181d0329eSjoerg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2281d0329eSjoerg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2381d0329eSjoerg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2481d0329eSjoerg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2581d0329eSjoerg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2681d0329eSjoerg  * SUCH DAMAGE.
2781d0329eSjoerg  */
2881d0329eSjoerg 
2981d0329eSjoerg #include <sys/cdefs.h>
30*ace5b9b5Schristos __RCSID("$NetBSD: newlocale.c,v 1.5 2024/01/20 14:52:48 christos Exp $");
3181d0329eSjoerg 
3281d0329eSjoerg #include "namespace.h"
3381d0329eSjoerg #include <assert.h>
3481d0329eSjoerg #include <errno.h>
3581d0329eSjoerg #include <locale.h>
3681d0329eSjoerg #include <stdlib.h>
3781d0329eSjoerg #include <string.h>
3881d0329eSjoerg 
3981d0329eSjoerg #include "setlocale_local.h"
4081d0329eSjoerg 
__weak_alias(newlocale,_newlocale)4181d0329eSjoerg __weak_alias(newlocale, _newlocale)
4281d0329eSjoerg 
4381d0329eSjoerg locale_t
4481d0329eSjoerg newlocale(int mask, const char *name, locale_t src)
4581d0329eSjoerg {
4681d0329eSjoerg 	struct _locale *dst;
4781d0329eSjoerg 	char head[_LOCALENAME_LEN_MAX * (_LC_LAST - 1)], *tail;
4881d0329eSjoerg 	const char *tokens[_LC_LAST - 1];
4981d0329eSjoerg 	_locale_set_t l;
5081d0329eSjoerg 	int i, howmany, categories[_LC_LAST - 1];
5181d0329eSjoerg 
5281d0329eSjoerg 	if (name == NULL)
5381d0329eSjoerg 		name = _C_LOCALE;
5481d0329eSjoerg 	dst = malloc(sizeof(*dst));
5581d0329eSjoerg 	if (dst == NULL)
5681d0329eSjoerg 		return (locale_t)NULL;
5781d0329eSjoerg 	if (src == NULL)
58e0ac190eSjoerg 		src = _current_locale();
5981d0329eSjoerg 	memcpy(dst, src, sizeof(*src));
609d594313Smlelstv 	if (strlcpy(&head[0], name, sizeof(head)) >= sizeof(head)) {
619d594313Smlelstv 		free(dst);
629d594313Smlelstv 		return (locale_t)NULL;
639d594313Smlelstv 	}
6481d0329eSjoerg 	tokens[0] = (const char *)&head[0];
65*ace5b9b5Schristos 	tail = __UNCONST(strchr(tokens[0], '/'));
6681d0329eSjoerg 	if (tail == NULL) {
6781d0329eSjoerg 		for (i = 1; i < _LC_LAST; ++i) {
6881d0329eSjoerg 			if (mask & (1 << i)) {
6981d0329eSjoerg 				l = _find_category(i);
7081d0329eSjoerg 				_DIAGASSERT(l != NULL);
7181d0329eSjoerg 				(*l)(tokens[0], dst);
7281d0329eSjoerg 			}
7381d0329eSjoerg 		}
7481d0329eSjoerg 	} else {
7581d0329eSjoerg 		*tail++ = '\0';
7681d0329eSjoerg 		howmany = 0;
7781d0329eSjoerg 		for (i = 1; i < _LC_LAST; ++i) {
7881d0329eSjoerg 			if (mask & (1 << i))
7981d0329eSjoerg 				categories[howmany++] = i;
8081d0329eSjoerg 		}
8181d0329eSjoerg 		if (howmany-- > 0) {
8281d0329eSjoerg 			for (i = 1; i < howmany; ++i) {
839d594313Smlelstv 				*tail++ = '\0';
8481d0329eSjoerg 				tokens[i] = (const char *)tail;
85*ace5b9b5Schristos 				tail = __UNCONST(strchr(tokens[i], '/'));
8681d0329eSjoerg 				if (tail == NULL) {
8781d0329eSjoerg 					free(dst);
8881d0329eSjoerg 					return NULL;
8981d0329eSjoerg 				}
9081d0329eSjoerg 			}
919d594313Smlelstv 			*tail++ = '\0';
9281d0329eSjoerg 			tokens[howmany] = tail;
93*ace5b9b5Schristos 			tail = __UNCONST(strchr(tokens[howmany], '/'));
9481d0329eSjoerg 			if (tail != NULL) {
9581d0329eSjoerg 				free(dst);
9681d0329eSjoerg 				return NULL;
9781d0329eSjoerg 			}
9881d0329eSjoerg 			for (i = 0; i <= howmany; ++i) {
9981d0329eSjoerg 				l = _find_category(categories[i]);
10081d0329eSjoerg 				_DIAGASSERT(l != NULL);
10181d0329eSjoerg 				(*l)(tokens[i], dst);
10281d0329eSjoerg 			}
10381d0329eSjoerg 		}
10481d0329eSjoerg 	}
105bdfde3daSjoerg 	if (_setlocale_cache(dst, NULL)) {
106bdfde3daSjoerg 		free(dst);
107bdfde3daSjoerg 		return NULL;
108bdfde3daSjoerg 	}
10981d0329eSjoerg 	return (locale_t)dst;
11081d0329eSjoerg }
111