xref: /illumos-gate/usr/src/lib/lib9p/common/sbuf/sbuf.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * Redistribution and use in source and binary forms, with or without
3  * modification, are permitted providing that the following conditions
4  * are met:
5  * 1. Redistributions of source code must retain the above copyright
6  *    notice, this list of conditions and the following disclaimer.
7  * 2. Redistributions in binary form must reproduce the above copyright
8  *    notice, this list of conditions and the following disclaimer in the
9  *    documentation and/or other materials provided with the distribution.
10  *
11  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
15  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
17  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  * POSSIBILITY OF SUCH DAMAGE.
22  *
23  */
24 
25 /*
26  * Minimal libsbuf wrapper around libcustr for illumos.
27  */
28 
29 #include <stdlib.h>
30 #include "sbuf.h"
31 
32 struct sbuf *
33 sbuf_new_auto()
34 {
35 	struct sbuf *s;
36 
37 	s = malloc(sizeof(struct sbuf));
38 	if (s == NULL)
39 		return (s);
40 	if (custr_alloc(&s->s_custr) != 0) {
41 		free(s);
42 		return (NULL);
43 	}
44 	return (s);
45 }
46 
47 int
48 sbuf_printf(struct sbuf *s, const char *fmt, ...)
49 {
50 	int ret;
51 	va_list ap;
52 
53 	va_start(ap, fmt);
54 	ret = custr_append_vprintf(s->s_custr, fmt, ap);
55 	va_end(ap);
56 
57 	return (ret);
58 }
59 
60 void
61 sbuf_delete(struct sbuf *s)
62 {
63 	custr_free(s->s_custr);
64 	free(s);
65 }
66