1NEWLOCALE(3)               Linux Programmer's Manual              NEWLOCALE(3)
2
3
4

NAME

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

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

VERSIONS

97       The  newlocale()  and  freelocale() functions first appeared in version
98       2.3 of the GNU C library.
99

CONFORMING TO

101       POSIX.1-2008.
102

NOTES

104       Each locale object created by newlocale() should be  deallocated  using
105       freelocale().
106

EXAMPLES

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

SEE ALSO

223       locale(1), duplocale(3),  setlocale(3),  uselocale(3),  locale(5),  lo‐
224       cale(7)
225

COLOPHON

227       This  page  is  part of release 5.12 of the Linux man-pages project.  A
228       description of the project, information about reporting bugs,  and  the
229       latest     version     of     this    page,    can    be    found    at
230       https://www.kernel.org/doc/man-pages/.
231
232
233
234Linux                             2021-03-22                      NEWLOCALE(3)
Impressum