2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)str.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
43 #define MALLOC_INCR 128
46 * tc.str.c: Short string package
47 * This has been a lesson of how to write buggy code!
50 #include <sys/types.h>
73 for (n = 0; src[n] != NULL; n++)
75 sdst = dst = (Char **) xmalloc((size_t) ((n + 1) * sizeof(Char *)));
77 for (; *src != NULL; src++)
93 for (n = 0; src[n] != NULL; n++)
95 sdst = dst = (char **) xmalloc((size_t) ((n + 1) * sizeof(char *)));
97 for (; *src != NULL; src++)
98 *dst++ = strsave(short2str(*src));
108 static size_t dstsize = 0;
114 if (sdst == (NULL)) {
115 dstsize = MALLOC_INCR;
116 sdst = (Char *) xmalloc((size_t) dstsize * sizeof(Char));
120 edst = &dst[dstsize];
122 *dst++ = (Char) ((unsigned char) *src++);
124 dstsize += MALLOC_INCR;
125 sdst = (Char *) xrealloc((ptr_t) sdst,
126 (size_t) dstsize * sizeof(Char));
127 edst = &sdst[dstsize];
128 dst = &edst[-MALLOC_INCR];
139 static char *sdst = NULL;
140 static size_t dstsize = 0;
147 dstsize = MALLOC_INCR;
148 sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
151 edst = &dst[dstsize];
153 *dst++ = (char) *src++;
155 dstsize += MALLOC_INCR;
156 sdst = (char *) xrealloc((ptr_t) sdst,
157 (size_t) dstsize * sizeof(char));
158 edst = &sdst[dstsize];
159 dst = &edst[-MALLOC_INCR];
173 while ((*dst++ = *src++) != '\0')
179 s_strncpy(dst, src, n)
190 if ((*dst++ = *src++) == '\0') {
209 while ((*dst++ = *src++) != '\0')
216 s_strncat(dst, src, n)
232 if ((*dst++ = *src++) == '\0')
276 for (n = 0; *str++; n++)
285 for (; *str1 && *str1 == *str2; str1++, str2++)
288 * The following case analysis is necessary so that characters which look
289 * negative collate low against normal characters but high against the
292 if (*str1 == '\0' && *str2 == '\0')
294 else if (*str1 == '\0')
296 else if (*str2 == '\0')
299 return (*str1 - *str2);
303 s_strncmp(str1, str2, n)
310 if (*str1 != *str2) {
312 * The following case analysis is necessary so that characters
313 * which look negative collate low against normal characters
314 * but high against the end-of-string NUL.
318 else if (*str2 == '\0')
321 return (*str1 - *str2);
342 n = p = (Char *) xmalloc((size_t) ((p - s) * sizeof(Char)));
343 while ((*p++ = *s++) != '\0')
363 ep = (Char *) xmalloc((size_t)
364 (((p - cp) + (q - dp) - 1) * sizeof(Char)));
365 for (p = ep, q = cp; (*p++ = *q++) != '\0';)
367 for (p--, q = dp; (*p++ = *q++) != '\0';)
394 while (*ss++ == *tt++);
395 } while (*s++ != '\0');
398 #endif /* SHORT_STRINGS */
404 static char *sdst = NULL;
405 static size_t dstsize = 0;
412 dstsize = MALLOC_INCR;
413 sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
416 edst = &dst[dstsize];
421 dstsize += MALLOC_INCR;
422 sdst = (char *) xrealloc((ptr_t) sdst,
423 (size_t) dstsize * sizeof(char));
424 edst = &sdst[dstsize];
425 dst = &edst[-MALLOC_INCR];
428 *dst++ = (char) *src++;
430 dstsize += MALLOC_INCR;
431 sdst = (char *) xrealloc((ptr_t) sdst,
432 (size_t) dstsize * sizeof(char));
433 edst = &sdst[dstsize];
434 dst = &edst[-MALLOC_INCR];
442 * XXX: Should we worry about QUOTE'd chars?
448 static char *sdst = NULL;
449 static size_t dstsize = 0;
456 for (dp = cp; *dp++;)
458 n = ((dp - cp) << 2) + 1; /* 4 times + NULL */
460 sdst = (char *) (dstsize ?
461 xrealloc(sdst, (size_t) n * sizeof(char)) :
462 xmalloc((size_t) n * sizeof(char)));
466 * XXX: When we are in AsciiOnly we want all characters >= 0200 to
467 * be encoded, but currently there is no way in vis to do that.
469 (void) strvis(sdst, short2str(cp), VIS_NOSLASH);