2000-04-15 04:41:27 +00:00
|
|
|
/*
|
|
|
|
* tc.str.c: Short string package
|
|
|
|
* This has been a lesson of how to write buggy code!
|
|
|
|
*/
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1991 The Regents of the University of California.
|
|
|
|
* 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.
|
2002-07-24 16:23:10 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2000-04-15 04:41:27 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 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.
|
|
|
|
*/
|
|
|
|
#include "sh.h"
|
|
|
|
|
2012-02-20 00:53:59 +00:00
|
|
|
#include <assert.h>
|
2005-04-24 19:41:08 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
2000-04-15 04:41:27 +00:00
|
|
|
#define MALLOC_INCR 128
|
2005-04-24 19:41:08 +00:00
|
|
|
#ifdef WIDE_STRINGS
|
|
|
|
#define MALLOC_SURPLUS MB_LEN_MAX /* Space for one multibyte character */
|
|
|
|
#else
|
|
|
|
#define MALLOC_SURPLUS 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WIDE_STRINGS
|
|
|
|
size_t
|
2012-02-20 00:53:59 +00:00
|
|
|
one_mbtowc(Char *pwc, const char *s, size_t n)
|
2005-04-24 19:41:08 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = rt_mbtowc(pwc, s, n);
|
|
|
|
if (len == -1) {
|
2009-07-10 21:47:20 +00:00
|
|
|
reset_mbtowc();
|
2005-04-24 19:41:08 +00:00
|
|
|
*pwc = (unsigned char)*s | INVALID_BYTE;
|
|
|
|
}
|
|
|
|
if (len <= 0)
|
|
|
|
len = 1;
|
|
|
|
return len;
|
|
|
|
}
|
2000-04-15 04:41:27 +00:00
|
|
|
|
2005-04-24 19:41:08 +00:00
|
|
|
size_t
|
2012-02-20 00:53:59 +00:00
|
|
|
one_wctomb(char *s, Char wchar)
|
2005-04-24 19:41:08 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
2017-03-18 21:41:53 +00:00
|
|
|
#if INVALID_BYTE != 0
|
|
|
|
if ((wchar & INVALID_BYTE) == INVALID_BYTE) { /* wchar >= INVALID_BYTE */
|
|
|
|
/* invalid char
|
|
|
|
* exmaple)
|
|
|
|
* if wchar = f0000090(=90|INVALID_BYTE), then *s = ffffff90 */
|
|
|
|
*s = (char)wchar;
|
2005-04-24 19:41:08 +00:00
|
|
|
len = 1;
|
2017-03-18 21:41:53 +00:00
|
|
|
#else
|
|
|
|
if (wchar & (CHAR & INVALID_BYTE)) {
|
|
|
|
s[0] = wchar & (CHAR & 0xFF);
|
|
|
|
len = 1;
|
|
|
|
#endif
|
2005-04-24 19:41:08 +00:00
|
|
|
} else {
|
2017-03-18 21:41:53 +00:00
|
|
|
#if INVALID_BYTE != 0
|
|
|
|
wchar &= MAX_UTF32;
|
|
|
|
#else
|
|
|
|
wchar &= CHAR;
|
|
|
|
#endif
|
2012-02-20 00:53:59 +00:00
|
|
|
#ifdef UTF16_STRINGS
|
|
|
|
if (wchar >= 0x10000) {
|
|
|
|
/* UTF-16 systems can't handle these values directly in calls to
|
|
|
|
wctomb. Convert value to UTF-16 surrogate and call wcstombs to
|
|
|
|
convert the "string" to the correct multibyte representation,
|
|
|
|
if any. */
|
|
|
|
wchar_t ws[3];
|
|
|
|
wchar -= 0x10000;
|
|
|
|
ws[0] = 0xd800 | (wchar >> 10);
|
|
|
|
ws[1] = 0xdc00 | (wchar & 0x3ff);
|
|
|
|
ws[2] = 0;
|
|
|
|
/* The return value of wcstombs excludes the trailing 0, so len is
|
|
|
|
the correct number of multibytes for the Unicode char. */
|
|
|
|
len = wcstombs (s, ws, MB_CUR_MAX + 1);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
len = wctomb(s, (wchar_t) wchar);
|
2005-04-24 19:41:08 +00:00
|
|
|
if (len == -1)
|
|
|
|
s[0] = wchar;
|
|
|
|
if (len <= 0)
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
2007-03-11 22:33:41 +00:00
|
|
|
|
2005-04-24 19:41:08 +00:00
|
|
|
int
|
2012-02-20 00:53:59 +00:00
|
|
|
rt_mbtowc(Char *pwc, const char *s, size_t n)
|
2005-04-24 19:41:08 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char back[MB_LEN_MAX];
|
2012-02-20 00:53:59 +00:00
|
|
|
wchar_t tmp;
|
|
|
|
#if defined(UTF16_STRINGS) && defined(HAVE_MBRTOWC)
|
|
|
|
# if defined(AUTOSET_KANJI)
|
|
|
|
static mbstate_t mb_zero, mb;
|
|
|
|
/*
|
|
|
|
* Workaround the Shift-JIS endcoding that translates unshifted 7 bit ASCII!
|
|
|
|
*/
|
|
|
|
if (!adrof(STRnokanji) && n && pwc && s && (*s == '\\' || *s == '~') &&
|
|
|
|
!memcmp(&mb, &mb_zero, sizeof(mb)))
|
|
|
|
{
|
|
|
|
*pwc = *s;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
# else
|
|
|
|
mbstate_t mb;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
memset (&mb, 0, sizeof mb);
|
|
|
|
ret = mbrtowc(&tmp, s, n, &mb);
|
|
|
|
#else
|
|
|
|
ret = mbtowc(&tmp, s, n);
|
|
|
|
#endif
|
|
|
|
if (ret > 0) {
|
|
|
|
*pwc = tmp;
|
|
|
|
#if defined(UTF16_STRINGS) && defined(HAVE_MBRTOWC)
|
|
|
|
if (tmp >= 0xd800 && tmp <= 0xdbff) {
|
|
|
|
/* UTF-16 surrogate pair. Fetch second half and compute
|
|
|
|
UTF-32 value. Dispense with the inverse test in this case. */
|
|
|
|
size_t n2 = mbrtowc(&tmp, s + ret, n - ret, &mb);
|
|
|
|
if (n2 == 0 || n2 == (size_t)-1 || n2 == (size_t)-2)
|
|
|
|
ret = -1;
|
|
|
|
else {
|
|
|
|
*pwc = (((*pwc & 0x3ff) << 10) | (tmp & 0x3ff)) + 0x10000;
|
|
|
|
ret += n2;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (wctomb(back, *pwc) != ret || memcmp(s, back, ret) != 0)
|
|
|
|
ret = -1;
|
2005-04-24 19:41:08 +00:00
|
|
|
|
2012-02-20 00:53:59 +00:00
|
|
|
} else if (ret == -2)
|
2005-04-24 19:41:08 +00:00
|
|
|
ret = -1;
|
2012-02-20 00:53:59 +00:00
|
|
|
else if (ret == 0)
|
|
|
|
*pwc = '\0';
|
|
|
|
|
2005-04-24 19:41:08 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2007-03-11 22:33:41 +00:00
|
|
|
#endif
|
2005-04-24 19:41:08 +00:00
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
#ifdef SHORT_STRINGS
|
2000-04-15 04:41:27 +00:00
|
|
|
Char **
|
2007-03-11 22:33:41 +00:00
|
|
|
blk2short(char **src)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
size_t n;
|
2005-04-24 19:41:08 +00:00
|
|
|
Char **sdst, **dst;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Count
|
|
|
|
*/
|
|
|
|
for (n = 0; src[n] != NULL; n++)
|
|
|
|
continue;
|
2007-03-11 22:33:41 +00:00
|
|
|
sdst = dst = xmalloc((n + 1) * sizeof(Char *));
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
for (; *src != NULL; src++)
|
|
|
|
*dst++ = SAVE(*src);
|
|
|
|
*dst = NULL;
|
|
|
|
return (sdst);
|
|
|
|
}
|
|
|
|
|
|
|
|
char **
|
2007-03-11 22:33:41 +00:00
|
|
|
short2blk(Char **src)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
size_t n;
|
2005-04-24 19:41:08 +00:00
|
|
|
char **sdst, **dst;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Count
|
|
|
|
*/
|
|
|
|
for (n = 0; src[n] != NULL; n++)
|
|
|
|
continue;
|
2007-03-11 22:33:41 +00:00
|
|
|
sdst = dst = xmalloc((n + 1) * sizeof(char *));
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
for (; *src != NULL; src++)
|
|
|
|
*dst++ = strsave(short2str(*src));
|
|
|
|
*dst = NULL;
|
|
|
|
return (sdst);
|
|
|
|
}
|
|
|
|
|
|
|
|
Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
str2short(const char *src)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2007-03-11 22:33:41 +00:00
|
|
|
static struct Strbuf buf; /* = Strbuf_INIT; */
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
if (src == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
buf.len = 0;
|
|
|
|
while (*src) {
|
|
|
|
Char wc;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
src += one_mbtowc(&wc, src, MB_LEN_MAX);
|
|
|
|
Strbuf_append1(&buf, wc);
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
2007-03-11 22:33:41 +00:00
|
|
|
Strbuf_terminate(&buf);
|
|
|
|
return buf.s;
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2007-03-11 22:33:41 +00:00
|
|
|
short2str(const Char *src)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
static char *sdst = NULL;
|
|
|
|
static size_t dstsize = 0;
|
2005-04-24 19:41:08 +00:00
|
|
|
char *dst, *edst;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
if (src == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (sdst == NULL) {
|
|
|
|
dstsize = MALLOC_INCR;
|
2007-03-11 22:33:41 +00:00
|
|
|
sdst = xmalloc((dstsize + MALLOC_SURPLUS) * sizeof(char));
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
|
|
|
dst = sdst;
|
|
|
|
edst = &dst[dstsize];
|
|
|
|
while (*src) {
|
2017-03-18 21:41:53 +00:00
|
|
|
dst += one_wctomb(dst, *src);
|
2005-04-24 19:41:08 +00:00
|
|
|
src++;
|
|
|
|
if (dst >= edst) {
|
2009-07-10 21:47:20 +00:00
|
|
|
char *wdst = dst;
|
|
|
|
char *wedst = edst;
|
|
|
|
|
2000-04-15 04:41:27 +00:00
|
|
|
dstsize += MALLOC_INCR;
|
2007-03-11 22:33:41 +00:00
|
|
|
sdst = xrealloc(sdst, (dstsize + MALLOC_SURPLUS) * sizeof(char));
|
2000-04-15 04:41:27 +00:00
|
|
|
edst = &sdst[dstsize];
|
|
|
|
dst = &edst[-MALLOC_INCR];
|
2009-07-10 21:47:20 +00:00
|
|
|
while (wdst > wedst) {
|
|
|
|
dst++;
|
|
|
|
wdst--;
|
|
|
|
}
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*dst = 0;
|
|
|
|
return (sdst);
|
|
|
|
}
|
|
|
|
|
2012-02-20 00:53:59 +00:00
|
|
|
#if !defined (WIDE_STRINGS) || defined (UTF16_STRINGS)
|
2000-04-15 04:41:27 +00:00
|
|
|
Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strcpy(Char *dst, const Char *src)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2005-04-24 19:41:08 +00:00
|
|
|
Char *sdst;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
sdst = dst;
|
|
|
|
while ((*dst++ = *src++) != '\0')
|
|
|
|
continue;
|
|
|
|
return (sdst);
|
|
|
|
}
|
|
|
|
|
|
|
|
Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strncpy(Char *dst, const Char *src, size_t n)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2005-04-24 19:41:08 +00:00
|
|
|
Char *sdst;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return(dst);
|
|
|
|
|
|
|
|
sdst = dst;
|
2021-05-12 06:01:23 +00:00
|
|
|
do
|
2000-04-15 04:41:27 +00:00
|
|
|
if ((*dst++ = *src++) == '\0') {
|
|
|
|
while (--n != 0)
|
|
|
|
*dst++ = '\0';
|
|
|
|
return(sdst);
|
|
|
|
}
|
|
|
|
while (--n != 0);
|
|
|
|
return (sdst);
|
|
|
|
}
|
|
|
|
|
|
|
|
Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strcat(Char *dst, const Char *src)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2007-03-11 22:33:41 +00:00
|
|
|
Strcpy(Strend(dst), src);
|
|
|
|
return dst;
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NOTUSED
|
|
|
|
Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strncat(Char *dst, const Char *src, size_t n)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2005-04-24 19:41:08 +00:00
|
|
|
Char *sdst;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
2021-05-12 06:01:23 +00:00
|
|
|
if (n == 0)
|
2000-04-15 04:41:27 +00:00
|
|
|
return (dst);
|
|
|
|
|
|
|
|
sdst = dst;
|
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
while (*dst)
|
|
|
|
dst++;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
2021-05-12 06:01:23 +00:00
|
|
|
do
|
2000-04-15 04:41:27 +00:00
|
|
|
if ((*dst++ = *src++) == '\0')
|
|
|
|
return(sdst);
|
|
|
|
while (--n != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*dst = '\0';
|
|
|
|
return (sdst);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strchr(const Char *str, int ch)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
do
|
|
|
|
if (*str == ch)
|
2005-04-24 19:41:08 +00:00
|
|
|
return ((Char *)(intptr_t)str);
|
2000-04-15 04:41:27 +00:00
|
|
|
while (*str++);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strrchr(const Char *str, int ch)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2005-04-24 19:41:08 +00:00
|
|
|
const Char *rstr;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
rstr = NULL;
|
|
|
|
do
|
|
|
|
if (*str == ch)
|
|
|
|
rstr = str;
|
|
|
|
while (*str++);
|
2005-04-24 19:41:08 +00:00
|
|
|
return ((Char *)(intptr_t)rstr);
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strlen(const Char *str)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2005-04-24 19:41:08 +00:00
|
|
|
size_t n;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
for (n = 0; *str++; n++)
|
|
|
|
continue;
|
|
|
|
return (n);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strcmp(const Char *str1, const Char *str2)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
for (; *str1 && *str1 == *str2; str1++, str2++)
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* The following case analysis is necessary so that characters which look
|
|
|
|
* negative collate low against normal characters but high against the
|
|
|
|
* end-of-string NUL.
|
|
|
|
*/
|
|
|
|
if (*str1 == '\0' && *str2 == '\0')
|
|
|
|
return (0);
|
|
|
|
else if (*str1 == '\0')
|
|
|
|
return (-1);
|
|
|
|
else if (*str2 == '\0')
|
|
|
|
return (1);
|
|
|
|
else
|
|
|
|
return (*str1 - *str2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strncmp(const Char *str1, const Char *str2, size_t n)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
if (n == 0)
|
|
|
|
return (0);
|
|
|
|
do {
|
|
|
|
if (*str1 != *str2) {
|
|
|
|
/*
|
2021-05-12 06:01:23 +00:00
|
|
|
* The following case analysis is necessary so that characters
|
2000-04-15 04:41:27 +00:00
|
|
|
* which look negative collate low against normal characters
|
|
|
|
* but high against the end-of-string NUL.
|
|
|
|
*/
|
|
|
|
if (*str1 == '\0')
|
|
|
|
return (-1);
|
|
|
|
else if (*str2 == '\0')
|
|
|
|
return (1);
|
|
|
|
else
|
|
|
|
return (*str1 - *str2);
|
|
|
|
}
|
|
|
|
if (*str1 == '\0')
|
|
|
|
return(0);
|
|
|
|
str1++, str2++;
|
|
|
|
} while (--n != 0);
|
|
|
|
return(0);
|
|
|
|
}
|
2005-04-24 19:41:08 +00:00
|
|
|
#endif /* not WIDE_STRINGS */
|
2000-04-15 04:41:27 +00:00
|
|
|
|
2004-07-11 02:17:56 +00:00
|
|
|
int
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strcasecmp(const Char *str1, const Char *str2)
|
2004-07-11 02:17:56 +00:00
|
|
|
{
|
2005-04-24 19:41:08 +00:00
|
|
|
#ifdef WIDE_STRINGS
|
2012-02-20 00:53:59 +00:00
|
|
|
wint_t l1 = 0, l2 = 0;
|
|
|
|
for (; *str1; str1++, str2++)
|
|
|
|
if (*str1 == *str2)
|
|
|
|
l1 = l2 = 0;
|
|
|
|
else if ((l1 = towlower(*str1)) != (l2 = towlower(*str2)))
|
|
|
|
break;
|
2005-04-24 19:41:08 +00:00
|
|
|
#else
|
2012-02-20 00:53:59 +00:00
|
|
|
unsigned char l1 = 0, l2 = 0;
|
|
|
|
for (; *str1; str1++, str2++)
|
|
|
|
if (*str1 == *str2)
|
|
|
|
l1 = l2 = 0;
|
|
|
|
else if ((l1 = tolower((unsigned char)*str1)) !=
|
|
|
|
(l2 = tolower((unsigned char)*str2)))
|
|
|
|
break;
|
2005-04-24 19:41:08 +00:00
|
|
|
#endif
|
2004-07-11 02:17:56 +00:00
|
|
|
/*
|
|
|
|
* The following case analysis is necessary so that characters which look
|
|
|
|
* negative collate low against normal characters but high against the
|
|
|
|
* end-of-string NUL.
|
|
|
|
*/
|
|
|
|
if (*str1 == '\0' && *str2 == '\0')
|
|
|
|
return (0);
|
|
|
|
else if (*str1 == '\0')
|
|
|
|
return (-1);
|
|
|
|
else if (*str2 == '\0')
|
|
|
|
return (1);
|
|
|
|
else if (l1 == l2) /* They are zero when they are equal */
|
|
|
|
return (*str1 - *str2);
|
|
|
|
else
|
|
|
|
return (l1 - l2);
|
|
|
|
}
|
|
|
|
|
2000-04-15 04:41:27 +00:00
|
|
|
Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strnsave(const Char *s, size_t len)
|
|
|
|
{
|
|
|
|
Char *n;
|
|
|
|
|
|
|
|
n = xmalloc((len + 1) * sizeof (*n));
|
|
|
|
memcpy(n, s, len * sizeof (*n));
|
|
|
|
n[len] = '\0';
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
Char *
|
|
|
|
s_strsave(const Char *s)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
Char *n;
|
2007-03-11 22:33:41 +00:00
|
|
|
size_t size;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
if (s == NULL)
|
2000-04-15 04:41:27 +00:00
|
|
|
s = STRNULL;
|
2007-03-11 22:33:41 +00:00
|
|
|
size = (Strlen(s) + 1) * sizeof(*n);
|
|
|
|
n = xmalloc(size);
|
|
|
|
memcpy(n, s, size);
|
2000-04-15 04:41:27 +00:00
|
|
|
return (n);
|
|
|
|
}
|
|
|
|
|
|
|
|
Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strspl(const Char *cp, const Char *dp)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2007-03-11 22:33:41 +00:00
|
|
|
Char *res, *ep;
|
|
|
|
const Char *p, *q;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
if (!cp)
|
|
|
|
cp = STRNULL;
|
|
|
|
if (!dp)
|
|
|
|
dp = STRNULL;
|
2007-03-11 22:33:41 +00:00
|
|
|
for (p = cp; *p++;)
|
2000-04-15 04:41:27 +00:00
|
|
|
continue;
|
2007-03-11 22:33:41 +00:00
|
|
|
for (q = dp; *q++;)
|
2000-04-15 04:41:27 +00:00
|
|
|
continue;
|
2007-03-11 22:33:41 +00:00
|
|
|
res = xmalloc(((p - cp) + (q - dp) - 1) * sizeof(Char));
|
|
|
|
for (ep = res, q = cp; (*ep++ = *q++) != '\0';)
|
2000-04-15 04:41:27 +00:00
|
|
|
continue;
|
2007-03-11 22:33:41 +00:00
|
|
|
for (ep--, q = dp; (*ep++ = *q++) != '\0';)
|
2000-04-15 04:41:27 +00:00
|
|
|
continue;
|
2007-03-11 22:33:41 +00:00
|
|
|
return (res);
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strend(const Char *cp)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
if (!cp)
|
2005-04-24 19:41:08 +00:00
|
|
|
return ((Char *)(intptr_t) cp);
|
2000-04-15 04:41:27 +00:00
|
|
|
while (*cp)
|
|
|
|
cp++;
|
2005-04-24 19:41:08 +00:00
|
|
|
return ((Char *)(intptr_t) cp);
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
s_strstr(const Char *s, const Char *t)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
do {
|
2005-04-24 19:41:08 +00:00
|
|
|
const Char *ss = s;
|
|
|
|
const Char *tt = t;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
if (*tt == '\0')
|
2005-04-24 19:41:08 +00:00
|
|
|
return ((Char *)(intptr_t) s);
|
2000-04-15 04:41:27 +00:00
|
|
|
while (*ss++ == *tt++);
|
|
|
|
} while (*s++ != '\0');
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
#else /* !SHORT_STRINGS */
|
|
|
|
char *
|
|
|
|
caching_strip(const char *s)
|
|
|
|
{
|
|
|
|
static char *buf = NULL;
|
|
|
|
static size_t buf_size = 0;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return NULL;
|
|
|
|
size = strlen(s) + 1;
|
|
|
|
if (buf_size < size) {
|
|
|
|
buf = xrealloc(buf, size);
|
|
|
|
buf_size = size;
|
|
|
|
}
|
|
|
|
memcpy(buf, s, size);
|
|
|
|
strip(buf);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
#endif
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
char *
|
2007-03-11 22:33:41 +00:00
|
|
|
short2qstr(const Char *src)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
static char *sdst = NULL;
|
|
|
|
static size_t dstsize = 0;
|
2005-04-24 19:41:08 +00:00
|
|
|
char *dst, *edst;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
if (src == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (sdst == NULL) {
|
|
|
|
dstsize = MALLOC_INCR;
|
2007-03-11 22:33:41 +00:00
|
|
|
sdst = xmalloc((dstsize + MALLOC_SURPLUS) * sizeof(char));
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
|
|
|
dst = sdst;
|
|
|
|
edst = &dst[dstsize];
|
|
|
|
while (*src) {
|
|
|
|
if (*src & QUOTE) {
|
|
|
|
*dst++ = '\\';
|
|
|
|
if (dst == edst) {
|
|
|
|
dstsize += MALLOC_INCR;
|
2007-03-11 22:33:41 +00:00
|
|
|
sdst = xrealloc(sdst,
|
|
|
|
(dstsize + MALLOC_SURPLUS) * sizeof(char));
|
2000-04-15 04:41:27 +00:00
|
|
|
edst = &sdst[dstsize];
|
|
|
|
dst = &edst[-MALLOC_INCR];
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 21:41:53 +00:00
|
|
|
dst += one_wctomb(dst, *src);
|
2005-04-24 19:41:08 +00:00
|
|
|
src++;
|
|
|
|
if (dst >= edst) {
|
2009-07-10 21:47:20 +00:00
|
|
|
ptrdiff_t i = dst - edst;
|
2000-04-15 04:41:27 +00:00
|
|
|
dstsize += MALLOC_INCR;
|
2007-03-11 22:33:41 +00:00
|
|
|
sdst = xrealloc(sdst, (dstsize + MALLOC_SURPLUS) * sizeof(char));
|
2000-04-15 04:41:27 +00:00
|
|
|
edst = &sdst[dstsize];
|
2009-07-10 21:47:20 +00:00
|
|
|
dst = &edst[-MALLOC_INCR + i];
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*dst = 0;
|
|
|
|
return (sdst);
|
|
|
|
}
|
2007-03-11 22:33:41 +00:00
|
|
|
|
2009-07-10 21:47:20 +00:00
|
|
|
struct blk_buf *
|
2017-03-18 21:41:53 +00:00
|
|
|
bb_alloc(void)
|
2009-07-10 21:47:20 +00:00
|
|
|
{
|
|
|
|
return xcalloc(1, sizeof(struct blk_buf));
|
|
|
|
}
|
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
static void
|
|
|
|
bb_store(struct blk_buf *bb, Char *str)
|
|
|
|
{
|
|
|
|
if (bb->len == bb->size) { /* Keep space for terminating NULL */
|
|
|
|
if (bb->size == 0)
|
|
|
|
bb->size = 16; /* Arbitrary */
|
|
|
|
else
|
|
|
|
bb->size *= 2;
|
|
|
|
bb->vec = xrealloc(bb->vec, bb->size * sizeof (*bb->vec));
|
|
|
|
}
|
|
|
|
bb->vec[bb->len] = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bb_append(struct blk_buf *bb, Char *str)
|
|
|
|
{
|
|
|
|
bb_store(bb, str);
|
|
|
|
bb->len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bb_cleanup(void *xbb)
|
|
|
|
{
|
|
|
|
struct blk_buf *bb;
|
|
|
|
size_t i;
|
|
|
|
|
2017-03-18 21:41:53 +00:00
|
|
|
bb = (struct blk_buf *)xbb;
|
|
|
|
if (bb->vec) {
|
|
|
|
for (i = 0; i < bb->len; i++)
|
|
|
|
xfree(bb->vec[i]);
|
|
|
|
xfree(bb->vec);
|
|
|
|
}
|
|
|
|
bb->vec = NULL;
|
|
|
|
bb->len = 0;
|
2007-03-11 22:33:41 +00:00
|
|
|
}
|
|
|
|
|
2009-07-10 21:47:20 +00:00
|
|
|
void
|
|
|
|
bb_free(void *bb)
|
|
|
|
{
|
|
|
|
bb_cleanup(bb);
|
|
|
|
xfree(bb);
|
|
|
|
}
|
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
Char **
|
|
|
|
bb_finish(struct blk_buf *bb)
|
|
|
|
{
|
|
|
|
bb_store(bb, NULL);
|
|
|
|
return xrealloc(bb->vec, (bb->len + 1) * sizeof (*bb->vec));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DO_STRBUF(STRBUF, CHAR, STRLEN) \
|
2009-07-10 21:47:20 +00:00
|
|
|
\
|
|
|
|
struct STRBUF * \
|
|
|
|
STRBUF##_alloc(void) \
|
|
|
|
{ \
|
|
|
|
return xcalloc(1, sizeof(struct STRBUF)); \
|
|
|
|
} \
|
|
|
|
\
|
2007-03-11 22:33:41 +00:00
|
|
|
static void \
|
|
|
|
STRBUF##_store1(struct STRBUF *buf, CHAR c) \
|
|
|
|
{ \
|
|
|
|
if (buf->size == buf->len) { \
|
|
|
|
if (buf->size == 0) \
|
|
|
|
buf->size = 64; /* Arbitrary */ \
|
|
|
|
else \
|
|
|
|
buf->size *= 2; \
|
|
|
|
buf->s = xrealloc(buf->s, buf->size * sizeof(*buf->s)); \
|
|
|
|
} \
|
2012-02-20 00:53:59 +00:00
|
|
|
assert(buf->s); \
|
2007-03-11 22:33:41 +00:00
|
|
|
buf->s[buf->len] = c; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
/* Like strbuf_append1(buf, '\0'), but don't advance len */ \
|
|
|
|
void \
|
|
|
|
STRBUF##_terminate(struct STRBUF *buf) \
|
|
|
|
{ \
|
|
|
|
STRBUF##_store1(buf, '\0'); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
void \
|
|
|
|
STRBUF##_append1(struct STRBUF *buf, CHAR c) \
|
|
|
|
{ \
|
|
|
|
STRBUF##_store1(buf, c); \
|
|
|
|
buf->len++; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
void \
|
|
|
|
STRBUF##_appendn(struct STRBUF *buf, const CHAR *s, size_t len) \
|
|
|
|
{ \
|
|
|
|
if (buf->size < buf->len + len) { \
|
|
|
|
if (buf->size == 0) \
|
|
|
|
buf->size = 64; /* Arbitrary */ \
|
|
|
|
while (buf->size < buf->len + len) \
|
|
|
|
buf->size *= 2; \
|
|
|
|
buf->s = xrealloc(buf->s, buf->size * sizeof(*buf->s)); \
|
|
|
|
} \
|
|
|
|
memcpy(buf->s + buf->len, s, len * sizeof(*buf->s)); \
|
|
|
|
buf->len += len; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
void \
|
|
|
|
STRBUF##_append(struct STRBUF *buf, const CHAR *s) \
|
|
|
|
{ \
|
|
|
|
STRBUF##_appendn(buf, s, STRLEN(s)); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
CHAR * \
|
|
|
|
STRBUF##_finish(struct STRBUF *buf) \
|
|
|
|
{ \
|
|
|
|
STRBUF##_append1(buf, 0); \
|
|
|
|
return xrealloc(buf->s, buf->len * sizeof(*buf->s)); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
void \
|
|
|
|
STRBUF##_cleanup(void *xbuf) \
|
|
|
|
{ \
|
|
|
|
struct STRBUF *buf; \
|
|
|
|
\
|
|
|
|
buf = xbuf; \
|
|
|
|
xfree(buf->s); \
|
|
|
|
} \
|
|
|
|
\
|
2009-07-10 21:47:20 +00:00
|
|
|
void \
|
|
|
|
STRBUF##_free(void *xbuf) \
|
|
|
|
{ \
|
|
|
|
STRBUF##_cleanup(xbuf); \
|
|
|
|
xfree(xbuf); \
|
|
|
|
} \
|
|
|
|
\
|
2007-03-11 22:33:41 +00:00
|
|
|
const struct STRBUF STRBUF##_init /* = STRBUF##_INIT; */
|
|
|
|
|
|
|
|
DO_STRBUF(strbuf, char, strlen);
|
|
|
|
DO_STRBUF(Strbuf, Char, Strlen);
|