]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 1991-2014 Free Software Foundation, Inc. |
b6ab06ce UD |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
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. | |
8 | ||
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 | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
b6ab06ce UD |
17 | |
18 | #include <libintl.h> | |
7e4afad5 | 19 | #include <stdbool.h> |
b6ab06ce | 20 | #include <stdio.h> |
7e4afad5 | 21 | #include <stdlib.h> |
b6ab06ce UD |
22 | #include <string.h> |
23 | #include <sys/param.h> | |
eb96ffb0 | 24 | #include <_itoa.h> |
b6ab06ce UD |
25 | |
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 | ||
35 | /* Return a string describing the errno code in ERRNUM. */ | |
36 | char * | |
37 | __strerror_r (int errnum, char *buf, size_t buflen) | |
38 | { | |
4259230f UD |
39 | if (__builtin_expect (errnum < 0 || errnum >= _sys_nerr_internal |
40 | || _sys_errlist_internal[errnum] == NULL, 0)) | |
b6ab06ce UD |
41 | { |
42 | /* Buffer we use to print the number in. For a maximum size for | |
43 | `int' of 8 bytes we never need more than 20 digits. */ | |
44 | char numbuf[21]; | |
45 | const char *unk = _("Unknown error "); | |
7e4afad5 | 46 | size_t unklen = strlen (unk); |
b6ab06ce | 47 | char *p, *q; |
7e4afad5 | 48 | bool negative = errnum < 0; |
b6ab06ce UD |
49 | |
50 | numbuf[20] = '\0'; | |
7e4afad5 | 51 | p = _itoa_word (abs (errnum), &numbuf[20], 10, 0); |
b6ab06ce UD |
52 | |
53 | /* Now construct the result while taking care for the destination | |
54 | buffer size. */ | |
55 | q = __mempcpy (buf, unk, MIN (unklen, buflen)); | |
7e4afad5 UD |
56 | if (negative && unklen < buflen) |
57 | { | |
58 | *q++ = '-'; | |
59 | ++unklen; | |
60 | } | |
b6ab06ce UD |
61 | if (unklen < buflen) |
62 | memcpy (q, p, MIN ((size_t) (&numbuf[21] - p), buflen - unklen)); | |
63 | ||
64 | /* Terminate the string in any case. */ | |
65 | if (buflen > 0) | |
66 | buf[buflen - 1] = '\0'; | |
67 | ||
68 | return buf; | |
69 | } | |
70 | ||
71 | return (char *) _(_sys_errlist_internal[errnum]); | |
72 | } | |
73 | weak_alias (__strerror_r, strerror_r) | |
74 | libc_hidden_def (__strerror_r) |