Fix the MS-Windows build as followup to Gnulib regex import

* lib-src/ntlib.c (nl_langinfo): New function.  (Bug#32194)
This commit is contained in:
Eli Zaretskii 2018-08-06 17:50:55 +03:00
parent 9c022a488b
commit bedf905dd3

View file

@ -31,6 +31,10 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <ctype.h>
#include <sys/timeb.h>
#include <mbstring.h>
#include <locale.h>
#include <nl_types.h>
#include <langinfo.h>
#include "ntlib.h"
@ -423,3 +427,66 @@ sys_open (const char * path, int oflag, int mode)
{
return _open (path, oflag, mode);
}
/* Emulation of nl_langinfo that supports only CODESET.
Used in Gnulib regex.c. */
char *
nl_langinfo (nl_item item)
{
switch (item)
{
case CODESET:
{
/* Shamelessly stolen from Gnulib's nl_langinfo.c, modulo
CPP directives. */
static char buf[2 + 10 + 1];
char const *locale = setlocale (LC_CTYPE, NULL);
char *codeset = buf;
size_t codesetlen;
codeset[0] = '\0';
if (locale && locale[0])
{
/* If the locale name contains an encoding after the
dot, return it. */
char *dot = strchr (locale, '.');
if (dot)
{
/* Look for the possible @... trailer and remove it,
if any. */
char *codeset_start = dot + 1;
char const *modifier = strchr (codeset_start, '@');
if (! modifier)
codeset = codeset_start;
else
{
codesetlen = modifier - codeset_start;
if (codesetlen < sizeof buf)
{
codeset = memcpy (buf, codeset_start, codesetlen);
codeset[codesetlen] = '\0';
}
}
}
}
/* If setlocale is successful, it returns the number of the
codepage, as a string. Otherwise, fall back on Windows
API GetACP, which returns the locale's codepage as a
number (although this doesn't change according to what
the 'setlocale' call specified). Either way, prepend
"CP" to make it a valid codeset name. */
codesetlen = strlen (codeset);
if (0 < codesetlen && codesetlen < sizeof buf - 2)
memmove (buf + 2, codeset, codesetlen + 1);
else
sprintf (buf + 2, "%u", GetACP ());
codeset = memcpy (buf, "CP", 2);
return codeset;
}
default:
return (char *) "";
}
}