2006-03-16 Roland McGrath <roland@redhat.com>
[glibc/history.git] / string / _strerror.c
blobf6f16ff2af12b48d3b02eafdfcc9eee0517567be
1 /* Copyright (C) 1991,93,95,96,97,98,2000,2002 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <libintl.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/param.h>
23 #include <stdio-common/_itoa.h>
25 /* It is critical here that we always use the `dcgettext' function for
26 the message translation. Since <libintl.h> only defines the macro
27 `dgettext' to use `dcgettext' for optimizing programs this is not
28 always guaranteed. */
29 #ifndef dgettext
30 # include <locale.h> /* We need LC_MESSAGES. */
31 # define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
32 #endif
34 /* Return a string describing the errno code in ERRNUM. */
35 char *
36 __strerror_r (int errnum, char *buf, size_t buflen)
38 if (errnum < 0 || errnum >= _sys_nerr_internal
39 || _sys_errlist_internal[errnum] == NULL)
41 /* Buffer we use to print the number in. For a maximum size for
42 `int' of 8 bytes we never need more than 20 digits. */
43 char numbuf[21];
44 const char *unk = _("Unknown error ");
45 const size_t unklen = strlen (unk);
46 char *p, *q;
48 numbuf[20] = '\0';
49 p = _itoa_word (errnum, &numbuf[20], 10, 0);
51 /* Now construct the result while taking care for the destination
52 buffer size. */
53 q = __mempcpy (buf, unk, MIN (unklen, buflen));
54 if (unklen < buflen)
55 memcpy (q, p, MIN ((size_t) (&numbuf[21] - p), buflen - unklen));
57 /* Terminate the string in any case. */
58 if (buflen > 0)
59 buf[buflen - 1] = '\0';
61 return buf;
64 return (char *) _(_sys_errlist_internal[errnum]);
66 weak_alias (__strerror_r, strerror_r)
67 libc_hidden_def (__strerror_r)