X-Git-Url: https://git.deb.at/?a=blobdiff_plain;f=xmalloc.c;h=3a34e1ce187a864a029c434a221a4c4fd2fbe0db;hb=126dbdaf881905b0406be15a27fa842fd91d11e1;hp=ca784be299a418063bc5fb4478a7e5959136e11e;hpb=4b8f9231090ada43f7e16987ec46ac7f45a914ec;p=pkg%2Fabook.git diff --git a/xmalloc.c b/xmalloc.c index ca784be..3a34e1c 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -149,3 +149,21 @@ xstrdup(const char *s) return (char *)memcpy(new, s, len + 1); } +char * +xstrndup(const char *s, size_t len) +{ + char *new; + size_t n = strlen(s); + + if(n > len) + n = len; + + new = xmalloc_inc(n, 1); + if(new == NULL) + return NULL; + + memcpy(new, s, n); + new[n] = '\0'; + + return new; +}