summaryrefslogtreecommitdiffstats
path: root/man3/duplocale.3
blob: 65292285f67567b28206a6b436a116a099f0647a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
.\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.TH DUPLOCALE 3 2020-11-01 "Linux" "Linux Programmer's Manual"
.SH NAME
duplocale \- duplicate a locale object
.SH SYNOPSIS
.nf
.B #include <locale.h>
.PP
.BI "locale_t duplocale(locale_t " locobj );
.fi
.PP
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
.PP
.BR duplocale ():
.PD 0
.RS 4
.TP
Since glibc 2.10:
_XOPEN_SOURCE\ >=\ 700
.TP
Before glibc 2.10:
_GNU_SOURCE
.RE
.PD
.SH DESCRIPTION
The
.BR duplocale ()
function creates a duplicate of the locale object referred to by
.IR locobj .
.PP
If
.I locobj
is
.BR LC_GLOBAL_LOCALE ,
.BR duplocale ()
creates a locale object containing a copy of the global locale
determined by
.BR setlocale (3).
.SH RETURN VALUE
On success,
.BR duplocale ()
returns a handle for the new locale object.
On error, it returns
.IR "(locale_t)\ 0",
and sets
.I errno
to indicate the cause of the error.
.SH ERRORS
.TP
.B ENOMEM
Insufficient memory to create the duplicate locale object.
.SH VERSIONS
The
.BR duplocale ()
function first appeared in version 2.3 of the GNU C library.
.SH CONFORMING TO
POSIX.1-2008.
.SH NOTES
Duplicating a locale can serve the following purposes:
.IP * 3
To create a copy of a locale object in which one of more categories
are to be modified (using
.BR newlocale (3)).
.IP *
To obtain a handle for the current locale which can used in
other functions that employ a locale handle, such as
.BR toupper_l (3).
This is done by applying
.BR duplocale ()
to the value returned by the following call:
.IP
    loc = uselocale((locale_t) 0);
.IP
This technique is necessary, because the above
.BR uselocale (3)
call may return the value
.BR LC_GLOBAL_LOCALE ,
which results in undefined behavior if passed to functions such as
.BR toupper_l (3).
Calling
.BR duplocale ()
can be used to ensure that the
.BR LC_GLOBAL_LOCALE
value is converted into a usable locale object.
See EXAMPLES, below.
.PP
Each locale object created by
.BR duplocale ()
should be deallocated using
.BR  freelocale (3).
.SH EXAMPLES
The program below uses
.BR uselocale (3)
and
.BR duplocale ()
to obtain a handle for the current locale which is then passed to
.BR toupper_l (3).
The program takes one command-line argument,
a string of characters that is converted to uppercase and
displayed on standard output.
An example of its use is the following:
.PP
.in +4n
.EX
$ \fB./a.out abc\fP
ABC
.EE
.in
.SS Program source
\&
.EX
#define _XOPEN_SOURCE 700
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \e
                        } while (0)

int
main(int argc, char *argv[])
{
    locale_t loc, nloc;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s string\en", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* This sequence is necessary, because uselocale() might return
       the value LC_GLOBAL_LOCALE, which can\(aqt be passed as an
       argument to toupper_l() */

    loc = uselocale((locale_t) 0);
    if (loc == (locale_t) 0)
        errExit("uselocale");

    nloc = duplocale(loc);
    if (nloc == (locale_t) 0)
        errExit("duplocale");

    for (char *p = argv[1]; *p; p++)
        putchar(toupper_l(*p, nloc));

    printf("\en");

    freelocale(nloc);

    exit(EXIT_SUCCESS);
}
.EE
.SH SEE ALSO
.BR freelocale (3),
.BR newlocale (3),
.BR setlocale (3),
.BR uselocale (3),
.BR locale (5),
.BR locale (7)
.SH COLOPHON
This page is part of release 5.09 of the Linux
.I man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
\%https://www.kernel.org/doc/man\-pages/.