1 /* $Header: /src/pub/tcsh/tc.str.c,v 3.13 2004/02/21 20:34:25 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
36 RCSID("$Id: tc.str.c,v 3.13 2004/02/21 20:34:25 christos Exp $")
38 #define MALLOC_INCR 128
46 register Char **sdst, **dst;
51 for (n = 0; src[n] != NULL; n++)
53 sdst = dst = (Char **) xmalloc((size_t) ((n + 1) * sizeof(Char *)));
55 for (; *src != NULL; src++)
66 register char **sdst, **dst;
71 for (n = 0; src[n] != NULL; n++)
73 sdst = dst = (char **) xmalloc((size_t) ((n + 1) * sizeof(char *)));
75 for (; *src != NULL; src++)
76 *dst++ = strsave(short2str(*src));
83 register const char *src;
86 static size_t dstsize = 0;
87 register Char *dst, *edst;
93 dstsize = MALLOC_INCR;
94 sdst = (Char *) xmalloc((size_t) (dstsize * sizeof(Char)));
99 while ((unsigned char) *src) {
100 *dst++ = (Char) ((unsigned char) *src++);
102 dstsize += MALLOC_INCR;
103 sdst = (Char *) xrealloc((ptr_t) sdst,
104 (size_t) (dstsize * sizeof(Char)));
105 edst = &sdst[dstsize];
106 dst = &edst[-MALLOC_INCR];
115 register const Char *src;
117 static char *sdst = NULL;
118 static size_t dstsize = 0;
119 register char *dst, *edst;
125 dstsize = MALLOC_INCR;
126 sdst = (char *) xmalloc((size_t) (dstsize * sizeof(char)));
129 edst = &dst[dstsize];
131 *dst++ = (char) *src++;
133 dstsize += MALLOC_INCR;
134 sdst = (char *) xrealloc((ptr_t) sdst,
135 (size_t) (dstsize * sizeof(char)));
136 edst = &sdst[dstsize];
137 dst = &edst[-MALLOC_INCR];
147 register const Char *src;
152 while ((*dst++ = *src++) != '\0')
158 s_strncpy(dst, src, n)
160 register const Char *src;
170 if ((*dst++ = *src++) == '\0') {
182 register const Char *src;
184 register short *sdst;
190 while ((*dst++ = *src++) != '\0')
197 s_strncat(dst, src, n)
199 register const Char *src;
214 if ((*dst++ = *src++) == '\0')
227 register const Char *str;
232 return ((Char *) str);
239 register const Char *str;
242 register const Char *rstr;
249 return ((Char *) rstr);
254 register const Char *str;
258 for (n = 0; *str++; n++)
265 register const Char *str1, *str2;
267 for (; *str1 && *str1 == *str2; str1++, str2++)
270 * The following case analysis is necessary so that characters which look
271 * negative collate low against normal characters but high against the
274 if (*str1 == '\0' && *str2 == '\0')
276 else if (*str1 == '\0')
278 else if (*str2 == '\0')
281 return (*str1 - *str2);
285 s_strncmp(str1, str2, n)
286 register const Char *str1, *str2;
292 if (*str1 != *str2) {
294 * The following case analysis is necessary so that characters
295 * which look negative collate low against normal characters
296 * but high against the end-of-string NUL.
300 else if (*str2 == '\0')
303 return (*str1 - *str2);
313 s_strcasecmp(str1, str2)
314 register const Char *str1, *str2;
316 unsigned char c1, c2, l1 = 0, l2 = 0;
317 for (; *str1 && ((*str1 == *str2 && (l1 = l2 = 0) == 0) ||
318 ((c1 = (unsigned char)*str1) == *str1 &&
319 (c2 = (unsigned char)*str2) == *str2 &&
320 (l1 = tolower(c1)) == (l2 = tolower(c2)))); str1++, str2++)
323 * The following case analysis is necessary so that characters which look
324 * negative collate low against normal characters but high against the
327 if (*str1 == '\0' && *str2 == '\0')
329 else if (*str1 == '\0')
331 else if (*str2 == '\0')
333 else if (l1 == l2) /* They are zero when they are equal */
334 return (*str1 - *str2);
341 register const Char *s;
348 for (p = (Char *) s; *p++;)
350 n = p = (Char *) xmalloc((size_t)
351 ((((const Char *) p) - s) * sizeof(Char)));
352 while ((*p++ = *s++) != '\0')
362 register Char *p, *q;
368 for (p = (Char *) cp; *p++;)
370 for (q = (Char *) dp; *q++;)
372 ep = (Char *) xmalloc((size_t)
373 (((((const Char *) p) - cp) +
374 (((const Char *) q) - dp) - 1) * sizeof(Char)));
375 for (p = ep, q = (Char*) cp; (*p++ = *q++) != '\0';)
377 for (p--, q = (Char *) dp; (*p++ = *q++) != '\0';)
384 register const Char *cp;
387 return ((Char *) cp);
390 return ((Char *) cp);
395 register const Char *s, *t;
398 register const Char *ss = s;
399 register const Char *tt = t;
404 while (*ss++ == *tt++);
405 } while (*s++ != '\0');
409 #endif /* SHORT_STRINGS */
413 register const Char *src;
415 static char *sdst = NULL;
416 static size_t dstsize = 0;
417 register char *dst, *edst;
423 dstsize = MALLOC_INCR;
424 sdst = (char *) xmalloc((size_t) (dstsize * sizeof(char)));
427 edst = &dst[dstsize];
432 dstsize += MALLOC_INCR;
433 sdst = (char *) xrealloc((ptr_t) sdst,
434 (size_t) (dstsize * sizeof(char)));
435 edst = &sdst[dstsize];
436 dst = &edst[-MALLOC_INCR];
439 *dst++ = (char) *src++;
441 dstsize += MALLOC_INCR;
442 sdst = (char *) xrealloc((ptr_t) sdst,
443 (size_t) (dstsize * sizeof(char)));
444 edst = &sdst[dstsize];
445 dst = &edst[-MALLOC_INCR];