]> git.alsa-project.org Git - alsa-lib.git/commitdiff
add snd_strlcat() function
authorJaroslav Kysela <perex@perex.cz>
Mon, 14 Apr 2025 16:34:45 +0000 (18:34 +0200)
committerJaroslav Kysela <perex@perex.cz>
Mon, 14 Apr 2025 16:41:42 +0000 (18:41 +0200)
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
include/local.h
src/error.c

index 55f252dfbc52528911b287c42b74bddc661167df..2498cd2684bfbdc240962c35f4f7f96aa8e4fd0e 100644 (file)
@@ -267,6 +267,7 @@ int _snd_safe_strtod(const char *str, double *val);
 int snd_send_fd(int sock, void *data, size_t len, int fd);
 int snd_receive_fd(int sock, void *data, size_t len, int *fd);
 size_t snd_strlcpy(char *dst, const char *src, size_t size);
+size_t snd_strlcat(char *dst, const char *src, size_t size);
 
 /*
  * error messages
index c06af7c7f7e58513f681b506122312cb4831a857..8db7556bf79cd296c4b79dab90affd4d2e38e2e8 100644 (file)
@@ -191,11 +191,38 @@ snd_lib_error_handler_t snd_err_msg = snd_err_msg_default;
  */
 size_t snd_strlcpy(char *dst, const char *src, size_t size)
 {
-  size_t ret = strlen(src);
-  if (size) {
-    size_t len = ret >= size ? size - 1 : ret;
-    memcpy(dst, src, len);
-    dst[len] = '\0';
-  }
-  return ret;
+       size_t ret = strlen(src);
+       if (size) {
+               size_t len = ret >= size ? size - 1 : ret;
+               memcpy(dst, src, len);
+               dst[len] = '\0';
+       }
+       return ret;
+}
+
+/**
+ * \brief Append a C-string into a sized buffer
+ * \param dst Where to append the string to
+ * \param src Where to copy the string from
+ * \param size Size of destination buffer
+ * \retval The total string length (no trimming)
+ *
+ * The result is always a valid NUL-terminated string that fits
+ * in the buffer (unless, of course, the buffer size is zero).
+ * It does not pad out the result.
+ */
+size_t snd_strlcat(char *dst, const char *src, size_t size)
+{
+       size_t dst_len = strlen(dst);
+       size_t len = strlen(src);
+       size_t ret = dst_len + len;
+       if (dst_len < size) {
+               dst += dst_len;
+               size -= dst_len;
+               if (len >= size)
+                       len = size - 1;
+               memcpy(dst, src, len);
+               dst[len] = '\0';
+       }
+       return ret;
 }