1 /* $Header: /src/pub/tcsh/tc.str.c,v 3.19 2005/01/06 16:56:26 christos Exp $ */
3 * tc.str.c: Short string package
4 * This has been a lesson of how to write buggy code!
7 * Copyright (c) 1980, 1991 The Regents of the University of California.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 RCSID("$Id: tc.str.c,v 3.19 2005/01/06 16:56:26 christos Exp $")
40 #define MALLOC_INCR 128
42 #define MALLOC_SURPLUS MB_LEN_MAX /* Space for one multibyte character */
44 #define MALLOC_SURPLUS 0
49 one_mbtowc(wchar_t *pwc, const char *s, size_t n)
53 len = rt_mbtowc(pwc, s, n);
55 mbtowc(NULL, NULL, 0);
56 *pwc = (unsigned char)*s | INVALID_BYTE;
64 one_wctomb(char *s, wchar_t wchar)
68 if (wchar & INVALID_BYTE) {
72 len = wctomb(s, wchar);
84 rt_mbtowc(wchar_t *pwc, const char *s, size_t n)
87 char back[MB_LEN_MAX];
89 ret = mbtowc(pwc, s, n);
90 if (ret > 0 && (wctomb(back, *pwc) != ret || memcmp(s, back, ret) != 0))
105 for (n = 0; src[n] != NULL; n++)
107 sdst = dst = (Char **) xmalloc((size_t) ((n + 1) * sizeof(Char *)));
109 for (; *src != NULL; src++)
125 for (n = 0; src[n] != NULL; n++)
127 sdst = dst = (char **) xmalloc((size_t) ((n + 1) * sizeof(char *)));
129 for (; *src != NULL; src++)
130 *dst++ = strsave(short2str(*src));
140 static size_t dstsize = 0;
146 if (sdst == (NULL)) {
147 dstsize = MALLOC_INCR;
148 sdst = (Char *) xmalloc((size_t) (dstsize * sizeof(Char)));
152 edst = &dst[dstsize];
153 while ((unsigned char) *src) {
154 src += one_mbtowc(dst, src, MB_LEN_MAX);
157 dstsize += MALLOC_INCR;
158 sdst = (Char *) xrealloc((ptr_t) sdst,
159 (size_t) (dstsize * sizeof(Char)));
160 edst = &sdst[dstsize];
161 dst = &edst[-MALLOC_INCR];
172 static char *sdst = NULL;
173 static size_t dstsize = 0;
180 dstsize = MALLOC_INCR;
181 sdst = (char *) xmalloc((size_t) ((dstsize + MALLOC_SURPLUS)
185 edst = &dst[dstsize];
187 dst += one_wctomb(dst, *src & CHAR);
190 dstsize += MALLOC_INCR;
191 sdst = (char *) xrealloc((ptr_t) sdst,
192 (size_t) ((dstsize + MALLOC_SURPLUS)
194 edst = &sdst[dstsize];
195 dst = &edst[-MALLOC_INCR];
211 while ((*dst++ = *src++) != '\0')
217 s_strncpy(dst, src, n)
229 if ((*dst++ = *src++) == '\0') {
249 while ((*dst++ = *src++) != '\0')
256 s_strncat(dst, src, n)
273 if ((*dst++ = *src++) == '\0')
291 return ((Char *)(intptr_t)str);
308 return ((Char *)(intptr_t)rstr);
317 for (n = 0; *str++; n++)
324 const Char *str1, *str2;
326 for (; *str1 && *str1 == *str2; str1++, str2++)
329 * The following case analysis is necessary so that characters which look
330 * negative collate low against normal characters but high against the
333 if (*str1 == '\0' && *str2 == '\0')
335 else if (*str1 == '\0')
337 else if (*str2 == '\0')
340 return (*str1 - *str2);
344 s_strncmp(str1, str2, n)
345 const Char *str1, *str2;
351 if (*str1 != *str2) {
353 * The following case analysis is necessary so that characters
354 * which look negative collate low against normal characters
355 * but high against the end-of-string NUL.
359 else if (*str2 == '\0')
362 return (*str1 - *str2);
370 #endif /* not WIDE_STRINGS */
373 s_strcasecmp(str1, str2)
374 const Char *str1, *str2;
377 wchar_t l1 = 0, l2 = 0;
378 for (; *str1 && ((*str1 == *str2 && (l1 = l2 = 0) == 0) ||
379 (l1 = towlower(*str1)) == (l2 = towlower(*str2))); str1++, str2++)
383 unsigned char c1, c2, l1 = 0, l2 = 0;
384 for (; *str1 && ((*str1 == *str2 && (l1 = l2 = 0) == 0) ||
385 ((c1 = (unsigned char)*str1) == *str1 &&
386 (c2 = (unsigned char)*str2) == *str2 &&
387 (l1 = tolower(c1)) == (l2 = tolower(c2)))); str1++, str2++)
391 * The following case analysis is necessary so that characters which look
392 * negative collate low against normal characters but high against the
395 if (*str1 == '\0' && *str2 == '\0')
397 else if (*str1 == '\0')
399 else if (*str2 == '\0')
401 else if (l1 == l2) /* They are zero when they are equal */
402 return (*str1 - *str2);
416 for (p = (Char *)(intptr_t)s; *p++;)
418 n = p = (Char *) xmalloc((size_t)
419 ((((const Char *) p) - s) * sizeof(Char)));
420 while ((*p++ = *s++) != '\0')
436 for (p = (Char *)(intptr_t) cp; *p++;)
438 for (q = (Char *)(intptr_t) dp; *q++;)
440 ep = (Char *) xmalloc((size_t)
441 (((((const Char *) p) - cp) +
442 (((const Char *) q) - dp) - 1) * sizeof(Char)));
443 for (p = ep, q = (Char*)(intptr_t) cp; (*p++ = *q++) != '\0';)
445 for (p--, q = (Char *)(intptr_t) dp; (*p++ = *q++) != '\0';)
455 return ((Char *)(intptr_t) cp);
458 return ((Char *)(intptr_t) cp);
471 return ((Char *)(intptr_t) s);
472 while (*ss++ == *tt++);
473 } while (*s++ != '\0');
477 #endif /* SHORT_STRINGS */
483 static char *sdst = NULL;
484 static size_t dstsize = 0;
491 dstsize = MALLOC_INCR;
492 sdst = (char *) xmalloc((size_t) ((dstsize + MALLOC_SURPLUS)
496 edst = &dst[dstsize];
501 dstsize += MALLOC_INCR;
502 sdst = (char *) xrealloc((ptr_t) sdst,
503 (size_t) ((dstsize + MALLOC_SURPLUS)
505 edst = &sdst[dstsize];
506 dst = &edst[-MALLOC_INCR];
509 dst += one_wctomb(dst, *src & CHAR);
512 dstsize += MALLOC_INCR;
513 sdst = (char *) xrealloc((ptr_t) sdst,
514 (size_t) ((dstsize + MALLOC_SURPLUS)
516 edst = &sdst[dstsize];
517 dst = &edst[-MALLOC_INCR];