xref: /openbsd-src/lib/libc/locale/setlocale.3 (revision 6e23b9dc8de20118188a638df3b19d6b633aed2b)
1*6e23b9dcSjsg.\"	$OpenBSD: setlocale.3,v 1.24 2022/08/04 06:20:24 jsg Exp $
292f63f89Smickey.\"	$NetBSD: setlocale.3,v 1.3 1997/07/14 23:19:47 kleink Exp $
392f63f89Smickey.\"
492f63f89Smickey.\" Copyright (c) 1993
592f63f89Smickey.\"	The Regents of the University of California.  All rights reserved.
692f63f89Smickey.\"
792f63f89Smickey.\" This code is derived from software contributed to Berkeley by
892f63f89Smickey.\" Donn Seeley at BSDI.
992f63f89Smickey.\"
1092f63f89Smickey.\" Redistribution and use in source and binary forms, with or without
1192f63f89Smickey.\" modification, are permitted provided that the following conditions
1292f63f89Smickey.\" are met:
1392f63f89Smickey.\" 1. Redistributions of source code must retain the above copyright
1492f63f89Smickey.\"    notice, this list of conditions and the following disclaimer.
1592f63f89Smickey.\" 2. Redistributions in binary form must reproduce the above copyright
1692f63f89Smickey.\"    notice, this list of conditions and the following disclaimer in the
1792f63f89Smickey.\"    documentation and/or other materials provided with the distribution.
186580fee3Smillert.\" 3. Neither the name of the University nor the names of its contributors
1992f63f89Smickey.\"    may be used to endorse or promote products derived from this software
2092f63f89Smickey.\"    without specific prior written permission.
2192f63f89Smickey.\"
2292f63f89Smickey.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2392f63f89Smickey.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2492f63f89Smickey.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2592f63f89Smickey.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2692f63f89Smickey.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2792f63f89Smickey.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2892f63f89Smickey.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2992f63f89Smickey.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3092f63f89Smickey.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3192f63f89Smickey.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3292f63f89Smickey.\" SUCH DAMAGE.
3392f63f89Smickey.\"
3492f63f89Smickey.\"	@(#)setlocale.3	8.1 (Berkeley) 6/9/93
3592f63f89Smickey.\"
36*6e23b9dcSjsg.Dd $Mdocdate: August 4 2022 $
3792f63f89Smickey.Dt SETLOCALE 3
38fc8533a3Saaron.Os
3992f63f89Smickey.Sh NAME
40e6d2a11eSschwarze.Nm setlocale
417f9d96bfSschwarze.Nd select character encoding
4292f63f89Smickey.Sh SYNOPSIS
4364d4e987Stedu.In locale.h
4492f63f89Smickey.Ft char *
4592f63f89Smickey.Fn setlocale "int category" "const char *locale"
4692f63f89Smickey.Sh DESCRIPTION
4792f63f89SmickeyThe
4892f63f89Smickey.Fn setlocale
497f9d96bfSschwarzefunction sets and retrieves the active
5092f63f89Smickey.Fa locale
517f9d96bfSschwarzefor the current process.
527f9d96bfSschwarzeThe locale modifies the behaviour of some functions in the C library
537f9d96bfSschwarzewith respect to the character encoding, and on other operating systems
547f9d96bfSschwarzealso with respect to some language and cultural conventions.
557f9d96bfSschwarzeFor more information about locales in general, see the
567f9d96bfSschwarze.Xr locale 1
577f9d96bfSschwarzemanual page.
587f9d96bfSschwarze.Pp
597f9d96bfSschwarzeOn
607f9d96bfSschwarze.Ox ,
617f9d96bfSschwarzethe only useful value for the
627f9d96bfSschwarze.Fa category
637f9d96bfSschwarzeis
647f9d96bfSschwarze.Dv LC_CTYPE .
657f9d96bfSschwarzeIt sets the locale used for character encoding, character classification,
667f9d96bfSschwarzeand case conversion.
677f9d96bfSschwarzeFor compatibility with natural language support in
687f9d96bfSschwarze.Xr packages 7 ,
697f9d96bfSschwarzeall other categories \(em
707f9d96bfSschwarze.Dv LC_COLLATE ,
717f9d96bfSschwarze.Dv LC_MESSAGES ,
727f9d96bfSschwarze.Dv LC_MONETARY ,
737f9d96bfSschwarze.Dv LC_NUMERIC ,
747f9d96bfSschwarzeand
757f9d96bfSschwarze.Dv LC_TIME
767f9d96bfSschwarze\(em can be set and retrieved, too, but their values are ignored by the
777f9d96bfSschwarze.Ox
787f9d96bfSschwarzeC library.
797f9d96bfSschwarzeA category of
807f9d96bfSschwarze.Dv LC_ALL
814d16b858Sschwarzesets the entire locale generically, which is strongly discouraged for
824d16b858Sschwarzesecurity reasons in portable programs.
837f9d96bfSschwarze.Pp
847f9d96bfSschwarzeThe syntax and semantics of the
857f9d96bfSschwarze.Fa locale
867f9d96bfSschwarzeargument are not standardized and vary among operating systems.
877f9d96bfSschwarzeOn
887f9d96bfSschwarze.Ox ,
897f9d96bfSschwarzeif the
907f9d96bfSschwarze.Fa locale
917f9d96bfSschwarzestring ends with
927f9d96bfSschwarze.Qq ".UTF-8" ,
937f9d96bfSschwarzethe UTF-8 locale is selected; otherwise, the
947f9d96bfSschwarze.Qq C
957f9d96bfSschwarzelocale is selected, which uses the ASCII character set.
967f9d96bfSschwarzeIf the
977f9d96bfSschwarze.Fa locale
987f9d96bfSschwarzecontains a dot but does not end with
997f9d96bfSschwarze.Qq ".UTF-8" ,
10092f63f89Smickey.Fn setlocale
1017f9d96bfSschwarzefails.
1027f9d96bfSschwarze.Pp
1037f9d96bfSschwarzeIf
1047f9d96bfSschwarze.Fa locale
1057f9d96bfSschwarzeis an empty string
1067f9d96bfSschwarze.Pq Qq ,
1077f9d96bfSschwarzethe value of the environment variable
1087f9d96bfSschwarze.Ev LC_ALL ,
1097f9d96bfSschwarzewith a fallback to the variable corresponding to
1107f9d96bfSschwarze.Fa category ,
1117f9d96bfSschwarzeand with a further fallback to
1127f9d96bfSschwarze.Ev LANG ,
1137f9d96bfSschwarzeis used instead, as documented in the
1147f9d96bfSschwarze.Xr locale 1
1157f9d96bfSschwarzemanual page.
1167f9d96bfSschwarze.Pp
1177f9d96bfSschwarzeIf
1187f9d96bfSschwarze.Fa locale
1197f9d96bfSschwarzeis
1207f9d96bfSschwarze.Dv NULL ,
1217f9d96bfSschwarzethe locale remains unchanged.
1227f9d96bfSschwarzeThis can be used to determine the currently active locale.
1237f9d96bfSschwarze.Pp
12492f63f89SmickeyBy default, C programs start in the
1257d298bb4Sschwarze.Qq C
12692f63f89Smickeylocale.
12792f63f89SmickeyThe only function in the library that sets the locale is
12892f63f89Smickey.Fn setlocale ;
12992f63f89Smickeythe locale is never changed as a side effect of some other routine.
1304d16b858Sschwarze.Pp
1314d16b858SschwarzeThe
1324d16b858Sschwarze.Dv LC_CTYPE
1334d16b858Sschwarzecategory modifies the behaviour of at least the following functions:
1344d16b858Sschwarze.Xr iswctype 3 ,
1354d16b858Sschwarze.Xr mblen 3 ,
1364d16b858Sschwarze.Xr mbrlen 3 ,
1374d16b858Sschwarze.Xr mbrtowc 3 ,
1384d16b858Sschwarze.Xr mbsrtowcs 3 ,
1394d16b858Sschwarze.Xr mbstowcs 3 ,
1404d16b858Sschwarze.Xr mbtowc 3 ,
1414d16b858Sschwarze.Xr towctrans 3 ,
1424d16b858Sschwarze.Xr towlower 3 ,
1434d16b858Sschwarze.Xr towupper 3 ,
1444d16b858Sschwarze.Xr wcrtomb 3 ,
1454d16b858Sschwarze.Xr wcscasecmp 3 ,
1464d16b858Sschwarze.Xr wcsrtombs 3 ,
1474d16b858Sschwarze.Xr wcstombs 3 ,
1484d16b858Sschwarze.Xr wctomb 3 ,
1494d16b858Sschwarze.Xr wctrans 3 ,
1504d16b858Sschwarze.Xr wctype 3 ,
1514d16b858Sschwarzeand the functions documented in
1524d16b858Sschwarze.Xr iswalnum 3 .
153b05c4c8bSaaron.Sh RETURN VALUES
1547f9d96bfSschwarzeIn case of success,
15592f63f89Smickey.Fn setlocale
1567f9d96bfSschwarzereturns a pointer to a static string describing the locale
1577f9d96bfSschwarzethat is in force after the call.
1587f9d96bfSschwarzeSubsequent calls to
1597f9d96bfSschwarze.Fn setlocale
1607f9d96bfSschwarzemay change the content of the string.
1617f9d96bfSschwarzeThe format of the string is not standardized and varies among
1627f9d96bfSschwarzeoperating systems.
1637f9d96bfSschwarze.Pp
1647f9d96bfSschwarzeOn
1657f9d96bfSschwarze.Ox ,
1667f9d96bfSschwarzeif
1677f9d96bfSschwarze.Fn setlocale
1687f9d96bfSschwarzewas never called with a
1697f9d96bfSschwarze.Pf non- Dv NULL
17092f63f89Smickey.Fa locale
1717f9d96bfSschwarzeargument, the string
1727f9d96bfSschwarze.Qq C
1737f9d96bfSschwarzeis returned.
1747f9d96bfSschwarzeOtherwise, if the
1757f9d96bfSschwarze.Fa category
1767f9d96bfSschwarzewas not
1777f9d96bfSschwarze.Dv LC_ALL
1787f9d96bfSschwarzeor if the locale is the same for all categories, a copy of the
1797f9d96bfSschwarze.Fa locale
1807f9d96bfSschwarzeargument is returned.
1817f9d96bfSschwarzeOtherwise, the locales for the six categories
1827f9d96bfSschwarze.Dv LC_COLLATE ,
1837f9d96bfSschwarze.Dv LC_CTYPE ,
1847f9d96bfSschwarze.Dv LC_MESSAGES ,
1857f9d96bfSschwarze.Dv LC_MONETARY ,
1867f9d96bfSschwarze.Dv LC_NUMERIC ,
1877f9d96bfSschwarze.Dv LC_TIME
1887f9d96bfSschwarzeare concatenated in that order, with slash
1897f9d96bfSschwarze.Pq Ql /
1907f9d96bfSschwarzecharacters in between.
1917f9d96bfSschwarze.Pp
1927f9d96bfSschwarzeIn case of failure,
19392f63f89Smickey.Fn setlocale
1947f9d96bfSschwarzereturns
1957f9d96bfSschwarze.Dv NULL .
1967f9d96bfSschwarzeOn
1977f9d96bfSschwarze.Ox ,
1987f9d96bfSschwarzethat can only happen if the
1997f9d96bfSschwarze.Fa category
2007f9d96bfSschwarzeis invalid, if a character encoding other than UTF-8 is requested,
2017f9d96bfSschwarzeif the requested
2027f9d96bfSschwarze.Fa locale
2037f9d96bfSschwarzename is of excessive length, or if memory allocation fails.
2047f9d96bfSschwarze.Sh EXAMPLES
2057f9d96bfSschwarzeCalling
2067f9d96bfSschwarze.Pp
2077f9d96bfSschwarze.Dl setlocale(LC_CTYPE, \(dqen_US.UTF-8\(dq);
2087f9d96bfSschwarze.Pp
2097f9d96bfSschwarzeat the beginning of a program selects the UTF-8 locale and returns
2107f9d96bfSschwarze.Qq en_US.UTF-8 .
2117f9d96bfSschwarzeCalling
2127f9d96bfSschwarze.Pp
2137f9d96bfSschwarze.Dl setlocale(LC_ALL, NULL);
2147f9d96bfSschwarze.Pp
2157f9d96bfSschwarzeright afterwards leaves the locale unchanged and returns
2167f9d96bfSschwarze.Qq C/en_US.UTF-8/C/C/C/C .
2179f6079deSschwarze.Sh SEE ALSO
2189f6079deSschwarze.Xr locale 1 ,
2199f6079deSschwarze.Xr newlocale 3 ,
2209f6079deSschwarze.Xr nl_langinfo 3 ,
2219f6079deSschwarze.Xr uselocale 3
22292f63f89Smickey.Sh STANDARDS
22392f63f89SmickeyThe
22492f63f89Smickey.Fn setlocale
225e6d2a11eSschwarzefunction conforms to
22692f63f89Smickey.St -ansiC .
22792f63f89Smickey.Sh HISTORY
22892f63f89SmickeyThe
22992f63f89Smickey.Fn setlocale
230e6d2a11eSschwarzefunction first appeared in
231*6e23b9dcSjsg.Bx 4.3 Net/2 .
2324d16b858Sschwarze.Sh CAVEATS
2334d16b858SschwarzeOn systems other than
2344d16b858Sschwarze.Ox ,
2354d16b858Sschwarzecalling
2364d16b858Sschwarze.Fn setlocale
2374d16b858Sschwarzeor
2384d16b858Sschwarze.Xr uselocale 3
2394d16b858Sschwarzewith a
2404d16b858Sschwarze.Fa category
2414d16b858Sschwarzeother than
2424d16b858Sschwarze.Dv LC_CTYPE
2434d16b858Sschwarzecan cause erratic behaviour of many library functions.
2444d16b858SschwarzeFor security reasons, make sure that portable programs only use
2454d16b858Sschwarze.Dv LC_CTYPE .
2464d16b858Sschwarze.Pp
2474d16b858SschwarzeFor example, the following functions may be affected.
2484d16b858SschwarzeThe list is probably incomplete.
2494d16b858SschwarzeFor example, additional library functions may be impacted
2504d16b858Sschwarzeif they directly or indirectly call affected functions,
2514d16b858Sschwarzeor if they attempt to imitate aspects of their behaviour.
2524d16b858SschwarzeFunctions that are not standardized may be affected too.
2534d16b858Sschwarze.Bl -tag -width Ds
2544d16b858Sschwarze.It Dv LC_COLLATE
2554d16b858Sschwarze.Xr glob 3 ,
2564d16b858Sschwarze.Xr strcoll 3 ,
2574d16b858Sschwarze.Xr strxfrm 3 ,
2584d16b858Sschwarze.Xr wcscoll 3 ,
2594d16b858Sschwarze.Xr wcsxfrm 3 ,
2604d16b858Sschwarzeand the functions documented in
2614d16b858Sschwarze.Xr regexec 3
2624d16b858Sschwarze.It Dv LC_MESSAGES
2634d16b858Sschwarze.Xr catgets 3 ,
2644d16b858Sschwarze.Xr catopen 3 ,
2654d16b858Sschwarze.Xr nl_langinfo 3 ,
2664d16b858Sschwarze.Xr perror 3 ,
2674d16b858Sschwarze.Xr psignal 3 ,
2684d16b858Sschwarze.Xr strerror 3 ,
2694d16b858Sschwarze.Xr strsignal 3 ,
2704d16b858Sschwarzeand the functions documented in
2714d16b858Sschwarze.Xr err 3
2724d16b858Sschwarze.It Dv LC_MONETARY
2734d16b858Sschwarze.Xr localeconv 3 ,
2744d16b858Sschwarze.Xr nl_langinfo 3 ,
2754d16b858Sschwarze.Fn strfmon
2764d16b858Sschwarze.It Dv LC_NUMERIC
2774d16b858Sschwarze.Xr atof 3 ,
2784d16b858Sschwarze.Xr localeconv 3 ,
2794d16b858Sschwarze.Xr nl_langinfo 3 ,
2804d16b858Sschwarze.Fn strfmon ,
2814d16b858Sschwarzeand the functions documented in
2824d16b858Sschwarze.Xr printf 3 ,
2834d16b858Sschwarze.Xr scanf 3 ,
2844d16b858Sschwarze.Xr strtod 3 ,
2854d16b858Sschwarze.Xr wcstod 3 ,
2864d16b858Sschwarze.Xr wprintf 3 ,
2874d16b858Sschwarze.Xr wscanf 3 .
2884d16b858SschwarzeThis category is particularly dangerous because it can cause bugs
2894d16b858Sschwarzein the parsing and formatting of numbers, for example failures to
2904d16b858Sschwarzerecognize or properly write decimal points.
2914d16b858Sschwarze.It Dv LC_TIME
2924d16b858Sschwarze.Fn getdate ,
2934d16b858Sschwarze.Xr nl_langinfo 3 ,
2944d16b858Sschwarze.Xr strftime 3 ,
2954d16b858Sschwarze.Xr strptime 3 .
2964d16b858SschwarzeSimilarly, this is prone to causing bugs in the parsing and formatting
2974d16b858Sschwarzeof date strings.
2984d16b858Sschwarze.It Dv LC_CTYPE
2994d16b858SschwarzeOn systems other than
3004d16b858Sschwarze.Ox ,
3014d16b858Sschwarzethis category may affect the behaviour of additional functions,
3024d16b858Sschwarzefor example:
3034d16b858Sschwarze.Xr btowc 3 ,
3044d16b858Sschwarze.Xr isalnum 3 ,
3054d16b858Sschwarze.Xr isalpha 3 ,
3064d16b858Sschwarze.Xr isblank 3 ,
3074d16b858Sschwarze.Xr iscntrl 3 ,
3084d16b858Sschwarze.Xr isdigit 3 ,
3094d16b858Sschwarze.Xr isgraph 3 ,
3104d16b858Sschwarze.Xr islower 3 ,
3114d16b858Sschwarze.Xr isprint 3 ,
3124d16b858Sschwarze.Xr ispunct 3 ,
3134d16b858Sschwarze.Xr isspace 3 ,
3144d16b858Sschwarze.Xr isupper 3 ,
3154d16b858Sschwarze.Xr isxdigit 3 ,
3164d16b858Sschwarze.Xr mbsinit 3 ,
3174d16b858Sschwarze.Xr strcasecmp 3 ,
3184d16b858Sschwarze.Xr strcoll 3 ,
3194d16b858Sschwarze.Xr strxfrm 3 ,
3204d16b858Sschwarze.Xr tolower 3 ,
3214d16b858Sschwarze.Xr toupper 3 ,
3224d16b858Sschwarze.Xr vis 3 ,
3234d16b858Sschwarze.Xr wcscoll 3 ,
3244d16b858Sschwarze.Xr wcsxfrm 3 ,
3254d16b858Sschwarze.Xr wctob 3
3264d16b858Sschwarze.El
327