newlocale Subroutine

Purpose

Creates or modifies a locale object.

Library

Standard C Library (libc.a)

Syntax

#include <locale.h>

locale_t newlocale(category_mask, locale, base);
int category_mask;
const char *locale;
locale_t base;

Description

The newlocale subroutine creates a new locale object or modifies an existing one. If the base argument is (locale_t)0, a new locale object is created.

The category_mask argument specifies the locale categories to be set or modified. Values for category_mask are constructed by a bitwise-inclusive OR of the symbolic constants LC_CTYPE_MASK , LC_NUMERIC_MASK , LC_TIME_MASK , LC_COLLATE_MASK , LC_MONETARY_MASK , and LC_MESSAGES_MASK.

For each category with the corresponding bit set in category_mask, the data from the locale named by the locale argument is used. When modifying an existing locale object, the data from the locale named by locale replaces the existing data within the locale object. If a completely new locale object is created, the data for all sections not requested by category_mask are taken from the default locale.

Special Values

The following are the special values for the locale parameter:
Item Description
POSIX Specifies the minimal environment for C-language translation called the POSIX locale.
C Equivalent to POSIX.
"" Specifies an implementation-defined native environment. This corresponds to the value of the associated environment variables, LC_* and LANG. Refer to XBD Locale and Environment Variables.
The results are undefined if the base argument is the special locale object LC_GLOBAL_LOCALE.

Return Values

If successful, the newlocale subroutine returns a handle which the caller may use on subsequent calls to the duplocale, freelocale, and other subroutines that take a locale_t argument.

If there is failure, the newlocale subroutine returns (locale_t)0 and sets the errno global variable to indicate the error.

Error Codes

The newlocale subroutine fails if the following is true:

Item Description
ENOMEM There is not enough memory available to create the locale object or load the locale data.
EINVAL The category_mask argument contains a bit that does not correspond to a valid category.
ENOENT For any of the categories in category_mask argument, the locale data is not available.

The newlocale subroutine may fail if the following is true:

Item Description
EINVAL The locale argument is not a valid string pointer.

Example

The following example shows the construction of a locale where the LC_CTYPE category data comes from a locale loc1 and the LC_TIME category data from a locale loc2:

#include <locale.h>

...
locale_t loc, new_loc;
/* Get the "loc1" data. */

loc = newlocale (LC_CTYPE_MASK, "loc1", NULL);
if (loc == (locale_t)0)
abort();
/* Get the "loc2" data. */

new_loc = newlocale (LC_TIME_MASK, "loc2", loc);
if (new_loc != (locale_t)0)
/* We do not abort if this fails. In this case this
       simply used to unchanged locale object. */
loc = new_loc;

....