blob: d2b2ca619e1fe489ec2b6f3e13c516c250006d8a [file] [log] [blame]
Brian Silverman01be0002014-05-10 15:44:38 -07001#include "aos/common/util/aos_strerror.h"
2
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
36char *aos_strerror(int error) {
37 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}