1 /* Query the name of the current global locale, without locking.
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2019. */
22 #include "setlocale_null.h"
27 #if defined _WIN32 && !defined __CYGWIN__
31 /* Use the system's setlocale() function, not the gnulib override, here. */
35 setlocale_null_unlocked (int category
)
37 const char *result
= setlocale (category
, NULL
);
66 setlocale_null_r_unlocked (int category
, char *buf
, size_t bufsize
)
68 #if defined _WIN32 && !defined __CYGWIN__ && defined _MSC_VER
69 /* On native Windows, nowadays, the setlocale() implementation is based
70 on _wsetlocale() and uses malloc() for the result. We are better off
71 using _wsetlocale() directly. */
72 const wchar_t *result
= _wsetlocale (category
, NULL
);
76 /* CATEGORY is invalid. */
78 /* Return an empty string in BUF.
79 This is a convenience for callers that don't want to write explicit
80 code for handling EINVAL. */
86 size_t length
= wcslen (result
);
91 /* Convert wchar_t[] -> char[], assuming plain ASCII. */
92 for (i
= 0; i
<= length
; i
++)
101 /* Return a truncated result in BUF.
102 This is a convenience for callers that don't want to write
103 explicit code for handling ERANGE. */
106 /* Convert wchar_t[] -> char[], assuming plain ASCII. */
107 for (i
= 0; i
< bufsize
; i
++)
109 buf
[bufsize
- 1] = '\0';
115 const char *result
= setlocale_null_unlocked (category
);
119 /* CATEGORY is invalid. */
121 /* Return an empty string in BUF.
122 This is a convenience for callers that don't want to write explicit
123 code for handling EINVAL. */
129 size_t length
= strlen (result
);
130 if (length
< bufsize
)
132 memcpy (buf
, result
, length
+ 1);
139 /* Return a truncated result in BUF.
140 This is a convenience for callers that don't want to write
141 explicit code for handling ERANGE. */
142 memcpy (buf
, result
, bufsize
- 1);
143 buf
[bufsize
- 1] = '\0';