diff options
author | Willy Tarreau <w@1wt.eu> | 2022-03-21 18:33:10 +0100 |
---|---|---|
committer | Paul E. McKenney <paulmck@kernel.org> | 2022-04-20 17:05:45 -0700 |
commit | 0e7b492943ec8cfdc7fffd9304d496315f781ea7 (patch) | |
tree | 827a64796b348b5966f31f4626cc0a343743920a /tools/include/nolibc | |
parent | tools/nolibc/stdio: add support for '%p' to vfprintf() (diff) | |
download | linux-dev-0e7b492943ec8cfdc7fffd9304d496315f781ea7.tar.xz linux-dev-0e7b492943ec8cfdc7fffd9304d496315f781ea7.zip |
tools/nolibc/string: add strcmp() and strncmp()
We need these functions all the time, including when checking environment
variables and parsing command-line arguments. These implementations were
optimized to show optimal code size on a wide range of compilers (22 bytes
return included for strcmp(), 33 for strncmp()).
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Diffstat (limited to 'tools/include/nolibc')
-rw-r--r-- | tools/include/nolibc/string.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index 4554b6fcb400..0d5e870c7c0b 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -103,6 +103,17 @@ char *strchr(const char *s, int c) } static __attribute__((unused)) +int strcmp(const char *a, const char *b) +{ + unsigned int c; + int diff; + + while (!(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c) + ; + return diff; +} + +static __attribute__((unused)) char *strcpy(char *dst, const char *src) { char *ret = dst; @@ -184,6 +195,18 @@ char *strncat(char *dst, const char *src, size_t size) return orig; } +static __attribute__((unused)) +int strncmp(const char *a, const char *b, size_t size) +{ + unsigned int c; + int diff = 0; + + while (size-- && + !(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c) + ; + + return diff; +} static __attribute__((unused)) char *strncpy(char *dst, const char *src, size_t size) |