2 * Copyright 2016 Jakub Klama <jceel@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted providing that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
29 * Minimal libsbuf reimplementation for Mac OS X.
39 #define SBUF_INITIAL_SIZE 128
46 s = malloc(sizeof(struct sbuf));
47 s->s_buf = calloc(1, SBUF_INITIAL_SIZE + 1);
48 s->s_capacity = s->s_buf != NULL ? SBUF_INITIAL_SIZE : 0;
55 sbuf_cat(struct sbuf *s, const char *str)
57 int req = (int)strlen(str);
59 if (s->s_size + req >= s->s_capacity) {
60 s->s_capacity = s->s_size + req + 1;
61 s->s_buf = realloc(s->s_buf, (size_t)s->s_capacity);
66 strcpy(s->s_buf + s->s_size, str);
73 sbuf_printf(struct sbuf *s, const char *fmt, ...)
79 ret = sbuf_vprintf(s, fmt, ap);
86 sbuf_vprintf(struct sbuf *s, const char *fmt, va_list args)
92 req = vsnprintf(NULL, 0, fmt, copy);
95 if (s->s_size + req >= s->s_capacity) {
96 s->s_capacity = s->s_size + req + 1;
97 s->s_buf = realloc(s->s_buf, (size_t)s->s_capacity);
102 req = vsnprintf(s->s_buf + s->s_size, req + 1, fmt, args);
109 sbuf_data(struct sbuf *s)
115 sbuf_finish(struct sbuf *s)
117 if (s->s_buf != NULL)
118 s->s_buf[s->s_size] = '\0';
123 sbuf_delete(struct sbuf *s)