tep_strerror.c (1396B)
1// SPDX-License-Identifier: LGPL-2.1 2#undef _GNU_SOURCE 3#include <string.h> 4#include <stdio.h> 5 6#include "event-parse.h" 7 8#undef _PE 9#define _PE(code, str) str 10static const char * const tep_error_str[] = { 11 TEP_ERRORS 12}; 13#undef _PE 14 15/* 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. 18 * 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. 24 * 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. 28 */ 29int tep_strerror(struct tep_handle *tep __maybe_unused, 30 enum tep_errno errnum, char *buf, size_t buflen) 31{ 32 const char *msg; 33 int idx; 34 35 if (!buflen) 36 return 0; 37 38 if (errnum >= 0) { 39 int err = strerror_r(errnum, buf, buflen); 40 buf[buflen - 1] = 0; 41 return err; 42 } 43 44 if (errnum <= __TEP_ERRNO__START || 45 errnum >= __TEP_ERRNO__END) 46 return -1; 47 48 idx = errnum - __TEP_ERRNO__START - 1; 49 msg = tep_error_str[idx]; 50 snprintf(buf, buflen, "%s", msg); 51 52 return 0; 53}