]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
upstream: add xextendf() to extend a string with a format
authordjm@openbsd.org <djm@openbsd.org>
Fri, 24 Jan 2020 23:54:40 +0000 (23:54 +0000)
committerDamien Miller <djm@mindrot.org>
Sat, 25 Jan 2020 00:27:29 +0000 (11:27 +1100)
(reallocating as necessary). ok aja@ as part of a larger diff

OpenBSD-Commit-ID: 30796b50d330b3e0e201747fe40cdf9aa70a77f9

misc.c
misc.h

diff --git a/misc.c b/misc.c
index f25b8cf5cc307ed193e3cd536dc2927360a00df3..74e01a4c09cb6d391fffb5c076b052c59b2dec64 100644 (file)
--- 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)
 {
diff --git a/misc.h b/misc.h
index 2221a54c805ab48d822b88bc954c8d64bda41396..4a05db2da482ca89b76b596407e97d6f3cdfa3ce 100644 (file)
--- a/misc.h
+++ b/misc.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.h,v 1.83 2020/01/23 07:10:22 dtucker Exp $ */
+/* $OpenBSD: misc.h,v 1.84 2020/01/24 23:54:40 djm Exp $ */
 
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -69,6 +69,8 @@ long   convtime(const char *);
 char   *tilde_expand_filename(const char *, uid_t);
 char   *percent_expand(const char *, ...) __attribute__((__sentinel__));
 char   *tohex(const void *, size_t);
+void    xextendf(char **s, const char *sep, const char *fmt, ...)
+    __attribute__((__format__ (printf, 3, 4))) __attribute__((__nonnull__ (3)));
 void    sanitise_stdfd(void);
 void    ms_subtract_diff(struct timeval *, int *);
 void    ms_to_timeval(struct timeval *, int);