xref: /openbsd-src/lib/libc/locale/_get_locname.c (revision 3a628b46e7aaa520a6215eccabf31d313c2e7de0)
1*3a628b46Sschwarze /*	$OpenBSD: _get_locname.c,v 1.1 2017/09/05 03:16:13 schwarze Exp $ */
2*3a628b46Sschwarze /*
3*3a628b46Sschwarze  * Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
4*3a628b46Sschwarze  *
5*3a628b46Sschwarze  * Permission to use, copy, modify, and distribute this software for any
6*3a628b46Sschwarze  * purpose with or without fee is hereby granted, provided that the above
7*3a628b46Sschwarze  * copyright notice and this permission notice appear in all copies.
8*3a628b46Sschwarze  *
9*3a628b46Sschwarze  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*3a628b46Sschwarze  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*3a628b46Sschwarze  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*3a628b46Sschwarze  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*3a628b46Sschwarze  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*3a628b46Sschwarze  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*3a628b46Sschwarze  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*3a628b46Sschwarze  */
17*3a628b46Sschwarze 
18*3a628b46Sschwarze #include <errno.h>
19*3a628b46Sschwarze #include <locale.h>
20*3a628b46Sschwarze #include <paths.h>
21*3a628b46Sschwarze #include <stdio.h>
22*3a628b46Sschwarze #include <stdlib.h>
23*3a628b46Sschwarze #include <string.h>
24*3a628b46Sschwarze 
25*3a628b46Sschwarze #include "rune.h"
26*3a628b46Sschwarze #include "rune_local.h"
27*3a628b46Sschwarze 
28*3a628b46Sschwarze /*
29*3a628b46Sschwarze  * Supplement the input with locale(1) variables from the
30*3a628b46Sschwarze  * environment, validate it, and initialize UTF-8 when needed.
31*3a628b46Sschwarze  * Return NULL on error, "" if unset (for LC_ALL only),
32*3a628b46Sschwarze  * or the name of the locale that takes effect.
33*3a628b46Sschwarze  */
34*3a628b46Sschwarze const char *
_get_locname(int category,const char * locname)35*3a628b46Sschwarze _get_locname(int category, const char *locname)
36*3a628b46Sschwarze {
37*3a628b46Sschwarze 	static const char *const catname[_LC_LAST - 1] = {
38*3a628b46Sschwarze 		"LC_COLLATE",	"LC_CTYPE",	"LC_MONETARY",
39*3a628b46Sschwarze 		"LC_NUMERIC",	"LC_TIME",	"LC_MESSAGES"
40*3a628b46Sschwarze 	};
41*3a628b46Sschwarze 
42*3a628b46Sschwarze 	FILE		*fp;
43*3a628b46Sschwarze 	const char	*cp;
44*3a628b46Sschwarze 	int		 save_errno;
45*3a628b46Sschwarze 
46*3a628b46Sschwarze 	/* For empty input, inspect the environment. */
47*3a628b46Sschwarze 	if (*locname == '\0')
48*3a628b46Sschwarze 		locname = getenv("LC_ALL");
49*3a628b46Sschwarze 
50*3a628b46Sschwarze 	if (category != LC_ALL) {
51*3a628b46Sschwarze 		if (locname == NULL || *locname == '\0')
52*3a628b46Sschwarze 			locname = getenv(catname[category - 1]);
53*3a628b46Sschwarze 		if (locname == NULL || *locname == '\0')
54*3a628b46Sschwarze 			locname = getenv("LANG");
55*3a628b46Sschwarze 	}
56*3a628b46Sschwarze 
57*3a628b46Sschwarze 	/*
58*3a628b46Sschwarze 	 * If still empty, treat LC_ALL as unset, but
59*3a628b46Sschwarze 	 * fall back to the default locale for other categories.
60*3a628b46Sschwarze 	 */
61*3a628b46Sschwarze 	if (locname == NULL || *locname == '\0')
62*3a628b46Sschwarze 		locname = category == LC_ALL ? "" : "C";
63*3a628b46Sschwarze 
64*3a628b46Sschwarze 	/* Accept and ignore all language and territory prefixes. */
65*3a628b46Sschwarze 	if ((cp = strrchr(locname, '.')) == NULL)
66*3a628b46Sschwarze 		return locname;
67*3a628b46Sschwarze 
68*3a628b46Sschwarze 	/* Reject all encodings except ASCII and UTF-8. */
69*3a628b46Sschwarze 	if (strcmp(cp + 1, "UTF-8") != 0)
70*3a628b46Sschwarze 		return NULL;
71*3a628b46Sschwarze 
72*3a628b46Sschwarze 	/* Initialize the UTF-8 character encoding locale, if needed. */
73*3a628b46Sschwarze 	if (_Utf8RuneLocale != NULL)
74*3a628b46Sschwarze 		return locname;
75*3a628b46Sschwarze 	save_errno = errno;
76*3a628b46Sschwarze 	if ((fp = fopen(_PATH_LOCALE "/UTF-8/LC_CTYPE", "re")) != NULL) {
77*3a628b46Sschwarze 		_Utf8RuneLocale = _Read_RuneMagi(fp);
78*3a628b46Sschwarze 		fclose(fp);
79*3a628b46Sschwarze 	}
80*3a628b46Sschwarze 	errno = save_errno;
81*3a628b46Sschwarze 	return _Utf8RuneLocale == NULL ? NULL : locname;
82*3a628b46Sschwarze }
83