Home | History | Annotate | Line # | Download | only in csh
str.c revision 1.1
      1 /*-
      2  * Copyright (c) 1991 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      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.
     20  *
     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
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char sccsid[] = "@(#)str.c	5.5 (Berkeley) 6/27/91";
     36 #endif /* not lint */
     37 
     38 /*
     39  * tc.str.c: Short string package
     40  * 	     This has been a lesson of how to write buggy code!
     41  */
     42 #ifdef SHORT_STRINGS
     43 
     44 #if __STDC__
     45 # include <stdarg.h>
     46 #else
     47 # include <varargs.h>
     48 #endif
     49 
     50 #include "csh.h"
     51 #include "extern.h"
     52 
     53 Char  **
     54 blk2short(src)
     55     register char **src;
     56 {
     57     size_t     n;
     58     register Char **sdst, **dst;
     59 
     60     /*
     61      * Count
     62      */
     63     for (n = 0; src[n] != NULL; n++);
     64     sdst = dst = (Char **) xmalloc((size_t) ((n + 1) * sizeof(Char *)));
     65 
     66     for (; *src != NULL; src++)
     67 	*dst++ = SAVE(*src);
     68     *dst = NULL;
     69     return (sdst);
     70 }
     71 
     72 char  **
     73 short2blk(src)
     74     register Char **src;
     75 {
     76     size_t     n;
     77     register char **sdst, **dst;
     78 
     79     /*
     80      * Count
     81      */
     82     for (n = 0; src[n] != NULL; n++);
     83     sdst = dst = (char **) xmalloc((size_t) ((n + 1) * sizeof(char *)));
     84 
     85     for (; *src != NULL; src++)
     86 	*dst++ = strsave(short2str(*src));
     87     *dst = NULL;
     88     return (sdst);
     89 }
     90 
     91 #define MALLOC_INCR	1024
     92 Char   *
     93 str2short(src)
     94     register char *src;
     95 {
     96     static Char *sdst;
     97     static size_t dstsize = 0;
     98     register Char *dst, *edst;
     99 
    100     if (src == NULL)
    101 	return (NULL);
    102 
    103     if (sdst == (NULL)) {
    104 	dstsize = MALLOC_INCR;
    105 	sdst = (Char *) xmalloc((size_t) dstsize * sizeof(Char));
    106     }
    107 
    108     dst = sdst;
    109     edst = &dst[dstsize];
    110     while (*src) {
    111 	*dst++ = (Char) ((unsigned char) *src++);
    112 	if (dst == edst) {
    113 	    dstsize += MALLOC_INCR;
    114 	    sdst = (Char *) xrealloc((ptr_t) sdst,
    115 				     (size_t) dstsize * sizeof(Char));
    116 	    edst = &sdst[dstsize];
    117 	    dst = &edst[-MALLOC_INCR];
    118 	}
    119     }
    120     *dst = 0;
    121     return (sdst);
    122 }
    123 
    124 char   *
    125 short2qstr(src)
    126     register Char *src;
    127 {
    128     static char *sdst = NULL;
    129     static size_t dstsize = 0;
    130     register char *dst, *edst;
    131 
    132     if (src == NULL)
    133 	return (NULL);
    134 
    135     if (sdst == NULL) {
    136 	dstsize = MALLOC_INCR;
    137 	sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
    138     }
    139     dst = sdst;
    140     edst = &dst[dstsize];
    141     while (*src) {
    142 	if (*src & QUOTE) {
    143 	    *dst++ = '\\';
    144 	    if (dst == edst) {
    145 		dstsize += MALLOC_INCR;
    146 		sdst = (char *) xrealloc((ptr_t) sdst,
    147 					 (size_t) dstsize * sizeof(char));
    148 		edst = &sdst[dstsize];
    149 		dst = &edst[-MALLOC_INCR];
    150 	    }
    151 	}
    152 	*dst++ = (char) *src++;
    153 	if (dst == edst) {
    154 	    dstsize += MALLOC_INCR;
    155 	    sdst = (char *) xrealloc((ptr_t) sdst,
    156 				     (size_t) dstsize * sizeof(char));
    157 	    edst = &sdst[dstsize];
    158 	    dst = &edst[-MALLOC_INCR];
    159 	}
    160     }
    161     *dst = 0;
    162     return (sdst);
    163 }
    164 char   *
    165 short2str(src)
    166     register Char *src;
    167 {
    168     static char *sdst = NULL;
    169     static size_t dstsize = 0;
    170     register char *dst, *edst;
    171 
    172     if (src == NULL)
    173 	return (NULL);
    174 
    175     if (sdst == NULL) {
    176 	dstsize = MALLOC_INCR;
    177 	sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
    178     }
    179     dst = sdst;
    180     edst = &dst[dstsize];
    181     while (*src) {
    182 	*dst++ = (char) *src++;
    183 	if (dst == edst) {
    184 	    dstsize += MALLOC_INCR;
    185 	    sdst = (char *) xrealloc((ptr_t) sdst,
    186 				     (size_t) dstsize * sizeof(char));
    187 	    edst = &sdst[dstsize];
    188 	    dst = &edst[-MALLOC_INCR];
    189 	}
    190     }
    191     *dst = 0;
    192     return (sdst);
    193 }
    194 
    195 Char   *
    196 s_strcpy(dst, src)
    197     register Char *dst, *src;
    198 {
    199     register Char *sdst;
    200 
    201     sdst = dst;
    202     while (*dst++ = *src++);
    203     return (sdst);
    204 }
    205 
    206 Char   *
    207 s_strncpy(dst, src, n)
    208     register Char *dst, *src;
    209     register size_t n;
    210 {
    211     register Char *sdst;
    212 
    213     if (n == 0)
    214 	return(dst);
    215 
    216     sdst = dst;
    217     do
    218 	if ((*dst++ = *src++) == '\0') {
    219 	    while (--n != 0)
    220 		*dst++ = '\0';
    221 	    return(sdst);
    222 	}
    223     while (--n != 0);
    224     return (sdst);
    225 }
    226 
    227 Char   *
    228 s_strcat(dst, src)
    229     register Char *dst, *src;
    230 {
    231     register short *sdst;
    232 
    233     sdst = dst;
    234     while (*dst++);
    235     --dst;
    236     while (*dst++ = *src++);
    237     return (sdst);
    238 }
    239 
    240 #ifdef NOTUSED
    241 Char   *
    242 s_strncat(dst, src, n)
    243     register Char *dst, *src;
    244     register size_t n;
    245 {
    246     register Char *sdst;
    247 
    248     if (n == 0)
    249 	return (dst);
    250 
    251     sdst = dst;
    252 
    253     while (*dst++);
    254     --dst;
    255 
    256     do
    257 	if ((*dst++ = *src++) == '\0')
    258 	    return(sdst);
    259     while (--n != 0);
    260 
    261     *dst = '\0';
    262     return (sdst);
    263 }
    264 
    265 #endif
    266 
    267 Char   *
    268 s_strchr(str, ch)
    269     register Char *str;
    270     int ch;
    271 {
    272     do
    273 	if (*str == ch)
    274 	    return (str);
    275     while (*str++);
    276     return (NULL);
    277 }
    278 
    279 Char   *
    280 s_strrchr(str, ch)
    281     register Char *str;
    282     int ch;
    283 {
    284     register Char *rstr;
    285 
    286     rstr = NULL;
    287     do
    288 	if (*str == ch)
    289 	    rstr = str;
    290     while (*str++);
    291     return (rstr);
    292 }
    293 
    294 size_t
    295 s_strlen(str)
    296     register Char *str;
    297 {
    298     register size_t n;
    299 
    300     for (n = 0; *str++; n++);
    301     return (n);
    302 }
    303 
    304 int
    305 s_strcmp(str1, str2)
    306     register Char *str1, *str2;
    307 {
    308     for (; *str1 && *str1 == *str2; str1++, str2++);
    309     /*
    310      * The following case analysis is necessary so that characters which look
    311      * negative collate low against normal characters but high against the
    312      * end-of-string NUL.
    313      */
    314     if (*str1 == '\0' && *str2 == '\0')
    315 	return (0);
    316     else if (*str1 == '\0')
    317 	return (-1);
    318     else if (*str2 == '\0')
    319 	return (1);
    320     else
    321 	return (*str1 - *str2);
    322 }
    323 
    324 int
    325 s_strncmp(str1, str2, n)
    326     register Char *str1, *str2;
    327     register size_t n;
    328 {
    329     if (n == 0)
    330 	return (0);
    331     do {
    332         if (*str1 == '\0' || *str1 != *str2)
    333 	    break;
    334 	str1++, str2++;
    335     } while (--n != 0);
    336     /*
    337      * The following case analysis is necessary so that characters which look
    338      * negative collate low against normal characters but high against the
    339      * end-of-string NUL.
    340      */
    341     if (*str1 == '\0' && *str2 == '\0')
    342 	return (0);
    343     else if (*str1 == '\0')
    344 	return (-1);
    345     else if (*str2 == '\0')
    346 	return (1);
    347     else
    348 	return (*str1 - *str2);
    349     return(0);
    350 }
    351 
    352 Char   *
    353 s_strsave(s)
    354     register Char *s;
    355 {
    356     Char   *n;
    357     register Char *p;
    358 
    359     if (s == 0)
    360 	s = STRNULL;
    361     for (p = s; *p++;);
    362     n = p = (Char *) xmalloc((size_t) ((p - s) * sizeof(Char)));
    363     while (*p++ = *s++);
    364     return (n);
    365 }
    366 
    367 Char   *
    368 s_strspl(cp, dp)
    369     Char   *cp, *dp;
    370 {
    371     Char   *ep;
    372     register Char *p, *q;
    373 
    374     if (!cp)
    375 	cp = STRNULL;
    376     if (!dp)
    377 	dp = STRNULL;
    378     for (p = cp; *p++;);
    379     for (q = dp; *q++;);
    380     ep = (Char *) xmalloc((size_t)
    381 			  (((p - cp) + (q - dp) - 1) * sizeof(Char)));
    382     for (p = ep, q = cp; *p++ = *q++;);
    383     for (p--, q = dp; *p++ = *q++;);
    384     return (ep);
    385 }
    386 
    387 Char   *
    388 s_strend(cp)
    389     register Char *cp;
    390 {
    391     if (!cp)
    392 	return (cp);
    393     while (*cp)
    394 	cp++;
    395     return (cp);
    396 }
    397 
    398 #ifdef NOTUSED
    399 Char   *
    400 s_strstr(s, t)
    401     register Char *s, *t;
    402 {
    403     do {
    404 	register Char *ss = s;
    405 	register Char *tt = t;
    406 
    407 	do
    408 	    if (*tt == '\0')
    409 		return (s);
    410 	while (*ss++ == *tt++);
    411     } while (*s++ != '\0');
    412     return (NULL);
    413 }
    414 #endif
    415 
    416 #endif				/* SHORT_STRINGS */
    417