1newlocale(3)               Library Functions Manual               newlocale(3)
2
3
4

NAME

6       newlocale, freelocale - create, modify, and free a locale object
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <locale.h>
13
14       locale_t newlocale(int category_mask, const char *locale,
15                          locale_t base);
16       void freelocale(locale_t locobj);
17
18   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20       newlocale(), freelocale():
21           Since glibc 2.10:
22               _XOPEN_SOURCE >= 700
23           Before glibc 2.10:
24               _GNU_SOURCE
25

DESCRIPTION

27       The  newlocale()  function  creates a new locale object, or modifies an
28       existing object, returning a reference to the new or modified object as
29       the function result.  Whether the call creates a new object or modifies
30       an existing object is determined by the value of base:
31
32       •  If base is (locale_t) 0, a new object is created.
33
34       •  If base refers to valid existing locale object (i.e., an object  re‐
35          turned by a previous call to newlocale() or duplocale(3)), then that
36          object is modified by the call.  If the call is successful, the con‐
37          tents of base are unspecified (in particular, the object referred to
38          by base may be freed, and a new  object  created).   Therefore,  the
39          caller  should  ensure  that  it stops using base before the call to
40          newlocale(), and should subsequently refer to  the  modified  object
41          via  the  reference  returned  as  the function result.  If the call
42          fails, the contents of base remain valid and unchanged.
43
44       If base is the  special  locale  object  LC_GLOBAL_LOCALE  (see  duplo‐
45       cale(3)),  or is not (locale_t) 0 and is not a valid locale object han‐
46       dle, the behavior is undefined.
47
48       The category_mask argument is a bit mask that specifies the locale cat‐
49       egories that are to be set in a newly created locale object or modified
50       in an existing object.  The mask is constructed by a bitwise OR of  the
51       constants LC_ADDRESS_MASK, LC_CTYPE_MASK, LC_COLLATE_MASK, LC_IDENTIFI‐
52       CATION_MASK, LC_MEASUREMENT_MASK,  LC_MESSAGES_MASK,  LC_MONETARY_MASK,
53       LC_NUMERIC_MASK,  LC_NAME_MASK,  LC_PAPER_MASK,  LC_TELEPHONE_MASK, and
54       LC_TIME_MASK.  Alternatively, the mask can be specified as LC_ALL_MASK,
55       which is equivalent to ORing all of the preceding constants.
56
57       For  each category specified in category_mask, the locale data from lo‐
58       cale will be used in the object returned by newlocale().  If a new  lo‐
59       cale  object is being created, data for all categories not specified in
60       category_mask is taken from the default ("POSIX") locale.
61
62       The following preset values of locale are defined  for  all  categories
63       that can be specified in category_mask:
64
65       "POSIX"
66              A minimal locale environment for C language programs.
67
68       "C"    Equivalent to "POSIX".
69
70       ""     An  implementation-defined  native  environment corresponding to
71              the values of the LC_* and LANG environment variables  (see  lo‐
72              cale(7)).
73
74   freelocale()
75       The freelocale() function deallocates the resources associated with lo‐
76       cobj, a locale object previously returned by a call to  newlocale()  or
77       duplocale(3).  If locobj is LC_GLOBAL_LOCALE or is not valid locale ob‐
78       ject handle, the results are undefined.
79
80       Once a locale object has been freed, the program should make no further
81       use of it.
82

RETURN VALUE

84       On  success,  newlocale() returns a handle that can be used in calls to
85       duplocale(3), freelocale(), and other functions that  take  a  locale_t
86       argument.   On  error, newlocale() returns (locale_t) 0, and sets errno
87       to indicate the error.
88

ERRORS

90       EINVAL One or more bits in category_mask do not correspond to  a  valid
91              locale category.
92
93       EINVAL locale is NULL.
94
95       ENOENT locale is not a string pointer referring to a valid locale.
96
97       ENOMEM Insufficient memory to create a locale object.
98

STANDARDS

100       POSIX.1-2008.
101

HISTORY

103       glibc 2.3.
104

NOTES

106       Each  locale  object created by newlocale() should be deallocated using
107       freelocale().
108

EXAMPLES

110       The program below takes up to two command-line  arguments,  which  each
111       identify  locales.   The first argument is required, and is used to set
112       the LC_NUMERIC category in a locale object created  using  newlocale().
113       The  second  command-line argument is optional; if it is present, it is
114       used to set the LC_TIME category of the locale object.
115
116       Having created and initialized the locale object, the program then  ap‐
117       plies  it  using  uselocale(3), and then tests the effect of the locale
118       changes by:
119
120       (1)  Displaying a floating-point number with a fractional  part.   This
121            output  will be affected by the LC_NUMERIC setting.  In many Euro‐
122            pean-language locales, the fractional part of the number is  sepa‐
123            rated from the integer part using a comma, rather than a period.
124
125       (2)  Displaying  the  date.  The format and language of the output will
126            be affected by the LC_TIME setting.
127
128       The following shell sessions show some example runs of this program.
129
130       Set the LC_NUMERIC category to fr_FR (French):
131
132           $ ./a.out fr_FR
133           123456,789
134           Fri Mar  7 00:25:08 2014
135
136       Set the LC_NUMERIC category to fr_FR (French), and the LC_TIME category
137       to it_IT (Italian):
138
139           $ ./a.out fr_FR it_IT
140           123456,789
141           ven 07 mar 2014 00:26:01 CET
142
143       Specify  the LC_TIME setting as an empty string, which causes the value
144       to be taken from environment variable settings  (which,  here,  specify
145       mi_NZ, New Zealand Māori):
146
147           $ LC_ALL=mi_NZ ./a.out fr_FR ""
148           123456,789
149           Te Paraire, te 07 o Poutū-te-rangi, 2014 00:38:44 CET
150
151   Program source
152       #define _XOPEN_SOURCE 700
153       #include <locale.h>
154       #include <stdio.h>
155       #include <stdlib.h>
156       #include <time.h>
157
158       #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
159                               } while (0)
160
161       int
162       main(int argc, char *argv[])
163       {
164           char buf[100];
165           time_t t;
166           size_t s;
167           struct tm *tm;
168           locale_t loc, nloc;
169
170           if (argc < 2) {
171               fprintf(stderr, "Usage: %s locale1 [locale2]\n", argv[0]);
172               exit(EXIT_FAILURE);
173           }
174
175           /* Create a new locale object, taking the LC_NUMERIC settings
176              from the locale specified in argv[1]. */
177
178           loc = newlocale(LC_NUMERIC_MASK, argv[1], (locale_t) 0);
179           if (loc == (locale_t) 0)
180               errExit("newlocale");
181
182           /* If a second command-line argument was specified, modify the
183              locale object to take the LC_TIME settings from the locale
184              specified in argv[2]. We assign the result of this newlocale()
185              call to 'nloc' rather than 'loc', since in some cases, we might
186              want to preserve 'loc' if this call fails. */
187
188           if (argc > 2) {
189               nloc = newlocale(LC_TIME_MASK, argv[2], loc);
190               if (nloc == (locale_t) 0)
191                   errExit("newlocale");
192               loc = nloc;
193           }
194
195           /* Apply the newly created locale to this thread. */
196
197           uselocale(loc);
198
199           /* Test effect of LC_NUMERIC. */
200
201           printf("%8.3f\n", 123456.789);
202
203           /* Test effect of LC_TIME. */
204
205           t = time(NULL);
206           tm = localtime(&t);
207           if (tm == NULL)
208               errExit("time");
209
210           s = strftime(buf, sizeof(buf), "%c", tm);
211           if (s == 0)
212               errExit("strftime");
213
214           printf("%s\n", buf);
215
216           /* Free the locale object. */
217
218           uselocale(LC_GLOBAL_LOCALE);    /* So 'loc' is no longer in use */
219           freelocale(loc);
220
221           exit(EXIT_SUCCESS);
222       }
223

SEE ALSO

225       locale(1),  duplocale(3),  setlocale(3),  uselocale(3),  locale(5), lo‐
226       cale(7)
227
228
229
230Linux man-pages 6.05              2023-05-03                      newlocale(3)
Impressum