summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bftw.c5
-rw-r--r--dstring.c23
-rw-r--r--dstring.h8
3 files changed, 21 insertions, 15 deletions
diff --git a/bftw.c b/bftw.c
index 8dd98c9..60bc145 100644
--- a/bftw.c
+++ b/bftw.c
@@ -627,7 +627,10 @@ static int bftw_path_concat(struct bftw_state *state, const char *subpath) {
state->status = BFTW_CHILD;
- return dstrcatat(&state->path, nameoff, subpath);
+ if (dstresize(&state->path, nameoff) != 0) {
+ return -1;
+ }
+ return dstrcat(&state->path, subpath);
}
/**
diff --git a/dstring.c b/dstring.c
index 9c65757..5a6d4b3 100644
--- a/dstring.c
+++ b/dstring.c
@@ -75,23 +75,26 @@ int dstresize(char **dstr, size_t length) {
return 0;
}
-int dstrcat(char **dest, const char *src) {
- struct dstring *header = dstrheader(*dest);
- return dstrcatat(dest, header->length, src);
-}
+static int dstrcat_impl(char **dest, const char *src, size_t srclen) {
+ size_t oldlen = dstrlen(*dest);
+ size_t newlen = oldlen + srclen;
-int dstrcatat(char **dest, size_t pos, const char *src) {
- size_t srclen = strlen(src);
- size_t destlen = pos + srclen;
-
- if (dstresize(dest, destlen) != 0) {
+ if (dstresize(dest, newlen) != 0) {
return -1;
}
- memcpy(*dest + pos, src, srclen);
+ memcpy(*dest + oldlen, src, srclen);
return 0;
}
+int dstrcat(char **dest, const char *src) {
+ return dstrcat_impl(dest, src, strlen(src));
+}
+
+int dstrncat(char **dest, const char *src, size_t n) {
+ return dstrcat_impl(dest, src, strnlen(src, n));
+}
+
void dstrfree(char *dstr) {
if (dstr) {
free(dstrheader(dstr));
diff --git a/dstring.h b/dstring.h
index 7e46da8..cd867ad 100644
--- a/dstring.h
+++ b/dstring.h
@@ -65,17 +65,17 @@ int dstresize(char **dstr, size_t length);
int dstrcat(char **dest, const char *src);
/**
- * Append to a dynamic string at a particular offset.
+ * Append to a dynamic string.
*
* @param dest
* The destination dynamic string.
- * @param pos
- * The offset at which to append.
* @param src
* The string to append.
+ * @param n
+ * The maximum number of characters to take from src.
* @return 0 on success, -1 on failure.
*/
-int dstrcatat(char **dest, size_t pos, const char *src);
+int dstrncat(char **dest, const char *src, size_t n);
/**
* Free a dynamic string.