check_curl: enable internal cookie handling
[monitoring-plugins.git] / gl / setlocale_null-unlocked.c
blob0a86f0df7843f670cdc593e698f9725e98252e7c
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. */
19 #include <config.h>
21 /* Specification. */
22 #include "setlocale_null.h"
24 #include <errno.h>
25 #include <locale.h>
26 #include <string.h>
27 #if defined _WIN32 && !defined __CYGWIN__
28 # include <wchar.h>
29 #endif
31 /* Use the system's setlocale() function, not the gnulib override, here. */
32 #undef setlocale
34 const char *
35 setlocale_null_unlocked (int category)
37 const char *result = setlocale (category, NULL);
39 #ifdef __ANDROID__
40 if (result == NULL)
41 switch (category)
43 case LC_CTYPE:
44 case LC_NUMERIC:
45 case LC_TIME:
46 case LC_COLLATE:
47 case LC_MONETARY:
48 case LC_MESSAGES:
49 case LC_ALL:
50 case LC_PAPER:
51 case LC_NAME:
52 case LC_ADDRESS:
53 case LC_TELEPHONE:
54 case LC_MEASUREMENT:
55 result = "C";
56 break;
57 default:
58 break;
60 #endif
62 return result;
65 int
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);
74 if (result == NULL)
76 /* CATEGORY is invalid. */
77 if (bufsize > 0)
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. */
81 buf[0] = '\0';
82 return EINVAL;
84 else
86 size_t length = wcslen (result);
87 if (length < bufsize)
89 size_t i;
91 /* Convert wchar_t[] -> char[], assuming plain ASCII. */
92 for (i = 0; i <= length; i++)
93 buf[i] = result[i];
95 return 0;
97 else
99 if (bufsize > 0)
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. */
104 size_t i;
106 /* Convert wchar_t[] -> char[], assuming plain ASCII. */
107 for (i = 0; i < bufsize; i++)
108 buf[i] = result[i];
109 buf[bufsize - 1] = '\0';
111 return ERANGE;
114 #else
115 const char *result = setlocale_null_unlocked (category);
117 if (result == NULL)
119 /* CATEGORY is invalid. */
120 if (bufsize > 0)
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. */
124 buf[0] = '\0';
125 return EINVAL;
127 else
129 size_t length = strlen (result);
130 if (length < bufsize)
132 memcpy (buf, result, length + 1);
133 return 0;
135 else
137 if (bufsize > 0)
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';
145 return ERANGE;
148 #endif