blob: 4bca5001768b283091ce3911b43f25199c33090d [file] [log] [blame]
Brian Silvermanaf784862014-05-13 08:14:55 -07001#include "aos/common/libc/aos_strerror.h"
Brian Silverman01be0002014-05-10 15:44:38 -07002
3#include <assert.h>
4#include <sys/types.h>
5#include <string.h>
6#include <stdio.h>
7
8#include "aos/linux_code/thread_local.h"
9
10// This code uses an overloaded function to handle the result from either
11// version of strerror_r correctly without needing a way to get the choice out
12// of the compiler/glibc/whatever explicitly.
13
14namespace {
15
16const size_t kBufferSize = 128;
17
18// Handle the result from the GNU version of strerror_r. It never fails, so
19// that's pretty easy...
20__attribute__((unused))
21char *aos_strerror_handle_result(int /*error*/, char *ret, char * /*buffer*/) {
22 return ret;
23}
24
25// Handle the result from the POSIX version of strerror_r.
26__attribute__((unused))
27char *aos_strerror_handle_result(int error, int ret, char *buffer) {
28 if (ret != 0) {
Brian Silvermanfe457de2014-05-26 22:04:08 -070029 const int r = snprintf(buffer, kBufferSize, "Unknown error %d", error);
30 assert(r > 0);
Brian Silverman01be0002014-05-10 15:44:38 -070031 }
32 return buffer;
33}
34
35} // namespace
36
Brian Silvermanaf784862014-05-13 08:14:55 -070037const char *aos_strerror(int error) {
Brian Silverman01be0002014-05-10 15:44:38 -070038 static AOS_THREAD_LOCAL char buffer[kBufferSize];
39
40 // Call the overload for whichever version we're using.
41 return aos_strerror_handle_result(
42 error, strerror_r(error, buffer, sizeof(buffer)), buffer);
43}