man-pages/man3/duplocale.3.html
2021-03-31 01:06:50 +01:00

274 lines
7.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of DUPLOCALE</TITLE>
</HEAD><BODY>
<H1>DUPLOCALE</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
duplocale - duplicate a locale object
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/locale.h">locale.h</A>&gt;</B>
<B>locale_t duplocale(locale_t </B><I>locobj</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>duplocale</B>():
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2">Since glibc 2.10:<DD>
_XOPEN_SOURCE&nbsp;&gt;=&nbsp;700
<DT id="3">Before glibc 2.10:<DD>
_GNU_SOURCE
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>duplocale</B>()
function creates a duplicate of the locale object referred to by
<I>locobj</I>.
<P>
If
<I>locobj</I>
is
<B>LC_GLOBAL_LOCALE</B>,
<B>duplocale</B>()
creates a locale object containing a copy of the global locale
determined by
<B><A HREF="/cgi-bin/man/man2html?3+setlocale">setlocale</A></B>(3).
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>duplocale</B>()
returns a handle for the new locale object.
On error, it returns
<I>(locale_t)&nbsp;0,</I>
and sets
<I>errno</I>
to indicate the cause of the error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="4"><B>ENOMEM</B>
<DD>
Insufficient memory to create the duplicate locale object.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
The
<B>duplocale</B>()
function first appeared in version 2.3 of the GNU C library.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2008.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
Duplicating a locale can serve the following purposes:
<DL COMPACT>
<DT id="5">*<DD>
To create a copy of a locale object in which one of more categories
are to be modified (using
<B><A HREF="/cgi-bin/man/man2html?3+newlocale">newlocale</A></B>(3)).
<DT id="6">*<DD>
To obtain a handle for the current locale which can used in
other functions that employ a locale handle, such as
<B><A HREF="/cgi-bin/man/man2html?3+toupper_l">toupper_l</A></B>(3).
This is done by applying
<B>duplocale</B>()
to the value returned by the following call:
<DT id="7"><DD>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;loc&nbsp;=&nbsp;uselocale((locale_t)&nbsp;0);
<DT id="8"><DD>
This technique is necessary, because the above
<B><A HREF="/cgi-bin/man/man2html?3+uselocale">uselocale</A></B>(3)
call may return the value
<B>LC_GLOBAL_LOCALE</B>,
which results in undefined behavior if passed to functions such as
<B><A HREF="/cgi-bin/man/man2html?3+toupper_l">toupper_l</A></B>(3).
Calling
<B>duplocale</B>()
can be used to ensure that the
<B>LC_GLOBAL_LOCALE</B>
value is converted into a usable locale object.
See EXAMPLE, below.
</DL>
<P>
Each locale object created by
<B>duplocale</B>()
should be deallocated using
<B><A HREF="/cgi-bin/man/man2html?3+freelocale">freelocale</A></B>(3).
<A NAME="lbAJ">&nbsp;</A>
<H2>EXAMPLE</H2>
The program below uses
<B><A HREF="/cgi-bin/man/man2html?3+uselocale">uselocale</A></B>(3)
and
<B>duplocale</B>()
to obtain a handle for the current locale which is then passed to
<B><A HREF="/cgi-bin/man/man2html?3+toupper_l">toupper_l</A></B>(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:
<P>
$ <B>./a.out abc</B>
ABC
<A NAME="lbAK">&nbsp;</A>
<H3>Program source</H3>
#define _XOPEN_SOURCE 700
#include &lt;<A HREF="file:///usr/include/ctype.h">ctype.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/locale.h">locale.h</A>&gt;
<P>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;while&nbsp;(0)
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;locale_t&nbsp;loc,&nbsp;nloc;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*p;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;string\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;This&nbsp;sequence&nbsp;is&nbsp;necessary,&nbsp;because&nbsp;uselocale()&nbsp;might&nbsp;return
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;value&nbsp;LC_GLOBAL_LOCALE,&nbsp;which&nbsp;can't&nbsp;be&nbsp;passed&nbsp;as&nbsp;an
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;argument&nbsp;to&nbsp;toupper_l()&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;loc&nbsp;=&nbsp;uselocale((locale_t)&nbsp;0);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(loc&nbsp;==&nbsp;(locale_t)&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExit(&quot;uselocale&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;nloc&nbsp;=&nbsp;duplocale(loc);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(nloc&nbsp;==&nbsp;(locale_t)&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExit(&quot;duplocale&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(p&nbsp;=&nbsp;argv[1];&nbsp;*p;&nbsp;p++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putchar(toupper_l(*p,&nbsp;nloc));
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;freelocale(nloc);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+freelocale">freelocale</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+newlocale">newlocale</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+setlocale">setlocale</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+uselocale">uselocale</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?5+locale">locale</A></B>(5),
<B><A HREF="/cgi-bin/man/man2html?7+locale">locale</A></B>(7)
<A NAME="lbAM">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="9"><A HREF="#lbAB">NAME</A><DD>
<DT id="10"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="11"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="12"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="13"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="14"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="15"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="16"><A HREF="#lbAI">NOTES</A><DD>
<DT id="17"><A HREF="#lbAJ">EXAMPLE</A><DD>
<DL>
<DT id="18"><A HREF="#lbAK">Program source</A><DD>
</DL>
<DT id="19"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="20"><A HREF="#lbAM">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:39 GMT, March 31, 2021
</BODY>
</HTML>