From patchwork Tue Jun 23 21:39:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 487795 X-Patchwork-Delegate: sjg@chromium.org 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 2D90314012C for ; Wed, 24 Jun 2015 07:43:22 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b=mSvrLaG3; dkim-atps=neutral Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id C8E844B6A2; Tue, 23 Jun 2015 23:42:23 +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 G68x_5Fxm1bZ; Tue, 23 Jun 2015 23:42:23 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5F5704B74D; Tue, 23 Jun 2015 23:41:19 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0C0974B652 for ; Tue, 23 Jun 2015 23:40:13 +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 VtGiVLi8NA9U for ; Tue, 23 Jun 2015 23:40:12 +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 mail-ie0-f178.google.com (mail-ie0-f178.google.com [209.85.223.178]) by theia.denx.de (Postfix) with ESMTPS id 48C374B66A for ; Tue, 23 Jun 2015 23:40:03 +0200 (CEST) Received: by ieqy10 with SMTP id y10so21149020ieq.0 for ; Tue, 23 Jun 2015 14:40:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=1DXlHO+bFyfLaZXRhAH0F3L5TJecTMgjCaZV6eOfeGU=; b=mSvrLaG3rY3rBeWiMGSlStvov+EUaiGPtfH62vw2H8XpsRnPLVT2OlO12W00Cih/kL mi9svi7JwKVjiTyoaKcE1qkYyVd4HSPQ5dKhCvOvFmsfO4PdWg3F1ws6B0jxDF/4PsLa qzBEp21RCyZ0L6N6mbxhhFiO3ka+hlcULwY2b7V9P04masw7ySzk9XRhYOneegVoedko xP8y0IwSi1WpGdQYBIZM+P4qKRVqySAbfhcFLj6PEnU5amSKdIcRghXF8SmCjrJn5FXQ GH1OF6Kv0WaRQxIOkrVTLTeApI4yQA+EtZRq8DCf6zNKuYfsSfUk7U9yuYqVIHeKh/+S r+7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=1DXlHO+bFyfLaZXRhAH0F3L5TJecTMgjCaZV6eOfeGU=; b=XPo2UPLKQSaK8ynsUewM7TJvm3kp0n/6zK7nRsTax56Tyb8UjffOmmLRWUSkgIhc61 uMWw7v8rxoSLKK31h+d4vEJEWzYELR6CMkIX7wutlZDLzilZtDvPoF9h28L/QBgVZv5o lF0lSRobpb5qmrRqka9cOJACQqTYNdrBDH3+/yrN1enFfkrv+9AEJzJF9GfzirnE32YD sR8aMIVp7hvu/x7dVYqJz1oR3DA2F1ekRSsS8XBpka5zj+N9a+R+vEdxeS+L3aRlj2zO 1opGQGIYasZDa4yQ3OqATssmHIe1DW1HQZm4jY8PihFPa07G+9L2hlO9yLl9RCPmRJl1 L5Pg== X-Gm-Message-State: ALoCoQkcpLGLOkowAY+qxB72/J7SHNMWJ2eybKqbAHjWBSQ1RiNlf0+K6VNWvWbTaPJWGO2m3mJw X-Received: by 10.107.168.150 with SMTP id e22mr44502717ioj.9.1435095602199; Tue, 23 Jun 2015 14:40:02 -0700 (PDT) Received: from kaki.bld.corp.google.com ([172.29.216.32]) by mx.google.com with ESMTPSA id 140sm15921630ion.16.2015.06.23.14.39.58 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 23 Jun 2015 14:40:00 -0700 (PDT) Received: by kaki.bld.corp.google.com (Postfix, from userid 121222) id 77CA9222133; Tue, 23 Jun 2015 15:39:53 -0600 (MDT) From: Simon Glass To: U-Boot Mailing List Date: Tue, 23 Jun 2015 15:39:08 -0600 Message-Id: <1435095556-15924-47-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 2.4.3.573.g4eafbef In-Reply-To: <1435095556-15924-1-git-send-email-sjg@chromium.org> References: <1435095556-15924-1-git-send-email-sjg@chromium.org> Cc: Tom Rini , Przemyslaw Marczak Subject: [U-Boot] [PATCH v3 46/54] lib: Add function to extract a number from the end of a string 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" Split out the code in fdtdec which finds a number at the end of a string. It can be useful in other situations. Signed-off-by: Simon Glass --- Changes in v3: None Changes in v2: None include/vsprintf.h | 26 ++++++++++++++++++++++++++ lib/fdtdec.c | 14 ++++++-------- lib/vsprintf.c | 19 +++++++++++++++++++ 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/include/vsprintf.h b/include/vsprintf.h index 09c8abd..3af8a4f 100644 --- a/include/vsprintf.h +++ b/include/vsprintf.h @@ -41,6 +41,32 @@ unsigned long long simple_strtoull(const char *cp, char **endp, long simple_strtol(const char *cp, char **endp, unsigned int base); /** + * trailing_strtol() - extract a trailing integer from a string + * + * Given a string this finds a trailing number on the string and returns it. + * For example, "abc123" would return 123. + * + * @str: String to exxamine + * @return training number if found, else -1 + */ +long trailing_strtol(const char *str); + +/** + * trailing_strtoln() - extract a trailing integer from a fixed-length string + * + * Given a fixed-length string this finds a trailing number on the string + * and returns it. For example, "abc123" would return 123. Only the + * characters between @str and @end - 1 are examined. If @end is NULL, it is + * set to str + strlen(str). + * + * @str: String to exxamine + * @end: Pointer to end of string to examine, or NULL to use the + * whole string + * @return training number if found, else -1 + */ +long trailing_strtoln(const char *str, const char *end); + +/** * panic() - Print a message and reset/hang * * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is diff --git a/lib/fdtdec.c b/lib/fdtdec.c index 9877849..add9adc 100644 --- a/lib/fdtdec.c +++ b/lib/fdtdec.c @@ -505,8 +505,7 @@ int fdtdec_get_alias_seq(const void *blob, const char *base, int offset, const char *prop; const char *name; const char *slash; - const char *p; - int len; + int len, val; prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len); debug(" - %s, %s\n", name, prop); @@ -517,12 +516,11 @@ int fdtdec_get_alias_seq(const void *blob, const char *base, int offset, slash = strrchr(prop, '/'); if (strcmp(slash + 1, find_name)) continue; - for (p = name + strlen(name) - 1; p > name; p--) { - if (!isdigit(*p)) { - *seqp = simple_strtoul(p + 1, NULL, 10); - debug("Found seq %d\n", *seqp); - return 0; - } + val = trailing_strtol(name); + if (val != -1) { + *seqp = val; + debug("Found seq %d\n", *seqp); + return 0; } } diff --git a/lib/vsprintf.c b/lib/vsprintf.c index bedc865..e7e569e 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -166,6 +166,25 @@ unsigned long long simple_strtoull(const char *cp, char **endp, return result; } +long trailing_strtoln(const char *str, const char *end) +{ + const char *p; + + if (!end) + end = str + strlen(str); + for (p = end - 1; p > str; p--) { + if (!isdigit(*p)) + return simple_strtoul(p + 1, NULL, 10); + } + + return -1; +} + +long trailing_strtol(const char *str) +{ + return trailing_strtoln(str, NULL); +} + /* we use this so that we can do without the ctype library */ #define is_digit(c) ((c) >= '0' && (c) <= '9')