]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 1993-2019 Free Software Foundation, Inc. |
478b92f0 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
478b92f0 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 8 | |
478b92f0 UD |
9 | The GNU C Library 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 GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
28f540f4 | 17 | |
6818bcae | 18 | #include <libintl.h> |
28f540f4 RM |
19 | #include <stdio.h> |
20 | #include <string.h> | |
21 | #include <mach/error.h> | |
421f82e5 | 22 | #include <errorlib.h> |
af69217f | 23 | #include <sys/param.h> |
eb96ffb0 | 24 | #include <_itoa.h> |
28f540f4 | 25 | |
6ed0492f UD |
26 | /* It is critical here that we always use the `dcgettext' function for |
27 | the message translation. Since <libintl.h> only defines the macro | |
28 | `dgettext' to use `dcgettext' for optimizing programs this is not | |
29 | always guaranteed. */ | |
30 | #ifndef dgettext | |
31 | # include <locale.h> /* We need LC_MESSAGES. */ | |
32 | # define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES) | |
33 | #endif | |
34 | ||
28f540f4 RM |
35 | /* Return a string describing the errno code in ERRNUM. */ |
36 | char * | |
310b3460 | 37 | __strerror_r (int errnum, char *buf, size_t buflen) |
28f540f4 | 38 | { |
86d2c878 | 39 | int system; |
28f540f4 RM |
40 | int sub; |
41 | int code; | |
42 | const struct error_system *es; | |
43 | extern void __mach_error_map_compat (int *); | |
44 | ||
45 | __mach_error_map_compat (&errnum); | |
46 | ||
47 | system = err_get_system (errnum); | |
48 | sub = err_get_sub (errnum); | |
49 | code = err_get_code (errnum); | |
50 | ||
421f82e5 | 51 | if (system > err_max_system || ! __mach_error_systems[system].bad_sub) |
28f540f4 | 52 | { |
af69217f UD |
53 | /* Buffer we use to print the number in. For a maximum size for |
54 | `int' of 8 bytes we never need more than 20 digits. */ | |
55 | char numbuf[21]; | |
b3a59f40 RM |
56 | const char *unk = _("Error in unknown error system: "); |
57 | const size_t unklen = strlen (unk); | |
af69217f UD |
58 | char *p, *q; |
59 | ||
60 | numbuf[20] = '\0'; | |
61 | p = _itoa_word (errnum, &numbuf[20], 16, 1); | |
62 | ||
63 | /* Now construct the result while taking care for the destination | |
64 | buffer size. */ | |
65 | q = __mempcpy (buf, unk, MIN (unklen, buflen)); | |
66 | if (unklen < buflen) | |
04ea3b0f | 67 | memcpy (q, p, MIN (&numbuf[21] - p, buflen - unklen)); |
af69217f UD |
68 | |
69 | /* Terminate the string in any case. */ | |
70 | if (buflen > 0) | |
71 | buf[buflen - 1] = '\0'; | |
72 | ||
73 | return buf; | |
28f540f4 RM |
74 | } |
75 | ||
76 | es = &__mach_error_systems[system]; | |
77 | ||
78 | if (sub >= es->max_sub) | |
79 | return (char *) es->bad_sub; | |
80 | ||
81 | if (code >= es->subsystem[sub].max_code) | |
82 | { | |
af69217f UD |
83 | /* Buffer we use to print the number in. For a maximum size for |
84 | `int' of 8 bytes we never need more than 20 digits. */ | |
85 | char numbuf[21]; | |
b3a59f40 RM |
86 | const char *unk = _("Unknown error "); |
87 | const size_t unklen = strlen (unk); | |
af69217f | 88 | char *p, *q; |
273d56ce | 89 | size_t len = strlen (es->subsystem[sub].subsys_name); |
af69217f UD |
90 | |
91 | numbuf[20] = '\0'; | |
92 | p = _itoa_word (errnum, &numbuf[20], 10, 0); | |
93 | ||
94 | /* Now construct the result while taking care for the destination | |
95 | buffer size. */ | |
96 | q = __mempcpy (buf, unk, MIN (unklen, buflen)); | |
97 | if (unklen < buflen) | |
98 | { | |
99 | q = __mempcpy (q, es->subsystem[sub].subsys_name, | |
100 | MIN (len, buflen - unklen)); | |
101 | if (unklen + len < buflen) | |
102 | { | |
103 | *q++ = ' '; | |
104 | if (unklen + len + 1 < buflen) | |
04ea3b0f UD |
105 | memcpy (q, p, |
106 | MIN (&numbuf[21] - p, buflen - unklen - len - 1)); | |
af69217f UD |
107 | } |
108 | } | |
109 | ||
110 | /* Terminate the string in any case. */ | |
111 | if (buflen > 0) | |
112 | buf[buflen - 1] = '\0'; | |
113 | ||
114 | return buf; | |
28f540f4 RM |
115 | } |
116 | ||
b3a59f40 | 117 | return (char *) _(es->subsystem[sub].codes[code]); |
28f540f4 | 118 | } |
7a9ce79a | 119 | libc_hidden_def (__strerror_r) |
310b3460 | 120 | weak_alias (__strerror_r, strerror_r) |