Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame^] | 1 | #include "aos/common/libc/aos_strerror.h" |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 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 | |
| 14 | namespace { |
| 15 | |
| 16 | const 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)) |
| 21 | char *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)) |
| 27 | char *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 Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame^] | 36 | const char *aos_strerror(int error) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 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 | } |