From patchwork Sat Jan 4 00:14:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 2491 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9B035605CD for ; Sat, 4 Jan 2020 01:14:21 +0100 (CET) Received: from neptunite.amanokami.net (unknown [96.44.9.94]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 00007A3C; Sat, 4 Jan 2020 01:14:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1578096861; bh=1lZxjm3xWwEbemKW0g/mKeNajsQnkPWILjS0yntCsFQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AW1km2VQ828WJNe/n/paxWCxbETTt3aBK+I0vMOul73fGu/xFbAQp1ItvtddX3UP8 xvppbHSJauqYfOyiXR52+zHyy3N6YvDcq5AvIAZtFz5QyVqIqfp8i3i/9vD6fO67Do QojHCO9e2e4DavhNaN0Dw1zoszTmhI65ykaahetw= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Fri, 3 Jan 2020 19:14:09 -0500 Message-Id: <20200104001414.12755-2-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200104001414.12755-1-paul.elder@ideasonboard.com> References: <20200104001414.12755-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v6 1/5] libcamera: utils: Add strlcpy X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 04 Jan 2020 00:14:21 -0000 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 --- 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 #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..2d6e082e 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -182,6 +182,25 @@ 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 + * + * 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 */