From f87541521e517e8b65a196eada58aceb098ca918 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sun, 14 Jan 2018 22:53:21 -0600 Subject: stdlib: Implement most LSB functions plus more * Fix prototype of __realpath_chk. * Add all strto* functions from LSB, plus the ones thata were previously incorrectly in string.c The main missing LSB functions are the reentrant random functions, which should not be used anyway. Signed-off-by: Samuel Holland --- libgcompat/stdlib.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++--- libgcompat/string.c | 13 ------ 2 files changed, 107 insertions(+), 18 deletions(-) diff --git a/libgcompat/stdlib.c b/libgcompat/stdlib.c index 6783615..c1eaa92 100644 --- a/libgcompat/stdlib.c +++ b/libgcompat/stdlib.c @@ -1,14 +1,116 @@ #include /* assert */ -#include /* strtod */ +#include /* PATH_MAX */ +#include /* locale_t */ +#include /* NULL, size_t */ +#include /* getenv, realpath, strto* */ +#include /* get*id */ -char *__realpath_chk(const char *path, char *resolved_path) +/** + * Resolve a pathname, with buffer overflow checking. + * + * LSB 5.0: LSB-Core-generic/baselib---realpath-chk-1.html + */ +char *__realpath_chk(const char *path, char *resolved_path, size_t resolved_len) { assert(path != NULL); + assert(resolved_path != NULL); + assert(resolved_len >= PATH_MAX); + return realpath(path, resolved_path); } -double __strtod_internal(const char *__nptr, char **__endptr, int __group) +/** + * Get an environment variable. + */ +char *__secure_getenv(const char *name) +{ + if (geteuid() != getuid() || getegid() != getgid()) { + return NULL; + } + + return getenv(name); +} + +/** + * Underlying function for strtod. + * + * "__group shall be 0 or the behavior of __strtod_internal() is undefined." + * + * LSB 5.0: LSB-Core-generic/baselib---strtod-internal-1.html + */ +double __strtod_internal(const char *nptr, char **endptr, int group) +{ + assert(group == 0); + + return strtod(nptr, endptr); +} + +/** + * Underlying function for strtof. + * + * "__group shall be 0 or the behavior of __strtof_internal() is undefined." + * + * LSB 5.0: LSB-Core-generic/baselib---strtof-internal.html + */ +float __strtof_internal(const char *nptr, char **endptr, int group) +{ + assert(group == 0); + + return strtof(nptr, endptr); +} + +/** + * Underlying function for strtold. + * + * "__group shall be 0 or the behavior of __strtold_internal() is undefined." + * + * LSB 5.0: LSB-Core-generic/baselib---strtold-internal-1.html + */ +long double __strtold_internal(const char *nptr, char **endptr, int group) +{ + assert(group == 0); + + return strtold(nptr, endptr); +} + +/** + * Convert string value to a long long integer. + * + * Some day, when musl supports LC_NUMERIC, we can probably remove this. + */ +long long int strtoll_l(const char *nptr, char **endptr, int base, + locale_t locale) +{ + return strtoll(nptr, endptr, base); +} + +/** + * Convert string value to a long long integer. + * + * LSB 5.0: LSB-Core-generic/baselib-strtoq-3.html + */ +long long strtoq(const char *nptr, char **endptr, int base) +{ + return strtoll(nptr, endptr, base); +} + +/** + * Convert a string to an unsigned long long. + * + * Some day, when musl supports LC_NUMERIC, we can probably remove this. + */ +unsigned long long int strtoull_l(const char *nptr, char **endptr, int base, + locale_t locale) +{ + return strtoull(nptr, endptr, base); +} + +/** + * Convert a string to an unsigned long long. + * + * LSB 5.0: LSB-Core-generic/baselib-strtouq-3.html + */ +unsigned long long strtouq(const char *nptr, char **endptr, int base) { - assert(__group == 0); - return strtod(__nptr, __endptr); + return strtoull(nptr, endptr, base); } diff --git a/libgcompat/string.c b/libgcompat/string.c index 7ca0bf6..2c05caf 100644 --- a/libgcompat/string.c +++ b/libgcompat/string.c @@ -84,16 +84,3 @@ char *__strsep_g(char **stringp, const char *delim) { return strsep(stringp, delim); } - -/* Some day, when musl supports LC_NUMERIC, we can probably remove these */ -long long int strtoll_l(const char *nptr, char **endptr, int base, - locale_t locale) -{ - return strtoll(nptr, endptr, base); -} - -unsigned long long int strtoull_l(const char *nptr, char **endptr, int base, - locale_t locale) -{ - return strtoull(nptr, endptr, base); -} -- cgit v1.2.3-70-g09d2