1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #undef _GNU_SOURCE
3*4882a593Smuzhiyun #include <string.h>
4*4882a593Smuzhiyun #include <stdio.h>
5*4882a593Smuzhiyun #include <linux/string.h>
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun /*
8*4882a593Smuzhiyun * The tools so far have been using the strerror_r() GNU variant, that returns
9*4882a593Smuzhiyun * a string, be it the buffer passed or something else.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * But that, besides being tricky in cases where we expect that the function
12*4882a593Smuzhiyun * using strerror_r() returns the error formatted in a provided buffer (we have
13*4882a593Smuzhiyun * to check if it returned something else and copy that instead), breaks the
14*4882a593Smuzhiyun * build on systems not using glibc, like Alpine Linux, where musl libc is
15*4882a593Smuzhiyun * used.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * So, introduce yet another wrapper, str_error_r(), that has the GNU
18*4882a593Smuzhiyun * interface, but uses the portable XSI variant of strerror_r(), so that users
19*4882a593Smuzhiyun * rest asured that the provided buffer is used and it is what is returned.
20*4882a593Smuzhiyun */
str_error_r(int errnum,char * buf,size_t buflen)21*4882a593Smuzhiyun char *str_error_r(int errnum, char *buf, size_t buflen)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun int err = strerror_r(errnum, buf, buflen);
24*4882a593Smuzhiyun if (err)
25*4882a593Smuzhiyun snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, [buf], %zd)=%d", errnum, buflen, err);
26*4882a593Smuzhiyun return buf;
27*4882a593Smuzhiyun }
28