From 8529e01d1f73bcf4628dc6023c2a503ede3ef6c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 5 Nov 2003 23:53:29 +0000 Subject: [PATCH] Implemented strerror_r() - the actual error description is now returned by a static function and used by both, strerror() and strerror_r(). git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5261 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/libroot/posix/string/strerror.c | 47 ++++++++++++++++------ 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/src/kernel/libroot/posix/string/strerror.c b/src/kernel/libroot/posix/string/strerror.c index 5273a1d2bf..ffcd0d6834 100644 --- a/src/kernel/libroot/posix/string/strerror.c +++ b/src/kernel/libroot/posix/string/strerror.c @@ -3,17 +3,17 @@ ** Distributed under the terms of the NewOS License. */ -#include + #include -#include #include +#include #include -char * -strerror(int errnum) +static char * +error_description(int error) { - switch (errnum) { + switch (error) { /* General Errors */ case B_NO_ERROR: return "No Error"; @@ -79,11 +79,9 @@ strerror(int errnum) case ESPIPE: return "Illegal seek"; // "Seek not allowed on file descriptor" -// case ERR_UNIMPLEMENTED: case ENOSYS: return "Unimplemented"; // "Function not implemented" -// case ERR_TOO_BIG: case EDOM: return "Numerical argument out of range"; // "Domain Error" @@ -579,11 +577,34 @@ strerror(int errnum) return "Data is not a message"; default: - { - static char unknown[28]; - - sprintf(unknown, "Unknown Error (%d)", errnum); - return unknown; - } + return NULL; } } + + +char * +strerror(int error) +{ + static char unknown[28]; + + char *description = error_description(error); + if (description != NULL) + return description; + + sprintf(unknown, "Unknown Error (%d)", error); + return unknown; +} + + +int +strerror_r(int error, char *buffer, size_t bufferSize) +{ + char *description = error_description(error); + if (description == NULL) + return EINVAL; + + strlcpy(buffer, description, bufferSize); + return 0; + // ToDo: could return ERANGE if buffer is too small +} +