blob: d5fc2b43ad1cbc978873791d121202556e775998 [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) {
29 assert(snprintf(buffer, kBufferSize, "Unknown error %d", error) > 0);
30 }
31 return buffer;
32}
33
34} // namespace
35
Brian Silvermanaf784862014-05-13 08:14:55 -070036const char *aos_strerror(int error) {
Brian Silverman01be0002014-05-10 15:44:38 -070037 static AOS_THREAD_LOCAL char buffer[kBufferSize];
38
39 // Call the overload for whichever version we're using.
40 return aos_strerror_handle_result(
41 error, strerror_r(error, buffer, sizeof(buffer)), buffer);
42}