1 /* Copyright (C) 1991,93,95,96,97,98,2000,2002,2006,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
24 #include <sys/param.h>
27 /* It is critical here that we always use the `dcgettext' function for
28 the message translation. Since <libintl.h> only defines the macro
29 `dgettext' to use `dcgettext' for optimizing programs this is not
32 # include <locale.h> /* We need LC_MESSAGES. */
33 # define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
36 /* Return a string describing the errno code in ERRNUM. */
38 __strerror_r (int errnum
, char *buf
, size_t buflen
)
40 if (__builtin_expect (errnum
< 0 || errnum
>= _sys_nerr_internal
41 || _sys_errlist_internal
[errnum
] == NULL
, 0))
43 /* Buffer we use to print the number in. For a maximum size for
44 `int' of 8 bytes we never need more than 20 digits. */
46 const char *unk
= _("Unknown error ");
47 size_t unklen
= strlen (unk
);
49 bool negative
= errnum
< 0;
52 p
= _itoa_word (abs (errnum
), &numbuf
[20], 10, 0);
54 /* Now construct the result while taking care for the destination
56 q
= __mempcpy (buf
, unk
, MIN (unklen
, buflen
));
57 if (negative
&& unklen
< buflen
)
63 memcpy (q
, p
, MIN ((size_t) (&numbuf
[21] - p
), buflen
- unklen
));
65 /* Terminate the string in any case. */
67 buf
[buflen
- 1] = '\0';
72 return (char *) _(_sys_errlist_internal
[errnum
]);
74 weak_alias (__strerror_r
, strerror_r
)
75 libc_hidden_def (__strerror_r
)