From patchwork Wed Dec 28 23:17:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 62470 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 910D33858005 for ; Wed, 28 Dec 2022 23:18:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 910D33858005 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1672269516; bh=/eSZYsPQZnGv+IJSbq0/Jy04l/4rTD21eKcKyp5bhec=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=kPxwFy8+oDeFqK88xVwv80NEzY9dxdvaMAh0DHPbQIwNiCuS4KXXJYTeXZdkQJ56M 1MJz4ahwAu3LoXzxXJy3P0BGM6e5+7ZYoWwKQw0/3gYK9axSY8oGLT2IcOFZ9rMaC4 3zq7rMQcsSj7u8SQbptMwDaX8RgvUgdLzhKnjFHg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by sourceware.org (Postfix) with ESMTPS id A0B4B3858408 for ; Wed, 28 Dec 2022 23:18:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A0B4B3858408 Received: by mail-wm1-x32a.google.com with SMTP id m3so3192335wmq.0 for ; Wed, 28 Dec 2022 15:18:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/eSZYsPQZnGv+IJSbq0/Jy04l/4rTD21eKcKyp5bhec=; b=So2slDOUZGRwrpZA3YuIFdRS05AUCH1bEEM866Gm5H/iPicIbBqteHVUqRDMRIP5o9 YZPsx2aWqMc4o4kffbJXIAm+YKeT8mxNnt3jj+9u+uZGgCOsEBwyIJC9vExh73PwZ3it LRxIkbTJdDXqSMjdnidVrhkaKokl1gh8HRqLqUe4qkPnN9RBmJOmIeTS4mzG8DVnstWK usee/ojobA0cXcZczTyewOm8w1apyenjHitdLwBc+EkERy6KSIe8E188/jCb8cbpRkzp LfusRwalS7GGNDlFV6fMLzXSBGiE8R1vHd/XiChRevnd/Pfhu+CbP8JcPkEMHdBZY2PA JhKA== X-Gm-Message-State: AFqh2koR8A6gbFUK4aQelTlgEKdLM1fBpmi++y1ZHFuJG8xWeYMyWyCy wE/7kxcZRPxzd60q82gU8W0GHIPVVic= X-Google-Smtp-Source: AMrXdXueN5c95hBbcoW4dW+UnymTxz+QTyZ2PcJPwwL6O1qyx77MLhhdqH+vpr/yhNEED9poena9lQ== X-Received: by 2002:a05:600c:5014:b0:3d3:446a:b46a with SMTP id n20-20020a05600c501400b003d3446ab46amr19680474wmr.38.1672269490240; Wed, 28 Dec 2022 15:18:10 -0800 (PST) Received: from asus5775.alejandro-colomar.es ([170.253.36.171]) by smtp.googlemail.com with ESMTPSA id l27-20020a05600c1d1b00b003cfd4e6400csm24357367wms.19.2022.12.28.15.18.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Dec 2022 15:18:09 -0800 (PST) X-Google-Original-From: Alejandro Colomar To: libc-alpha@sourceware.org Cc: Alejandro Colomar Subject: [PATCH v2 1/3] string: Add stpecpy() Date: Thu, 29 Dec 2022 00:17:40 +0100 Message-Id: <20221228231741.125945-2-alx@kernel.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20221222214217.1619716-1-alx@kernel.org> References: <20221222214217.1619716-1-alx@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3199; i=alx@kernel.org; h=from:subject; bh=S9d8W/yHdkrv7TWponnKp4CmHP/qasfb95BbVL2XkXg=; b=owEBbQKS/ZANAwAKAZ6MGvu+/9syAcsmYgBjrM6NagZtKwtjoYOC+/Fae0Bku+JB8LT3tLeQ05Wl Vs3KDXOJAjMEAAEKAB0WIQTqOofwpOugMORd8kCejBr7vv/bMgUCY6zOjQAKCRCejBr7vv/bMn/bD/ 9M1jt2X+3oGgLsFFHXm2doT134PQyRoxqp2fAmFwmtZUrQ7JU4qafzLGTkl6Vg5u6DqJfDNi1ZZT6+ q6qT17ii1nmhlZfYtaw0WUlrD97hlI0Xe2NPlfMKcLSy7Ds1oOei+4CFkpIufSdRBS5o0TeTAuSvwv Vk3toIqQ7yvlaO2+Xzxz0nHz1Y+VYJgGflkQ9acGMBTfxu2Cbep2ABKDDHzUW/LK++KSli9Xc2oxtu TBe5OUnZ2ehj0L6NuhwS702MZbao0XR48h/rq3/vMjA54ilQaHQZLjaB3DQxfafDNTGWTUL9og+rZu V8K7+xR/ggZjksWVJ09h+lyh+njKqK52kmJps1agzPnWLrK0qSG10GOiKgQhUUrcC5w+ZTCbBRaV3Z +7mb+H1npPMgoz9FmlaSI2GS3ueeTeQ4lzhTG6nszoX/tsDcnxWY8S4r8XA1zpuynYgAamdGIlyxJP cKuosq5RaFcOJM3OoNbprPA2l2YHCDdSE8IjzTAAaXJqQzXMvXvFFFXQvMpJI0+atfhTrjKpK0GfX7 ze3uhzo01U6TS/D3e4G2j0NWjpiZCt37KnypusjFSUOLT4ydhG6QfJoCtAeCOg2RAz+OhwAm4K2ACN n3wtQxA3Vf+4wBsWDr9YbbuaaT+ceQpz4lmp/tfU9V4GwOphmCX2/hOSjESQ== X-Developer-Key: i=alx@kernel.org; a=openpgp; fpr=A9348594CE31283A826FBDD8D57633D441E25BB5 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alejandro Colomar via Libc-alpha From: Alejandro Colomar Reply-To: Alejandro Colomar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This function is similar to stpcpy(3), but it tuncates the destination string if it doesn't fit the buffer. It's much simpler to use than strscpy(9) or strlcpy(3), and slightly faster. It also allows chaining with stpeprintf(3), which has the same interface as stpecpy(3), but prints a formatted string (like snprintf(3)). Signed-off-by: Alejandro Colomar --- string/Makefile | 1 + string/stpecpy.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ string/string.h | 7 +++++++ 3 files changed, 53 insertions(+) create mode 100644 string/stpecpy.c diff --git a/string/Makefile b/string/Makefile index 938f528b8d..95e9ebce6d 100644 --- a/string/Makefile +++ b/string/Makefile @@ -73,6 +73,7 @@ routines := \ sigabbrev_np \ sigdescr_np \ stpcpy \ + stpecpy \ stpncpy \ strcasecmp \ strcasecmp_l \ diff --git a/string/stpecpy.c b/string/stpecpy.c new file mode 100644 index 0000000000..6884a949a0 --- /dev/null +++ b/string/stpecpy.c @@ -0,0 +1,45 @@ +/* Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include + +char * +stpecpy(char *dst, char *end, const char *restrict src) +{ + bool trunc; + size_t dsize, dlen, slen; + + if (dst == end) + return end; + if (dst == NULL) // Allow chaining with stpeprintf(). + return NULL; + if (dst > end) + __builtin_unreachable(); + + dsize = end - dst; + slen = strnlen(src, dsize); + trunc = (slen == dsize); + dlen = slen - trunc; + dst[dlen] = '\0'; + + return mempcpy(dst, src, dlen) + trunc; +} diff --git a/string/string.h b/string/string.h index 54dd8344de..966a8cb744 100644 --- a/string/string.h +++ b/string/string.h @@ -502,6 +502,13 @@ extern char *stpncpy (char *__restrict __dest, #endif #ifdef __USE_GNU +/* Copy the string SRC into a null-terminated string at DEST, + truncating if it would run after END. Return a pointer to + the terminating null byte, or END if the string was truncated, + or NULL if DEST was NULL. */ +extern char *stpecpy (char *__dest, char *__end, const char *__restrict __src) + __THROW __nonnull ((2, 3)); + /* Compare S1 and S2 as strings holding name & indices/version numbers. */ extern int strverscmp (const char *__s1, const char *__s2) __THROW __attribute_pure__ __nonnull ((1, 2));