Re: [PATCH] lib: string.c: Added a funktion function strzcpy

From: Andrew Morton
Date: Tue Sep 23 2014 - 18:41:05 EST


On Wed, 24 Sep 2014 00:13:36 +0200 Rickard Strandqvist <rickard_strandqvist@xxxxxxxxxxxxxxxxxx> wrote:

> +/**
> + * strzcpy - Copy a length-limited, C-string
> + * @dest: Where to copy the string to
> + * @src: Where to copy the string from
> + * @count: The maximum number of bytes to copy
> + *
> + * The result is %NUL-terminated,
> + * as long as count is greater than zero.
> + *
> + * In the case where the length of @src is less than that of
> + * count, the remainder of @dest will be padded with %NUL.

As far as I can tell, this sentence describes the only difference
between strzcpy() and strlcpy(). Only the sentence is wrong - the
implementation doesn't actually do that.

> + */
> +char *strzcpy(char *dest, const char *src, size_t count)
> +{
> + char *tmp = dest;
> +
> + while (count) {
> + if ((*tmp = *src) != 0)
> + src++;
> + tmp++;
> + count--;
> + }
> +
> + if (dest != tmp)
> + *--tmp = '\0';
> +
> + return dest;
> +}
> +EXPORT_SYMBOL(strzcpy);
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/