1/* $Header: /p/tcsh/cvsroot/tcsh/tc.str.c,v 3.26 2006/03/02 18:46:45 christos Exp $ */
| 1/* $Header: /p/tcsh/cvsroot/tcsh/tc.str.c,v 3.30 2009/06/25 21:27:38 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. 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. 21 * 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 32 * SUCH DAMAGE. 33 */ 34#include "sh.h" 35 36#include <limits.h> 37
| 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. 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. 21 * 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 32 * SUCH DAMAGE. 33 */ 34#include "sh.h" 35 36#include <limits.h> 37
|
38RCSID("$tcsh: tc.str.c,v 3.26 2006/03/02 18:46:45 christos Exp $")
| 38RCSID("$tcsh: tc.str.c,v 3.30 2009/06/25 21:27:38 christos Exp $")
|
39 40#define MALLOC_INCR 128 41#ifdef WIDE_STRINGS 42#define MALLOC_SURPLUS MB_LEN_MAX /* Space for one multibyte character */ 43#else 44#define MALLOC_SURPLUS 0 45#endif 46 47#ifdef WIDE_STRINGS 48size_t 49one_mbtowc(wchar_t *pwc, const char *s, size_t n) 50{ 51 int len; 52 53 len = rt_mbtowc(pwc, s, n); 54 if (len == -1) {
| 39 40#define MALLOC_INCR 128 41#ifdef WIDE_STRINGS 42#define MALLOC_SURPLUS MB_LEN_MAX /* Space for one multibyte character */ 43#else 44#define MALLOC_SURPLUS 0 45#endif 46 47#ifdef WIDE_STRINGS 48size_t 49one_mbtowc(wchar_t *pwc, const char *s, size_t n) 50{ 51 int len; 52 53 len = rt_mbtowc(pwc, s, n); 54 if (len == -1) {
|
55 mbtowc(NULL, NULL, 0);
| 55 reset_mbtowc();
|
56 *pwc = (unsigned char)*s | INVALID_BYTE; 57 } 58 if (len <= 0) 59 len = 1; 60 return len; 61} 62 63size_t 64one_wctomb(char *s, wchar_t wchar) 65{ 66 int len; 67 68 if (wchar & INVALID_BYTE) { 69 s[0] = wchar & 0xFF; 70 len = 1; 71 } else { 72 len = wctomb(s, wchar); 73 if (len == -1) 74 s[0] = wchar; 75 if (len <= 0) 76 len = 1; 77 } 78 return len; 79} 80 81int 82rt_mbtowc(wchar_t *pwc, const char *s, size_t n) 83{ 84 int ret; 85 char back[MB_LEN_MAX]; 86 87 ret = mbtowc(pwc, s, n); 88 if (ret > 0 && (wctomb(back, *pwc) != ret || memcmp(s, back, ret) != 0)) 89 ret = -1; 90 return ret; 91} 92#endif 93 94#ifdef SHORT_STRINGS 95Char ** 96blk2short(char **src) 97{ 98 size_t n; 99 Char **sdst, **dst; 100 101 /* 102 * Count 103 */ 104 for (n = 0; src[n] != NULL; n++) 105 continue; 106 sdst = dst = xmalloc((n + 1) * sizeof(Char *)); 107 108 for (; *src != NULL; src++) 109 *dst++ = SAVE(*src); 110 *dst = NULL; 111 return (sdst); 112} 113 114char ** 115short2blk(Char **src) 116{ 117 size_t n; 118 char **sdst, **dst; 119 120 /* 121 * Count 122 */ 123 for (n = 0; src[n] != NULL; n++) 124 continue; 125 sdst = dst = xmalloc((n + 1) * sizeof(char *)); 126 127 for (; *src != NULL; src++) 128 *dst++ = strsave(short2str(*src)); 129 *dst = NULL; 130 return (sdst); 131} 132 133Char * 134str2short(const char *src) 135{ 136 static struct Strbuf buf; /* = Strbuf_INIT; */ 137 138 if (src == NULL) 139 return (NULL); 140 141 buf.len = 0; 142 while (*src) { 143 Char wc; 144 145 src += one_mbtowc(&wc, src, MB_LEN_MAX); 146 Strbuf_append1(&buf, wc); 147 } 148 Strbuf_terminate(&buf); 149 return buf.s; 150} 151 152char * 153short2str(const Char *src) 154{ 155 static char *sdst = NULL; 156 static size_t dstsize = 0; 157 char *dst, *edst; 158 159 if (src == NULL) 160 return (NULL); 161 162 if (sdst == NULL) { 163 dstsize = MALLOC_INCR; 164 sdst = xmalloc((dstsize + MALLOC_SURPLUS) * sizeof(char)); 165 } 166 dst = sdst; 167 edst = &dst[dstsize]; 168 while (*src) { 169 dst += one_wctomb(dst, *src & CHAR); 170 src++; 171 if (dst >= edst) {
| 56 *pwc = (unsigned char)*s | INVALID_BYTE; 57 } 58 if (len <= 0) 59 len = 1; 60 return len; 61} 62 63size_t 64one_wctomb(char *s, wchar_t wchar) 65{ 66 int len; 67 68 if (wchar & INVALID_BYTE) { 69 s[0] = wchar & 0xFF; 70 len = 1; 71 } else { 72 len = wctomb(s, wchar); 73 if (len == -1) 74 s[0] = wchar; 75 if (len <= 0) 76 len = 1; 77 } 78 return len; 79} 80 81int 82rt_mbtowc(wchar_t *pwc, const char *s, size_t n) 83{ 84 int ret; 85 char back[MB_LEN_MAX]; 86 87 ret = mbtowc(pwc, s, n); 88 if (ret > 0 && (wctomb(back, *pwc) != ret || memcmp(s, back, ret) != 0)) 89 ret = -1; 90 return ret; 91} 92#endif 93 94#ifdef SHORT_STRINGS 95Char ** 96blk2short(char **src) 97{ 98 size_t n; 99 Char **sdst, **dst; 100 101 /* 102 * Count 103 */ 104 for (n = 0; src[n] != NULL; n++) 105 continue; 106 sdst = dst = xmalloc((n + 1) * sizeof(Char *)); 107 108 for (; *src != NULL; src++) 109 *dst++ = SAVE(*src); 110 *dst = NULL; 111 return (sdst); 112} 113 114char ** 115short2blk(Char **src) 116{ 117 size_t n; 118 char **sdst, **dst; 119 120 /* 121 * Count 122 */ 123 for (n = 0; src[n] != NULL; n++) 124 continue; 125 sdst = dst = xmalloc((n + 1) * sizeof(char *)); 126 127 for (; *src != NULL; src++) 128 *dst++ = strsave(short2str(*src)); 129 *dst = NULL; 130 return (sdst); 131} 132 133Char * 134str2short(const char *src) 135{ 136 static struct Strbuf buf; /* = Strbuf_INIT; */ 137 138 if (src == NULL) 139 return (NULL); 140 141 buf.len = 0; 142 while (*src) { 143 Char wc; 144 145 src += one_mbtowc(&wc, src, MB_LEN_MAX); 146 Strbuf_append1(&buf, wc); 147 } 148 Strbuf_terminate(&buf); 149 return buf.s; 150} 151 152char * 153short2str(const Char *src) 154{ 155 static char *sdst = NULL; 156 static size_t dstsize = 0; 157 char *dst, *edst; 158 159 if (src == NULL) 160 return (NULL); 161 162 if (sdst == NULL) { 163 dstsize = MALLOC_INCR; 164 sdst = xmalloc((dstsize + MALLOC_SURPLUS) * sizeof(char)); 165 } 166 dst = sdst; 167 edst = &dst[dstsize]; 168 while (*src) { 169 dst += one_wctomb(dst, *src & CHAR); 170 src++; 171 if (dst >= edst) {
|
| 172 char *wdst = dst; 173 char *wedst = edst; 174
|
172 dstsize += MALLOC_INCR; 173 sdst = xrealloc(sdst, (dstsize + MALLOC_SURPLUS) * sizeof(char)); 174 edst = &sdst[dstsize]; 175 dst = &edst[-MALLOC_INCR];
| 175 dstsize += MALLOC_INCR; 176 sdst = xrealloc(sdst, (dstsize + MALLOC_SURPLUS) * sizeof(char)); 177 edst = &sdst[dstsize]; 178 dst = &edst[-MALLOC_INCR];
|
| 179 while (wdst > wedst) { 180 dst++; 181 wdst--; 182 }
|
176 } 177 } 178 *dst = 0; 179 return (sdst); 180} 181 182#ifndef WIDE_STRINGS 183Char * 184s_strcpy(Char *dst, const Char *src) 185{ 186 Char *sdst; 187 188 sdst = dst; 189 while ((*dst++ = *src++) != '\0') 190 continue; 191 return (sdst); 192} 193 194Char * 195s_strncpy(Char *dst, const Char *src, size_t n) 196{ 197 Char *sdst; 198 199 if (n == 0) 200 return(dst); 201 202 sdst = dst; 203 do 204 if ((*dst++ = *src++) == '\0') { 205 while (--n != 0) 206 *dst++ = '\0'; 207 return(sdst); 208 } 209 while (--n != 0); 210 return (sdst); 211} 212 213Char * 214s_strcat(Char *dst, const Char *src) 215{ 216 Strcpy(Strend(dst), src); 217 return dst; 218} 219 220#ifdef NOTUSED 221Char * 222s_strncat(Char *dst, const Char *src, size_t n) 223{ 224 Char *sdst; 225 226 if (n == 0) 227 return (dst); 228 229 sdst = dst; 230 231 while (*dst) 232 dst++; 233 234 do 235 if ((*dst++ = *src++) == '\0') 236 return(sdst); 237 while (--n != 0) 238 continue; 239 240 *dst = '\0'; 241 return (sdst); 242} 243 244#endif 245 246Char * 247s_strchr(const Char *str, int ch) 248{ 249 do 250 if (*str == ch) 251 return ((Char *)(intptr_t)str); 252 while (*str++); 253 return (NULL); 254} 255 256Char * 257s_strrchr(const Char *str, int ch) 258{ 259 const Char *rstr; 260 261 rstr = NULL; 262 do 263 if (*str == ch) 264 rstr = str; 265 while (*str++); 266 return ((Char *)(intptr_t)rstr); 267} 268 269size_t 270s_strlen(const Char *str) 271{ 272 size_t n; 273 274 for (n = 0; *str++; n++) 275 continue; 276 return (n); 277} 278 279int 280s_strcmp(const Char *str1, const Char *str2) 281{ 282 for (; *str1 && *str1 == *str2; str1++, str2++) 283 continue; 284 /* 285 * The following case analysis is necessary so that characters which look 286 * negative collate low against normal characters but high against the 287 * end-of-string NUL. 288 */ 289 if (*str1 == '\0' && *str2 == '\0') 290 return (0); 291 else if (*str1 == '\0') 292 return (-1); 293 else if (*str2 == '\0') 294 return (1); 295 else 296 return (*str1 - *str2); 297} 298 299int 300s_strncmp(const Char *str1, const Char *str2, size_t n) 301{ 302 if (n == 0) 303 return (0); 304 do { 305 if (*str1 != *str2) { 306 /* 307 * The following case analysis is necessary so that characters 308 * which look negative collate low against normal characters 309 * but high against the end-of-string NUL. 310 */ 311 if (*str1 == '\0') 312 return (-1); 313 else if (*str2 == '\0') 314 return (1); 315 else 316 return (*str1 - *str2); 317 } 318 if (*str1 == '\0') 319 return(0); 320 str1++, str2++; 321 } while (--n != 0); 322 return(0); 323} 324#endif /* not WIDE_STRINGS */ 325 326int 327s_strcasecmp(const Char *str1, const Char *str2) 328{ 329#ifdef WIDE_STRINGS 330 wchar_t l1 = 0, l2 = 0; 331 for (; *str1 && ((*str1 == *str2 && (l1 = l2 = 0) == 0) || 332 (l1 = towlower(*str1)) == (l2 = towlower(*str2))); str1++, str2++) 333 continue; 334 335#else 336 unsigned char c1, c2, l1 = 0, l2 = 0; 337 for (; *str1 && ((*str1 == *str2 && (l1 = l2 = 0) == 0) || 338 ((c1 = (unsigned char)*str1) == *str1 && 339 (c2 = (unsigned char)*str2) == *str2 && 340 (l1 = tolower(c1)) == (l2 = tolower(c2)))); str1++, str2++) 341 continue; 342#endif 343 /* 344 * The following case analysis is necessary so that characters which look 345 * negative collate low against normal characters but high against the 346 * end-of-string NUL. 347 */ 348 if (*str1 == '\0' && *str2 == '\0') 349 return (0); 350 else if (*str1 == '\0') 351 return (-1); 352 else if (*str2 == '\0') 353 return (1); 354 else if (l1 == l2) /* They are zero when they are equal */ 355 return (*str1 - *str2); 356 else 357 return (l1 - l2); 358} 359 360Char * 361s_strnsave(const Char *s, size_t len) 362{ 363 Char *n; 364 365 n = xmalloc((len + 1) * sizeof (*n)); 366 memcpy(n, s, len * sizeof (*n)); 367 n[len] = '\0'; 368 return n; 369} 370 371Char * 372s_strsave(const Char *s) 373{ 374 Char *n; 375 size_t size; 376 377 if (s == NULL) 378 s = STRNULL; 379 size = (Strlen(s) + 1) * sizeof(*n); 380 n = xmalloc(size); 381 memcpy(n, s, size); 382 return (n); 383} 384 385Char * 386s_strspl(const Char *cp, const Char *dp) 387{ 388 Char *res, *ep; 389 const Char *p, *q; 390 391 if (!cp) 392 cp = STRNULL; 393 if (!dp) 394 dp = STRNULL; 395 for (p = cp; *p++;) 396 continue; 397 for (q = dp; *q++;) 398 continue; 399 res = xmalloc(((p - cp) + (q - dp) - 1) * sizeof(Char)); 400 for (ep = res, q = cp; (*ep++ = *q++) != '\0';) 401 continue; 402 for (ep--, q = dp; (*ep++ = *q++) != '\0';) 403 continue; 404 return (res); 405} 406 407Char * 408s_strend(const Char *cp) 409{ 410 if (!cp) 411 return ((Char *)(intptr_t) cp); 412 while (*cp) 413 cp++; 414 return ((Char *)(intptr_t) cp); 415} 416 417Char * 418s_strstr(const Char *s, const Char *t) 419{ 420 do { 421 const Char *ss = s; 422 const Char *tt = t; 423 424 do 425 if (*tt == '\0') 426 return ((Char *)(intptr_t) s); 427 while (*ss++ == *tt++); 428 } while (*s++ != '\0'); 429 return (NULL); 430} 431 432#else /* !SHORT_STRINGS */ 433char * 434caching_strip(const char *s) 435{ 436 static char *buf = NULL; 437 static size_t buf_size = 0; 438 size_t size; 439 440 if (s == NULL) 441 return NULL; 442 size = strlen(s) + 1; 443 if (buf_size < size) { 444 buf = xrealloc(buf, size); 445 buf_size = size; 446 } 447 memcpy(buf, s, size); 448 strip(buf); 449 return buf; 450} 451#endif 452 453char * 454short2qstr(const Char *src) 455{ 456 static char *sdst = NULL; 457 static size_t dstsize = 0; 458 char *dst, *edst; 459 460 if (src == NULL) 461 return (NULL); 462 463 if (sdst == NULL) { 464 dstsize = MALLOC_INCR; 465 sdst = xmalloc((dstsize + MALLOC_SURPLUS) * sizeof(char)); 466 } 467 dst = sdst; 468 edst = &dst[dstsize]; 469 while (*src) { 470 if (*src & QUOTE) { 471 *dst++ = '\\'; 472 if (dst == edst) { 473 dstsize += MALLOC_INCR; 474 sdst = xrealloc(sdst, 475 (dstsize + MALLOC_SURPLUS) * sizeof(char)); 476 edst = &sdst[dstsize]; 477 dst = &edst[-MALLOC_INCR]; 478 } 479 } 480 dst += one_wctomb(dst, *src & CHAR); 481 src++; 482 if (dst >= edst) {
| 183 } 184 } 185 *dst = 0; 186 return (sdst); 187} 188 189#ifndef WIDE_STRINGS 190Char * 191s_strcpy(Char *dst, const Char *src) 192{ 193 Char *sdst; 194 195 sdst = dst; 196 while ((*dst++ = *src++) != '\0') 197 continue; 198 return (sdst); 199} 200 201Char * 202s_strncpy(Char *dst, const Char *src, size_t n) 203{ 204 Char *sdst; 205 206 if (n == 0) 207 return(dst); 208 209 sdst = dst; 210 do 211 if ((*dst++ = *src++) == '\0') { 212 while (--n != 0) 213 *dst++ = '\0'; 214 return(sdst); 215 } 216 while (--n != 0); 217 return (sdst); 218} 219 220Char * 221s_strcat(Char *dst, const Char *src) 222{ 223 Strcpy(Strend(dst), src); 224 return dst; 225} 226 227#ifdef NOTUSED 228Char * 229s_strncat(Char *dst, const Char *src, size_t n) 230{ 231 Char *sdst; 232 233 if (n == 0) 234 return (dst); 235 236 sdst = dst; 237 238 while (*dst) 239 dst++; 240 241 do 242 if ((*dst++ = *src++) == '\0') 243 return(sdst); 244 while (--n != 0) 245 continue; 246 247 *dst = '\0'; 248 return (sdst); 249} 250 251#endif 252 253Char * 254s_strchr(const Char *str, int ch) 255{ 256 do 257 if (*str == ch) 258 return ((Char *)(intptr_t)str); 259 while (*str++); 260 return (NULL); 261} 262 263Char * 264s_strrchr(const Char *str, int ch) 265{ 266 const Char *rstr; 267 268 rstr = NULL; 269 do 270 if (*str == ch) 271 rstr = str; 272 while (*str++); 273 return ((Char *)(intptr_t)rstr); 274} 275 276size_t 277s_strlen(const Char *str) 278{ 279 size_t n; 280 281 for (n = 0; *str++; n++) 282 continue; 283 return (n); 284} 285 286int 287s_strcmp(const Char *str1, const Char *str2) 288{ 289 for (; *str1 && *str1 == *str2; str1++, str2++) 290 continue; 291 /* 292 * The following case analysis is necessary so that characters which look 293 * negative collate low against normal characters but high against the 294 * end-of-string NUL. 295 */ 296 if (*str1 == '\0' && *str2 == '\0') 297 return (0); 298 else if (*str1 == '\0') 299 return (-1); 300 else if (*str2 == '\0') 301 return (1); 302 else 303 return (*str1 - *str2); 304} 305 306int 307s_strncmp(const Char *str1, const Char *str2, size_t n) 308{ 309 if (n == 0) 310 return (0); 311 do { 312 if (*str1 != *str2) { 313 /* 314 * The following case analysis is necessary so that characters 315 * which look negative collate low against normal characters 316 * but high against the end-of-string NUL. 317 */ 318 if (*str1 == '\0') 319 return (-1); 320 else if (*str2 == '\0') 321 return (1); 322 else 323 return (*str1 - *str2); 324 } 325 if (*str1 == '\0') 326 return(0); 327 str1++, str2++; 328 } while (--n != 0); 329 return(0); 330} 331#endif /* not WIDE_STRINGS */ 332 333int 334s_strcasecmp(const Char *str1, const Char *str2) 335{ 336#ifdef WIDE_STRINGS 337 wchar_t l1 = 0, l2 = 0; 338 for (; *str1 && ((*str1 == *str2 && (l1 = l2 = 0) == 0) || 339 (l1 = towlower(*str1)) == (l2 = towlower(*str2))); str1++, str2++) 340 continue; 341 342#else 343 unsigned char c1, c2, l1 = 0, l2 = 0; 344 for (; *str1 && ((*str1 == *str2 && (l1 = l2 = 0) == 0) || 345 ((c1 = (unsigned char)*str1) == *str1 && 346 (c2 = (unsigned char)*str2) == *str2 && 347 (l1 = tolower(c1)) == (l2 = tolower(c2)))); str1++, str2++) 348 continue; 349#endif 350 /* 351 * The following case analysis is necessary so that characters which look 352 * negative collate low against normal characters but high against the 353 * end-of-string NUL. 354 */ 355 if (*str1 == '\0' && *str2 == '\0') 356 return (0); 357 else if (*str1 == '\0') 358 return (-1); 359 else if (*str2 == '\0') 360 return (1); 361 else if (l1 == l2) /* They are zero when they are equal */ 362 return (*str1 - *str2); 363 else 364 return (l1 - l2); 365} 366 367Char * 368s_strnsave(const Char *s, size_t len) 369{ 370 Char *n; 371 372 n = xmalloc((len + 1) * sizeof (*n)); 373 memcpy(n, s, len * sizeof (*n)); 374 n[len] = '\0'; 375 return n; 376} 377 378Char * 379s_strsave(const Char *s) 380{ 381 Char *n; 382 size_t size; 383 384 if (s == NULL) 385 s = STRNULL; 386 size = (Strlen(s) + 1) * sizeof(*n); 387 n = xmalloc(size); 388 memcpy(n, s, size); 389 return (n); 390} 391 392Char * 393s_strspl(const Char *cp, const Char *dp) 394{ 395 Char *res, *ep; 396 const Char *p, *q; 397 398 if (!cp) 399 cp = STRNULL; 400 if (!dp) 401 dp = STRNULL; 402 for (p = cp; *p++;) 403 continue; 404 for (q = dp; *q++;) 405 continue; 406 res = xmalloc(((p - cp) + (q - dp) - 1) * sizeof(Char)); 407 for (ep = res, q = cp; (*ep++ = *q++) != '\0';) 408 continue; 409 for (ep--, q = dp; (*ep++ = *q++) != '\0';) 410 continue; 411 return (res); 412} 413 414Char * 415s_strend(const Char *cp) 416{ 417 if (!cp) 418 return ((Char *)(intptr_t) cp); 419 while (*cp) 420 cp++; 421 return ((Char *)(intptr_t) cp); 422} 423 424Char * 425s_strstr(const Char *s, const Char *t) 426{ 427 do { 428 const Char *ss = s; 429 const Char *tt = t; 430 431 do 432 if (*tt == '\0') 433 return ((Char *)(intptr_t) s); 434 while (*ss++ == *tt++); 435 } while (*s++ != '\0'); 436 return (NULL); 437} 438 439#else /* !SHORT_STRINGS */ 440char * 441caching_strip(const char *s) 442{ 443 static char *buf = NULL; 444 static size_t buf_size = 0; 445 size_t size; 446 447 if (s == NULL) 448 return NULL; 449 size = strlen(s) + 1; 450 if (buf_size < size) { 451 buf = xrealloc(buf, size); 452 buf_size = size; 453 } 454 memcpy(buf, s, size); 455 strip(buf); 456 return buf; 457} 458#endif 459 460char * 461short2qstr(const Char *src) 462{ 463 static char *sdst = NULL; 464 static size_t dstsize = 0; 465 char *dst, *edst; 466 467 if (src == NULL) 468 return (NULL); 469 470 if (sdst == NULL) { 471 dstsize = MALLOC_INCR; 472 sdst = xmalloc((dstsize + MALLOC_SURPLUS) * sizeof(char)); 473 } 474 dst = sdst; 475 edst = &dst[dstsize]; 476 while (*src) { 477 if (*src & QUOTE) { 478 *dst++ = '\\'; 479 if (dst == edst) { 480 dstsize += MALLOC_INCR; 481 sdst = xrealloc(sdst, 482 (dstsize + MALLOC_SURPLUS) * sizeof(char)); 483 edst = &sdst[dstsize]; 484 dst = &edst[-MALLOC_INCR]; 485 } 486 } 487 dst += one_wctomb(dst, *src & CHAR); 488 src++; 489 if (dst >= edst) {
|
| 490 ptrdiff_t i = dst - edst;
|
483 dstsize += MALLOC_INCR; 484 sdst = xrealloc(sdst, (dstsize + MALLOC_SURPLUS) * sizeof(char)); 485 edst = &sdst[dstsize];
| 491 dstsize += MALLOC_INCR; 492 sdst = xrealloc(sdst, (dstsize + MALLOC_SURPLUS) * sizeof(char)); 493 edst = &sdst[dstsize];
|
486 dst = &edst[-MALLOC_INCR];
| 494 dst = &edst[-MALLOC_INCR + i];
|
487 } 488 } 489 *dst = 0; 490 return (sdst); 491} 492
| 495 } 496 } 497 *dst = 0; 498 return (sdst); 499} 500
|
| 501struct blk_buf * 502bb_alloc() 503{ 504 return xcalloc(1, sizeof(struct blk_buf)); 505} 506
|
493static void 494bb_store(struct blk_buf *bb, Char *str) 495{ 496 if (bb->len == bb->size) { /* Keep space for terminating NULL */ 497 if (bb->size == 0) 498 bb->size = 16; /* Arbitrary */ 499 else 500 bb->size *= 2; 501 bb->vec = xrealloc(bb->vec, bb->size * sizeof (*bb->vec)); 502 } 503 bb->vec[bb->len] = str; 504} 505 506void 507bb_append(struct blk_buf *bb, Char *str) 508{ 509 bb_store(bb, str); 510 bb->len++; 511} 512 513void 514bb_cleanup(void *xbb) 515{ 516 struct blk_buf *bb; 517 size_t i; 518 519 bb = xbb; 520 for (i = 0; i < bb->len; i++) 521 xfree(bb->vec[i]); 522 xfree(bb->vec); 523} 524
| 507static void 508bb_store(struct blk_buf *bb, Char *str) 509{ 510 if (bb->len == bb->size) { /* Keep space for terminating NULL */ 511 if (bb->size == 0) 512 bb->size = 16; /* Arbitrary */ 513 else 514 bb->size *= 2; 515 bb->vec = xrealloc(bb->vec, bb->size * sizeof (*bb->vec)); 516 } 517 bb->vec[bb->len] = str; 518} 519 520void 521bb_append(struct blk_buf *bb, Char *str) 522{ 523 bb_store(bb, str); 524 bb->len++; 525} 526 527void 528bb_cleanup(void *xbb) 529{ 530 struct blk_buf *bb; 531 size_t i; 532 533 bb = xbb; 534 for (i = 0; i < bb->len; i++) 535 xfree(bb->vec[i]); 536 xfree(bb->vec); 537} 538
|
| 539void 540bb_free(void *bb) 541{ 542 bb_cleanup(bb); 543 xfree(bb); 544} 545
|
525Char ** 526bb_finish(struct blk_buf *bb) 527{ 528 bb_store(bb, NULL); 529 return xrealloc(bb->vec, (bb->len + 1) * sizeof (*bb->vec)); 530} 531 532#define DO_STRBUF(STRBUF, CHAR, STRLEN) \
| 546Char ** 547bb_finish(struct blk_buf *bb) 548{ 549 bb_store(bb, NULL); 550 return xrealloc(bb->vec, (bb->len + 1) * sizeof (*bb->vec)); 551} 552 553#define DO_STRBUF(STRBUF, CHAR, STRLEN) \
|
| 554 \ 555struct STRBUF * \ 556STRBUF##_alloc(void) \ 557{ \ 558 return xcalloc(1, sizeof(struct STRBUF)); \ 559} \ 560 \
|
533static void \ 534STRBUF##_store1(struct STRBUF *buf, CHAR c) \ 535{ \ 536 if (buf->size == buf->len) { \ 537 if (buf->size == 0) \ 538 buf->size = 64; /* Arbitrary */ \ 539 else \ 540 buf->size *= 2; \ 541 buf->s = xrealloc(buf->s, buf->size * sizeof(*buf->s)); \ 542 } \ 543 buf->s[buf->len] = c; \ 544} \ 545 \ 546/* Like strbuf_append1(buf, '\0'), but don't advance len */ \ 547void \ 548STRBUF##_terminate(struct STRBUF *buf) \ 549{ \ 550 STRBUF##_store1(buf, '\0'); \ 551} \ 552 \ 553void \ 554STRBUF##_append1(struct STRBUF *buf, CHAR c) \ 555{ \ 556 STRBUF##_store1(buf, c); \ 557 buf->len++; \ 558} \ 559 \ 560void \ 561STRBUF##_appendn(struct STRBUF *buf, const CHAR *s, size_t len) \ 562{ \ 563 if (buf->size < buf->len + len) { \ 564 if (buf->size == 0) \ 565 buf->size = 64; /* Arbitrary */ \ 566 while (buf->size < buf->len + len) \ 567 buf->size *= 2; \ 568 buf->s = xrealloc(buf->s, buf->size * sizeof(*buf->s)); \ 569 } \ 570 memcpy(buf->s + buf->len, s, len * sizeof(*buf->s)); \ 571 buf->len += len; \ 572} \ 573 \ 574void \ 575STRBUF##_append(struct STRBUF *buf, const CHAR *s) \ 576{ \ 577 STRBUF##_appendn(buf, s, STRLEN(s)); \ 578} \ 579 \ 580CHAR * \ 581STRBUF##_finish(struct STRBUF *buf) \ 582{ \ 583 STRBUF##_append1(buf, 0); \ 584 return xrealloc(buf->s, buf->len * sizeof(*buf->s)); \ 585} \ 586 \ 587void \ 588STRBUF##_cleanup(void *xbuf) \ 589{ \ 590 struct STRBUF *buf; \ 591 \ 592 buf = xbuf; \ 593 xfree(buf->s); \ 594} \ 595 \
| 561static void \ 562STRBUF##_store1(struct STRBUF *buf, CHAR c) \ 563{ \ 564 if (buf->size == buf->len) { \ 565 if (buf->size == 0) \ 566 buf->size = 64; /* Arbitrary */ \ 567 else \ 568 buf->size *= 2; \ 569 buf->s = xrealloc(buf->s, buf->size * sizeof(*buf->s)); \ 570 } \ 571 buf->s[buf->len] = c; \ 572} \ 573 \ 574/* Like strbuf_append1(buf, '\0'), but don't advance len */ \ 575void \ 576STRBUF##_terminate(struct STRBUF *buf) \ 577{ \ 578 STRBUF##_store1(buf, '\0'); \ 579} \ 580 \ 581void \ 582STRBUF##_append1(struct STRBUF *buf, CHAR c) \ 583{ \ 584 STRBUF##_store1(buf, c); \ 585 buf->len++; \ 586} \ 587 \ 588void \ 589STRBUF##_appendn(struct STRBUF *buf, const CHAR *s, size_t len) \ 590{ \ 591 if (buf->size < buf->len + len) { \ 592 if (buf->size == 0) \ 593 buf->size = 64; /* Arbitrary */ \ 594 while (buf->size < buf->len + len) \ 595 buf->size *= 2; \ 596 buf->s = xrealloc(buf->s, buf->size * sizeof(*buf->s)); \ 597 } \ 598 memcpy(buf->s + buf->len, s, len * sizeof(*buf->s)); \ 599 buf->len += len; \ 600} \ 601 \ 602void \ 603STRBUF##_append(struct STRBUF *buf, const CHAR *s) \ 604{ \ 605 STRBUF##_appendn(buf, s, STRLEN(s)); \ 606} \ 607 \ 608CHAR * \ 609STRBUF##_finish(struct STRBUF *buf) \ 610{ \ 611 STRBUF##_append1(buf, 0); \ 612 return xrealloc(buf->s, buf->len * sizeof(*buf->s)); \ 613} \ 614 \ 615void \ 616STRBUF##_cleanup(void *xbuf) \ 617{ \ 618 struct STRBUF *buf; \ 619 \ 620 buf = xbuf; \ 621 xfree(buf->s); \ 622} \ 623 \
|
| 624void \ 625STRBUF##_free(void *xbuf) \ 626{ \ 627 STRBUF##_cleanup(xbuf); \ 628 xfree(xbuf); \ 629} \ 630 \
|
596const struct STRBUF STRBUF##_init /* = STRBUF##_INIT; */ 597 598DO_STRBUF(strbuf, char, strlen); 599DO_STRBUF(Strbuf, Char, Strlen);
| 631const struct STRBUF STRBUF##_init /* = STRBUF##_INIT; */ 632 633DO_STRBUF(strbuf, char, strlen); 634DO_STRBUF(Strbuf, Char, Strlen);
|