exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / xstrerror.c
blob39ec37c1ee0812aa602415ab57d36cc1b4a9b8ea
1 /* Return diagnostic string based on error code.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include "xstrerror.h"
22 #include <string.h>
24 #include "xvasprintf.h"
25 #include "xalloc.h"
26 #include "gettext.h"
28 #define _(msgid) gettext (msgid)
30 char *
31 xstrerror (const char *message, int errnum)
33 /* Get the internationalized description of errnum,
34 in a stack-allocated buffer. */
35 const char *errdesc;
36 char errbuf[1024];
37 #if !GNULIB_STRERROR_R_POSIX && STRERROR_R_CHAR_P
38 errdesc = strerror_r (errnum, errbuf, sizeof errbuf);
39 #else
40 if (strerror_r (errnum, errbuf, sizeof errbuf) == 0)
41 errdesc = errbuf;
42 else
43 errdesc = NULL;
44 #endif
45 if (errdesc == NULL)
46 errdesc = _("Unknown system error");
48 if (message != NULL)
50 /* Allow e.g. French translators to insert a space before the colon. */
51 char *result = xasprintf (_("%s: %s"), message, errdesc);
52 if (result == NULL)
53 /* Probably a buggy translation. Use a safe fallback. */
54 result = xasprintf ("%s%s%s", message, ": ", errdesc);
55 return result;
57 else
58 return xstrdup (errdesc);