/src/gettext-0.26/gettext-tools/libgettextpo/xstrerror.c
Line | Count | Source |
1 | | /* Return diagnostic string based on error code. |
2 | | Copyright (C) 2023-2025 Free Software Foundation, Inc. |
3 | | |
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. |
8 | | |
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. |
13 | | |
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/>. */ |
16 | | |
17 | | #include <config.h> |
18 | | |
19 | | /* Specification. */ |
20 | | #include "xstrerror.h" |
21 | | |
22 | | #include <string.h> |
23 | | |
24 | | #include "xvasprintf.h" |
25 | | #include "xalloc.h" |
26 | | #include "gettext.h" |
27 | | |
28 | 0 | #define _(msgid) dgettext ("gnulib", msgid) |
29 | | |
30 | | char * |
31 | | xstrerror (const char *message, int errnum) |
32 | 0 | { |
33 | | /* Get the internationalized description of errnum, |
34 | | in a stack-allocated buffer. */ |
35 | 0 | const char *errdesc; |
36 | 0 | char errbuf[1024]; |
37 | | #if !GNULIB_STRERROR_R_POSIX && STRERROR_R_CHAR_P |
38 | | errdesc = strerror_r (errnum, errbuf, sizeof errbuf); |
39 | | #else |
40 | 0 | if (strerror_r (errnum, errbuf, sizeof errbuf) == 0) |
41 | 0 | errdesc = errbuf; |
42 | 0 | else |
43 | 0 | errdesc = NULL; |
44 | 0 | #endif |
45 | 0 | if (errdesc == NULL) |
46 | 0 | errdesc = _("Unknown system error"); |
47 | |
|
48 | 0 | if (message != NULL) |
49 | 0 | { |
50 | | /* Allow e.g. French translators to insert a space before the colon. */ |
51 | 0 | char *result = xasprintf (_("%s: %s"), message, errdesc); |
52 | 0 | if (result == NULL) |
53 | | /* Probably a buggy translation. Use a safe fallback. */ |
54 | 0 | result = xasprintf ("%s%s%s", message, ": ", errdesc); |
55 | 0 | return result; |
56 | 0 | } |
57 | 0 | else |
58 | 0 | return xstrdup (errdesc); |
59 | 0 | } |