54d9148a36
Start a new clean history from here.
33 lines
770 B
C
33 lines
770 B
C
#include "strutil.h"
|
|
|
|
size_t xstrncpy(char *dest, const char *src, size_t n) {
|
|
size_t i;
|
|
for (i = 0; i < n - 1 && src[i] != '\0'; ++i)
|
|
dest[i] = src[i];
|
|
if (n)
|
|
dest[i] = '\0';
|
|
return src[i] == '\0' ? i : n;
|
|
}
|
|
|
|
size_t xstrjoin(char *dst, const char *const tokens[], char sep, size_t n) {
|
|
const char *const *remaining = tokens;
|
|
const char *token = *remaining++;
|
|
size_t i = 0;
|
|
while (token) {
|
|
if (i) {
|
|
dst[i++] = sep;
|
|
if (i == n)
|
|
goto truncated;
|
|
}
|
|
size_t w = xstrncpy(dst + i, token, n - i);
|
|
if (w >= n - i)
|
|
goto truncated;
|
|
i += w;
|
|
token = *remaining++;
|
|
}
|
|
return i;
|
|
|
|
truncated:
|
|
dst[n - 1] = '\0';
|
|
return n;
|
|
}
|