From 4910ff05c06b70f25e4799705913c4e282abfd86 Mon Sep 17 00:00:00 2001 From: Paul Elder Date: Fri, 3 Jan 2020 15:52:19 -0500 Subject: 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 Reviewed-by: Laurent Pinchart --- src/libcamera/include/utils.h | 3 +++ src/libcamera/utils.cpp | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) (limited to 'src/libcamera') 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 #include #include +#include #include #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) @@ -112,6 +113,8 @@ inline _hex hex(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..5de9e481 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -182,6 +182,28 @@ operator<<(std::basic_ostream> &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 */ -- cgit v1.2.1