xref: /illumos-gate/usr/src/man/man3c/getlocalename_l.3c (revision 004345e48064ccd168d15f66eba2031c6090ccee)
1*004345e4SRobert Mustacchi.\"
2*004345e4SRobert Mustacchi.\" This file and its contents are supplied under the terms of the
3*004345e4SRobert Mustacchi.\" Common Development and Distribution License ("CDDL"), version 1.0.
4*004345e4SRobert Mustacchi.\" You may only use this file in accordance with the terms of version
5*004345e4SRobert Mustacchi.\" 1.0 of the CDDL.
6*004345e4SRobert Mustacchi.\"
7*004345e4SRobert Mustacchi.\" A full copy of the text of the CDDL should have accompanied this
8*004345e4SRobert Mustacchi.\" source.  A copy of the CDDL is also available via the Internet at
9*004345e4SRobert Mustacchi.\" http://www.illumos.org/license/CDDL.
10*004345e4SRobert Mustacchi.\"
11*004345e4SRobert Mustacchi.\"
12*004345e4SRobert Mustacchi.\" Copyright 2025 Oxide Computer Company
13*004345e4SRobert Mustacchi.\"
14*004345e4SRobert Mustacchi.Dd April 11, 2025
15*004345e4SRobert Mustacchi.Dt GETLOCALENAME_L 3C
16*004345e4SRobert Mustacchi.Os
17*004345e4SRobert Mustacchi.Sh NAME
18*004345e4SRobert Mustacchi.Nm getlocalename_l
19*004345e4SRobert Mustacchi.Nd get name of locale
20*004345e4SRobert Mustacchi.Sh LIBRARY
21*004345e4SRobert Mustacchi.Lb libc
22*004345e4SRobert Mustacchi.Sh SYNOPSIS
23*004345e4SRobert Mustacchi.In locale.h
24*004345e4SRobert Mustacchi.Ft "const char *"
25*004345e4SRobert Mustacchi.Fo getlocalename_l
26*004345e4SRobert Mustacchi.Fa "int category"
27*004345e4SRobert Mustacchi.Fa "locale_t locale"
28*004345e4SRobert Mustacchi.Fc
29*004345e4SRobert Mustacchi.Sh DESCRIPTION
30*004345e4SRobert MustacchiThe
31*004345e4SRobert Mustacchi.Fn getlocalename_l
32*004345e4SRobert Mustacchifunction returns the name of the specific
33*004345e4SRobert Mustacchi.Fa category
34*004345e4SRobert Mustacchiof the specified
35*004345e4SRobert Mustacchi.Fa locale .
36*004345e4SRobert MustacchiIf
37*004345e4SRobert Mustacchi.Fa locale
38*004345e4SRobert Mustacchiis
39*004345e4SRobert Mustacchi.Dv LC_GLOBAL_LOCALE ,
40*004345e4SRobert Mustacchithen information will be returned about the current global locale.
41*004345e4SRobert MustacchiThe global locale is changed by calls to
42*004345e4SRobert Mustacchi.Xr setlocale 3C
43*004345e4SRobert Mustacchiand not impacted by a thread calling
44*004345e4SRobert Mustacchi.Xr uselocale 3C .
45*004345e4SRobert Mustacchi.Pp
46*004345e4SRobert MustacchiWhen the category is
47*004345e4SRobert Mustacchi.Dv LC_ALL ,
48*004345e4SRobert Mustacchithe returned string is a form that when passed to
49*004345e4SRobert Mustacchi.Xr setlocale 3C
50*004345e4SRobert Mustacchiit will result in the same locale being activated.
51*004345e4SRobert MustacchiSimilarly, passing this locale to
52*004345e4SRobert Mustacchi.Xr newlocale 3C
53*004345e4SRobert Mustacchiwill result in a comparable locale.
54*004345e4SRobert MustacchiNote, the latter is not strictly guaranteed by POSIX so portable applications
55*004345e4SRobert Mustacchimay not want to rely on this behavior.
56*004345e4SRobert Mustacchi.Pp
57*004345e4SRobert MustacchiThe returned string's lifetime and the validity of the pointer is tied to the
58*004345e4SRobert Mustacchilifetime of the locale itself.
59*004345e4SRobert MustacchiThis varies depending on whether this refers to the global locale or not.
60*004345e4SRobert MustacchiIf it is not the global locale, calling either
61*004345e4SRobert Mustacchi.Xr newlocale 3C
62*004345e4SRobert Mustacchior
63*004345e4SRobert Mustacchi.Xr freelocale 3C
64*004345e4SRobert Mustacchion
65*004345e4SRobert Mustacchi.Fa locale ,
66*004345e4SRobert Mustacchimeans that the string is no longer valid.
67*004345e4SRobert Mustacchi.Pp
68*004345e4SRobert MustacchiWhen querying the global locale, data lifetimes are more complicated.
69*004345e4SRobert MustacchiIt is possible that calls to the
70*004345e4SRobert Mustacchi.Fn getlocalename_l
71*004345e4SRobert Mustacchifunction may race with other threads calling
72*004345e4SRobert Mustacchi.Xr setlocale 3C .
73*004345e4SRobert MustacchiThe returned data will always be valid; however, depending on the interleaving
74*004345e4SRobert Mustacchiof thread execution, the returned name may be from the prior locale during a
75*004345e4SRobert Mustacchicontemporaneous
76*004345e4SRobert Mustacchi.Xr setlocale 3C .
77*004345e4SRobert MustacchiThis is the exception to the thread safety documented for this function.
78*004345e4SRobert MustacchiPortable applications should assume that data returned related to the global
79*004345e4SRobert Mustacchilocale may be invalidated by the calling thread exiting or calling
80*004345e4SRobert Mustacchi.Fn getlocalename_l
81*004345e4SRobert Mustacchiagain with
82*004345e4SRobert Mustacchi.Dv LC_GLOBAL_LOCALE .
83*004345e4SRobert Mustacchi.Sh RETURN VALUES
84*004345e4SRobert MustacchiUpon successful completion, the
85*004345e4SRobert Mustacchi.Fn getlocalename_l
86*004345e4SRobert Mustacchireturns the name of the
87*004345e4SRobert Mustacchi.Fa category
88*004345e4SRobert Mustacchiof
89*004345e4SRobert Mustacchi.Fa locale .
90*004345e4SRobert MustacchiOtherwise,
91*004345e4SRobert Mustacchi.Dv NULL
92*004345e4SRobert Mustacchiis returned.
93*004345e4SRobert Mustacchi.Sh EXAMPLES
94*004345e4SRobert Mustacchi.Sy Example 1
95*004345e4SRobert MustacchiPrinting the current thread's locale.
96*004345e4SRobert Mustacchi.Pp
97*004345e4SRobert MustacchiThis example queries the current thread's locale; however, it does not call
98*004345e4SRobert Mustacchi.Xr freelocale 3C .
99*004345e4SRobert MustacchiLocales that are queried at run-time are still in use and therefore should not
100*004345e4SRobert Mustacchibe freed.
101*004345e4SRobert MustacchiInstead, the logic that set the locale should free it after it changes the
102*004345e4SRobert Mustacchilocale again.
103*004345e4SRobert Mustacchi.Bd -literal
104*004345e4SRobert Mustacchi#include <stdio.h>
105*004345e4SRobert Mustacchi#include <locale.h>
106*004345e4SRobert Mustacchi
107*004345e4SRobert Mustacchivoid
108*004345e4SRobert Mustacchiprint_locale(void)
109*004345e4SRobert Mustacchi{
110*004345e4SRobert Mustacchi        locale_t loc = uselocale(NULL);
111*004345e4SRobert Mustacchi        printf("%s\en", getlocalename_l(LC_ALL, LC_GLOBAL_LOCALE));
112*004345e4SRobert Mustacchi}
113*004345e4SRobert Mustacchi.Ed
114*004345e4SRobert Mustacchi.Pp
115*004345e4SRobert Mustacchi.Sy Example 2
116*004345e4SRobert MustacchiPrinting the global locale.
117*004345e4SRobert Mustacchi.Bd -literal
118*004345e4SRobert Mustacchi#include <stdio.h>
119*004345e4SRobert Mustacchi#include <locale.h>
120*004345e4SRobert Mustacchi
121*004345e4SRobert Mustacchiint
122*004345e4SRobert Mustacchimain(void)
123*004345e4SRobert Mustacchi{
124*004345e4SRobert Mustacchi        (void) setlocale(LC_ALL, "");
125*004345e4SRobert Mustacchi        printf("%s\en", getlocalename_l(LC_ALL, LC_GLOBAL_LOCALE));
126*004345e4SRobert Mustacchi        return (0);
127*004345e4SRobert Mustacchi}
128*004345e4SRobert Mustacchi.Ed
129*004345e4SRobert Mustacchi.Sh INTERFACE STABILITY
130*004345e4SRobert Mustacchi.Sy Committed
131*004345e4SRobert Mustacchi.Sh MT-LEVEL
132*004345e4SRobert Mustacchi.Sy MT-Safe with Exceptions
133*004345e4SRobert Mustacchi.Sh SEE ALSO
134*004345e4SRobert Mustacchi.Xr locale 1 ,
135*004345e4SRobert Mustacchi.Xr freelocale 3C ,
136*004345e4SRobert Mustacchi.Xr newlocale 3C ,
137*004345e4SRobert Mustacchi.Xr setlocale 3C ,
138*004345e4SRobert Mustacchi.Xr uselocale 3C ,
139*004345e4SRobert Mustacchi.Xr locale 7
140