WIP FPC-III support
[linux/fpc-iii.git] / tools / lib / traceevent / tep_strerror.c
blob4ac26445b2f665739df806eb367ef4db0da9d861
1 // SPDX-License-Identifier: LGPL-2.1
2 #undef _GNU_SOURCE
3 #include <string.h>
4 #include <stdio.h>
6 #include "event-parse.h"
8 #undef _PE
9 #define _PE(code, str) str
10 static const char * const tep_error_str[] = {
11 TEP_ERRORS
13 #undef _PE
16 * The tools so far have been using the strerror_r() GNU variant, that returns
17 * a string, be it the buffer passed or something else.
19 * But that, besides being tricky in cases where we expect that the function
20 * using strerror_r() returns the error formatted in a provided buffer (we have
21 * to check if it returned something else and copy that instead), breaks the
22 * build on systems not using glibc, like Alpine Linux, where musl libc is
23 * used.
25 * So, introduce yet another wrapper, str_error_r(), that has the GNU
26 * interface, but uses the portable XSI variant of strerror_r(), so that users
27 * rest asured that the provided buffer is used and it is what is returned.
29 int tep_strerror(struct tep_handle *tep __maybe_unused,
30 enum tep_errno errnum, char *buf, size_t buflen)
32 const char *msg;
33 int idx;
35 if (!buflen)
36 return 0;
38 if (errnum >= 0) {
39 int err = strerror_r(errnum, buf, buflen);
40 buf[buflen - 1] = 0;
41 return err;
44 if (errnum <= __TEP_ERRNO__START ||
45 errnum >= __TEP_ERRNO__END)
46 return -1;
48 idx = errnum - __TEP_ERRNO__START - 1;
49 msg = tep_error_str[idx];
50 snprintf(buf, buflen, "%s", msg);
52 return 0;