diff options
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibC/inttypes.cpp | 33 | ||||
-rw-r--r-- | Userland/Libraries/LibC/inttypes.h | 3 |
2 files changed, 36 insertions, 0 deletions
diff --git a/Userland/Libraries/LibC/inttypes.cpp b/Userland/Libraries/LibC/inttypes.cpp index 87bbb7d024..eeb5ff11ec 100644 --- a/Userland/Libraries/LibC/inttypes.cpp +++ b/Userland/Libraries/LibC/inttypes.cpp @@ -24,7 +24,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <AK/NumericLimits.h> +#include <errno.h> #include <inttypes.h> +#include <stdlib.h> extern "C" { @@ -41,4 +44,34 @@ imaxdiv_t imaxdiv(intmax_t numerator, intmax_t denominator) return result; } + +intmax_t strtoimax(const char* str, char** endptr, int base) +{ + long long_value = strtoll(str, endptr, base); + + intmax_t max_int_value = NumericLimits<intmax_t>::max(); + intmax_t min_int_value = NumericLimits<intmax_t>::min(); + if (long_value > max_int_value) { + errno = -ERANGE; + return max_int_value; + } else if (long_value < min_int_value) { + errno = -ERANGE; + return min_int_value; + } + + return long_value; +} + +uintmax_t strtoumax(const char* str, char** endptr, int base) +{ + unsigned long ulong_value = strtoull(str, endptr, base); + + uintmax_t max_uint_value = NumericLimits<uintmax_t>::max(); + if (ulong_value > max_uint_value) { + errno = -ERANGE; + return max_uint_value; + } + + return ulong_value; +} } diff --git a/Userland/Libraries/LibC/inttypes.h b/Userland/Libraries/LibC/inttypes.h index fc301d5407..3ceae444d0 100644 --- a/Userland/Libraries/LibC/inttypes.h +++ b/Userland/Libraries/LibC/inttypes.h @@ -78,4 +78,7 @@ typedef struct imaxdiv_t { } imaxdiv_t; imaxdiv_t imaxdiv(intmax_t, intmax_t); +intmax_t strtoimax(const char*, char** endptr, int base); +uintmax_t strtoumax(const char*, char** endptr, int base); + __END_DECLS |