X-Git-Url: https://git.deb.at/w?a=blobdiff_plain;f=xmalloc.c;h=3a34e1ce187a864a029c434a221a4c4fd2fbe0db;hb=7c88cafcd333277b6bfd55370a5ec1c5b76c2b0d;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; +}