From: Florian Forster Date: Wed, 9 Jun 2010 17:02:30 +0000 (+0200) Subject: common.[ch]: Implement "strlcat". X-Git-Tag: v4.0.0~288 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=4bd1536081d134fdb0d21067d0c7ddcdc422e6c9;p=collection4.git common.[ch]: Implement "strlcat". --- diff --git a/common.c b/common.c index a2bfd09..ec267af 100644 --- a/common.c +++ b/common.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "common.h" @@ -142,4 +143,33 @@ int foreach_host (callback_host_t callback, /* {{{ */ return (foreach_dir (DATA_DIR, callback, user_data)); } /* }}} int foreach_host */ +size_t c_strlcat (char *dst, const char *src, size_t size) /* {{{ */ +{ + size_t retval; + size_t dst_len; + size_t src_len; + + dst_len = strlen (dst); + src_len = strlen (src); + retval = dst_len + src_len; + + if ((dst_len + 1) >= size) + return (retval); + + dst += dst_len; + size -= dst_len; + assert (size >= 2); + + /* Result will be truncated. */ + if (src_len >= size) + src_len = size - 1; + + memcpy (dst, src, src_len); + dst[src_len] = 0; + + return (retval); +} /* }}} size_t c_strlcat */ + + + /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/common.h b/common.h index 01c8710..e211406 100644 --- a/common.h +++ b/common.h @@ -12,5 +12,8 @@ int foreach_type (const char *host, const char *plugin, int foreach_plugin (const char *host, callback_plugin_t, void *user_data); int foreach_host (callback_host_t, void *user_data); +size_t c_strlcat (char *dst, const char *src, size_t size); +#define strlcat c_strlcat + #endif /* COMMON_H */ /* vim: set sw=2 sts=2 et fdm=marker : */