introduce GNUNET_strlcpy
authorlurchi <lurchi@strangeplace.net>
Thu, 27 Jun 2019 08:49:09 +0000 (10:49 +0200)
committerlurchi <lurchi@strangeplace.net>
Thu, 27 Jun 2019 08:49:09 +0000 (10:49 +0200)
src/include/gnunet_strings_lib.h
src/util/strings.c

index f43567611004015af5a2156c011ae3d3db09ac20..db657f54ebf682ecdae78d099a1bd31f6b5f60ba 100644 (file)
@@ -541,6 +541,25 @@ GNUNET_STRINGS_get_utf8_args (int argc,
                               char *const **u8argv);
 
 
+/**
+ * Like strlcpy but portable. The given string @a src is copied in full length
+ * (until its null byte). The destination buffer is guaranteed to be
+ * null-terminated.
+ *
+ * to a destination buffer
+ * and ensures that the destination string is null-terminated.
+ *
+ * @param dst destination of the copy
+ * @param src source of the copy, must be null-terminated
+ * @param n the length of the string to copy, including its terminating null
+ *          byte
+ * @return the length of the string that was copied, excluding the terminating
+ *        null byte
+ */
+size_t
+GNUNET_strlcpy(char *dst, const char *src, size_t n);
+
+
 /* ***************** IPv4/IPv6 parsing ****************** */
 
 struct GNUNET_STRINGS_PortPolicy
@@ -641,7 +660,6 @@ struct GNUNET_STRINGS_IPv6NetworkPolicy *
 GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX);
 
 
-
 #if 0                           /* keep Emacsens' auto-indent happy */
 {
 #endif
index 2cbdb640b6fd8abc1ad3e30f2f008df387cceac2..d69244e831f361dd8b892f2efeb4887af30216dd 100644 (file)
@@ -202,6 +202,36 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
 }
 
 
+/**
+ * Like strlcpy but portable. The given string @a src is copied in full length
+ * (until its null byte). The destination buffer is guaranteed to be
+ * null-terminated.
+ *
+ * to a destination buffer
+ * and ensures that the destination string is null-terminated.
+ *
+ * @param dst destination of the copy
+ * @param src source of the copy, must be null-terminated
+ * @param n the length of the string to copy, including its terminating null
+ *          byte
+ * @return the length of the string that was copied, excluding the terminating
+ *        null byte
+ */
+size_t
+GNUNET_strlcpy(char *dst, const char *src, size_t n)
+{
+  size_t ret;
+  size_t slen;
+
+  GNUNET_assert (0 != n);
+  ret = strlen (src);
+  slen = GNUNET_MIN (ret, n - 1);
+  memcpy (dst, src, slen);
+  dst[slen] = '\0';
+  return ret;
+}
+
+
 /**
  * Unit conversion table entry for 'convert_with_table'.
  */