NEWLOCALE(3) Library Functions Manual NEWLOCALE(3)

newlocalecreate or modify a locale object


#include <locale.h>

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

The newlocale () function shall create a new locale object or modify an existing one. If the base argument is ( locale_t) 0, a new locale object shall be created. It is unspecified whether the locale object pointed to by base shall be modified or freed and a new locale object created.

The category_mask argument specifies the locale categories to be set or modified. Values for category_mask shall be constructed by a bitwise-inclusive OR of the symbolic constants and or any of the other implementation-defined values defined in <locale.h>.

For each category with the corresponding bit set in category_mask the data from the locale named by locale shall be used. In the case of modifying an existing locale object, the data from the locale named by locale shall replace 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 shall be taken from the default locale.

The following preset values of locale are defined for all settings of category_mask:

"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 ; see XBD and .

If the base argument is not ( locale_t) 0 and the newlocale () function call succeeds, the contents of base are unspecified. Applications shall ensure that they stop using base as a locale object before calling newlocale (). If the function call fails and the base argument is not ( locale_t) 0, the contents of base shall remain valid and unchanged.

The results are undefined if the base argument is the special locale object LC_GLOBAL_LOCALE .

Upon successful completion, the newlocale () function shall return a handle which the caller may use on subsequent calls to duplocale(3), freelocale(3), and other functions taking a locale_t argument.

Upon failure, the newlocale () function shall return ( locale_t) 0 and set to indicate the error.

The newlocale () function shall fail if:

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

The newlocale () function may fail if:

The locale argument is not a valid string pointer.

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

#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 don t abort if this fails. In this case this
       simply used to unchanged locale object. */
    loc = new_loc;


...

The following example shows a code fragment to free a locale object created by newlocale ():

#include <locale.h>
...


/* Every locale object allocated with newlocale() should be
 * freed using freelocale():
 */


locale_t loc;


/* Get the locale. */


loc = newlocale (LC_CTYPE_MASK | LC_TIME_MASK, "locname", NULL);


/* ... Use the locale object ... */
...


/* Free the locale object resources. */
freelocale (loc);

Handles for locale objects created by the newlocale () function should be released by a corresponding call to freelocale(3).

The special locale object LC_GLOBAL_LOCALE must not be passed for the base argument, even when returned by the uselocale(3) function.

None.

None.

duplocale(3) , freelocale(3) , uselocale(3)

XBD V1_chap07(7) , V1_chap08(7) , <locale.h>

First released in Issue 7.

January 1, 2008 posix.fail