summaryrefslogtreecommitdiff
path: root/src/libcamera/utils.cpp
diff options
context:
space:
mode:
authorPaul Elder <paul.elder@ideasonboard.com>2020-01-03 15:52:19 -0500
committerPaul Elder <paul.elder@ideasonboard.com>2020-01-03 19:53:07 -0500
commit4910ff05c06b70f25e4799705913c4e282abfd86 (patch)
tree92986b561302e85635db8a1012d2db4d9123e227 /src/libcamera/utils.cpp
parent1acad98f7d162ee4e9d5f869489b0c3b17ad80aa (diff)
libcamera: utils: Add strlcpy
strlcpy is available in libbsd, bionic, musl, and ulibc, but not in glibc. Instead of checking for strlcpy availability and modifying dependencies, implement it in utils, as a wrapper around strncpy. Signed-off-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'src/libcamera/utils.cpp')
-rw-r--r--src/libcamera/utils.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
index d632f6e6..5de9e481 100644
--- a/src/libcamera/utils.cpp
+++ b/src/libcamera/utils.cpp
@@ -182,6 +182,28 @@ operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex
* otherwise. The \a os stream configuration is not modified.
*/
+/**
+ * \brief Copy a string with a size limit
+ * \param[in] dst The destination string
+ * \param[in] src The source string
+ * \param[in] size The size of the destination string
+ *
+ * This function copies the null-terminated string \a src to \a dst with a limit
+ * of \a size - 1 characters, and null-terminates the result if \a size is
+ * larger than 0. If \a src is larger than \a size - 1, \a dst is truncated.
+ *
+ * \return The size of \a src
+ */
+size_t strlcpy(char *dst, const char *src, size_t size)
+{
+ if (size) {
+ strncpy(dst, src, size);
+ dst[size - 1] = '\0';
+ }
+
+ return strlen(src);
+}
+
} /* namespace utils */
} /* namespace libcamera */