1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
7 */
8
9#include <stdarg.h> /* va_list etc. */
10#include <string.h> /* strlen */
11
12/* copy at most (n-1) bytes from src to dst and add a terminating NULL
13 * byte; return length of src (i.e. can be more than what is copied) */
14static inline size_t
15strcpy_len(char *restrict dst, const char *restrict src, size_t n)
16{
17 if (dst != NULL && n != 0) {
18 for (size_t i = 0; i < n; i++) {
19 if ((dst[i] = src[i]) == 0)
20 return i;
21 }
22 dst[n - 1] = 0;
23 return n-1;
24 }
25 return strlen(src);
26}
27
28/* copy the NULL terminated list of src strings with a maximum of n
29 * bytes to dst; return the combined length of the src strings */
30static inline size_t
31strconcat_len(char *restrict dst, size_t n, const char *restrict src, ...)
32{
33 va_list ap;
34 size_t i = 0;
35
36 va_start(ap, src);
37 while (src) {
38 size_t l;
39 if (dst && i < n)
40 l = strcpy_len(dst + i, src, n - i);
41 else
42 l = strlen(src);
43 i += l;
44 src = va_arg(ap, const char *);
45 }
46 va_end(ap);
47 return i;
48}
49