NetBSD/lib/libc/locale/localeio.c
ginsbach bcd54758b8 Add support for additional locale categories: LC_MESSAGES, LC_MONETARY,
LC_NUMERIC.

The code used to load LC_TIME was refactored in to a more general routine.
This common routine is now used to load LC_TIME along with the newly added
categories.

Changes discussed with/reviewed by christos.
2008-05-17 03:49:54 +00:00

155 lines
4.0 KiB
C

/* $NetBSD: localeio.c,v 1.1 2008/05/17 03:49:54 ginsbach Exp $ */
/*
* Copyright (c) 2008, The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Brian Ginsbach.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: localeio.c,v 1.1 2008/05/17 03:49:54 ginsbach Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <sys/localedef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <locale.h>
#include <paths.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "localeio.h"
int
__loadlocale(const char *name, size_t nstr, size_t nbytes, size_t localesize,
void *currentlocale, const void *defaultlocale)
{
int fd, i;
unsigned char **ap, *buf, *bp, *cp, *cbp, *ebp;
unsigned char ***locale;
struct stat st;
size_t bufsize;
_DIAGASSERT(name != NULL);
_DIAGASSERT(localesize != 0);
_DIAGASSERT(currentlocale != NULL);
_DIAGASSERT(defaultlocale != NULL);
if ((fd = open(name, O_RDONLY)) == -1)
return 0;
if ((fstat(fd, &st) == -1) || (st.st_size <= 0))
goto error1;
bufsize = localesize + (size_t)st.st_size;
if ((buf = malloc(bufsize)) == NULL)
goto error1;
bp = buf + localesize;
if (read(fd, bp, (size_t)st.st_size) != st.st_size)
goto error2;
ap = (unsigned char **)(void *)buf;
for (i = 0, ebp = buf + bufsize; i < nstr; i++) {
ap[i] = bp;
while (bp != ebp && *bp != '\n')
bp++;
if (bp == ebp)
goto error2;
*bp++ = '\0';
}
cp = buf + (sizeof(unsigned char *) * nstr);
for (i = 0, cbp = bp; i < nbytes; i++) {
int n;
while (bp != ebp && *bp != '\n')
bp++;
if (bp == ebp)
goto error2;
/* ignore overflow/underflow and bad characters */
n = (unsigned char)strtol((char *)cbp, NULL, 0);
cp[i] = (unsigned char)(n & CHAR_MAX);
cbp = bp;
}
locale = currentlocale;
if (*locale != defaultlocale)
free(*locale);
*locale = (unsigned char **)(void *)buf;
(void)close(fd);
return 1;
error2:
free(buf);
error1:
(void)close(fd);
return 0;
}
/*
* Convert a grouping sequence string into POSIX form.
*
* Examples: "3;3;-1" -> "\003\003\177\000"
* "3" -> "\003\000"
*/
static const char nogrouping[] = { CHAR_MAX, '\0' };
const char *
__convertgrouping(const char *str)
{
char *src, *dst;
_DIAGASSERT(str != NULL);
src = dst = __UNCONST(str);
while (*src != '\0') {
char *ep;
int n;
if ((n = strtol(src, &ep, 0)) >= CHAR_MAX ||
((*ep != ';') && (*ep != '\0')))
return nogrouping; /* invalid grouping string */
*dst++ = n & CHAR_MAX;
src = (*ep == ';')? ep + 1 : ep;
}
*dst = '\0';
return (dst == str)? nogrouping : str;
}