1 /* Copyright (C) 1991-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
23 #include <sys/param.h>
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
31 # include <locale.h> /* We need LC_MESSAGES. */
32 # define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
35 /* Return a string describing the errno code in ERRNUM. */
37 __strerror_r (int errnum
, char *buf
, size_t buflen
)
39 if (__builtin_expect (errnum
< 0 || errnum
>= _sys_nerr_internal
40 || _sys_errlist_internal
[errnum
] == NULL
, 0))
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. */
45 const char *unk
= _("Unknown error ");
46 size_t unklen
= strlen (unk
);
48 bool negative
= errnum
< 0;
51 p
= _itoa_word (abs (errnum
), &numbuf
[20], 10, 0);
53 /* Now construct the result while taking care for the destination
55 q
= __mempcpy (buf
, unk
, MIN (unklen
, buflen
));
56 if (negative
&& unklen
< buflen
)
62 memcpy (q
, p
, MIN ((size_t) (&numbuf
[21] - p
), buflen
- unklen
));
64 /* Terminate the string in any case. */
66 buf
[buflen
- 1] = '\0';
71 return (char *) _(_sys_errlist_internal
[errnum
]);
73 weak_alias (__strerror_r
, strerror_r
)
74 libc_hidden_def (__strerror_r
)