diff options
Diffstat (limited to 'lib/xfuncs.c')
-rw-r--r-- | lib/xfuncs.c | 22 |
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); |