aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2020-10-25 10:41:18 +0100
committerDavid Gibson <david@gibson.dropbear.id.au>2022-01-25 17:10:47 +1100
commit651410e54cb9a478f2fbb16cb493a5e7808ad8fe (patch)
tree0b4467d024918934e1780548986983a78602d727
parent4048aed12b81c5a0154b9af438edc99ec7d2b6a1 (diff)
downloaddtc-651410e54cb9a478f2fbb16cb493a5e7808ad8fe.tar.gz
util: introduce xstrndup helper
We already have xstrdup, add xstrndup as well to make it straight-forward to clone part of a string. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
-rw-r--r--util.c11
-rw-r--r--util.h1
2 files changed, 12 insertions, 0 deletions
diff --git a/util.c b/util.c
index 14d3868..507f012 100644
--- a/util.c
+++ b/util.c
@@ -33,6 +33,17 @@ char *xstrdup(const char *s)
return d;
}
+char *xstrndup(const char *s, size_t n)
+{
+ size_t len = strnlen(s, n) + 1;
+ char *d = xmalloc(len);
+
+ memcpy(d, s, len - 1);
+ d[len - 1] = '\0';
+
+ return d;
+}
+
int xavsprintf_append(char **strp, const char *fmt, va_list ap)
{
int n, size = 0; /* start with 128 bytes */
diff --git a/util.h b/util.h
index 7a4e910..9d38ede 100644
--- a/util.h
+++ b/util.h
@@ -61,6 +61,7 @@ static inline void *xrealloc(void *p, size_t len)
}
extern char *xstrdup(const char *s);
+extern char *xstrndup(const char *s, size_t len);
extern int PRINTF(2, 3) xasprintf(char **strp, const char *fmt, ...);
extern int PRINTF(2, 3) xasprintf_append(char **strp, const char *fmt, ...);