1 /* 2 * Copyright (c) 1994 Christos Zoulas 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 Christos Zoulas. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 static char *rcsid = "$Id: str.c,v 1.3 2005/05/16 03:12:59 deraadt Exp $"; 34 #endif 35 36 /* 37 * Counted strings 38 */ 39 #include <stdlib.h> 40 #include <string.h> 41 42 #include "util.h" 43 #include "str.h" 44 45 /* 46 * str_init(): Initialize string 47 */ 48 void 49 str_init(struct string *s) 50 { 51 s->s_str = NULL; 52 s->s_len = 0; 53 } 54 55 56 /* 57 * str_append(): Append string allocating buffer as necessary 58 */ 59 void 60 str_append(struct string *buf, const char *str, int del) 61 { 62 size_t len = strlen(str) + 1; 63 64 if (buf->s_str == NULL) 65 buf->s_str = emalloc(len); 66 else { 67 buf->s_str = erealloc(buf->s_str, buf->s_len + 68 len + (del ? 2 : 1)); 69 if (del) 70 buf->s_str[buf->s_len++] = del; 71 } 72 73 memcpy(&buf->s_str[buf->s_len], str, len); 74 buf->s_len += len - 1; 75 } 76 77 /* 78 * str_prepend(): Prepend string allocating buffer as necessary 79 */ 80 void 81 str_prepend(struct string *buf, const char *str, int del) 82 { 83 char *ptr, *sptr; 84 size_t len = strlen(str) + 1; 85 86 sptr = ptr = emalloc(buf->s_len + len + (del ? 2 : 1)); 87 88 if (del) 89 *ptr++ = del; 90 91 memcpy(ptr, str, len); 92 93 if (buf->s_str) { 94 memcpy(&ptr[len - 1], buf->s_str, buf->s_len + 1); 95 free(buf->s_str); 96 } 97 98 buf->s_str = sptr; 99 buf->s_len += del ? len : len - 1; 100 } 101 102 /* 103 * str_free(): Free a string 104 */ 105 void 106 str_free(struct string *s) 107 { 108 free(s->s_str); 109 s->s_str = NULL; 110 s->s_len = 0; 111 } 112