]> git.ipfire.org Git - thirdparty/glibc.git/blame - string/_strerror.c
2.5-18.1
[thirdparty/glibc.git] / string / _strerror.c
CommitLineData
0ecb606c
JJ
1/* Copyright (C) 1991,93,95,96,97,98,2000,2002,2006
2 Free Software Foundation, Inc.
478b92f0 3 This file is part of the GNU C Library.
28f540f4 4
478b92f0 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
28f540f4 9
478b92f0
UD
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
41bdb6e2 13 Lesser General Public License for more details.
28f540f4 14
41bdb6e2
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
28f540f4 19
46ec036d 20#include <libintl.h>
28f540f4
RM
21#include <stdio.h>
22#include <string.h>
74015205
UD
23#include <sys/param.h>
24#include <stdio-common/_itoa.h>
28f540f4 25
6ed0492f
UD
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
46ec036d 34
28f540f4
RM
35/* Return a string describing the errno code in ERRNUM. */
36char *
310b3460 37__strerror_r (int errnum, char *buf, size_t buflen)
28f540f4 38{
0ecb606c
JJ
39 if (__builtin_expect (errnum < 0 || errnum >= _sys_nerr_internal
40 || _sys_errlist_internal[errnum] == NULL, 0))
28f540f4 41 {
74015205
UD
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];
6ed0492f 45 const char *unk = _("Unknown error ");
86d2c878 46 const size_t unklen = strlen (unk);
74015205
UD
47 char *p, *q;
48
49 numbuf[20] = '\0';
50 p = _itoa_word (errnum, &numbuf[20], 10, 0);
51
52 /* Now construct the result while taking care for the destination
53 buffer size. */
54 q = __mempcpy (buf, unk, MIN (unklen, buflen));
55 if (unklen < buflen)
c3301189 56 memcpy (q, p, MIN ((size_t) (&numbuf[21] - p), buflen - unklen));
74015205
UD
57
58 /* Terminate the string in any case. */
59 if (buflen > 0)
60 buf[buflen - 1] = '\0';
61
62 return buf;
28f540f4
RM
63 }
64
43e3d6e3 65 return (char *) _(_sys_errlist_internal[errnum]);
28f540f4 66}
310b3460 67weak_alias (__strerror_r, strerror_r)
5b070c75 68libc_hidden_def (__strerror_r)