diff --git a/libc/Makefile b/libc/Makefile index ecbfe8a5..6cb9e3af 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -116,7 +116,9 @@ string/strcoll.o \ string/strcpy.o \ string/strcspn.o \ string/strdup.o \ +string/strerror_l.o \ string/strerror.o \ +string/strerror_r.o \ string/strlcpy.o \ string/strlen.o \ string/strncasecmp.o \ diff --git a/libc/include/string.h b/libc/include/string.h index fbad9f5b..4cb13225 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -50,6 +50,7 @@ int strcoll_l(const char*, const char*, locale_t); size_t strcspn(const char*, const char*); char* strcpy(char* __restrict, const char* __restrict); char* strdup(const char*); +int strerror_r(int, char*, size_t); size_t strlcpy(char* __restrict, const char* __restrict, size_t); size_t strlen(const char*); char* strncat(char* __restrict, const char* __restrict, size_t); @@ -67,25 +68,22 @@ int strverscmp(const char*, const char*); size_t strxfrm(char* __restrict, const char* __restrict, size_t); size_t strxfrm_l(char* __restrict, const char* __restrict, size_t, locale_t); -/* TODO: These are not implemented in sortix libc yet. */ -#if defined(__SORTIX_SHOW_UNIMPLEMENTED) -char* strerror_l(int, locale_t); -int strerror_r(int, char*, size_t); -#endif - #if defined(_SORTIX_SOURCE) || defined(_GNU_SOURCE) char* strchrnul(const char* str, int c); #endif #if defined(_SORTIX_SOURCE) const char* sortix_strerror(int errnum); +const char* sortix_strerror_l(int, locale_t); const char* sortix_strsignal(int signum); #endif #if defined(_SOURCE_SOURCE) && __SORTIX_STDLIB_REDIRECTS const char* strerror(int errnum) asm ("sortix_strerror"); +const char* strerror_l(int, locale_t) asm ("sortix_strerror_l"); const char* strsignal(int signum) asm ("sortix_strsignal"); #else char* strerror(int errnum); +char* strerror_l(int, locale_t); char* strsignal(int signum); #endif diff --git a/libc/string/strerror_l.cpp b/libc/string/strerror_l.cpp new file mode 100644 index 00000000..d91528da --- /dev/null +++ b/libc/string/strerror_l.cpp @@ -0,0 +1,36 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + string/strerror_l.cpp + Convert error code to a string. + +*******************************************************************************/ + +#define __SORTIX_STDLIB_REDIRECTS 0 +#include + +extern "C" const char* sortix_strerror_l(int errnum, locale_t /*locale*/) +{ + return sortix_strerror(errnum); +} + +extern "C" char* strerror_l(int errnum, locale_t locale) +{ + return (char*) sortix_strerror_l(errnum, locale); +} diff --git a/libc/string/strerror_r.cpp b/libc/string/strerror_r.cpp new file mode 100644 index 00000000..08b992ca --- /dev/null +++ b/libc/string/strerror_r.cpp @@ -0,0 +1,36 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + string/strerror_r.cpp + Convert error code to a string. + +*******************************************************************************/ + +#include +#include + +extern "C" int strerror_r(int errnum, char* dest, size_t dest_len) +{ + const char* msg = sortix_strerror(errnum); + if ( !msg ) + return -1; + if ( strlcpy(dest, msg, dest_len) != strlen(msg) ) + errno = ERANGE; + return 0; +}