1 .\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH NEWLOCALE 3 2021-03-22 "Linux" "Linux Programmer's Manual"
27 newlocale, freelocale \- create, modify, and free a locale object
30 .B #include <locale.h>
32 .BI "locale_t newlocale(int " category_mask ", const char *" locale ,
33 .BI " locale_t " base );
34 .BI "void freelocale(locale_t " locobj );
38 Feature Test Macro Requirements for glibc (see
39 .BR feature_test_macros (7)):
53 function creates a new locale object, or modifies an existing object,
54 returning a reference to the new or modified object as the function result.
55 Whether the call creates a new object or modifies an existing object
56 is determined by the value of
63 a new object is created.
67 refers to valid existing locale object
68 (i.e., an object returned by a previous call to
72 then that object is modified by the call.
73 If the call is successful, the contents of
75 are unspecified (in particular, the object referred to by
77 may be freed, and a new object created).
78 Therefore, the caller should ensure that it stops using
82 and should subsequently refer to the modified object via the
83 reference returned as the function result.
84 If the call fails, the contents of
86 remain valid and unchanged.
90 is the special locale object
96 and is not a valid locale object handle,
97 the behavior is undefined.
101 argument is a bit mask that specifies the locale categories
102 that are to be set in a newly created locale object
103 or modified in an existing object.
104 The mask is constructed by a bitwise OR of the constants
105 .BR LC_ADDRESS_MASK ,
107 .BR LC_COLLATE_MASK ,
108 .BR LC_IDENTIFICATION_MASK ,
109 .BR LC_MEASUREMENT_MASK ,
110 .BR LC_MESSAGES_MASK ,
111 .BR LC_MONETARY_MASK ,
112 .BR LC_NUMERIC_MASK ,
115 .BR LC_TELEPHONE_MASK ,
118 Alternatively, the mask can be specified as
120 which is equivalent to ORing all of the preceding constants.
122 For each category specified in
126 will be used in the object returned by
128 If a new locale object is being created,
129 data for all categories not specified in
131 is taken from the default ("POSIX") locale.
133 The following preset values of
135 are defined for all categories that can be specified in
139 A minimal locale environment for C language programs.
142 Equivalent to "POSIX".
145 An implementation-defined native environment
146 corresponding to the values of the
150 environment variables (see
155 function deallocates the resources associated with
157 a locale object previously returned by a call to
165 or is not valid locale object handle, the results are undefined.
167 Once a locale object has been freed,
168 the program should make no further use of it.
172 returns a handle that can be used in calls to
175 and other functions that take a
184 to indicate the error.
190 do not correspond to a valid locale category.
198 is not a string pointer referring to a valid locale.
201 Insufficient memory to create a locale object.
207 functions first appeared in version 2.3 of the GNU C library.
211 Each locale object created by
213 should be deallocated using
216 The program below takes up to two command-line arguments,
217 which each identify locales.
218 The first argument is required, and is used to set the
220 category in a locale object created using
222 The second command-line argument is optional;
223 if it is present, it is used to set the
225 category of the locale object.
227 Having created and initialized the locale object,
228 the program then applies it using
230 and then tests the effect of the locale changes by:
232 Displaying a floating-point number with a fractional part.
233 This output will be affected by the
236 In many European-language locales,
237 the fractional part of the number is separated from the integer part
238 using a comma, rather than a period.
241 The format and language of the output will be affected by the
245 The following shell sessions show some example runs of this program.
255 $ \fB./a.out fr_FR\fP
257 Fri Mar 7 00:25:08 2014
274 $ \fB./a.out fr_FR it_IT\fP
276 ven 07 mar 2014 00:26:01 CET
282 setting as an empty string,
283 which causes the value to be taken from environment variable settings
284 (which, here, specify
290 $ LC_ALL=mi_NZ ./a.out fr_FR ""
292 Te Paraire, te 07 o Poutū\-te\-rangi, 2014 00:38:44 CET
297 #define _XOPEN_SOURCE 700
303 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
307 main(int argc, char *argv[])
316 fprintf(stderr, "Usage: %s locale1 [locale2]\en", argv[0]);
320 /* Create a new locale object, taking the LC_NUMERIC settings
321 from the locale specified in argv[1]. */
323 loc = newlocale(LC_NUMERIC_MASK, argv[1], (locale_t) 0);
324 if (loc == (locale_t) 0)
325 errExit("newlocale");
327 /* If a second command\-line argument was specified, modify the
328 locale object to take the LC_TIME settings from the locale
329 specified in argv[2]. We assign the result of this newlocale()
330 call to \(aqnloc\(aq rather than \(aqloc\(aq, since in some cases, we might
331 want to preserve \(aqloc\(aq if this call fails. */
334 nloc = newlocale(LC_TIME_MASK, argv[2], loc);
335 if (nloc == (locale_t) 0)
336 errExit("newlocale");
340 /* Apply the newly created locale to this thread. */
344 /* Test effect of LC_NUMERIC. */
346 printf("%8.3f\en", 123456.789);
348 /* Test effect of LC_TIME. */
355 s = strftime(buf, sizeof(buf), "%c", tm);
359 printf("%s\en", buf);
361 /* Free the locale object. */
363 uselocale(LC_GLOBAL_HANDLE); /* So \(aqloc\(aq is no longer in use */