summaryrefslogtreecommitdiff
path: root/libc/string/strdup.c
diff options
context:
space:
mode:
authorPeter Kjellerstedt <peter.kjellerstedt@axis.com>2006-10-19 10:17:12 +0000
committerPeter Kjellerstedt <peter.kjellerstedt@axis.com>2006-10-19 10:17:12 +0000
commitc2c7d1a6456fc63721323e9c6b15a17c87e4a484 (patch)
tree3be457a0d18b4e6b6623ca262ee0e77f98bd2827 /libc/string/strdup.c
parent6626da07e35651fbd94493a23ac4ecc342c2b0d9 (diff)
Make strdup() use memcpy() rather than strcpy() to duplicate the string.
The rationale is that we already have the length of the string to duplicate (from doing the malloc()), and memcpy() should then always be faster than strcpy() (or at least as fast).
Diffstat (limited to 'libc/string/strdup.c')
-rw-r--r--libc/string/strdup.c10
1 files changed, 4 insertions, 6 deletions
diff --git a/libc/string/strdup.c b/libc/string/strdup.c
index dff5af60a..d15345d0d 100644
--- a/libc/string/strdup.c
+++ b/libc/string/strdup.c
@@ -10,25 +10,23 @@
#ifdef WANT_WIDE
libc_hidden_proto(wcslen)
-libc_hidden_proto(wcscpy)
# define Wstrdup wcsdup
# define Wstrlen wcslen
-# define Wstrcpy wcscpy
#else
libc_hidden_proto(strdup)
libc_hidden_proto(strlen)
-libc_hidden_proto(strcpy)
# define Wstrdup strdup
# define Wstrlen strlen
-# define Wstrcpy strcpy
#endif
+libc_hidden_proto(memcpy)
Wchar *Wstrdup(register const Wchar *s1)
{
register Wchar *s;
+ register size_t l = (Wstrlen(s1) + 1) * sizeof(Wchar);
- if ((s = malloc((Wstrlen(s1) + 1) * sizeof(Wchar))) != NULL) {
- Wstrcpy(s, s1);
+ if ((s = malloc(l)) != NULL) {
+ memcpy(s, s1, l);
}
return s;