summaryrefslogtreecommitdiff
path: root/source3/lib/talloc/talloc.c
diff options
context:
space:
mode:
authorJeremy Allison <jra@samba.org>2007-09-14 17:40:58 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 12:30:47 -0500
commit47107b2cdea7c58c337639f6d1aaab94390bf0f6 (patch)
tree9b9572040dd6f838ece19c67ab9347010bea0575 /source3/lib/talloc/talloc.c
parent539635cd94dd21dbc72f070fbd8a54c726091f5a (diff)
downloadsamba-47107b2cdea7c58c337639f6d1aaab94390bf0f6.tar.gz
samba-47107b2cdea7c58c337639f6d1aaab94390bf0f6.tar.bz2
samba-47107b2cdea7c58c337639f6d1aaab94390bf0f6.zip
r25164: Add talloc_asprintf_append_buffer() and the docs for it.
Jeremy. (This used to be commit 2243a73d650319208aebbbaf65dfba2a508a7c10)
Diffstat (limited to 'source3/lib/talloc/talloc.c')
-rw-r--r--source3/lib/talloc/talloc.c65
1 files changed, 63 insertions, 2 deletions
diff --git a/source3/lib/talloc/talloc.c b/source3/lib/talloc/talloc.c
index c3e5d2c076..7920a66d64 100644
--- a/source3/lib/talloc/talloc.c
+++ b/source3/lib/talloc/talloc.c
@@ -1223,7 +1223,8 @@ char *talloc_asprintf(const void *t, const char *fmt, ...)
/**
* Realloc @p s to append the formatted result of @p fmt and @p ap,
* and return @p s, which may have moved. Good for gradually
- * accumulating output into a string buffer.
+ * accumulating output into a string buffer. Appends at the end
+ * of the string.
**/
char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
{
@@ -1245,7 +1246,52 @@ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
/* Either the vsnprintf failed or the format resulted in
* no characters being formatted. In the former case, we
* ought to return NULL, in the latter we ought to return
- * the original string. Most current callers of this
+ * the original string. Most current callers of this
+ * function expect it to never return NULL.
+ */
+ return s;
+ }
+
+ s = talloc_realloc(NULL, s, char, s_len + len+1);
+ if (!s) return NULL;
+
+ va_copy(ap2, ap);
+ vsnprintf(s+s_len, len+1, fmt, ap2);
+ va_end(ap2);
+ _talloc_set_name_const(s, s);
+
+ return s;
+}
+
+/**
+ * Realloc @p s to append the formatted result of @p fmt and @p ap,
+ * and return @p s, which may have moved. Always appends at the
+ * end of the talloc'ed buffer, not the end of the string.
+ **/
+char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
+{
+ struct talloc_chunk *tc;
+ int len, s_len;
+ va_list ap2;
+ char c;
+
+ if (s == NULL) {
+ return talloc_vasprintf(NULL, fmt, ap);
+ }
+
+ tc = talloc_chunk_from_ptr(s);
+
+ s_len = tc->size - 1;
+
+ va_copy(ap2, ap);
+ len = vsnprintf(&c, 1, fmt, ap2);
+ va_end(ap2);
+
+ if (len <= 0) {
+ /* Either the vsnprintf failed or the format resulted in
+ * no characters being formatted. In the former case, we
+ * ought to return NULL, in the latter we ought to return
+ * the original string. Most current callers of this
* function expect it to never return NULL.
*/
return s;
@@ -1278,6 +1324,21 @@ char *talloc_asprintf_append(char *s, const char *fmt, ...)
}
/*
+ Realloc @p s to append the formatted result of @p fmt and return @p
+ s, which may have moved. Good for gradually accumulating output
+ into a buffer.
+ */
+char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ s = talloc_vasprintf_append_buffer(s, fmt, ap);
+ va_end(ap);
+ return s;
+}
+
+/*
alloc an array, checking for integer overflow in the array size
*/
void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)