NEWLOCALE(3POSIX)

πŸ’– NEWLOCALE(3POSIX) πŸ’–

okay bestie, sit down, hydrate, because we're learning newlocale from the POSIX Programmer's Manual and honestly? it's giving locale-girlboss. πŸŽ€


✨ PROLOG ✨

real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β€” so like, consult the corresponding Linux manual page for the deets on Linux behavior β€” or the interface may not even be implemented on Linux at all. 😭 not every platform got the memo, iykyk.


🌸 NAME 🌸

newlocale β€” create or modify a locale object. that's it, that's the whole vibe. πŸ¦‹


✨ SYNOPSIS ✨

#include <locale.h>

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

look at her. three little args. iconic. πŸ‘‘


πŸ’œ DESCRIPTION πŸ’œ

So the newlocale() function shall create a brand-new locale object or modify an existing one. main character energy, two paths, one function. 🫢

If the base argument is (locale_t)0, a new locale object shall be created. And here's the spicy bit: it is unspecified whether the locale object pointed to by base shall be modified, or freed and a new locale object created. so don't be out here assuming, bestie. πŸ™ˆ

The category_mask argument specifies which locale categories to set or modify. Values for category_mask shall be 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
  • LC_MESSAGES_MASK

...or any of the implementation-defined mask values defined in <locale.h>. stack 'em, combine 'em, slay. πŸ’…

For each category with the corresponding bit set in category_mask, the data from the locale named by locale shall be used. And if you're 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. periodt.


πŸŽ€ preset locale values πŸŽ€

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. minimal. clean. no notes. ✨
  • "C" β€” equivalent to "POSIX". literally the same girl, different name tag. πŸ’–
  • "" β€” specifies an implementation-defined native environment. This corresponds to the value of the associated environment variables, LC_* and LANG; see the Base Definitions volume of POSIX.1‐2017, Chapter 7, Locale and Chapter 8, Environment Variables. 🌈

πŸ¦‹ the base rules (do NOT skip this) πŸ¦‹

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(). like, let her go, bestie. πŸ₯²

If the function call fails and the base argument is not (locale_t)0, the contents of base shall remain valid and unchanged. phew. 😌

⚠️ big red flag zone: the behavior is undefined if the base argument is the special locale object LC_GLOBAL_LOCALE, or is not a valid locale object handle and is not (locale_t)0. undefined behavior is not a personality trait, bestie. do not do this. πŸ’€

πŸ’… RETURN VALUE πŸ’…

Upon successful completion, the newlocale() function shall return a handle which the caller may use on subsequent calls to duplocale(), freelocale(), and other functions taking a locale_t argument. that handle is your new bestie, treat her right. 🫢

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


πŸ”₯ ERRORS πŸ”₯

The newlocale() function shall fail if:

  • ENOMEM β€” There is not enough memory available to create the locale object or load the locale data. the audacity of this machine. πŸ’€
  • EINVAL β€” The category_mask contains a bit that does not correspond to a valid category. random bit? not on my watch. πŸ™ˆ
  • ENOENT β€” For any of the categories in category_mask, the locale data is not available. she's not here, queen. 😭

The newlocale() function may fail if:

  • EINVAL β€” The locale argument is not a valid string pointer. yeah that's a no. βœ‹

The following sections are informative. (translation: cute to know, but not the rules-rules. πŸ’œ)


🌟 EXAMPLES 🌟

🌸 Constructing a Locale Object from Different Locales 🌸

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", (locale_t)0);
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;

...

see how she stacks loc as the base the second time? that's the whole trick, bestie. mixing categories like it's a fit check. πŸ‘‘

πŸŽ€ Freeing up a Locale Object πŸŽ€

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", (locale_t)0);

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

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

free your girl when you're done with her. it's called closure. πŸ’–


✨ APPLICATION USAGE ✨

Handles for locale objects created by the newlocale() function should either be released by a corresponding call to freelocale(), or be used as a base locale to another newlocale() call. no leaving her on read forever. πŸ₯²

The special locale object LC_GLOBAL_LOCALE must not be passed for the base argument β€” even when returned by the uselocale() function. i said what i said. 🚫


πŸ’œ RATIONALE πŸ’œ

None. (yes literally, the standard said "no thoughts, head empty" and honestly? respect. 😌)


πŸ¦‹ FUTURE DIRECTIONS πŸ¦‹

None. (same energy. the future is unknown but newlocale is not changing. ✨)


🌈 SEE ALSO 🌈

  • duplocale()
  • freelocale()
  • uselocale()
  • The Base Definitions volume of POSIX.1‐2017, Chapter 7, Locale, Chapter 8, Environment Variables, <locale.h>

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page, bestie!! πŸŽ€ this diva was brought to you by IEEE/The Open Group, 2017, and she answers to the name NEWLOCALE(3POSIX) β€” go forth, build locales, and always free your handles. mwah. πŸ’…βœ¨πŸ¦‹