From patchwork Mon Feb 13 13:55:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 64888 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 78F4C3886C53 for ; Mon, 13 Feb 2023 13:56:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 78F4C3886C53 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676296597; bh=ZVWq7zAWbA5/oYRxY+9yU9yA7zGCWzAZRvU9UjtdPUM=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=no4y5dS9SbKQoDkndVsX8V+UFRV8ZwwSJqoQGrNrOdr1O7XAqnFaf/iWSEoB/sMls F7KCAv+kgH6bKkIx9J4eRJsSHFD1CEahblLR+WzMKHm7VsdrQqO7f8D0GiIXpnoNh2 StVm9J9y5/g3qWm69ePxyAADcZ1Rxac6N2SjEVQw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc31.google.com (mail-oo1-xc31.google.com [IPv6:2607:f8b0:4864:20::c31]) by sourceware.org (Postfix) with ESMTPS id 9593E383FBAE for ; Mon, 13 Feb 2023 13:56:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9593E383FBAE Received: by mail-oo1-xc31.google.com with SMTP id i17-20020a4adf11000000b0051abd9835d4so1191352oou.1 for ; Mon, 13 Feb 2023 05:56:07 -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:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZVWq7zAWbA5/oYRxY+9yU9yA7zGCWzAZRvU9UjtdPUM=; b=b9nwBdNlXEvvdZxCmydOtIMeu5DcFnSdwIike/tPMDK1pVTF+V/4qb3fOD9TDBtL31 hKXzzw5SW8+bl339pSB+XSkMSLSiflv20487Du2mSWexdqkAPyWYsPofuWc93HggRxRW 0pupooVkyHwm1dQk7vBjPGUNLz6mkJ+J1tF07q6KrtkV+4uDVrCfRf9Ru4vIo5i/LcmQ MtziQzUXjZN39L6ay+Iduz6IWmfFVGOm2Wee9T/5FfaaLJu3k7PMOlrB9rxHD7byom4v 02s9X1hrcXRTkQavg/8K8oLknlMDxAm4vGh3XoZybiddk74a3yuggwIwKbwEVHN7VanZ LlCQ== X-Gm-Message-State: AO0yUKVBdpFxY+sk5ZVFmUPGCGcO1FuWA+tXP/izuwC0zbrkIs4c1AQ6 T3js6Yp62WQnvUPsB1GV335LQscEVa4EZWimvPo= X-Google-Smtp-Source: AK7set/xDZm6bojREBjkslphWXqIIx3D6dppDPuCXz5OJNQSIWWrntUD4K1+CGf350JXRGMtbdf+4w== X-Received: by 2002:a05:6820:198d:b0:51a:186f:88c4 with SMTP id bp13-20020a056820198d00b0051a186f88c4mr14025144oob.0.1676296566235; Mon, 13 Feb 2023 05:56:06 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c2:f662:138b:cd0c:7b7a:8860]) by smtp.gmail.com with ESMTPSA id y11-20020a4ade0b000000b0051762a73553sm4797168oot.18.2023.02.13.05.56.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Feb 2023 05:56:05 -0800 (PST) To: libc-alpha@sourceware.org, Wilco Dijkstra Subject: [PATCH 2/7] stdlib: Simplify getenv Date: Mon, 13 Feb 2023 10:55:53 -0300 Message-Id: <20230213135558.3328727-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230213135558.3328727-1-adhemerval.zanella@linaro.org> References: <20230213135558.3328727-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" And remove _STRING_ARCH_unaligned usage. The small size optimization just adds boilerplate code. Checked on x86_64-linux-gnu and i686-linux-gnu. Reviewed-by: Wilco Dijkstra --- stdlib/getenv.c | 63 ++++--------------------------------------------- 1 file changed, 4 insertions(+), 59 deletions(-) diff --git a/stdlib/getenv.c b/stdlib/getenv.c index e3157ce2f3..449bba1e16 100644 --- a/stdlib/getenv.c +++ b/stdlib/getenv.c @@ -15,76 +15,21 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include #include #include #include - -/* Return the value of the environment variable NAME. This implementation - is tuned a bit in that it assumes no environment variable has an empty - name which of course should always be true. We have a special case for - one character names so that for the general case we can assume at least - two characters which we can access. By doing this we can avoid using the - `strncmp' most of the time. */ char * getenv (const char *name) { - char **ep; - uint16_t name_start; - if (__environ == NULL || name[0] == '\0') return NULL; - if (name[1] == '\0') - { - /* The name of the variable consists of only one character. Therefore - the first two characters of the environment entry are this character - and a '=' character. */ -#if __BYTE_ORDER == __LITTLE_ENDIAN || !_STRING_ARCH_unaligned - name_start = ('=' << 8) | *(const unsigned char *) name; -#else - name_start = '=' | ((*(const unsigned char *) name) << 8); -#endif - for (ep = __environ; *ep != NULL; ++ep) - { -#if _STRING_ARCH_unaligned - uint16_t ep_start = *(uint16_t *) *ep; -#else - uint16_t ep_start = (((unsigned char *) *ep)[0] - | (((unsigned char *) *ep)[1] << 8)); -#endif - if (name_start == ep_start) - return &(*ep)[2]; - } - } - else + size_t len = __strchrnul (name, '=') - name; + for (char **ep = __environ; *ep != NULL; ++ep) { - size_t len = strlen (name); -#if _STRING_ARCH_unaligned - name_start = *(const uint16_t *) name; -#else - name_start = (((const unsigned char *) name)[0] - | (((const unsigned char *) name)[1] << 8)); -#endif - len -= 2; - name += 2; - - for (ep = __environ; *ep != NULL; ++ep) - { -#if _STRING_ARCH_unaligned - uint16_t ep_start = *(uint16_t *) *ep; -#else - uint16_t ep_start = (((unsigned char *) *ep)[0] - | (((unsigned char *) *ep)[1] << 8)); -#endif - - if (name_start == ep_start && !strncmp (*ep + 2, name, len) - && (*ep)[len + 2] == '=') - return &(*ep)[len + 3]; - } + if (strncmp (name, *ep, len) == 0 && (*ep)[len] == '=') + return *ep + len + 1; } return NULL;