[libcamera-devel] [PATCH v6 1/5] libcamera: utils: Add strlcpy

Paul Elder paul.elder at ideasonboard.com
Sat Jan 4 01:14:09 CET 2020


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 at ideasonboard.com>

---
New in v6
---
 src/libcamera/include/utils.h |  3 +++
 src/libcamera/utils.cpp       | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h
index a80f7d09..badc7753 100644
--- a/src/libcamera/include/utils.h
+++ b/src/libcamera/include/utils.h
@@ -12,6 +12,7 @@
 #include <memory>
 #include <ostream>
 #include <string>
+#include <string.h>
 #include <sys/time.h>
 
 #define ARRAY_SIZE(a)	(sizeof(a) / sizeof(a[0]))
@@ -112,6 +113,8 @@ inline _hex hex<uint64_t>(uint64_t value, unsigned int width)
 }
 #endif
 
+size_t strlcpy(char *dst, const char *src, size_t size);
+
 } /* namespace utils */
 
 } /* namespace libcamera */
diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
index d632f6e6..2d6e082e 100644
--- a/src/libcamera/utils.cpp
+++ b/src/libcamera/utils.cpp
@@ -182,6 +182,25 @@ 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
+ *
+ * strlcpy is available in libbsd, bionic, musl, and ulibc, but not in glibc.
+ * Instead of checking for strlcpy availability and modifying dependencies,
+ * it is implemented here as a wrapper around strncpy.
+ *
+ * \return The size of \a src
+ */
+size_t strlcpy(char *dst, const char *src, size_t size)
+{
+	strncpy(dst, src, size);
+	dst[size - 1] = '\0';
+	return strlen(src);
+}
+
 } /* namespace utils */
 
 } /* namespace libcamera */
-- 
2.24.1



More information about the libcamera-devel mailing list