From c4af6732c4021ffe8b3d0c82bdcf1eebb263bfc3 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 23 Jun 2015 15:39:08 -0600 Subject: lib: Add function to extract a number from the end of a string 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 --- lib/fdtdec.c | 14 ++++++-------- lib/vsprintf.c | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/fdtdec.c b/lib/fdtdec.c index 9c6b3619da2..f6c2b195a35 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 a9b8a3ae67f..4c82837cc41 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') -- cgit v1.2.3