summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2020-01-24 23:54:40 +0000
committerDamien Miller <djm@mindrot.org>2020-01-25 11:27:29 +1100
commit8075fccbd4f70a4371acabcfb47562471ff0de6f (patch)
treee8e16278bb3aa2eb8d124290c1495d0fd2140ca9 /misc.c
parentd15c8adf2c6f1a6b4845131074383eb9c3d05c3d (diff)
downloadopenssh-git-8075fccbd4f70a4371acabcfb47562471ff0de6f.tar.gz
upstream: add xextendf() to extend a string with a format
(reallocating as necessary). ok aja@ as part of a larger diff OpenBSD-Commit-ID: 30796b50d330b3e0e201747fe40cdf9aa70a77f9
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c29
1 files changed, 28 insertions, 1 deletions
diff --git a/misc.c b/misc.c
index f25b8cf5..74e01a4c 100644
--- a/misc.c
+++ b/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.144 2020/01/23 07:10:22 dtucker Exp $ */
+/* $OpenBSD: misc.c,v 1.145 2020/01/24 23:54:40 djm Exp $ */
/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
* Copyright (c) 2005,2006 Damien Miller. All rights reserved.
@@ -1244,6 +1244,33 @@ tohex(const void *vp, size_t l)
return (r);
}
+/*
+ * Extend string *sp by the specified format. If *sp is not NULL (or empty),
+ * then the separator 'sep' will be prepended before the formatted arguments.
+ * Extended strings are heap allocated.
+ */
+void
+xextendf(char **sp, const char *sep, const char *fmt, ...)
+{
+ va_list ap;
+ char *tmp1, *tmp2;
+
+ va_start(ap, fmt);
+ xvasprintf(&tmp1, fmt, ap);
+ va_end(ap);
+
+ if (*sp == NULL || **sp == '\0') {
+ free(*sp);
+ *sp = tmp1;
+ return;
+ }
+ xasprintf(&tmp2, "%s%s%s", *sp, sep == NULL ? "" : sep, tmp1);
+ free(tmp1);
+ free(*sp);
+ *sp = tmp2;
+}
+
+
u_int64_t
get_u64(const void *vp)
{