diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/utils.c | 49 | ||||
-rw-r--r-- | common/utils.h | 6 |
2 files changed, 53 insertions, 2 deletions
diff --git a/common/utils.c b/common/utils.c index 4a45d95..7f66ec2 100644 --- a/common/utils.c +++ b/common/utils.c @@ -1,7 +1,10 @@ /* * utils.c - * Miscellaneous utilities for string manipulation + * Miscellaneous utilities for string manipulation, + * file I/O and plist helper. * + * Copyright (c) 2014-2019 Nikias Bassen, All Rights Reserved. + * Copyright (c) 2013-2014 Martin Szulecki, All Rights Reserved. * Copyright (c) 2013 Federico Mena Quintero * * This library is free software; you can redistribute it and/or @@ -111,6 +114,50 @@ char *string_concat(const char *str, ...) return result; } +char *string_append(char* str, ...) +{ + size_t len = 0; + size_t slen; + va_list args; + char *s; + char *result; + char *dest; + + /* Compute final length */ + + if (str) { + len = strlen(str); + } + slen = len; + len++; /* plus 1 for the null terminator */ + + va_start(args, str); + s = va_arg(args, char *); + while (s) { + len += strlen(s); + s = va_arg(args, char*); + } + va_end(args); + + result = realloc(str, len); + if (!result) + return NULL; /* errno remains set */ + + dest = result + slen; + + /* Concat additional strings */ + + va_start(args, str); + s = va_arg(args, char *); + while (s) { + dest = stpcpy(dest, s); + s = va_arg(args, char *); + } + va_end(args); + + return result; +} + char *string_build_path(const char *elem, ...) { if (!elem) diff --git a/common/utils.h b/common/utils.h index 8426bc0..2c3acec 100644 --- a/common/utils.h +++ b/common/utils.h @@ -1,7 +1,10 @@ /* * utils.h - * Miscellaneous utilities for string manipulation + * Miscellaneous utilities for string manipulation, + * file I/O and plist helper. * + * Copyright (c) 2014-2019 Nikias Bassen, All Rights Reserved. + * Copyright (c) 2013-2014 Martin Szulecki, All Rights Reserved. * Copyright (c) 2013 Federico Mena Quintero * * This library is free software; you can redistribute it and/or @@ -39,6 +42,7 @@ char *stpcpy(char *s1, const char *s2); #endif char *string_concat(const char *str, ...); +char *string_append(char *str, ...); char *string_build_path(const char *elem, ...); char *string_format_size(uint64_t size); char *string_toupper(char *str); |