summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Kleine-Budde <mkl@pengutronix.de>2015-06-18 12:02:54 +0200
committerMarc Kleine-Budde <mkl@pengutronix.de>2015-06-18 12:02:54 +0200
commit9fb18621550dfbbc72041047d9d296315d1f5b21 (patch)
treede38ddb41f036a0a8173b26531e9e32a18eb1c5e
parent21d488f69eac655b4599c105b512a80c918eac4c (diff)
downloaddt-utils-9fb18621550dfbbc72041047d9d296315d1f5b21.tar.gz
dt-utils-9fb18621550dfbbc72041047d9d296315d1f5b21.tar.xz
common: import xstrndup() from busybox
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
-rw-r--r--src/dt/common.h21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/dt/common.h b/src/dt/common.h
index bc5fb02..720180d 100644
--- a/src/dt/common.h
+++ b/src/dt/common.h
@@ -161,6 +161,27 @@ static inline char *xstrdup(const char *s)
return p;
}
+static inline char *xstrndup(const char *s, size_t n)
+{
+ int m;
+ char *t;
+
+ /* We can just xmalloc(n+1) and strncpy into it, */
+ /* but think about xstrndup("abc", 10000) wastage! */
+ m = n;
+ t = (char*) s;
+ while (m) {
+ if (!*t) break;
+ m--;
+ t++;
+ }
+ n -= m;
+ t = xmalloc(n + 1);
+ t[n] = '\0';
+
+ return memcpy(t, s, n);
+}
+
static inline int erase(int fd, size_t count, loff_t offset)
{
struct erase_info_user erase = {