From 1fbe07ca9c1bb4e4af0079c1f25ef2b1017cd65c Mon Sep 17 00:00:00 2001 From: Firefly Date: Tue, 23 Jun 2015 15:39:08 -0600 Subject: [PATCH] [u-boot] 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: Firefly Signed-off-by: Firefly Conflicts: include/vsprintf.h --- u-boot/include/vsprintf.h | 36 ++++++++++++++++++++++++++++++++++++ u-boot/lib/fdtdec.c | 14 ++++++-------- u-boot/lib/vsprintf.c | 19 +++++++++++++++++++ 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/u-boot/include/vsprintf.h b/u-boot/include/vsprintf.h index 5624482d57..e18de8e3e0 100644 --- a/u-boot/include/vsprintf.h +++ b/u-boot/include/vsprintf.h @@ -39,6 +39,42 @@ int strict_strtoul(const char *cp, unsigned int base, unsigned long *res); unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base); 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 + * defined, then it will hang instead of reseting. + * + * @param fmt: printf() format string for message, which should not include + * \n, followed by arguments + */ void panic(const char *fmt, ...) __attribute__ ((format (__printf__, 1, 2), noreturn)); diff --git a/u-boot/lib/fdtdec.c b/u-boot/lib/fdtdec.c index 1c20ec5c16..1d4be18d22 100755 --- a/u-boot/lib/fdtdec.c +++ b/u-boot/lib/fdtdec.c @@ -393,8 +393,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); @@ -405,12 +404,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/u-boot/lib/vsprintf.c b/u-boot/lib/vsprintf.c index 6b5a1e9160..5c34a37720 100644 --- a/u-boot/lib/vsprintf.c +++ b/u-boot/lib/vsprintf.c @@ -169,6 +169,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')