diff options
author | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:00 -0500 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:32 -0500 |
commit | bd5969fc876a10b18613302db7087ef3c40f18e1 (patch) | |
tree | 7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/options/glibc/generic/string.cpp | |
parent | a95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff) |
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/options/glibc/generic/string.cpp')
-rw-r--r-- | lib/mlibc/options/glibc/generic/string.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/mlibc/options/glibc/generic/string.cpp b/lib/mlibc/options/glibc/generic/string.cpp new file mode 100644 index 0000000..19f77c7 --- /dev/null +++ b/lib/mlibc/options/glibc/generic/string.cpp @@ -0,0 +1,32 @@ +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif +#include <type_traits> +#include <string.h> + +/* This is a bit of a weird detail of the GNU implementation and C's lack of + * overloading and strictness: GNU takes const char * and returns a char * so + * that it autocasts to your desired constness, this function never actually + * modifies the string. + */ +char *__mlibc_gnu_basename_c(const char *path) { + char *basename_component = strrchr(path, '/'); + if (!basename_component) { + return const_cast<char *>(path); + } + return basename_component + 1; +} + + +/* GNU exposes these overloads, and as a result, we should probably have them + * checked, to make sure we actually match expectations. + */ +static_assert( + std::is_same_v<decltype(basename((const char *)nullptr)), const char*>, + "C++ overloads broken" +); + +static_assert( + std::is_same_v<decltype(basename((char *)nullptr)), char*>, + "C++ overloads broken" +); |