From patchwork Wed Oct 3 19:38:47 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Hershberger X-Patchwork-Id: 188909 X-Patchwork-Delegate: trini@ti.com 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 3B33A2C00B1 for ; Thu, 4 Oct 2012 06:19:40 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id C3B1528179; Wed, 3 Oct 2012 21:39:56 +0200 (CEST) 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 H0JC5rOOTwYZ; Wed, 3 Oct 2012 21:39:56 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 6C21A28181; Wed, 3 Oct 2012 21:39:34 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 64FBB28155 for ; Wed, 3 Oct 2012 21:39:24 +0200 (CEST) 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 wBJvg774Hriw for ; Wed, 3 Oct 2012 21:39:24 +0200 (CEST) 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 spamkiller06.natinst.com (mailserver6.natinst.com [130.164.80.6]) by theia.denx.de (Postfix) with ESMTP id 607092815A for ; Wed, 3 Oct 2012 21:39:19 +0200 (CEST) Received: from mailserv58-us.natinst.com (nb-hsrp-1338.natinst.com [130.164.19.133]) by spamkiller06.natinst.com (8.14.4/8.14.4) with ESMTP id q93Jd8g6004223; Wed, 3 Oct 2012 14:39:08 -0500 Received: from linux-xvxi.natinst.com ([130.164.14.197]) by mailserv58-us.natinst.com (Lotus Domino Release 8.5.3FP2 HF169) with ESMTP id 2012100314390883-376686 ; Wed, 3 Oct 2012 14:39:08 -0500 From: Joe Hershberger To: u-boot@lists.denx.de Date: Wed, 3 Oct 2012 14:38:47 -0500 Message-Id: <1349293130-5715-3-git-send-email-joe.hershberger@ni.com> X-Mailer: git-send-email 1.7.11.5 In-Reply-To: <1349293130-5715-1-git-send-email-joe.hershberger@ni.com> References: <1345236586-19076-1-git-send-email-joe.hershberger@ni.com> <1349293130-5715-1-git-send-email-joe.hershberger@ni.com> X-MIMETrack: Itemize by SMTP Server on MailServ58-US/AUS/H/NIC(Release 8.5.3FP2 HF169|September 14, 2012) at 10/03/2012 02:39:08 PM, Serialize by Router on MailServ58-US/AUS/H/NIC(Release 8.5.3FP2 HF169|September 14, 2012) at 10/03/2012 02:39:08 PM, Serialize complete at 10/03/2012 02:39:08 PM X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.7.7855, 1.0.431, 0.0.0000 definitions=2012-10-03_02:2012-10-03, 2012-10-02, 1970-01-01 signatures=0 Cc: Tom Rini , Joe Hershberger Subject: [U-Boot] [PATCH v2 2/5] tools/env: Remove unneeded complexity 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: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de The length included the name length, and then it was subtracted back out on each use. Now we don't include it in the first place. Also realloc as we process arguments and eliminate memset. Use memcpy instead of manually copying each byte. Signed-off-by: Joe Hershberger --- Changes in v2: - Further simplified based Mike's comments tools/env/fw_env.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c index 16073ea..af879f1 100644 --- a/tools/env/fw_env.c +++ b/tools/env/fw_env.c @@ -481,7 +481,6 @@ int fw_setenv(int argc, char *argv[]) int i, len; char *name; char *value = NULL; - char *tmpval = NULL; if (argc < 2) { errno = EINVAL; @@ -495,34 +494,28 @@ int fw_setenv(int argc, char *argv[]) name = argv[1]; - len = strlen(name) + 2; - for (i = 2; i < argc; ++i) - len += strlen(argv[i]) + 1; - - /* Allocate enough place to the data string */ + len = 0; for (i = 2; i < argc; ++i) { char *val = argv[i]; + size_t val_len = strlen(val); + + value = realloc(value, len + val_len + 1); if (!value) { - value = (char *)malloc(len - strlen(name)); - if (!value) { - fprintf(stderr, + fprintf(stderr, "Cannot malloc %zu bytes: %s\n", - len - strlen(name), strerror(errno)); - return -1; - } - memset(value, 0, len - strlen(name)); - tmpval = value; + len, strerror(errno)); + return -1; } - if (i != 2) - *tmpval++ = ' '; - while (*val != '\0') - *tmpval++ = *val++; + + memcpy(value + len, val, val_len); + len += val_len; + value[len++] = ' '; } + value[len - 1] = '\0'; fw_env_write(name, value); - if (value) - free(value); + free(value); return fw_env_close(); }