From f2b240595b1c0f10f3a73cceb3bc4f6226cb8818 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sat, 6 Jan 2024 19:46:20 +0100 Subject: [PATCH] lib/atoi/strtou_noneg.[ch]: Add strtou_noneg() It's like strtou_(), but rejects negative input, instead of silently converting it to unsigned. Link: Signed-off-by: Alejandro Colomar --- lib/atoi/strtou_noneg.c | 5 +++++ lib/atoi/strtou_noneg.h | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/atoi/strtou_noneg.c b/lib/atoi/strtou_noneg.c index 14602a9b1..ec8773d78 100644 --- a/lib/atoi/strtou_noneg.c +++ b/lib/atoi/strtou_noneg.c @@ -6,6 +6,11 @@ #include "atoi/strtou_noneg.h" +#include + + +extern inline uintmax_t strtou_noneg(const char *s, char **restrict endp, + int base, uintmax_t min, uintmax_t max, int *restrict status); extern inline unsigned long strtoul_noneg(const char *s, char **restrict endp, int base); diff --git a/lib/atoi/strtou_noneg.h b/lib/atoi/strtou_noneg.h index 5670dc9ba..a1d155696 100644 --- a/lib/atoi/strtou_noneg.h +++ b/lib/atoi/strtou_noneg.h @@ -9,11 +9,17 @@ #include #include +#include #include +#include "atoi/strtoi.h" #include "attr.h" +ATTR_STRING(1) ATTR_ACCESS(write_only, 2) ATTR_ACCESS(write_only, 6) +inline uintmax_t strtou_noneg(const char *s, char **restrict endp, + int base, uintmax_t min, uintmax_t max, int *restrict status); + ATTR_STRING(1) ATTR_ACCESS(write_only, 2) inline unsigned long strtoul_noneg(const char *s, char **restrict endp, int base); @@ -22,6 +28,21 @@ inline unsigned long long strtoull_noneg(const char *s, char **restrict endp, int base); +inline uintmax_t +strtou_noneg(const char *s, char **restrict endp, int base, + uintmax_t min, uintmax_t max, int *restrict status) +{ + int st; + + if (status == NULL) + status = &st; + if (strtoi_(s, endp, base, 0, 1, status) == 0 && *status == ERANGE) + return min; + + return strtou_(s, endp, base, min, max, status); +} + + inline unsigned long strtoul_noneg(const char *s, char **restrict endp, int base) { -- 2.47.2