Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #undef _GNU_SOURCE
0003 #include <string.h>
0004 #include <stdio.h>
0005 #include <linux/string.h>
0006 
0007 /*
0008  * The tools so far have been using the strerror_r() GNU variant, that returns
0009  * a string, be it the buffer passed or something else.
0010  *
0011  * But that, besides being tricky in cases where we expect that the function
0012  * using strerror_r() returns the error formatted in a provided buffer (we have
0013  * to check if it returned something else and copy that instead), breaks the
0014  * build on systems not using glibc, like Alpine Linux, where musl libc is
0015  * used.
0016  *
0017  * So, introduce yet another wrapper, str_error_r(), that has the GNU
0018  * interface, but uses the portable XSI variant of strerror_r(), so that users
0019  * rest asured that the provided buffer is used and it is what is returned.
0020  */
0021 char *str_error_r(int errnum, char *buf, size_t buflen)
0022 {
0023     int err = strerror_r(errnum, buf, buflen);
0024     if (err)
0025         snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, [buf], %zd)=%d", errnum, buflen, err);
0026     return buf;
0027 }