Use symbolic constants instead of hardcoded digits

Add range check for setrunelocale since it can be called
directly.
Remove _startup_setlocale compatibility function

Should go into 2.2
This commit is contained in:
Andrey A. Chernov 1997-02-06 09:11:06 +00:00
parent b6b0d266cd
commit 63407d3487
4 changed files with 51 additions and 32 deletions

View File

@ -35,8 +35,8 @@
#include <unistd.h>
#include <sysexits.h>
#include "collate.h"
#include "setlocale.h"
char *_PathLocale;
int __collate_load_error = 1;
char __collate_version[STR_LEN];
u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];

View File

@ -54,6 +54,7 @@ static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
#include <string.h>
#include <unistd.h>
#include "collate.h"
#include "setlocale.h"
/*
* Category names for getenv()
@ -70,7 +71,7 @@ static char *categories[_LC_LAST] = {
/*
* Current locales for each category
*/
static char current_categories[_LC_LAST][32] = {
static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
"C",
"C",
"C",
@ -82,11 +83,10 @@ static char current_categories[_LC_LAST][32] = {
/*
* The locales we are going to try and load
*/
static char new_categories[_LC_LAST][32];
static char saved_categories[_LC_LAST][32];
static char new_categories[_LC_LAST][ENCODING_LEN + 1];
static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
static char current_locale_string[_LC_LAST * 33];
char *_PathLocale;
static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
static char *currentlocale __P((void));
static char *loadlocale __P((int));
@ -134,33 +134,34 @@ setlocale(category, locale)
if (!env || !*env)
env = "C";
(void) strncpy(new_categories[category], env, 31);
new_categories[category][31] = 0;
(void) strncpy(new_categories[category], env, ENCODING_LEN);
new_categories[category][ENCODING_LEN] = '\0';
if (category == LC_ALL) {
for (i = 1; i < _LC_LAST; ++i) {
if (!(env = getenv(categories[i])) || !*env)
env = new_categories[LC_ALL];
(void)strncpy(new_categories[i], env, 31);
new_categories[i][31] = 0;
(void)strncpy(new_categories[i], env, ENCODING_LEN);
new_categories[i][ENCODING_LEN] = '\0';
}
}
} else if (category != LC_ALL) {
(void)strncpy(new_categories[category], locale, 31);
new_categories[category][31] = 0;
(void)strncpy(new_categories[category], locale, ENCODING_LEN);
new_categories[category][ENCODING_LEN] = '\0';
} else {
if ((r = strchr(locale, '/')) == 0) {
if ((r = strchr(locale, '/')) == NULL) {
for (i = 1; i < _LC_LAST; ++i) {
(void)strncpy(new_categories[i], locale, 31);
new_categories[i][31] = 0;
(void)strncpy(new_categories[i], locale, ENCODING_LEN);
new_categories[i][ENCODING_LEN] = '\0';
}
} else {
for (i = 1; r[1] == '/'; ++r);
if (!r[1])
return (NULL); /* Hmm, just slashes... */
do {
len = r - locale > 31 ? 31 : r - locale;
len = r - locale > ENCODING_LEN ? ENCODING_LEN : r - locale;
(void)strncpy(new_categories[i], locale, len);
new_categories[i++][len] = 0;
new_categories[i][len] = '\0';
i++;
locale = r;
while (*locale == '/')
++locale;
@ -190,17 +191,6 @@ setlocale(category, locale)
return (currentlocale());
}
/* To be compatible with crt0 hack */
void
_startup_setlocale(category, locale)
int category;
const char *locale;
{
#ifndef XPG4
(void) setlocale(category, locale);
#endif
}
static char *
currentlocale()
{

View File

@ -0,0 +1,30 @@
/*
* Copyright (C) 1997 by Andrey A. Chernov, Moscow, Russia.
* All rights reserved.
*
* 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 AUTHOR ``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 REGENTS 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.
*/
#define ENCODING_LEN 31
char *_PathLocale;

View File

@ -40,8 +40,7 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char *_PathLocale;
#include "setlocale.h"
extern int _none_init __P((_RuneLocale *));
#ifdef XPG4
@ -72,7 +71,7 @@ _xpg4_setrunelocale(encoding)
char name[PATH_MAX];
_RuneLocale *rl;
if (!encoding)
if (!encoding || strlen(encoding) > ENCODING_LEN)
return(EFAULT);
/*
@ -85,7 +84,7 @@ _xpg4_setrunelocale(encoding)
if (!_PathLocale)
_PathLocale = _PATH_LOCALE;
/* Range checking not needed, encoding has fixed size */
/* Range checking not needed, encoding length already checked above */
(void) strcpy(name, _PathLocale);
(void) strcat(name, "/");
(void) strcat(name, encoding);