1995-02-16 04:24:39 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
|
|
|
|
* at Electronni Visti IA, Kiev, Ukraine.
|
|
|
|
* 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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
2002-03-22 21:53:29 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-01-24 13:01:12 +00:00
|
|
|
#include "namespace.h"
|
2002-08-31 01:05:39 +00:00
|
|
|
#include <arpa/inet.h>
|
1995-02-16 04:24:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1995-02-17 16:36:12 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
1995-02-16 04:24:39 +00:00
|
|
|
#include <sysexits.h>
|
2001-01-24 13:01:12 +00:00
|
|
|
#include "un-namespace.h"
|
|
|
|
|
1995-02-16 04:24:39 +00:00
|
|
|
#include "collate.h"
|
1997-02-06 09:11:06 +00:00
|
|
|
#include "setlocale.h"
|
2002-08-08 05:51:54 +00:00
|
|
|
#include "ldpart.h"
|
1995-02-16 04:24:39 +00:00
|
|
|
|
2002-03-29 22:43:43 +00:00
|
|
|
#include "libc_private.h"
|
|
|
|
|
1995-02-16 04:24:39 +00:00
|
|
|
int __collate_load_error = 1;
|
1999-09-12 21:15:28 +00:00
|
|
|
int __collate_substitute_nontrivial;
|
2002-08-08 05:51:54 +00:00
|
|
|
|
1995-02-16 04:24:39 +00:00
|
|
|
u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
|
|
|
|
struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
|
2002-08-30 20:26:02 +00:00
|
|
|
struct __collate_st_chain_pri *__collate_chain_pri_table;
|
1995-02-16 04:24:39 +00:00
|
|
|
|
1996-09-14 03:00:32 +00:00
|
|
|
void __collate_err(int ex, const char *f) __dead2;
|
1995-02-17 16:36:12 +00:00
|
|
|
|
|
|
|
int
|
2002-08-08 05:51:54 +00:00
|
|
|
__collate_load_tables(const char *encoding)
|
1995-02-16 04:24:39 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2002-08-30 20:26:02 +00:00
|
|
|
int i, saverr, chains;
|
2002-08-31 01:05:39 +00:00
|
|
|
uint32_t u32;
|
2002-08-30 20:26:02 +00:00
|
|
|
char strbuf[STR_LEN], buf[PATH_MAX];
|
2002-08-13 14:55:17 +00:00
|
|
|
void *TMP_substitute_table, *TMP_char_pri_table, *TMP_chain_pri_table;
|
2002-08-08 05:51:54 +00:00
|
|
|
static char collate_encoding[ENCODING_LEN + 1];
|
1995-02-16 04:24:39 +00:00
|
|
|
|
2002-08-08 05:51:54 +00:00
|
|
|
/* 'encoding' must be already checked. */
|
|
|
|
if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
|
|
|
|
__collate_load_error = 1;
|
|
|
|
return (_LDP_CACHE);
|
1995-10-23 20:08:24 +00:00
|
|
|
}
|
2002-08-08 05:51:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the locale name is the same as our cache, use the cache.
|
|
|
|
*/
|
|
|
|
if (strcmp(encoding, collate_encoding) == 0) {
|
|
|
|
__collate_load_error = 0;
|
|
|
|
return (_LDP_CACHE);
|
1996-11-26 02:49:53 +00:00
|
|
|
}
|
2002-08-08 05:51:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Slurp the locale file into the cache.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* 'PathLocale' must be already set & checked. */
|
1997-02-05 19:17:10 +00:00
|
|
|
/* Range checking not needed, encoding has fixed size */
|
2002-08-07 18:02:45 +00:00
|
|
|
(void)strcpy(buf, _PathLocale);
|
|
|
|
(void)strcat(buf, "/");
|
|
|
|
(void)strcat(buf, encoding);
|
|
|
|
(void)strcat(buf, "/LC_COLLATE");
|
2002-08-08 05:51:54 +00:00
|
|
|
if ((fp = fopen(buf, "r")) == NULL)
|
|
|
|
return (_LDP_ERROR);
|
|
|
|
|
2002-08-30 20:26:02 +00:00
|
|
|
if (fread(strbuf, sizeof(strbuf), 1, fp) != 1) {
|
|
|
|
saverr = errno;
|
|
|
|
(void)fclose(fp);
|
|
|
|
errno = saverr;
|
|
|
|
return (_LDP_ERROR);
|
|
|
|
}
|
|
|
|
chains = -1;
|
|
|
|
if (strcmp(strbuf, COLLATE_VERSION) == 0)
|
|
|
|
chains = 0;
|
|
|
|
else if (strcmp(strbuf, COLLATE_VERSION1_1) == 0)
|
|
|
|
chains = 1;
|
|
|
|
if (chains < 0) {
|
|
|
|
(void)fclose(fp);
|
|
|
|
errno = EFTYPE;
|
|
|
|
return (_LDP_ERROR);
|
|
|
|
}
|
|
|
|
if (chains) {
|
2002-08-31 01:05:39 +00:00
|
|
|
if (fread(&u32, sizeof(u32), 1, fp) != 1) {
|
2002-08-30 20:26:02 +00:00
|
|
|
saverr = errno;
|
|
|
|
(void)fclose(fp);
|
|
|
|
errno = saverr;
|
|
|
|
return (_LDP_ERROR);
|
|
|
|
}
|
2002-08-31 01:05:39 +00:00
|
|
|
if ((chains = (int)ntohl(u32)) < 1) {
|
2002-08-30 20:26:02 +00:00
|
|
|
(void)fclose(fp);
|
|
|
|
errno = EFTYPE;
|
|
|
|
return (_LDP_ERROR);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
chains = TABLE_SIZE;
|
|
|
|
|
2002-08-08 05:51:54 +00:00
|
|
|
if ((TMP_substitute_table =
|
|
|
|
malloc(sizeof(__collate_substitute_table))) == NULL) {
|
2002-08-12 17:14:04 +00:00
|
|
|
saverr = errno;
|
2002-08-08 05:51:54 +00:00
|
|
|
(void)fclose(fp);
|
2002-08-12 17:14:04 +00:00
|
|
|
errno = saverr;
|
2002-08-08 05:51:54 +00:00
|
|
|
return (_LDP_ERROR);
|
1995-10-23 20:08:24 +00:00
|
|
|
}
|
2002-08-08 05:51:54 +00:00
|
|
|
if ((TMP_char_pri_table =
|
|
|
|
malloc(sizeof(__collate_char_pri_table))) == NULL) {
|
2002-08-12 17:14:04 +00:00
|
|
|
saverr = errno;
|
2002-08-08 05:51:54 +00:00
|
|
|
free(TMP_substitute_table);
|
2002-08-07 18:02:45 +00:00
|
|
|
(void)fclose(fp);
|
2002-08-12 17:14:04 +00:00
|
|
|
errno = saverr;
|
2002-08-08 05:51:54 +00:00
|
|
|
return (_LDP_ERROR);
|
1996-10-15 21:53:23 +00:00
|
|
|
}
|
2002-08-08 05:51:54 +00:00
|
|
|
if ((TMP_chain_pri_table =
|
2002-08-30 20:26:02 +00:00
|
|
|
malloc(sizeof(*__collate_chain_pri_table) * chains)) == NULL) {
|
2002-08-12 17:14:04 +00:00
|
|
|
saverr = errno;
|
2002-08-08 05:51:54 +00:00
|
|
|
free(TMP_substitute_table);
|
|
|
|
free(TMP_char_pri_table);
|
|
|
|
(void)fclose(fp);
|
2002-08-12 17:14:04 +00:00
|
|
|
errno = saverr;
|
2002-08-08 05:51:54 +00:00
|
|
|
return (_LDP_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FREAD(a, b, c, d) \
|
|
|
|
{ \
|
|
|
|
if (fread(a, b, c, d) != c) { \
|
|
|
|
saverr = errno; \
|
|
|
|
free(TMP_substitute_table); \
|
|
|
|
free(TMP_char_pri_table); \
|
|
|
|
free(TMP_chain_pri_table); \
|
|
|
|
(void)fclose(d); \
|
|
|
|
errno = saverr; \
|
|
|
|
return (_LDP_ERROR); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
FREAD(TMP_substitute_table, sizeof(__collate_substitute_table), 1, fp);
|
|
|
|
FREAD(TMP_char_pri_table, sizeof(__collate_char_pri_table), 1, fp);
|
2002-08-30 20:26:02 +00:00
|
|
|
FREAD(TMP_chain_pri_table,
|
2002-08-30 20:39:53 +00:00
|
|
|
sizeof(*__collate_chain_pri_table), chains, fp);
|
2002-08-07 18:02:45 +00:00
|
|
|
(void)fclose(fp);
|
2002-08-08 05:51:54 +00:00
|
|
|
|
|
|
|
(void)strcpy(collate_encoding, encoding);
|
2002-08-13 14:55:17 +00:00
|
|
|
if (__collate_substitute_table_ptr != NULL)
|
|
|
|
free(__collate_substitute_table_ptr);
|
|
|
|
__collate_substitute_table_ptr = TMP_substitute_table;
|
|
|
|
if (__collate_char_pri_table_ptr != NULL)
|
|
|
|
free(__collate_char_pri_table_ptr);
|
|
|
|
__collate_char_pri_table_ptr = TMP_char_pri_table;
|
2002-08-30 20:26:02 +00:00
|
|
|
if (__collate_chain_pri_table != NULL)
|
|
|
|
free(__collate_chain_pri_table);
|
|
|
|
__collate_chain_pri_table = TMP_chain_pri_table;
|
1999-09-12 21:15:28 +00:00
|
|
|
|
|
|
|
__collate_substitute_nontrivial = 0;
|
|
|
|
for (i = 0; i < UCHAR_MAX + 1; i++) {
|
|
|
|
if (__collate_substitute_table[i][0] != i ||
|
|
|
|
__collate_substitute_table[i][1] != 0) {
|
|
|
|
__collate_substitute_nontrivial = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-08-08 05:51:54 +00:00
|
|
|
__collate_load_error = 0;
|
1999-09-12 21:15:28 +00:00
|
|
|
|
2002-08-08 05:51:54 +00:00
|
|
|
return (_LDP_LOADED);
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u_char *
|
|
|
|
__collate_substitute(s)
|
|
|
|
const u_char *s;
|
|
|
|
{
|
1999-09-12 19:42:38 +00:00
|
|
|
int dest_len, len, nlen;
|
1995-02-16 04:24:39 +00:00
|
|
|
int delta = strlen(s);
|
1995-02-17 16:36:12 +00:00
|
|
|
u_char *dest_str = NULL;
|
1995-02-16 04:24:39 +00:00
|
|
|
|
2002-08-07 18:02:45 +00:00
|
|
|
if (s == NULL || *s == '\0')
|
|
|
|
return (__collate_strdup(""));
|
1999-09-12 19:42:38 +00:00
|
|
|
delta += delta / 8;
|
|
|
|
dest_str = malloc(dest_len = delta);
|
2002-08-07 18:02:45 +00:00
|
|
|
if (dest_str == NULL)
|
1999-09-12 19:42:38 +00:00
|
|
|
__collate_err(EX_OSERR, __FUNCTION__);
|
|
|
|
len = 0;
|
2002-08-07 18:02:45 +00:00
|
|
|
while (*s) {
|
1999-09-12 19:42:38 +00:00
|
|
|
nlen = len + strlen(__collate_substitute_table[*s]);
|
|
|
|
if (dest_len <= nlen) {
|
|
|
|
dest_str = reallocf(dest_str, dest_len = nlen + delta);
|
2002-08-07 18:02:45 +00:00
|
|
|
if (dest_str == NULL)
|
1995-02-17 16:36:12 +00:00
|
|
|
__collate_err(EX_OSERR, __FUNCTION__);
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
2002-08-07 18:02:45 +00:00
|
|
|
(void)strcpy(dest_str + len, __collate_substitute_table[*s++]);
|
1999-09-12 19:42:38 +00:00
|
|
|
len = nlen;
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
2002-08-07 18:02:45 +00:00
|
|
|
return (dest_str);
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
__collate_lookup(t, len, prim, sec)
|
1999-09-12 21:15:28 +00:00
|
|
|
const u_char *t;
|
1995-02-16 04:24:39 +00:00
|
|
|
int *len, *prim, *sec;
|
|
|
|
{
|
|
|
|
struct __collate_st_chain_pri *p2;
|
|
|
|
|
|
|
|
*len = 1;
|
|
|
|
*prim = *sec = 0;
|
2002-08-30 20:26:02 +00:00
|
|
|
for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++) {
|
|
|
|
if (*t == p2->str[0] &&
|
|
|
|
strncmp(t, p2->str, strlen(p2->str)) == 0) {
|
1995-02-16 04:24:39 +00:00
|
|
|
*len = strlen(p2->str);
|
|
|
|
*prim = p2->prim;
|
|
|
|
*sec = p2->sec;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*prim = __collate_char_pri_table[*t].prim;
|
|
|
|
*sec = __collate_char_pri_table[*t].sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_char *
|
|
|
|
__collate_strdup(s)
|
|
|
|
u_char *s;
|
|
|
|
{
|
1995-02-17 16:36:12 +00:00
|
|
|
u_char *t = strdup(s);
|
1995-02-16 04:24:39 +00:00
|
|
|
|
1995-02-17 16:36:12 +00:00
|
|
|
if (t == NULL)
|
|
|
|
__collate_err(EX_OSERR, __FUNCTION__);
|
2002-08-07 18:02:45 +00:00
|
|
|
return (t);
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
1996-09-14 03:00:32 +00:00
|
|
|
void
|
|
|
|
__collate_err(int ex, const char *f)
|
1995-02-17 16:36:12 +00:00
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
int serrno = errno;
|
|
|
|
|
2002-03-29 22:43:43 +00:00
|
|
|
s = _getprogname();
|
Simplify sytem call renaming. Instead of _foo() <-- _libc_foo <-- foo(),
just use _foo() <-- foo(). In the case of a libpthread that doesn't do
call conversion (such as linuxthreads and our upcoming libpthread), this
is adequate. In the case of libc_r, we still need three names, which are
now _thread_sys_foo() <-- _foo() <-- foo().
Convert all internal libc usage of: aio_suspend(), close(), fsync(), msync(),
nanosleep(), open(), fcntl(), read(), and write() to _foo() instead of foo().
Remove all internal libc usage of: creat(), pause(), sleep(), system(),
tcdrain(), wait(), and waitpid().
Make thread cancellation fully POSIX-compliant.
Suggested by: deischen
2000-01-27 23:07:25 +00:00
|
|
|
_write(STDERR_FILENO, s, strlen(s));
|
|
|
|
_write(STDERR_FILENO, ": ", 2);
|
1995-02-17 16:36:12 +00:00
|
|
|
s = f;
|
Simplify sytem call renaming. Instead of _foo() <-- _libc_foo <-- foo(),
just use _foo() <-- foo(). In the case of a libpthread that doesn't do
call conversion (such as linuxthreads and our upcoming libpthread), this
is adequate. In the case of libc_r, we still need three names, which are
now _thread_sys_foo() <-- _foo() <-- foo().
Convert all internal libc usage of: aio_suspend(), close(), fsync(), msync(),
nanosleep(), open(), fcntl(), read(), and write() to _foo() instead of foo().
Remove all internal libc usage of: creat(), pause(), sleep(), system(),
tcdrain(), wait(), and waitpid().
Make thread cancellation fully POSIX-compliant.
Suggested by: deischen
2000-01-27 23:07:25 +00:00
|
|
|
_write(STDERR_FILENO, s, strlen(s));
|
|
|
|
_write(STDERR_FILENO, ": ", 2);
|
1995-02-17 16:36:12 +00:00
|
|
|
s = strerror(serrno);
|
Simplify sytem call renaming. Instead of _foo() <-- _libc_foo <-- foo(),
just use _foo() <-- foo(). In the case of a libpthread that doesn't do
call conversion (such as linuxthreads and our upcoming libpthread), this
is adequate. In the case of libc_r, we still need three names, which are
now _thread_sys_foo() <-- _foo() <-- foo().
Convert all internal libc usage of: aio_suspend(), close(), fsync(), msync(),
nanosleep(), open(), fcntl(), read(), and write() to _foo() instead of foo().
Remove all internal libc usage of: creat(), pause(), sleep(), system(),
tcdrain(), wait(), and waitpid().
Make thread cancellation fully POSIX-compliant.
Suggested by: deischen
2000-01-27 23:07:25 +00:00
|
|
|
_write(STDERR_FILENO, s, strlen(s));
|
|
|
|
_write(STDERR_FILENO, "\n", 1);
|
1995-02-17 16:36:12 +00:00
|
|
|
exit(ex);
|
|
|
|
}
|
|
|
|
|
1995-02-16 04:24:39 +00:00
|
|
|
#ifdef COLLATE_DEBUG
|
|
|
|
void
|
|
|
|
__collate_print_tables()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct __collate_st_chain_pri *p2;
|
|
|
|
|
|
|
|
printf("Substitute table:\n");
|
|
|
|
for (i = 0; i < UCHAR_MAX + 1; i++)
|
1996-10-23 15:35:46 +00:00
|
|
|
if (i != *__collate_substitute_table[i])
|
1995-02-16 04:24:39 +00:00
|
|
|
printf("\t'%c' --> \"%s\"\n", i,
|
|
|
|
__collate_substitute_table[i]);
|
|
|
|
printf("Chain priority table:\n");
|
2002-08-30 20:39:53 +00:00
|
|
|
for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++)
|
|
|
|
printf("\t\"%s\" : %d %d\n", p2->str, p2->prim, p2->sec);
|
1995-02-16 04:24:39 +00:00
|
|
|
printf("Char priority table:\n");
|
|
|
|
for (i = 0; i < UCHAR_MAX + 1; i++)
|
|
|
|
printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
|
|
|
|
__collate_char_pri_table[i].sec);
|
|
|
|
}
|
|
|
|
#endif
|