* sysdeps/unix/sysv/linux/m68k/sysdep.h (INLINE_SYSCALL): Don't
[glibc.git] / sysdeps / generic / _strerror.c
blob766d88ecbed308ac5e58b12d6d1fa50d19ae3d40
1 /* Copyright (C) 1991, 93, 95, 96, 97, 98, 2000 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 #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 memcpy (q, p, MIN ((size_t) (&numbuf[21] - 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)