Merge "string: tidy up strndup()"

This commit is contained in:
David Turner 2010-05-10 14:52:02 -07:00 committed by Android Code Review
commit d791da7943

View File

@ -31,12 +31,14 @@
char* strndup(const char* s, size_t n) char* strndup(const char* s, size_t n)
{ {
size_t slen = (size_t)strlen(s); size_t slen = (size_t)strlen(s);
int len = slen < n ? slen : n; char* copy;
char* copy = malloc(len+1);
if (slen < n)
n = slen;
copy = malloc(n+1);
if (copy) { if (copy) {
memcpy( copy, s, len ); memcpy(copy, s, n);
copy[len] = 0; copy[n] = 0;
} }
return copy; return copy;
} }