From patchwork Fri Jan 4 18:14:04 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: York Sun X-Patchwork-Id: 209517 X-Patchwork-Delegate: wd@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id A41752C0080 for ; Sat, 5 Jan 2013 05:30:21 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 495514A054; Fri, 4 Jan 2013 19:30:10 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id br8NBlctMF+Y; Fri, 4 Jan 2013 19:30:10 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 989FE4A0BB; Fri, 4 Jan 2013 19:29:46 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 9CDAD4A054 for ; Fri, 4 Jan 2013 19:29:38 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id lV9ysPplzLAy for ; Fri, 4 Jan 2013 19:29:37 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from va3outboundpool.messaging.microsoft.com (va3ehsobe001.messaging.microsoft.com [216.32.180.11]) by theia.denx.de (Postfix) with ESMTPS id 962CD4A04E for ; Fri, 4 Jan 2013 19:29:36 +0100 (CET) Received: from mail141-va3-R.bigfish.com (10.7.14.241) by VA3EHSOBE003.bigfish.com (10.7.40.23) with Microsoft SMTP Server id 14.1.225.23; Fri, 4 Jan 2013 18:14:30 +0000 Received: from mail141-va3 (localhost [127.0.0.1]) by mail141-va3-R.bigfish.com (Postfix) with ESMTP id BDE9E2C017D for ; Fri, 4 Jan 2013 18:14:30 +0000 (UTC) X-Forefront-Antispam-Report: CIP:70.37.183.190; KIP:(null); UIP:(null); IPV:NLI; H:mail.freescale.net; RD:none; EFVD:NLI X-SpamScore: 10 X-BigFish: VS10(z2e28rzzz1de0h1202h1e76h1d1ah1d2ahzz8275bhz2dh2a8h668h839hd24he5bhf0ah1288h12a5h12a9h12bdh12e5h137ah139eh13b6h1441h1504h1537h162dh1631h1758h1155h) Received: from mail141-va3 (localhost.localdomain [127.0.0.1]) by mail141-va3 (MessageSwitch) id 1357323269935579_4388; Fri, 4 Jan 2013 18:14:29 +0000 (UTC) Received: from VA3EHSMHS008.bigfish.com (unknown [10.7.14.254]) by mail141-va3.bigfish.com (Postfix) with ESMTP id E011E30003F for ; Fri, 4 Jan 2013 18:14:29 +0000 (UTC) Received: from mail.freescale.net (70.37.183.190) by VA3EHSMHS008.bigfish.com (10.7.99.18) with Microsoft SMTP Server (TLS) id 14.1.225.23; Fri, 4 Jan 2013 18:14:27 +0000 Received: from az84smr01.freescale.net (10.64.34.197) by 039-SN1MMR1-003.039d.mgd.msft.net (10.84.1.16) with Microsoft SMTP Server (TLS) id 14.2.318.3; Fri, 4 Jan 2013 18:14:26 +0000 Received: from oslab-l1.am.freescale.net ([10.214.81.109]) by az84smr01.freescale.net (8.14.3/8.14.0) with ESMTP id r04IEKmC022122; Fri, 4 Jan 2013 11:14:25 -0700 From: York Sun To: Date: Fri, 4 Jan 2013 10:14:04 -0800 Message-ID: <1357323245-12455-6-git-send-email-yorksun@freescale.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1357323245-12455-1-git-send-email-yorksun@freescale.com> References: <1357323245-12455-1-git-send-email-yorksun@freescale.com> MIME-Version: 1.0 X-OriginatorOrg: freescale.com Cc: James Yang , afleming@freescale.com Subject: [U-Boot] [PATCH 6/7] getenv_f() env variable exist w/o needing a buffer X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de From: James Yang getenv_f() searches the environment for a variable name and copies the value of the variable to a buffer pointed to by one of the function's parameters. However, this means that the buffer needs to exist and needs to be of sufficient length (passed as another parameter to getenv_f()) to hold the requested variable's value, even if all that is desired is the mere detection of the existence of the variable itself. This patch removes the requirement that the buffer needs to exist. If the pointer to the buffer is set to NULL and the requested variable is found, getenv_f() returns 1, else it returns -1. The buffer length parameter is ignored if the pointer is set to NULL. The original functionality of getenv_f() is retained (return number of bytes copied if variable is found, -1 if not), other than being able to copy the variable's value to the address 0. Signed-off-by: James Yang Acked-by: Timur Tabi --- common/cmd_nvedit.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c index 7633f0c..caa8a36 100644 --- a/common/cmd_nvedit.c +++ b/common/cmd_nvedit.c @@ -587,6 +587,9 @@ char *getenv(const char *name) /* * Look up variable from environment for restricted C runtime env. + * If the variable is found, return the number of bytes copied. + * If buf is NULL, len is ignored, and, if the variable is found, return 1. + * If the variable is not found, return -1. */ int getenv_f(const char *name, char *buf, unsigned len) { @@ -604,7 +607,11 @@ int getenv_f(const char *name, char *buf, unsigned len) if (val < 0) continue; - /* found; copy out */ + /* found */ + if (!buf) + return 1; + + /* copy out */ for (n = 0; n < len; ++n, ++buf) { *buf = env_get_char(val++); if (*buf == '\0')