Replace FSF snail mail address with URLs.
[glibc.git] / string / _strerror.c
blob87540797d92aa3094d1d74fc5ca581850c97e3bc
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/>. */
19 #include <libintl.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/param.h>
25 #include <stdio-common/_itoa.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
30 always guaranteed. */
31 #ifndef dgettext
32 # include <locale.h> /* We need LC_MESSAGES. */
33 # define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
34 #endif
36 /* Return a string describing the errno code in ERRNUM. */
37 char *
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. */
45 char numbuf[21];
46 const char *unk = _("Unknown error ");
47 size_t unklen = strlen (unk);
48 char *p, *q;
49 bool negative = errnum < 0;
51 numbuf[20] = '\0';
52 p = _itoa_word (abs (errnum), &numbuf[20], 10, 0);
54 /* Now construct the result while taking care for the destination
55 buffer size. */
56 q = __mempcpy (buf, unk, MIN (unklen, buflen));
57 if (negative && unklen < buflen)
59 *q++ = '-';
60 ++unklen;
62 if (unklen < buflen)
63 memcpy (q, p, MIN ((size_t) (&numbuf[21] - p), buflen - unklen));
65 /* Terminate the string in any case. */
66 if (buflen > 0)
67 buf[buflen - 1] = '\0';
69 return buf;
72 return (char *) _(_sys_errlist_internal[errnum]);
74 weak_alias (__strerror_r, strerror_r)
75 libc_hidden_def (__strerror_r)