Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: LGPL-2.1
0002 #undef _GNU_SOURCE
0003 #include <string.h>
0004 #include <stdio.h>
0005 
0006 #include "event-parse.h"
0007 
0008 #undef _PE
0009 #define _PE(code, str) str
0010 static const char * const tep_error_str[] = {
0011     TEP_ERRORS
0012 };
0013 #undef _PE
0014 
0015 /*
0016  * The tools so far have been using the strerror_r() GNU variant, that returns
0017  * a string, be it the buffer passed or something else.
0018  *
0019  * But that, besides being tricky in cases where we expect that the function
0020  * using strerror_r() returns the error formatted in a provided buffer (we have
0021  * to check if it returned something else and copy that instead), breaks the
0022  * build on systems not using glibc, like Alpine Linux, where musl libc is
0023  * used.
0024  *
0025  * So, introduce yet another wrapper, str_error_r(), that has the GNU
0026  * interface, but uses the portable XSI variant of strerror_r(), so that users
0027  * rest asured that the provided buffer is used and it is what is returned.
0028  */
0029 int tep_strerror(struct tep_handle *tep __maybe_unused,
0030          enum tep_errno errnum, char *buf, size_t buflen)
0031 {
0032     const char *msg;
0033     int idx;
0034 
0035     if (!buflen)
0036         return 0;
0037 
0038     if (errnum >= 0) {
0039         int err = strerror_r(errnum, buf, buflen);
0040         buf[buflen - 1] = 0;
0041         return err;
0042     }
0043 
0044     if (errnum <= __TEP_ERRNO__START ||
0045         errnum >= __TEP_ERRNO__END)
0046         return -1;
0047 
0048     idx = errnum - __TEP_ERRNO__START - 1;
0049     msg = tep_error_str[idx];
0050     snprintf(buf, buflen, "%s", msg);
0051 
0052     return 0;
0053 }