3 <<duplocale>>---duplicate a locale object
13 locale_t duplocale(locale_t <[locobj]>);
15 locale_t _duplocale_r(void *<[reent]>, locale_t <[locobj]>);
18 The <<duplocale>> function shall create a duplicate copy of the locale
19 object referenced by the <[locobj]> argument.
21 If the <[locobj]> argument is LC_GLOBAL_LOCALE, duplocale() shall create
22 a new locale object containing a copy of the global locale determined by
23 the setlocale() function.
25 The behavior is undefined if the <[locobj]> argument is not a valid locale
29 Upon successful completion, the <<duplocale>> function shall return a
30 handle for a new locale object. Otherwise <<duplocale>> shall return
31 (locale_t) <<0>> and set errno to indicate the error.
34 <<duplocale>> is POSIX-1.2008.
40 #include "setlocale.h"
43 _duplocale_r (struct _reent
*p
, struct __locale_t
*locobj
)
45 struct __locale_t tmp_locale
, *new_locale
;
49 return __get_C_locale ();
50 #else /* _MB_CAPABLE */
51 /* LC_GLOBAL_LOCALE denotes the global locale. */
52 if (locobj
== LC_GLOBAL_LOCALE
)
53 locobj
= __get_global_locale ();
54 /* The "C" locale is used statically, never copied. */
55 else if (locobj
== __get_C_locale ())
56 return __get_C_locale ();
57 /* Copy locale content. */
59 #ifdef __HAVE_LOCALE_INFO__
60 for (i
= 1; i
< _LC_LAST
; ++i
)
61 if (locobj
->lc_cat
[i
].buf
)
63 /* If the object is not a "C" locale category, copy it. Just call
64 __loadlocale. It knows what to do to replicate the category. */
65 tmp_locale
.lc_cat
[i
].ptr
= NULL
;
66 tmp_locale
.lc_cat
[i
].buf
= NULL
;
67 tmp_locale
.categories
[i
][0] = '\0'; /* __loadlocale tests this! */
68 if (!__loadlocale (&tmp_locale
, i
, locobj
->categories
[i
]))
71 #endif /* __HAVE_LOCALE_INFO__ */
72 /* Allocate new locale_t. */
73 new_locale
= (struct __locale_t
*) _calloc_r (p
, 1, sizeof *new_locale
);
77 *new_locale
= tmp_locale
;
81 /* An error occured while we had already (potentially) allocated memory.
82 Free memory and return NULL. errno is supposed to be set already. */
83 #ifdef __HAVE_LOCALE_INFO__
85 if (tmp_locale
.lc_cat
[i
].buf
)
87 _free_r (p
, (void *) tmp_locale
.lc_cat
[i
].ptr
);
88 _free_r (p
, tmp_locale
.lc_cat
[i
].buf
);
90 #endif /* __HAVE_LOCALE_INFO__ */
93 #endif /* _MB_CAPABLE */
98 duplocale (struct __locale_t
*locobj
)
100 return _duplocale_r (_REENT
, locobj
);