s_sinf.c: Replace floor with simple casts
[glibc.git] / string / _strerror.c
blob4d3a52d9067cedb49a71f3879b394aa2d158695e
1 /* Copyright (C) 1991-2017 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/>. */
18 #include <libintl.h>
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/param.h>
24 #include <_itoa.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
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
35 /* Return a string describing the errno code in ERRNUM. */
36 char *
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. */
44 char numbuf[21];
45 const char *unk = _("Unknown error ");
46 size_t unklen = strlen (unk);
47 char *p, *q;
48 bool negative = errnum < 0;
50 numbuf[20] = '\0';
51 p = _itoa_word (abs (errnum), &numbuf[20], 10, 0);
53 /* Now construct the result while taking care for the destination
54 buffer size. */
55 q = __mempcpy (buf, unk, MIN (unklen, buflen));
56 if (negative && unklen < buflen)
58 *q++ = '-';
59 ++unklen;
61 if (unklen < buflen)
62 memcpy (q, p, MIN ((size_t) (&numbuf[21] - p), buflen - unklen));
64 /* Terminate the string in any case. */
65 if (buflen > 0)
66 buf[buflen - 1] = '\0';
68 return buf;
71 return (char *) _(_sys_errlist_internal[errnum]);
73 weak_alias (__strerror_r, strerror_r)
74 libc_hidden_def (__strerror_r)