torrentd

simple torrent daemon
git clone git@git.2f30.org/torrentd.git
Log | Files | Refs | LICENSE

commit 15515152f884cbf256193ebc167630b50bb54fc8
parent 9ec6f952489de1b39e909d441c1afbc658f7fb96
Author: sin <sin@2f30.org>
Date:   Sat, 19 Dec 2015 11:56:10 +0000

Import strlcpy() and strlcat() from OpenBSD

Diffstat:
Mstorrent.h | 4++++
Mutil.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/storrent.h b/storrent.h @@ -71,3 +71,7 @@ int readfile(char *, char **, size_t *); char *urlencode(char *, size_t); char *urldecode(char *); void writeall(int, const void *, size_t); +#undef strlcpy +size_t strlcpy(char *, const char *, size_t); +#undef strlcat +size_t strlcat(char *, const char *, size_t); diff --git a/util.c b/util.c @@ -196,3 +196,56 @@ writeall(int fd, const void *buf, size_t count) count -= n; } } + +size_t +strlcpy(char *dst, const char *src, size_t dsize) +{ + const char *osrc = src; + size_t nleft = dsize; + + /* Copy as many bytes as will fit. */ + if (nleft != 0) { + while (--nleft != 0) { + if ((*dst++ = *src++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src. */ + if (nleft == 0) { + if (dsize != 0) + *dst = '\0'; /* NUL-terminate dst */ + while (*src++) + ; + } + + return(src - osrc - 1); /* count does not include NUL */ +} + +size_t +strlcat(char *dst, const char *src, size_t dsize) +{ + const char *odst = dst; + const char *osrc = src; + size_t n = dsize; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end. */ + while (n-- != 0 && *dst != '\0') + dst++; + dlen = dst - odst; + n = dsize - dlen; + + if (n-- == 0) + return(dlen + strlen(src)); + while (*src != '\0') { + if (n != 0) { + *dst++ = *src; + n--; + } + src++; + } + *dst = '\0'; + + return(dlen + (src - osrc)); /* count does not include NUL */ +}