Update.
[glibc.git] / sysdeps / generic / _strerror.c
blob4a9b032c868fbe7f60592d977079451733c24d3d
1 /* Copyright (C) 1991, 93, 95, 96, 97, 98 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 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 #ifndef HAVE_GNU_LD
26 # define _sys_errlist sys_errlist
27 # define _sys_nerr sys_nerr
28 #endif
30 /* It is critical here that we always use the `dcgettext' function for
31 the message translation. Since <libintl.h> only defines the macro
32 `dgettext' to use `dcgettext' for optimizing programs this is not
33 always guaranteed. */
34 #ifndef dgettext
35 # include <locale.h> /* We need LC_MESSAGES. */
36 # define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
37 #endif
39 /* Return a string describing the errno code in ERRNUM. */
40 char *
41 __strerror_r (int errnum, char *buf, size_t buflen)
43 if (errnum < 0 || errnum >= _sys_nerr || _sys_errlist[errnum] == NULL)
45 /* Buffer we use to print the number in. For a maximum size for
46 `int' of 8 bytes we never need more than 20 digits. */
47 char numbuf[21];
48 const char *unk = _("Unknown error ");
49 const size_t unklen = strlen (unk);
50 char *p, *q;
52 numbuf[20] = '\0';
53 p = _itoa_word (errnum, &numbuf[20], 10, 0);
55 /* Now construct the result while taking care for the destination
56 buffer size. */
57 q = __mempcpy (buf, unk, MIN (unklen, buflen));
58 if (unklen < buflen)
59 __stpncpy (q, p, buflen - unklen);
61 /* Terminate the string in any case. */
62 if (buflen > 0)
63 buf[buflen - 1] = '\0';
65 return buf;
68 return (char *) _(_sys_errlist[errnum]);
70 weak_alias (__strerror_r, strerror_r)