1 /* $Header: /src/pub/tcsh/tc.str.c,v 3.9 1996/04/26 19:21:42 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 RCSID("$Id: tc.str.c,v 3.9 1996/04/26 19:21:42 christos Exp $")
42 #define MALLOC_INCR 128
50 register Char **sdst, **dst;
55 for (n = 0; src[n] != NULL; n++)
57 sdst = dst = (Char **) xmalloc((size_t) ((n + 1) * sizeof(Char *)));
59 for (; *src != NULL; src++)
70 register char **sdst, **dst;
75 for (n = 0; src[n] != NULL; n++)
77 sdst = dst = (char **) xmalloc((size_t) ((n + 1) * sizeof(char *)));
79 for (; *src != NULL; src++)
80 *dst++ = strsave(short2str(*src));
87 register const char *src;
90 static size_t dstsize = 0;
91 register Char *dst, *edst;
97 dstsize = MALLOC_INCR;
98 sdst = (Char *) xmalloc((size_t) (dstsize * sizeof(Char)));
102 edst = &dst[dstsize];
103 while ((unsigned char) *src) {
104 *dst++ = (Char) ((unsigned char) *src++);
106 dstsize += MALLOC_INCR;
107 sdst = (Char *) xrealloc((ptr_t) sdst,
108 (size_t) (dstsize * sizeof(Char)));
109 edst = &sdst[dstsize];
110 dst = &edst[-MALLOC_INCR];
119 register const Char *src;
121 static char *sdst = NULL;
122 static size_t dstsize = 0;
123 register char *dst, *edst;
129 dstsize = MALLOC_INCR;
130 sdst = (char *) xmalloc((size_t) (dstsize * sizeof(char)));
133 edst = &dst[dstsize];
135 *dst++ = (char) *src++;
137 dstsize += MALLOC_INCR;
138 sdst = (char *) xrealloc((ptr_t) sdst,
139 (size_t) (dstsize * sizeof(char)));
140 edst = &sdst[dstsize];
141 dst = &edst[-MALLOC_INCR];
151 register const Char *src;
156 while ((*dst++ = *src++) != '\0')
162 s_strncpy(dst, src, n)
164 register const Char *src;
174 if ((*dst++ = *src++) == '\0') {
186 register const Char *src;
188 register short *sdst;
194 while ((*dst++ = *src++) != '\0')
201 s_strncat(dst, src, n)
203 register const Char *src;
218 if ((*dst++ = *src++) == '\0')
231 register const Char *str;
236 return ((Char *) str);
243 register const Char *str;
246 register const Char *rstr;
253 return ((Char *) rstr);
258 register const Char *str;
262 for (n = 0; *str++; n++)
269 register const Char *str1, *str2;
271 for (; *str1 && *str1 == *str2; str1++, str2++)
274 * The following case analysis is necessary so that characters which look
275 * negative collate low against normal characters but high against the
278 if (*str1 == '\0' && *str2 == '\0')
280 else if (*str1 == '\0')
282 else if (*str2 == '\0')
285 return (*str1 - *str2);
289 s_strncmp(str1, str2, n)
290 register const Char *str1, *str2;
296 if (*str1 != *str2) {
298 * The following case analysis is necessary so that characters
299 * which look negative collate low against normal characters
300 * but high against the end-of-string NUL.
304 else if (*str2 == '\0')
307 return (*str1 - *str2);
318 register const Char *s;
325 for (p = (Char *) s; *p++;)
327 n = p = (Char *) xmalloc((size_t)
328 ((((const Char *) p) - s) * sizeof(Char)));
329 while ((*p++ = *s++) != '\0')
339 register Char *p, *q;
345 for (p = (Char *) cp; *p++;)
347 for (q = (Char *) dp; *q++;)
349 ep = (Char *) xmalloc((size_t)
350 (((((const Char *) p) - cp) +
351 (((const Char *) q) - dp) - 1) * sizeof(Char)));
352 for (p = ep, q = (Char*) cp; (*p++ = *q++) != '\0';)
354 for (p--, q = (Char *) dp; (*p++ = *q++) != '\0';)
361 register const Char *cp;
364 return ((Char *) cp);
367 return ((Char *) cp);
372 register const Char *s, *t;
375 register const Char *ss = s;
376 register const Char *tt = t;
381 while (*ss++ == *tt++);
382 } while (*s++ != '\0');
386 #endif /* SHORT_STRINGS */
390 register const Char *src;
392 static char *sdst = NULL;
393 static size_t dstsize = 0;
394 register char *dst, *edst;
400 dstsize = MALLOC_INCR;
401 sdst = (char *) xmalloc((size_t) (dstsize * sizeof(char)));
404 edst = &dst[dstsize];
409 dstsize += MALLOC_INCR;
410 sdst = (char *) xrealloc((ptr_t) sdst,
411 (size_t) (dstsize * sizeof(char)));
412 edst = &sdst[dstsize];
413 dst = &edst[-MALLOC_INCR];
416 *dst++ = (char) *src++;
418 dstsize += MALLOC_INCR;
419 sdst = (char *) xrealloc((ptr_t) sdst,
420 (size_t) (dstsize * sizeof(char)));
421 edst = &sdst[dstsize];
422 dst = &edst[-MALLOC_INCR];