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

Defined functions

blk2short defined in line 47; never used
rcsid defined in line 41; never used
s_scat defined in line 235; used 1 times
s_scmp defined in line 318; used 1 times
s_scpy defined in line 200; used 1 times
s_strcat defined in line 221; used 1 times
s_strchr defined in line 261; used 1 times
s_strcmp defined in line 298; used 1 times
s_strcpy defined in line 189; used 1 times
s_strend defined in line 382; used 1 times
s_strlen defined in line 288; used 1 times
s_strrchr defined in line 273; used 1 times
s_strsave defined in line 347; used 1 times
s_strspl defined in line 362; used 1 times
s_strstr defined in line 393; used 1 times
short2blk defined in line 66; never used
short2qstr defined in line 118; never used
short2str defined in line 158; never used
str2short defined in line 86; never used

Defined macros

MALLOC_INCR defined in line 85; used 11 times
Last modified: 1991-08-20
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 80
Valid CSS Valid XHTML 1.0 Strict