162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci#undef _GNU_SOURCE 362306a36Sopenharmony_ci#include <string.h> 462306a36Sopenharmony_ci#include <stdio.h> 562306a36Sopenharmony_ci#include <linux/string.h> 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci/* 862306a36Sopenharmony_ci * The tools so far have been using the strerror_r() GNU variant, that returns 962306a36Sopenharmony_ci * a string, be it the buffer passed or something else. 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * But that, besides being tricky in cases where we expect that the function 1262306a36Sopenharmony_ci * using strerror_r() returns the error formatted in a provided buffer (we have 1362306a36Sopenharmony_ci * to check if it returned something else and copy that instead), breaks the 1462306a36Sopenharmony_ci * build on systems not using glibc, like Alpine Linux, where musl libc is 1562306a36Sopenharmony_ci * used. 1662306a36Sopenharmony_ci * 1762306a36Sopenharmony_ci * So, introduce yet another wrapper, str_error_r(), that has the GNU 1862306a36Sopenharmony_ci * interface, but uses the portable XSI variant of strerror_r(), so that users 1962306a36Sopenharmony_ci * rest asured that the provided buffer is used and it is what is returned. 2062306a36Sopenharmony_ci */ 2162306a36Sopenharmony_cichar *str_error_r(int errnum, char *buf, size_t buflen) 2262306a36Sopenharmony_ci{ 2362306a36Sopenharmony_ci int err = strerror_r(errnum, buf, buflen); 2462306a36Sopenharmony_ci if (err) 2562306a36Sopenharmony_ci snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, [buf], %zd)=%d", errnum, buflen, err); 2662306a36Sopenharmony_ci return buf; 2762306a36Sopenharmony_ci} 28