]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
lib/misc: add some string concatenation helpes
authorVMware, Inc <>
Thu, 2 Aug 2012 06:57:12 +0000 (23:57 -0700)
committerDmitry Torokhov <dtor@vmware.com>
Thu, 2 Aug 2012 18:23:09 +0000 (11:23 -0700)
Signed-off-by: Dmitry Torokhov <dtor@vmware.com>
open-vm-tools/lib/include/strutil.h
open-vm-tools/lib/misc/strutil.c

index 7fed680a2a31bfacccba015e66f8b2ce8f4cc694..ebc65739d060641e3f26715d7679c8150c579495 100644 (file)
@@ -62,4 +62,8 @@ Bool StrUtil_VDynBufPrintf(struct DynBuf *b, const char *fmt, va_list args);
 Bool StrUtil_DynBufPrintf(struct DynBuf *b, const char *fmt, ...);
 void StrUtil_SafeDynBufPrintf(struct DynBuf *b, const char *fmt, ...);
 
+void StrUtil_SafeStrcat(char **prefix, const char *str);
+void StrUtil_SafeStrcatFV(char **prefix, const char *fmt, va_list args);
+void StrUtil_SafeStrcatF(char **prefix, const char *fmt, ...) PRINTF_DECL(2, 3);
+
 #endif /* STRUTIL_H */
index 096b5fb7db6504369751569471176062d36b0b37..246223b67ae3a076e7f9742138af9cbb4efb3720 100644 (file)
@@ -1,5 +1,5 @@
 /*********************************************************
- * Copyright (C) 1998 VMware, Inc. All rights reserved.
+ * Copyright (C) 1998-2012 VMware, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published
@@ -1059,3 +1059,97 @@ StrUtil_SafeDynBufPrintf(DynBuf *b,        // IN/OUT
 
    ASSERT_MEM_ALLOC(success);
 }
+
+
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * StrUtil_SafeStrcat --
+ *
+ *      Given an input buffer, append another string and return the resulting
+ *      string. The input buffer is freed along the way. A fancy strcat().
+ *
+ * Results:
+ *      New buffer returned via 'prefix'
+ *
+ * Side effects:
+ *      None.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+void
+StrUtil_SafeStrcat(char **prefix,    // IN/OUT
+                   const char *str)  // IN
+{
+   char *tmp;
+   size_t plen = *prefix != NULL ? strlen(*prefix) : 0;
+   size_t slen = strlen(str);
+
+   /* Check for overflow */
+   ASSERT_NOT_IMPLEMENTED((size_t)-1 - plen > slen + 1);
+
+   tmp = realloc(*prefix, plen + slen + 1 /* NUL */);
+   ASSERT_MEM_ALLOC(tmp);
+
+   memcpy(tmp + plen, str, slen + 1 /* NUL */);
+   *prefix = tmp;
+}
+
+
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * StrUtil_SafeStrcatFV --
+ *
+ *      Given an input buffer, append another string and return the resulting
+ *      string. The input buffer is freed along the way. A fancy vasprintf().
+ *
+ * Results:
+ *      New buffer returned via 'prefix'
+ *
+ * Side effects:
+ *      None.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+void
+StrUtil_SafeStrcatFV(char **prefix,    // IN/OUT
+                     const char *fmt,  // IN
+                     va_list args)     // IN
+{
+   char *str = Str_SafeVasprintf(NULL, fmt, args);
+   StrUtil_SafeStrcat(prefix, str);
+   free(str);
+}
+
+
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * StrUtil_SafeStrcatF --
+ *
+ *      Given an input buffer, append another string and return the resulting
+ *      string. The input buffer is freed along the way. A fancy asprintf().
+ *
+ * Results:
+ *      New buffer returned via 'prefix'
+ *
+ * Side effects:
+ *      None.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+void
+StrUtil_SafeStrcatF(char **prefix,    // IN/OUT
+                    const char *fmt,  // IN
+                    ...)              // IN
+{
+   va_list args;
+
+   va_start(args, fmt);
+   StrUtil_SafeStrcatFV(prefix, fmt, args);
+   va_end(args);
+}