/**
- * 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.
+ * Like strlcpy but portable. The given string @a src is copied until its null
+ * byte or until @a n - 1 bytes have been read. 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 dst destination of the copy (must be @a n bytes long)
+ * @param src source of the copy (at most @a n - 1 bytes will be read)
* @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
size_t slen;
GNUNET_assert (0 != n);
- ret = strlen (src);
- slen = GNUNET_MIN (ret, n - 1);
+ slen = strnlen (src, n - 1);
memcpy (dst, src, slen);
dst[slen] = '\0';
- return ret;
+ return slen;
}
/**
- * 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.
+ * Like strlcpy but portable. The given string @a src is copied until its null
+ * byte or until @a n - 1 bytes have been read. 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 dst destination of the copy (must be @a n bytes long)
+ * @param src source of the copy (at most @a n - 1 bytes will be read)
* @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
size_t slen;
GNUNET_assert (0 != n);
- ret = strlen (src);
- slen = GNUNET_MIN (ret, n - 1);
+ slen = strnlen (src, n - 1);
memcpy (dst, src, slen);
dst[slen] = '\0';
- return ret;
+ return slen;
}
/**
- * 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.
+ * Like strlcpy but portable. The given string @a src is copied until its null
+ * byte or until @a n - 1 bytes have been read. 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 dst destination of the copy (must be @a n bytes long)
+ * @param src source of the copy (at most @a n - 1 bytes will be read)
* @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
size_t slen;
GNUNET_assert (0 != n);
- ret = strlen (src);
- slen = GNUNET_MIN (ret, n - 1);
+ slen = strnlen (src, n - 1);
memcpy (dst, src, slen);
dst[slen] = '\0';
- return ret;
+ return slen;
}