forked from github/server
move strlcat and strlcpy to strings.h
This commit is contained in:
parent
57b161132d
commit
23ff404d2e
4 changed files with 65 additions and 63 deletions
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include "bsdstring.h"
|
||||
#include "log.h"
|
||||
#include "strings.h"
|
||||
|
||||
int wrptr(char **ptr, size_t * size, int result)
|
||||
{
|
||||
|
@ -35,64 +36,9 @@ int wrptr(char **ptr, size_t * size, int result)
|
|||
return ERANGE;
|
||||
}
|
||||
|
||||
#ifndef HAVE_BSDSTRING
|
||||
size_t strlcpy(char *dst, const char *src, size_t siz)
|
||||
{ /* copied from OpenBSD source code */
|
||||
register char *d = dst;
|
||||
register const char *s = src;
|
||||
register size_t n = siz;
|
||||
|
||||
assert(src);
|
||||
assert(dst);
|
||||
/* Copy as many bytes as will fit */
|
||||
if (n != 0 && --n != 0) {
|
||||
do {
|
||||
if ((*d++ = *s++) == 0)
|
||||
break;
|
||||
} while (--n != 0);
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
}
|
||||
|
||||
size_t strlcat(char *dst, const char *src, size_t siz)
|
||||
{
|
||||
register char *d = dst;
|
||||
register const char *s = src;
|
||||
register size_t n = siz;
|
||||
size_t dlen;
|
||||
|
||||
/* Find the end of dst and adjust bytes left but don't go past end */
|
||||
while (*d != '\0' && n-- != 0)
|
||||
d++;
|
||||
dlen = d - dst;
|
||||
n = siz - dlen;
|
||||
|
||||
if (n == 0)
|
||||
return (dlen + strlen(s));
|
||||
while (*s != '\0') {
|
||||
if (n != 1) {
|
||||
*d++ = *s;
|
||||
n--;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
*d = '\0';
|
||||
|
||||
return (dlen + (s - src)); /* count does not include NUL */
|
||||
}
|
||||
#endif
|
||||
|
||||
char * strlcpy_w(char *dst, const char *src, size_t *siz, const char *err, const char *file, int line)
|
||||
{
|
||||
size_t bytes = strlcpy(dst, src, *siz);
|
||||
size_t bytes = str_strlcpy(dst, src, *siz);
|
||||
char * buf = dst;
|
||||
assert(bytes <= INT_MAX);
|
||||
if (wrptr(&buf, siz, (int)bytes) != 0) {
|
||||
|
|
|
@ -3,13 +3,6 @@
|
|||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifndef HAVE_BSDSTRING
|
||||
size_t strlcpy(char *dst, const char *src, size_t siz);
|
||||
size_t strlcat(char *dst, const char *src, size_t siz);
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
int wrptr(char **ptr, size_t * size, int bytes);
|
||||
char * strlcpy_w(char *dst, const char *src, size_t *siz, const char *err, const char *file, int line);
|
||||
|
||||
|
|
|
@ -29,6 +29,67 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
size_t str_strlcpy(char *dst, const char *src, size_t len)
|
||||
{
|
||||
#ifdef HAVE_BSDSTRING
|
||||
return strlcpy(dst, src, len);
|
||||
#else
|
||||
register char *d = dst;
|
||||
register const char *s = src;
|
||||
register size_t n = len;
|
||||
|
||||
assert(src);
|
||||
assert(dst);
|
||||
/* Copy as many bytes as will fit */
|
||||
if (n != 0 && --n != 0) {
|
||||
do {
|
||||
if ((*d++ = *s++) == 0)
|
||||
break;
|
||||
} while (--n != 0);
|
||||
}
|
||||
|
||||
/* Not enough room in dst, add NUL and traverse rest of src */
|
||||
if (n == 0) {
|
||||
if (len != 0)
|
||||
*d = '\0'; /* NUL-terminate dst */
|
||||
while (*s++);
|
||||
}
|
||||
|
||||
return (s - src - 1); /* count does not include NUL */
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t str_strlcat(char *dst, const char *src, size_t len)
|
||||
{
|
||||
#ifdef HAVE_BSDSTRING
|
||||
return strlcat(dst, src, len);
|
||||
#else
|
||||
register char *d = dst;
|
||||
register const char *s = src;
|
||||
register size_t n = len;
|
||||
size_t dlen;
|
||||
|
||||
/* Find the end of dst and adjust bytes left but don't go past end */
|
||||
while (*d != '\0' && n-- != 0)
|
||||
d++;
|
||||
dlen = d - dst;
|
||||
n = len - dlen;
|
||||
|
||||
if (n == 0)
|
||||
return (dlen + strlen(s));
|
||||
while (*s != '\0') {
|
||||
if (n != 1) {
|
||||
*d++ = *s;
|
||||
n--;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
*d = '\0';
|
||||
|
||||
return (dlen + (s - src)); /* count does not include NUL */
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t str_slprintf(char * dst, size_t size, const char * format, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
|
|
@ -30,6 +30,8 @@ extern "C" {
|
|||
char *set_string(char **s, const char *neu);
|
||||
unsigned int str_hash(const char *s);
|
||||
size_t str_slprintf(char * dst, size_t size, const char * format, ...);
|
||||
size_t str_strlcpy(char *dst, const char *src, size_t len);
|
||||
size_t str_strlcat(char *dst, const char *src, size_t len);
|
||||
char *str_strdup(const char *s);
|
||||
|
||||
unsigned int jenkins_hash(unsigned int a);
|
||||
|
|
Loading…
Reference in a new issue