Add rstrlcpy() string helper function

- adopted from strlcpy() function of OpenBSD originally developed by
  Todd C. Miller <Todd.Miller@courtesan.com>
This commit is contained in:
Panu Matilainen 2008-04-17 17:33:57 +03:00
parent 02939c15fa
commit 119b912773
2 changed files with 40 additions and 0 deletions

View File

@ -288,3 +288,31 @@ char *rstrscat(char **dest, const char *arg, ...)
return dst;
}
/*
* Adapted from OpenBSD, strlcpy() originally developed by
* Todd C. Miller <Todd.Miller@courtesan.com>
*/
size_t rstrlcpy(char *dest, const char *src, size_t n)
{
char *d = dest;
const char *s = src;
size_t len = n;
/* Copy as many bytes as will fit */
if (len != 0) {
while (--len != 0) {
if ((*d++ = *s++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (len == 0) {
if (n != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return s - src - 1; /* count does not include NUL */
}

View File

@ -124,6 +124,18 @@ char *rstrcat(char **dest, const char *src);
*/
char *rstrscat(char **dest, const char *arg, ...) RPM_GNUC_NULL_TERMINATED;
/** \ingroup rpmstring
* strlcpy() clone:
* Copy src to string dest of size n. At most n-1 characters
* will be copied. Always zero-terminates (unless n == 0).
* Length of src is returned; if retval >= n, truncation occurred.
* @param dest destination buffer
* @param src string to copy
* @param n destination buffer size
* @return length of src string
*/
size_t rstrlcpy(char *dest, const char *src, size_t n);
/** \ingroup rpmstring
* Split string into fields separated by a character.
* @param str string