Blame man3/newlocale.3

Packit 7cfc04
'\" t -*- coding: UTF-8 -*-
Packit 7cfc04
.\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH NEWLOCALE 3 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
newlocale, freelocale \- create, modify, and free a locale object
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <locale.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "locale_t newlocale(int " category_mask ", const char *" locale ",
Packit 7cfc04
.BI "                   locale_t " base );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "void freelocale(locale_t " locobj );
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR newlocale (),
Packit 7cfc04
.BR freelocale ():
Packit 7cfc04
.PD 0
Packit 7cfc04
.RS 4
Packit 7cfc04
.TP
Packit 7cfc04
Since glibc 2.10:
Packit 7cfc04
_XOPEN_SOURCE\ >=\ 700
Packit 7cfc04
.TP
Packit 7cfc04
Before glibc 2.10:
Packit 7cfc04
_GNU_SOURCE
Packit 7cfc04
.RE
Packit 7cfc04
.PD
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR newlocale ()
Packit 7cfc04
function creates a new locale object, or modifies an existing object,
Packit 7cfc04
returning a reference to the new or modified object as the function result.
Packit 7cfc04
Whether the call creates a new object or modifies an existing object
Packit 7cfc04
is determined by the value of
Packit 7cfc04
.IR base :
Packit 7cfc04
.IP * 3
Packit 7cfc04
If
Packit 7cfc04
.I base
Packit 7cfc04
is
Packit 7cfc04
.IR "(locale_t)\ 0" ,
Packit 7cfc04
a new object is created.
Packit 7cfc04
.IP *
Packit 7cfc04
If
Packit 7cfc04
.I base
Packit 7cfc04
refers to valid existing locale object
Packit 7cfc04
(i.e., an object returned by a previous call to
Packit 7cfc04
.BR newlocale ()
Packit 7cfc04
or
Packit 7cfc04
.BR duplocale (3)),
Packit 7cfc04
then that object is modified by the call.
Packit 7cfc04
If the call is successful, the contents of
Packit 7cfc04
.I base
Packit 7cfc04
are unspecified (in particular, the object referred to by
Packit 7cfc04
.I base
Packit 7cfc04
may be freed, and a new object created).
Packit 7cfc04
Therefore, the caller should ensure that it stops using
Packit 7cfc04
.I base
Packit 7cfc04
before the call to
Packit 7cfc04
.BR newlocale (),
Packit 7cfc04
and should subsequently refer to the modified object via the
Packit 7cfc04
reference returned as the function result.
Packit 7cfc04
If the call fails, the contents of
Packit 7cfc04
.I base
Packit 7cfc04
remain valid and unchanged.
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I base
Packit 7cfc04
is the special locale object
Packit 7cfc04
.BR LC_GLOBAL_LOCALE
Packit 7cfc04
(see
Packit 7cfc04
.BR duplocale (3)),
Packit 7cfc04
or is not
Packit 7cfc04
.IR "(locale_t)\ 0"
Packit 7cfc04
and is not a valid locale object handle,
Packit 7cfc04
the behavior is undefined.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I category_mask
Packit 7cfc04
argument is a bit mask that specifies the locale categories
Packit 7cfc04
that are to be set in a newly created locale object
Packit 7cfc04
or modified in an existing object.
Packit 7cfc04
The mask is constructed by a bitwise OR of the constants
Packit 7cfc04
.BR LC_ADDRESS_MASK ,
Packit 7cfc04
.BR LC_CTYPE_MASK ,
Packit 7cfc04
.BR LC_COLLATE_MASK ,
Packit 7cfc04
.BR LC_IDENTIFICATION_MASK ,
Packit 7cfc04
.BR LC_MEASUREMENT_MASK ,
Packit 7cfc04
.BR LC_MESSAGES_MASK ,
Packit 7cfc04
.BR LC_MONETARY_MASK ,
Packit 7cfc04
.BR LC_NUMERIC_MASK ,
Packit 7cfc04
.BR LC_NAME_MASK ,
Packit 7cfc04
.BR LC_PAPER_MASK ,
Packit 7cfc04
.BR LC_TELEPHONE_MASK ,
Packit 7cfc04
and
Packit 7cfc04
.BR LC_TIME_MASK .
Packit 7cfc04
Alternatively, the mask can be specified as
Packit 7cfc04
.BR LC_ALL_MASK ,
Packit 7cfc04
which is equivalent to ORing all of the preceding constants.
Packit 7cfc04
.PP
Packit 7cfc04
For each category specified in
Packit 7cfc04
.IR category_mask ,
Packit 7cfc04
the locale data from
Packit 7cfc04
.I locale
Packit 7cfc04
will be used in the object returned by
Packit 7cfc04
.BR newlocale ().
Packit 7cfc04
If a new locale object is being created,
Packit 7cfc04
data for all categories not specified in
Packit 7cfc04
.IR category_mask
Packit 7cfc04
is taken from the default ("POSIX") locale.
Packit 7cfc04
.PP
Packit 7cfc04
The following preset values of
Packit 7cfc04
.I locale
Packit 7cfc04
are defined for all categories that can be specified in
Packit 7cfc04
.IR category_mask :
Packit 7cfc04
.TP
Packit 7cfc04
"POSIX"
Packit 7cfc04
A minimal locale environment for C language programs.
Packit 7cfc04
.TP
Packit 7cfc04
"C"
Packit 7cfc04
Equivalent to "POSIX".
Packit 7cfc04
.TP
Packit 7cfc04
""
Packit 7cfc04
An implementation-defined native environment
Packit 7cfc04
corresponding to the values of the
Packit 7cfc04
.BR LC_*
Packit 7cfc04
and
Packit 7cfc04
.B LANG
Packit 7cfc04
environment variables (see
Packit 7cfc04
.BR locale (7)).
Packit 7cfc04
.SS freelocale()
Packit 7cfc04
The
Packit 7cfc04
.BR freelocale ()
Packit 7cfc04
function deallocates the resources associated with
Packit 7cfc04
.IR locobj ,
Packit 7cfc04
a locale object previously returned by a call to
Packit 7cfc04
.BR newlocale ()
Packit 7cfc04
or
Packit 7cfc04
.BR duplocale (3).
Packit 7cfc04
If
Packit 7cfc04
.I locobj
Packit 7cfc04
is
Packit 7cfc04
.BR LC_GLOBAL_LOCALE
Packit 7cfc04
or is not valid locale object handle, the results are undefined.
Packit 7cfc04
.PP
Packit 7cfc04
Once a locale object has been freed,
Packit 7cfc04
the program should make no further use of it.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success,
Packit 7cfc04
.BR newlocale ()
Packit 7cfc04
returns a handle that can be used in calls to
Packit 7cfc04
.BR duplocale (3),
Packit 7cfc04
.BR freelocale (),
Packit 7cfc04
and other functions that take a
Packit 7cfc04
.I locale_t
Packit 7cfc04
argument.
Packit 7cfc04
On error,
Packit 7cfc04
.BR newlocale ()
Packit 7cfc04
returns
Packit 7cfc04
.IR "(locale_t)\ 0",
Packit 7cfc04
and sets
Packit 7cfc04
.I errno
Packit 7cfc04
to indicate the cause of the error.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
One or more bits in
Packit 7cfc04
.I category_mask
Packit 7cfc04
do not correspond to a valid locale category.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I locale
Packit 7cfc04
is NULL.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOENT
Packit 7cfc04
.I locale
Packit 7cfc04
is not a string pointer referring to a valid locale.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
Insufficient memory to create a locale object.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
The
Packit 7cfc04
.BR newlocale ()
Packit 7cfc04
and
Packit 7cfc04
.BR freelocale ()
Packit 7cfc04
functions first appeared in version 2.3 of the GNU C library.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2008.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
Each locale object created by
Packit 7cfc04
.BR newlocale ()
Packit 7cfc04
should be deallocated using
Packit 7cfc04
.BR freelocale ().
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
The program below takes up to two command-line arguments,
Packit 7cfc04
which each identify locales.
Packit 7cfc04
The first argument is required, and is used to set the
Packit 7cfc04
.B LC_NUMERIC
Packit 7cfc04
category in a locale object created using
Packit 7cfc04
.BR newlocale ().
Packit 7cfc04
The second command-line argument is optional;
Packit 7cfc04
if it is present, it is used to set the
Packit 7cfc04
.B LC_TIME
Packit 7cfc04
category of the locale object.
Packit 7cfc04
.PP
Packit 7cfc04
Having created and initialized the locale object,
Packit 7cfc04
the program then applies it using
Packit 7cfc04
.BR uselocale (3),
Packit 7cfc04
and then tests the effect of the locale changes by:
Packit 7cfc04
.IP 1. 3
Packit 7cfc04
Displaying a floating-point number with a fractional part.
Packit 7cfc04
This output will be affected by the
Packit 7cfc04
.B LC_NUMERIC
Packit 7cfc04
setting.
Packit 7cfc04
In many European-language locales,
Packit 7cfc04
the fractional part of the number is separated from the integer part
Packit 7cfc04
using a comma, rather than a period.
Packit 7cfc04
.IP 2.
Packit 7cfc04
Displaying the date.
Packit 7cfc04
The format and language of the output will be affected by the
Packit 7cfc04
.B LC_TIME
Packit 7cfc04
setting.
Packit 7cfc04
.PP
Packit 7cfc04
The following shell sessions show some example runs of this program.
Packit 7cfc04
.PP
Packit 7cfc04
Set the
Packit 7cfc04
.B LC_NUMERIC
Packit 7cfc04
category to
Packit 7cfc04
.IR fr_FR
Packit 7cfc04
(French):
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
$ \fB./a.out fr_FR\fP
Packit 7cfc04
123456,789
Packit 7cfc04
Fri Mar  7 00:25:08 2014
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
Set the
Packit 7cfc04
.B LC_NUMERIC
Packit 7cfc04
category to
Packit 7cfc04
.IR fr_FR
Packit 7cfc04
(French),
Packit 7cfc04
and the
Packit 7cfc04
.B LC_TIME
Packit 7cfc04
category to
Packit 7cfc04
.IR it_IT
Packit 7cfc04
(Italian):
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
$ \fB./a.out fr_FR it_IT\fP
Packit 7cfc04
123456,789
Packit 7cfc04
ven 07 mar 2014 00:26:01 CET
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
Specify the
Packit 7cfc04
.B LC_TIME
Packit 7cfc04
setting as an empty string,
Packit 7cfc04
which causes the value to be taken from environment variable settings
Packit 7cfc04
(which, here, specify
Packit 7cfc04
.IR mi_NZ ,
Packit 7cfc04
New Zealand Māori):
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
$ LC_ALL=mi_NZ ./a.out fr_FR ""
Packit 7cfc04
123456,789
Packit 7cfc04
Te Paraire, te 07 o Poutū-te-rangi, 2014 00:38:44 CET
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SS Program source
Packit 7cfc04
.EX
Packit 7cfc04
#define _XOPEN_SOURCE 700
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <locale.h>
Packit 7cfc04
#include <time.h>
Packit 7cfc04
Packit 7cfc04
#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \\
Packit 7cfc04
                        } while (0)
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    char buf[100];
Packit 7cfc04
    time_t t;
Packit 7cfc04
    size_t s;
Packit 7cfc04
    struct tm *tm;
Packit 7cfc04
    locale_t loc, nloc;
Packit 7cfc04
Packit 7cfc04
    if (argc < 2) {
Packit 7cfc04
        fprintf(stderr, "Usage: %s locale1 [locale2]\\n", argv[0]);
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /* Create a new locale object, taking the LC_NUMERIC settings
Packit 7cfc04
       from the locale specified in argv[1] */
Packit 7cfc04
Packit 7cfc04
    loc = newlocale(LC_NUMERIC_MASK, argv[1], (locale_t) 0);
Packit 7cfc04
    if (loc == (locale_t) 0)
Packit 7cfc04
        errExit("newlocale");
Packit 7cfc04
Packit 7cfc04
    /* If a second command\-line argument was specified, modify the
Packit 7cfc04
       locale object to take the LC_TIME settings from the locale
Packit 7cfc04
       specified in argv[2]. We assign the result of this newlocale()
Packit 7cfc04
       call to 'nloc' rather than 'loc', since in some cases, we might
Packit 7cfc04
       want to preserve 'loc' if this call fails. */
Packit 7cfc04
Packit 7cfc04
    if (argc > 2) {
Packit 7cfc04
        nloc = newlocale(LC_TIME_MASK, argv[2], loc);
Packit 7cfc04
        if (nloc == (locale_t) 0)
Packit 7cfc04
            errExit("newlocale");
Packit 7cfc04
        loc = nloc;
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /* Apply the newly created locale to this thread */
Packit 7cfc04
Packit 7cfc04
    uselocale(loc);
Packit 7cfc04
Packit 7cfc04
    /* Test effect of LC_NUMERIC */
Packit 7cfc04
Packit 7cfc04
    printf("%8.3f\\n", 123456.789);
Packit 7cfc04
Packit 7cfc04
    /* Test effect of LC_TIME */
Packit 7cfc04
Packit 7cfc04
    t = time(NULL);
Packit 7cfc04
    tm = localtime(&t);
Packit 7cfc04
    if (tm == NULL)
Packit 7cfc04
        errExit("time");
Packit 7cfc04
Packit 7cfc04
    s = strftime(buf, sizeof(buf), "%c", tm);
Packit 7cfc04
    if (s == 0)
Packit 7cfc04
        errExit("strftime");
Packit 7cfc04
Packit 7cfc04
    printf("%s\\n", buf);
Packit 7cfc04
Packit 7cfc04
    /* Free the locale object */
Packit 7cfc04
Packit 7cfc04
    freelocale(loc);
Packit 7cfc04
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR locale (1),
Packit 7cfc04
.BR duplocale (3),
Packit 7cfc04
.BR setlocale (3),
Packit 7cfc04
.BR uselocale (3),
Packit 7cfc04
.BR locale (5),
Packit 7cfc04
.BR locale (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.