From 1eebd1e00ae817c35d812c96bb2dc58abb3c74a2 Mon Sep 17 00:00:00 2001 From: Tyler Dauwalder Date: Sat, 27 Sep 2003 05:14:22 +0000 Subject: [PATCH] Updated user_strlcpy() to return length of source string instead of 0. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4828 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/core/arch/ppc/arch_cpu.c | 27 ++++++++++++++++++++++----- src/kernel/core/arch/x86/arch_cpu.c | 26 ++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/kernel/core/arch/ppc/arch_cpu.c b/src/kernel/core/arch/ppc/arch_cpu.c index d4482d874f..44415a8b8e 100755 --- a/src/kernel/core/arch/ppc/arch_cpu.c +++ b/src/kernel/core/arch/ppc/arch_cpu.c @@ -122,25 +122,42 @@ error: return B_BAD_ADDRESS; } +/*! \brief Copies at most (\a size - 1) characters from the string in \a from to + the string in \a to, NULL-terminating the result. + \param to Pointer to the destination C-string. + \param from Pointer to the source C-string. + \param size Size in bytes of the string buffer pointed to by \a to. + + \return strlen(\a from). +*/ int arch_cpu_user_strlcpy(char *to, const char *from, size_t size, addr *faultHandler) { + int from_length = 0; + *faultHandler = (addr)&&error; - to[--size] = '\0'; - while (size-- && (*to++ = *from++) != '\0') - ; + if (size > 0) { + to[--size] = '\0'; + // copy + for ( ; size; size--, from_length++, to++, from++) { + if ((*to = *from) == '\0') + break; + } + } + // count any leftover from chars + while (*from++ != '\0') + from_length++; *faultHandler = 0; - return 0; + return from_length; error: *faultHandler = 0; return B_BAD_ADDRESS; } - int arch_cpu_user_memset(void *s, char c, size_t count, addr *faultHandler) { diff --git a/src/kernel/core/arch/x86/arch_cpu.c b/src/kernel/core/arch/x86/arch_cpu.c index 39a7c2943c..4baed17844 100755 --- a/src/kernel/core/arch/x86/arch_cpu.c +++ b/src/kernel/core/arch/x86/arch_cpu.c @@ -195,18 +195,36 @@ error: return B_BAD_ADDRESS; } +/*! \brief Copies at most (\a size - 1) characters from the string in \a from to + the string in \a to, NULL-terminating the result. + \param to Pointer to the destination C-string. + \param from Pointer to the source C-string. + \param size Size in bytes of the string buffer pointed to by \a to. + + \return strlen(\a from). +*/ int arch_cpu_user_strlcpy(char *to, const char *from, size_t size, addr *faultHandler) { + int from_length = 0; + *faultHandler = (addr)&&error; - to[--size] = '\0'; - while (size-- && (*to++ = *from++) != '\0') - ; + if (size > 0) { + to[--size] = '\0'; + // copy + for ( ; size; size--, from_length++, to++, from++) { + if ((*to = *from) == '\0') + break; + } + } + // count any leftover from chars + while (*from++ != '\0') + from_length++; *faultHandler = 0; - return 0; + return from_length; error: *faultHandler = 0;