From dc39552d01094eff2bef5f9fcd1c16928909d20e Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 22 Sep 2016 12:41:55 +0200 Subject: core: New helper function _gpgme_strconcat. * src/conversion.c: Include stdarg.h. (do_strconcat): New. (_gpgme_strconcat): New. * src/util.h: Provide fallback for GPGRT_ATTR_SENTINEL. (_gpgme_strconcat): New with sentinel. * src/w32-util.c (find_program_in_dir): Replace malloc and stpcpy by _gpgme_strconcat. (find_program_at_standard_place): Ditto. (_gpgme_set_default_gpg_name): Ditto. (_gpgme_set_default_gpgconf_name): Ditto. (_gpgme_mkstemp): Ditto. (_gpgme_set_override_inst_dir): Repalce malloc and strcpy by strdup. -- The function has been taken from gnupg/common/stringhelp.c and license changed to LPGLv2.1+. I am the original author of that code. Signed-off-by: Werner Koch --- src/conversion.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'src/conversion.c') diff --git a/src/conversion.c b/src/conversion.c index 3df8fe59..6dfabe7e 100644 --- a/src/conversion.c +++ b/src/conversion.c @@ -31,6 +31,7 @@ #endif #include #include +#include #include "gpgme.h" #include "util.h" @@ -41,6 +42,61 @@ #define atoi_4(p) ((atoi_2(p) * 100) + atoi_2((p)+2)) + +static char * +do_strconcat (const char *s1, va_list arg_ptr) +{ + const char *argv[16]; + size_t argc; + size_t needed; + char *buffer, *p; + + argc = 0; + argv[argc++] = s1; + needed = strlen (s1); + while (((argv[argc] = va_arg (arg_ptr, const char *)))) + { + needed += strlen (argv[argc]); + if (argc >= DIM (argv)-1) + { + gpg_err_set_errno (EINVAL); + return NULL; + } + argc++; + } + needed++; + buffer = malloc (needed); + if (buffer) + { + for (p = buffer, argc=0; argv[argc]; argc++) + p = stpcpy (p, argv[argc]); + } + return buffer; +} + + +/* Concatenate the string S1 with all the following strings up to a + * NULL. Returns a malloced buffer with the new string or NULL on a + malloc error or if too many arguments are given. */ +char * +_gpgme_strconcat (const char *s1, ...) +{ + va_list arg_ptr; + char *result; + + if (!s1) + result = strdup (""); + else + { + va_start (arg_ptr, s1); + result = do_strconcat (s1, arg_ptr); + va_end (arg_ptr); + } + return result; +} + + + /* Convert two hexadecimal digits from STR to the value they represent. Returns -1 if one of the characters is not a -- cgit v1.2.3