18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci#undef _GNU_SOURCE 38c2ecf20Sopenharmony_ci#include <string.h> 48c2ecf20Sopenharmony_ci#include <stdio.h> 58c2ecf20Sopenharmony_ci#include <linux/string.h> 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci/* 88c2ecf20Sopenharmony_ci * The tools so far have been using the strerror_r() GNU variant, that returns 98c2ecf20Sopenharmony_ci * a string, be it the buffer passed or something else. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * But that, besides being tricky in cases where we expect that the function 128c2ecf20Sopenharmony_ci * using strerror_r() returns the error formatted in a provided buffer (we have 138c2ecf20Sopenharmony_ci * to check if it returned something else and copy that instead), breaks the 148c2ecf20Sopenharmony_ci * build on systems not using glibc, like Alpine Linux, where musl libc is 158c2ecf20Sopenharmony_ci * used. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * So, introduce yet another wrapper, str_error_r(), that has the GNU 188c2ecf20Sopenharmony_ci * interface, but uses the portable XSI variant of strerror_r(), so that users 198c2ecf20Sopenharmony_ci * rest asured that the provided buffer is used and it is what is returned. 208c2ecf20Sopenharmony_ci */ 218c2ecf20Sopenharmony_cichar *str_error_r(int errnum, char *buf, size_t buflen) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci int err = strerror_r(errnum, buf, buflen); 248c2ecf20Sopenharmony_ci if (err) 258c2ecf20Sopenharmony_ci snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, [buf], %zd)=%d", errnum, buflen, err); 268c2ecf20Sopenharmony_ci return buf; 278c2ecf20Sopenharmony_ci} 28