xref: /netbsd/usr.sbin/netgroup_mkdb/str.c (revision 550147bd)
1*550147bdSsnj /*	$NetBSD: str.c,v 1.7 2009/10/21 01:07:47 snj Exp $	*/
24871cd13Schristos 
329d4c186Schristos /*
429d4c186Schristos  * Copyright (c) 1994 Christos Zoulas
529d4c186Schristos  * All rights reserved.
629d4c186Schristos  *
729d4c186Schristos  * Redistribution and use in source and binary forms, with or without
829d4c186Schristos  * modification, are permitted provided that the following conditions
929d4c186Schristos  * are met:
1029d4c186Schristos  * 1. Redistributions of source code must retain the above copyright
1129d4c186Schristos  *    notice, this list of conditions and the following disclaimer.
1229d4c186Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1329d4c186Schristos  *    notice, this list of conditions and the following disclaimer in the
1429d4c186Schristos  *    documentation and/or other materials provided with the distribution.
1529d4c186Schristos  *
1629d4c186Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1729d4c186Schristos  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1829d4c186Schristos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1929d4c186Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
2029d4c186Schristos  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2129d4c186Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2229d4c186Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2329d4c186Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2429d4c186Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2529d4c186Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2629d4c186Schristos  * SUCH DAMAGE.
2729d4c186Schristos  */
2829d4c186Schristos 
294eb76530Slukem #include <sys/cdefs.h>
3029d4c186Schristos #ifndef lint
31*550147bdSsnj __RCSID("$NetBSD: str.c,v 1.7 2009/10/21 01:07:47 snj Exp $");
3229d4c186Schristos #endif
3329d4c186Schristos 
3429d4c186Schristos /*
3529d4c186Schristos  * Counted strings
3629d4c186Schristos  */
3729d4c186Schristos #include <stdlib.h>
3806b20ecaScgd #include <string.h>
391b75c508Schristos #include <err.h>
401b75c508Schristos #include <util.h>
4129d4c186Schristos 
4229d4c186Schristos #include "str.h"
4329d4c186Schristos 
4429d4c186Schristos /*
4529d4c186Schristos  * str_init(): Initialize string
4629d4c186Schristos  */
4729d4c186Schristos void
str_init(struct string * s)48bf31de00Schristos str_init(struct string  *s)
4929d4c186Schristos {
5029d4c186Schristos 	s->s_str = NULL;
5129d4c186Schristos 	s->s_len = 0;
5229d4c186Schristos }
5329d4c186Schristos 
5429d4c186Schristos 
5529d4c186Schristos /*
5629d4c186Schristos  * str_append(): Append string allocating buffer as necessary
5729d4c186Schristos  */
5829d4c186Schristos void
str_append(struct string * buf,const char * str,int del)59bf31de00Schristos str_append(struct string  *buf, const char *str, int del)
6029d4c186Schristos {
6129d4c186Schristos 	size_t          len = strlen(str) + 1;
6229d4c186Schristos 
6329d4c186Schristos 	if (buf->s_str == NULL)
6429d4c186Schristos 		buf->s_str = emalloc(len);
6529d4c186Schristos 	else {
6629d4c186Schristos 		buf->s_str = erealloc(buf->s_str, buf->s_len + len +
6729d4c186Schristos 						  (del ? 2 : 1));
6829d4c186Schristos 		if (del)
6929d4c186Schristos 			buf->s_str[buf->s_len++] = del;
7029d4c186Schristos 	}
7129d4c186Schristos 
7229d4c186Schristos 	memcpy(&buf->s_str[buf->s_len], str, len);
7329d4c186Schristos 	buf->s_len += len - 1;
7429d4c186Schristos }
7529d4c186Schristos 
7629d4c186Schristos /*
7729d4c186Schristos  * str_prepend(): Prepend string allocating buffer as necessary
7829d4c186Schristos  */
7929d4c186Schristos void
str_prepend(struct string * buf,const char * str,int del)80bf31de00Schristos str_prepend(struct string *buf, const char *str, int del)
8129d4c186Schristos {
8229d4c186Schristos 	char           *ptr, *sptr;
8329d4c186Schristos 	size_t          len = strlen(str) + 1;
8429d4c186Schristos 
8529d4c186Schristos 	sptr = ptr = emalloc(buf->s_len + len + (del ? 2 : 1));
8629d4c186Schristos 
8729d4c186Schristos 	if (del)
8829d4c186Schristos 		*ptr++ = del;
8929d4c186Schristos 
9029d4c186Schristos 	memcpy(ptr, str, len);
9129d4c186Schristos 
9229d4c186Schristos 	if (buf->s_str) {
9329d4c186Schristos 		memcpy(&ptr[len - 1], buf->s_str, buf->s_len + 1);
9429d4c186Schristos 		free(buf->s_str);
9529d4c186Schristos 	}
9629d4c186Schristos 
9729d4c186Schristos 	buf->s_str = sptr;
9829d4c186Schristos 	buf->s_len += del ? len : len - 1;
9929d4c186Schristos }
10029d4c186Schristos 
10129d4c186Schristos /*
10229d4c186Schristos  * str_free(): Free a string
10329d4c186Schristos  */
10429d4c186Schristos void
str_free(struct string * s)105bf31de00Schristos str_free(struct string *s)
10629d4c186Schristos {
10729d4c186Schristos 	free(s->s_str);
10829d4c186Schristos 	s->s_str = NULL;
10929d4c186Schristos 	s->s_len = 0;
11029d4c186Schristos }
111