On Thu, 23 Sep 2010 at 23:25:40 +0200, Tamas TEVESZ wrote:
> Subject: [PATCH] Add detection and local copy for strlcat()/strlcpy()
>
> Signed-off-by: Tamas TEVESZ <[email protected]>
> ---
> WINGs/WINGs/WUtil.h | 7 +++-
> WINGs/string.c | 108
> +++++++++++++++++++++++++++++++++++++++++++++++++++
> configure.ac | 8 ++++
> 3 files changed, 122 insertions(+), 1 deletions(-)
> +/*
> + * Copy src to string dst of size siz. At most siz-1 characters
> + * will be copied. Always NUL terminates (unless siz == 0).
> + * Returns strlen(src); if retval >= siz, truncation occurred.
> + */
> +size_t
> +strlcpy(char *dst, const char *src, size_t siz)
> +{
> + char *d = dst;
> + const char *s = src;
> + size_t n = siz;
> +
> + /* Copy as many bytes as will fit */
> + if (n != 0) {
> + while (--n != 0) {
> + if ((*d++ = *s++) == '\0')
> + break;
> + }
> + }
> +
> + /* Not enough room in dst, add NUL and traverse rest of src */
> + if (n == 0) {
> + if (siz != 0)
> + *d = '\0'; /* NUL-terminate dst */
> + while (*s++)
> + ;
> + }
> +
> + return(s - src - 1); /* count does not include NUL */
> +}
In the linux kernel lib/string.c there seems to be a "simpler" strlcpy()
implementation:
/**
* strlcpy - Copy a %NUL terminated string into a sized buffer
* @dest: Where to copy the string to
* @src: Where to copy the string from
* @size: size of destination buffer
*
* Compatible with *BSD: the result is always a valid
* NUL-terminated string that fits in the buffer (unless,
* of course, the buffer size is zero). It does not pad
* out the result like strncpy() does.
*/
size_t strlcpy(char *dest, const char *src, size_t size)
{
size_t ret = strlen(src);
if (size) {
size_t len = (ret >= size) ? size - 1 : ret;
memcpy(dest, src, len);
dest[len] = '\0';
}
return ret;
}
Perhaps we should just copy this into wmaker and use it without
even bothering to check for bsd libs and stuff? I think it would
be simpler and it would avoid some #ifdefs etc.
--
To unsubscribe, send mail to [email protected].