summaryrefslogtreecommitdiff
path: root/source3/lib/util_str.c
diff options
context:
space:
mode:
authorGerald Carter <jerry@samba.org>2005-06-08 22:10:34 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 10:57:08 -0500
commitfed660877c16562265327c6093ea645cf4176b5c (patch)
treee92ae1356542ba095d806bbe1093fa56fbc8ddcc /source3/lib/util_str.c
parent66bb4f03c3466205488f72e4878e8801c5bbb295 (diff)
downloadsamba-fed660877c16562265327c6093ea645cf4176b5c.tar.gz
samba-fed660877c16562265327c6093ea645cf4176b5c.tar.bz2
samba-fed660877c16562265327c6093ea645cf4176b5c.zip
r7415: * big change -- volker's new async winbindd from trunk
(This used to be commit a0ac9a8ffd4af31a0ebc423b4acbb2f043d865b8)
Diffstat (limited to 'source3/lib/util_str.c')
-rw-r--r--source3/lib/util_str.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c
index b13ec1f0da..f600d1704e 100644
--- a/source3/lib/util_str.c
+++ b/source3/lib/util_str.c
@@ -2128,3 +2128,69 @@ BOOL add_string_to_array(TALLOC_CTX *mem_ctx,
*num += 1;
return True;
}
+
+/* Append an sprintf'ed string. Double buffer size on demand. Usable without
+ * error checking in between. The indiation that something weird happened is
+ * string==NULL */
+
+void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len,
+ size_t *bufsize, const char *fmt, ...)
+{
+ va_list ap;
+ char *newstr;
+ int ret;
+ BOOL increased;
+
+ /* len<0 is an internal marker that something failed */
+ if (*len < 0)
+ goto error;
+
+ if (*string == NULL) {
+ if (*bufsize == 0)
+ *bufsize = 128;
+
+ if (mem_ctx != NULL)
+ *string = TALLOC_ARRAY(mem_ctx, char, *bufsize);
+ else
+ *string = SMB_MALLOC_ARRAY(char, *bufsize);
+
+ if (*string == NULL)
+ goto error;
+ }
+
+ va_start(ap, fmt);
+ ret = vasprintf(&newstr, fmt, ap);
+ va_end(ap);
+
+ if (ret < 0)
+ goto error;
+
+ increased = False;
+
+ while ((*len)+ret >= *bufsize) {
+ increased = True;
+ *bufsize *= 2;
+ if (*bufsize >= (1024*1024*256))
+ goto error;
+ }
+
+ if (increased) {
+ if (mem_ctx != NULL)
+ *string = TALLOC_REALLOC_ARRAY(mem_ctx, *string, char,
+ *bufsize);
+ else
+ *string = SMB_REALLOC_ARRAY(*string, char, *bufsize);
+
+ if (*string == NULL)
+ goto error;
+ }
+
+ StrnCpy((*string)+(*len), newstr, ret);
+ (*len) += ret;
+ free(newstr);
+ return;
+
+ error:
+ *len = -1;
+ *string = NULL;
+}