summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMarc Kleine-Budde <mkl@pengutronix.de>2015-06-17 22:47:01 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2015-06-18 10:58:58 +0200
commitdaeb9d8d76e353eaee45545e8f4e83df92af035c (patch)
tree165de4c54db0c8eeaf25133a27c1ded2e36bfe9b /lib
parent48751b1aa94ad2dd40e06a7d6a89fb9e786d9cf7 (diff)
downloadbarebox-daeb9d8d76e353eaee45545e8f4e83df92af035c.tar.gz
barebox-daeb9d8d76e353eaee45545e8f4e83df92af035c.tar.xz
xfuncs: import xstrndup() from busybox
This function is needed for the fixed length string feature in the state framework. Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/xfuncs.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/xfuncs.c b/lib/xfuncs.c
index 0e78b670a5..f0219c43a5 100644
--- a/lib/xfuncs.c
+++ b/lib/xfuncs.c
@@ -63,6 +63,28 @@ char *xstrdup(const char *s)
}
EXPORT_SYMBOL(xstrdup);
+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);
+}
+EXPORT_SYMBOL(xstrndup);
+
void* xmemalign(size_t alignment, size_t bytes)
{
void *p = memalign(alignment, bytes);