From patchwork Wed Apr 29 05:50:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Hershberger X-Patchwork-Id: 465894 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 5322E14032D for ; Wed, 29 Apr 2015 15:54:34 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 8609B4BB57; Wed, 29 Apr 2015 07:54:05 +0200 (CEST) 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 vB8jAFj99pyW; Wed, 29 Apr 2015 07:54:05 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id CB80F4BAE5; Wed, 29 Apr 2015 07:53:36 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id B34784BA45 for ; Wed, 29 Apr 2015 07:53:22 +0200 (CEST) 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 woqRP9FWeVuA for ; Wed, 29 Apr 2015 07:53:22 +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 ni.com (skprod2.natinst.com [130.164.80.23]) by theia.denx.de (Postfix) with ESMTPS id 0E9944BA7A for ; Wed, 29 Apr 2015 07:53:18 +0200 (CEST) Received: from us-aus-mgwout1.amer.corp.natinst.com (nb-chan1-1338.natinst.com [130.164.19.134]) by us-aus-skprod2.natinst.com (8.15.0.59/8.15.0.59) with ESMTP id t3T5rE1f020668; Wed, 29 Apr 2015 00:53:14 -0500 Received: from linux-xvxi.natinst.com ([130.164.14.198]) by us-aus-mgwout1.amer.corp.natinst.com (Lotus Domino Release 8.5.3FP6) with ESMTP id 2015042900531479-380890 ; Wed, 29 Apr 2015 00:53:14 -0500 From: Joe Hershberger To: u-boot@lists.denx.de Date: Wed, 29 Apr 2015 00:50:52 -0500 Message-Id: <1430286666-392-6-git-send-email-joe.hershberger@ni.com> X-Mailer: git-send-email 1.7.11.5 In-Reply-To: <1430286666-392-1-git-send-email-joe.hershberger@ni.com> References: <1429653771-11676-1-git-send-email-joe.hershberger@ni.com> <1430286666-392-1-git-send-email-joe.hershberger@ni.com> X-MIMETrack: Itemize by SMTP Server on US-AUS-MGWOut1/AUS/H/NIC(Release 8.5.3FP6|November 21, 2013) at 04/29/2015 12:53:14 AM, Serialize by Router on US-AUS-MGWOut1/AUS/H/NIC(Release 8.5.3FP6|November 21, 2013) at 04/29/2015 12:53:14 AM, Serialize complete at 04/29/2015 12:53:14 AM X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2015-04-29_01:, , signatures=0 Cc: Tom Rini , Joe Hershberger Subject: [U-Boot] [PATCH v2 05/19] env: Simplify the reverse_strstr() interface X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" The logic to find the whole matching name was split needlessly between the reverse_strstr function and its caller. Fully contain it to make the interface for calling it more consistent. Signed-off-by: Joe Hershberger --- Changes in v2: -Fix bisectability issue -Fix corner case in reverse_name_search() where searched starts with ' ' common/env_attr.c | 87 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/common/env_attr.c b/common/env_attr.c index e791f44..6e13184 100644 --- a/common/env_attr.c +++ b/common/env_attr.c @@ -109,33 +109,59 @@ int env_attr_walk(const char *attr_list, } /* - * Search for the last matching string in another string with the option to - * start looking at a certain point (i.e. ignore anything beyond that point). + * Search for the last exactly matching name in an attribute list */ -static char *reverse_strstr(const char *searched, const char *search_for, - const char *searched_start) +static int reverse_name_search(const char *searched, const char *search_for, + const char **result) { - char *result = NULL; + int result_size = 0; + const char *cur_searched = searched; - if (*search_for == '\0') - return (char *)searched; + if (result) + *result = NULL; + + if (*search_for == '\0') { + if (result) + *result = searched; + return strlen(searched); + } for (;;) { - char *match = strstr(searched, search_for); - - /* - * Stop looking if no new match is found or looking past the - * searched_start pointer - */ - if (match == NULL || (searched_start != NULL && - match + strlen(search_for) > searched_start)) + const char *match = strstr(cur_searched, search_for); + const char *prevch; + const char *nextch; + + /* Stop looking if no new match is found */ + if (match == NULL) break; - result = match; - searched = match + 1; + prevch = match - 1; + nextch = match + strlen(search_for); + + /* Skip spaces */ + while (*prevch == ' ' && prevch >= searched) + prevch--; + while (*nextch == ' ') + nextch++; + + /* Start looking past the current match so last is found */ + cur_searched = match + 1; + /* Check for an exact match */ + if (match != searched && + *prevch != ENV_ATTR_LIST_DELIM && + prevch != searched - 1) + continue; + if (*nextch != ENV_ATTR_SEP && + *nextch != ENV_ATTR_LIST_DELIM && + *nextch != '\0') + continue; + + if (result) + *result = match; + result_size = strlen(search_for); } - return result; + return result_size; } /* @@ -145,6 +171,7 @@ static char *reverse_strstr(const char *searched, const char *search_for, int env_attr_lookup(const char *attr_list, const char *name, char *attributes) { const char *entry = NULL; + int entry_len; if (!attributes) /* bad parameter */ @@ -153,32 +180,12 @@ int env_attr_lookup(const char *attr_list, const char *name, char *attributes) /* list not found */ return -EINVAL; - entry = reverse_strstr(attr_list, name, NULL); - while (entry != NULL) { - const char *prevch = entry - 1; - const char *nextch = entry + strlen(name); - - /* Skip spaces */ - while (*prevch == ' ') - prevch--; - while (*nextch == ' ') - nextch++; - - /* check for an exact match */ - if ((entry == attr_list || - *prevch == ENV_ATTR_LIST_DELIM) && - (*nextch == ENV_ATTR_SEP || - *nextch == ENV_ATTR_LIST_DELIM || - *nextch == '\0')) - break; - - entry = reverse_strstr(attr_list, name, entry); - } + entry_len = reverse_name_search(attr_list, name, &entry); if (entry != NULL) { int len; /* skip the name */ - entry += strlen(name); + entry += entry_len; /* skip spaces */ while (*entry == ' ') entry++;