LibC: Misc additions

This commit is contained in:
Sergey Bugaev 2019-10-02 22:48:14 +03:00 committed by Andreas Kling
parent 17597f4681
commit 89bf38864a
Notes: sideshowbarker 2024-07-19 11:50:59 +09:00
4 changed files with 40 additions and 4 deletions

View file

@ -4,6 +4,7 @@
__BEGIN_DECLS
#define RTLD_DEFAULT 0
#define RTLD_LAZY 1
#define RTLD_NOW 2
#define RTLD_GLOBAL 3

View file

@ -23,6 +23,9 @@
#define LONG_MAX 2147483647L
#define LONG_MIN (-LONG_MAX - 1L)
#define LONG_LONG_MAX 9223372036854775807LL
#define LONG_LONG_MIN (-LONG_LONG_MAX - 1LL)
#define CHAR_MIN SCHAR_MIN
#define CHAR_MAX SCHAR_MAX

View file

@ -167,6 +167,8 @@ int putenv(char* new_var)
return 0;
}
}
double strtod(const char* str, char** endptr)
{
(void)str;
@ -175,6 +177,15 @@ double strtod(const char* str, char** endptr)
ASSERT_NOT_REACHED();
}
long double strtold(const char* str, char** endptr)
{
(void)str;
(void)endptr;
dbgprintf("LibC: strtold: '%s'\n", str);
ASSERT_NOT_REACHED();
}
float strtof(const char* str, char** endptr)
{
(void)str;
@ -374,7 +385,8 @@ size_t mbstowcs(wchar_t*, const char*, size_t)
ASSERT_NOT_REACHED();
}
long strtol(const char* nptr, char** endptr, int base)
template<typename T, T min_value, T max_value>
static T strtol_impl(const char* nptr, char** endptr, int base)
{
errno = 0;
@ -411,8 +423,8 @@ long strtol(const char* nptr, char** endptr, int base)
}
}
long cutoff_point = is_negative ? (LONG_MIN / base) : (LONG_MAX / base);
int max_valid_digit_at_cutoff_point = is_negative ? (LONG_MIN % base) : (LONG_MAX % base);
long cutoff_point = is_negative ? (min_value / base) : (max_value / base);
int max_valid_digit_at_cutoff_point = is_negative ? (min_value % base) : (max_value % base);
long num = 0;
@ -441,7 +453,7 @@ long strtol(const char* nptr, char** endptr, int base)
if (is_past_cutoff || (num == cutoff_point && digit > max_valid_digit_at_cutoff_point)) {
has_overflowed = true;
num = is_negative ? LONG_MIN : LONG_MAX;
num = is_negative ? min_value : max_value;
errno = ERANGE;
} else {
num *= base;
@ -459,10 +471,27 @@ long strtol(const char* nptr, char** endptr, int base)
return num;
}
long strtol(const char* str, char** endptr, int base)
{
return strtol_impl<long, LONG_MIN, LONG_MAX>(str, endptr, base);
}
unsigned long strtoul(const char* str, char** endptr, int base)
{
auto value = strtol(str, endptr, base);
ASSERT(value >= 0);
return value;
}
long long strtoll(const char* str, char** endptr, int base)
{
return strtol_impl<long long, LONG_LONG_MIN, LONG_LONG_MAX>(str, endptr, base);
}
unsigned long long strtoull(const char* str, char** endptr, int base)
{
auto value = strtoll(str, endptr, base);
ASSERT(value >= 0);
return value;
}

View file

@ -23,8 +23,11 @@ int atoi(const char*);
long atol(const char*);
long long atoll(const char*);
double strtod(const char*, char** endptr);
long double strtold(const char*, char** endptr);
float strtof(const char*, char** endptr);
long strtol(const char*, char** endptr, int base);
long long strtoll(const char*, char** endptr, int base);
unsigned long long strtoull(const char*, char** endptr, int base);
unsigned long strtoul(const char*, char** endptr, int base);
void qsort(void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*));
void qsort_r(void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void* arg);