1NEWLOCALE(3) Linux Programmer's Manual NEWLOCALE(3)
2
3
4
6 newlocale, freelocale - create, modify, and free a locale object
7
9 #include <locale.h>
10
11 locale_t newlocale(int category_mask, const char *locale,
12 locale_t base);
13
14 void freelocale(locale_t locobj);
15
16 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18 newlocale(), freelocale():
19 Since glibc 2.10:
20 _XOPEN_SOURCE >= 700
21 Before glibc 2.10:
22 _GNU_SOURCE
23
25 The newlocale() function creates a new locale object, or modifies an
26 existing object, returning a reference to the new or modified object as
27 the function result. Whether the call creates a new object or modifies
28 an existing object is determined by the value of base:
29
30 * If base is (locale_t) 0, a new object is created.
31
32 * If base refers to valid existing locale object (i.e., an object re‐
33 turned by a previous call to newlocale() or duplocale(3)), then that
34 object is modified by the call. If the call is successful, the con‐
35 tents of base are unspecified (in particular, the object referred to
36 by base may be freed, and a new object created). Therefore, the
37 caller should ensure that it stops using base before the call to
38 newlocale(), and should subsequently refer to the modified object
39 via the reference returned as the function result. If the call
40 fails, the contents of base remain valid and unchanged.
41
42 If base is the special locale object LC_GLOBAL_LOCALE (see duplo‐
43 cale(3)), or is not (locale_t) 0 and is not a valid locale object han‐
44 dle, the behavior is undefined.
45
46 The category_mask argument is a bit mask that specifies the locale cat‐
47 egories that are to be set in a newly created locale object or modified
48 in an existing object. The mask is constructed by a bitwise OR of the
49 constants LC_ADDRESS_MASK, LC_CTYPE_MASK, LC_COLLATE_MASK, LC_IDENTIFI‐
50 CATION_MASK, LC_MEASUREMENT_MASK, LC_MESSAGES_MASK, LC_MONETARY_MASK,
51 LC_NUMERIC_MASK, LC_NAME_MASK, LC_PAPER_MASK, LC_TELEPHONE_MASK, and
52 LC_TIME_MASK. Alternatively, the mask can be specified as LC_ALL_MASK,
53 which is equivalent to ORing all of the preceding constants.
54
55 For each category specified in category_mask, the locale data from lo‐
56 cale will be used in the object returned by newlocale(). If a new lo‐
57 cale object is being created, data for all categories not specified in
58 category_mask is taken from the default ("POSIX") locale.
59
60 The following preset values of locale are defined for all categories
61 that can be specified in category_mask:
62
63 "POSIX"
64 A minimal locale environment for C language programs.
65
66 "C" Equivalent to "POSIX".
67
68 "" An implementation-defined native environment corresponding to
69 the values of the LC_* and LANG environment variables (see lo‐
70 cale(7)).
71
72 freelocale()
73 The freelocale() function deallocates the resources associated with lo‐
74 cobj, a locale object previously returned by a call to newlocale() or
75 duplocale(3). If locobj is LC_GLOBAL_LOCALE or is not valid locale ob‐
76 ject handle, the results are undefined.
77
78 Once a locale object has been freed, the program should make no further
79 use of it.
80
82 On success, newlocale() returns a handle that can be used in calls to
83 duplocale(3), freelocale(), and other functions that take a locale_t
84 argument. On error, newlocale() returns (locale_t) 0, and sets errno
85 to indicate the cause of the error.
86
88 EINVAL One or more bits in category_mask do not correspond to a valid
89 locale category.
90
91 EINVAL locale is NULL.
92
93 ENOENT locale is not a string pointer referring to a valid locale.
94
95 ENOMEM Insufficient memory to create a locale object.
96
98 The newlocale() and freelocale() functions first appeared in version
99 2.3 of the GNU C library.
100
102 POSIX.1-2008.
103
105 Each locale object created by newlocale() should be deallocated using
106 freelocale().
107
109 The program below takes up to two command-line arguments, which each
110 identify locales. The first argument is required, and is used to set
111 the LC_NUMERIC category in a locale object created using newlocale().
112 The second command-line argument is optional; if it is present, it is
113 used to set the LC_TIME category of the locale object.
114
115 Having created and initialized the locale object, the program then ap‐
116 plies it using uselocale(3), and then tests the effect of the locale
117 changes by:
118
119 1. Displaying a floating-point number with a fractional part. This
120 output will be affected by the LC_NUMERIC setting. In many Euro‐
121 pean-language locales, the fractional part of the number is sepa‐
122 rated from the integer part using a comma, rather than a period.
123
124 2. Displaying the date. The format and language of the output will be
125 affected by the LC_TIME setting.
126
127 The following shell sessions show some example runs of this program.
128
129 Set the LC_NUMERIC category to fr_FR (French):
130
131 $ ./a.out fr_FR
132 123456,789
133 Fri Mar 7 00:25:08 2014
134
135 Set the LC_NUMERIC category to fr_FR (French), and the LC_TIME category
136 to it_IT (Italian):
137
138 $ ./a.out fr_FR it_IT
139 123456,789
140 ven 07 mar 2014 00:26:01 CET
141
142 Specify the LC_TIME setting as an empty string, which causes the value
143 to be taken from environment variable settings (which, here, specify
144 mi_NZ, New Zealand Māori):
145
146 $ LC_ALL=mi_NZ ./a.out fr_FR ""
147 123456,789
148 Te Paraire, te 07 o Poutū-te-rangi, 2014 00:38:44 CET
149
150 Program source
151 #define _XOPEN_SOURCE 700
152 #include <stdio.h>
153 #include <stdlib.h>
154 #include <locale.h>
155 #include <time.h>
156
157 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
158 } while (0)
159
160 int
161 main(int argc, char *argv[])
162 {
163 char buf[100];
164 time_t t;
165 size_t s;
166 struct tm *tm;
167 locale_t loc, nloc;
168
169 if (argc < 2) {
170 fprintf(stderr, "Usage: %s locale1 [locale2]\n", argv[0]);
171 exit(EXIT_FAILURE);
172 }
173
174 /* Create a new locale object, taking the LC_NUMERIC settings
175 from the locale specified in argv[1] */
176
177 loc = newlocale(LC_NUMERIC_MASK, argv[1], (locale_t) 0);
178 if (loc == (locale_t) 0)
179 errExit("newlocale");
180
181 /* If a second command-line argument was specified, modify the
182 locale object to take the LC_TIME settings from the locale
183 specified in argv[2]. We assign the result of this newlocale()
184 call to 'nloc' rather than 'loc', since in some cases, we might
185 want to preserve 'loc' if this call fails. */
186
187 if (argc > 2) {
188 nloc = newlocale(LC_TIME_MASK, argv[2], loc);
189 if (nloc == (locale_t) 0)
190 errExit("newlocale");
191 loc = nloc;
192 }
193
194 /* Apply the newly created locale to this thread */
195
196 uselocale(loc);
197
198 /* Test effect of LC_NUMERIC */
199
200 printf("%8.3f\n", 123456.789);
201
202 /* Test effect of LC_TIME */
203
204 t = time(NULL);
205 tm = localtime(&t);
206 if (tm == NULL)
207 errExit("time");
208
209 s = strftime(buf, sizeof(buf), "%c", tm);
210 if (s == 0)
211 errExit("strftime");
212
213 printf("%s\n", buf);
214
215 /* Free the locale object */
216
217 uselocale(LC_GLOBAL_HANDLE); /* So 'loc' is no longer in use */
218 freelocale(loc);
219
220 exit(EXIT_SUCCESS);
221 }
222
224 locale(1), duplocale(3), setlocale(3), uselocale(3), locale(5), lo‐
225 cale(7)
226
228 This page is part of release 5.10 of the Linux man-pages project. A
229 description of the project, information about reporting bugs, and the
230 latest version of this page, can be found at
231 https://www.kernel.org/doc/man-pages/.
232
233
234
235Linux 2020-11-01 NEWLOCALE(3)