From aba3ee7d322f30735433e2e6ae98fa3d849a1c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Goddard=20Rosa?= Date: Sat, 30 Jan 2010 22:29:59 -0200 Subject: [PATCH] string: tidy up strndup() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It decreases code size: text data bss dec hex filename 161 0 0 161 a1 strndup-BEFORE.o 153 0 0 153 99 strndup-AFTER.o Signed-off-by: André Goddard Rosa --- libc/string/strndup.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libc/string/strndup.c b/libc/string/strndup.c index da9a55f45..9dca79c43 100644 --- a/libc/string/strndup.c +++ b/libc/string/strndup.c @@ -31,12 +31,14 @@ char* strndup(const char* s, size_t n) { size_t slen = (size_t)strlen(s); - int len = slen < n ? slen : n; - char* copy = malloc(len+1); + char* copy; + if (slen < n) + n = slen; + copy = malloc(n+1); if (copy) { - memcpy( copy, s, len ); - copy[len] = 0; + memcpy(copy, s, n); + copy[n] = 0; } return copy; }