summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/utils.c49
-rw-r--r--common/utils.h6
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 @@
1/* 1/*
2 * utils.c 2 * utils.c
3 * Miscellaneous utilities for string manipulation 3 * Miscellaneous utilities for string manipulation,
4 * file I/O and plist helper.
4 * 5 *
6 * Copyright (c) 2014-2019 Nikias Bassen, All Rights Reserved.
7 * Copyright (c) 2013-2014 Martin Szulecki, All Rights Reserved.
5 * Copyright (c) 2013 Federico Mena Quintero 8 * Copyright (c) 2013 Federico Mena Quintero
6 * 9 *
7 * This library is free software; you can redistribute it and/or 10 * This library is free software; you can redistribute it and/or
@@ -111,6 +114,50 @@ char *string_concat(const char *str, ...)
111 return result; 114 return result;
112} 115}
113 116
117char *string_append(char* str, ...)
118{
119 size_t len = 0;
120 size_t slen;
121 va_list args;
122 char *s;
123 char *result;
124 char *dest;
125
126 /* Compute final length */
127
128 if (str) {
129 len = strlen(str);
130 }
131 slen = len;
132 len++; /* plus 1 for the null terminator */
133
134 va_start(args, str);
135 s = va_arg(args, char *);
136 while (s) {
137 len += strlen(s);
138 s = va_arg(args, char*);
139 }
140 va_end(args);
141
142 result = realloc(str, len);
143 if (!result)
144 return NULL; /* errno remains set */
145
146 dest = result + slen;
147
148 /* Concat additional strings */
149
150 va_start(args, str);
151 s = va_arg(args, char *);
152 while (s) {
153 dest = stpcpy(dest, s);
154 s = va_arg(args, char *);
155 }
156 va_end(args);
157
158 return result;
159}
160
114char *string_build_path(const char *elem, ...) 161char *string_build_path(const char *elem, ...)
115{ 162{
116 if (!elem) 163 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 @@
1/* 1/*
2 * utils.h 2 * utils.h
3 * Miscellaneous utilities for string manipulation 3 * Miscellaneous utilities for string manipulation,
4 * file I/O and plist helper.
4 * 5 *
6 * Copyright (c) 2014-2019 Nikias Bassen, All Rights Reserved.
7 * Copyright (c) 2013-2014 Martin Szulecki, All Rights Reserved.
5 * Copyright (c) 2013 Federico Mena Quintero 8 * Copyright (c) 2013 Federico Mena Quintero
6 * 9 *
7 * This library is free software; you can redistribute it and/or 10 * This library is free software; you can redistribute it and/or
@@ -39,6 +42,7 @@
39char *stpcpy(char *s1, const char *s2); 42char *stpcpy(char *s1, const char *s2);
40#endif 43#endif
41char *string_concat(const char *str, ...); 44char *string_concat(const char *str, ...);
45char *string_append(char *str, ...);
42char *string_build_path(const char *elem, ...); 46char *string_build_path(const char *elem, ...);
43char *string_format_size(uint64_t size); 47char *string_format_size(uint64_t size);
44char *string_toupper(char *str); 48char *string_toupper(char *str);