* stdio-common/vfscanf.c: Small cleanups throughout.
[glibc.git] / string / _strerror.c
blobcb5d9e3609a4bdc61fd11f5eade3ac377bcca01e
1 /* Copyright (C) 1991,93,95,96,97,98,2000,2002,2006
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <libintl.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/param.h>
24 #include <stdio-common/_itoa.h>
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
35 /* Return a string describing the errno code in ERRNUM. */
36 char *
37 __strerror_r (int errnum, char *buf, size_t buflen)
39 if (__builtin_expect (errnum < 0 || errnum >= _sys_nerr_internal
40 || _sys_errlist_internal[errnum] == NULL, 0))
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];
45 const char *unk = _("Unknown error ");
46 const size_t unklen = strlen (unk);
47 char *p, *q;
49 numbuf[20] = '\0';
50 p = _itoa_word (errnum, &numbuf[20], 10, 0);
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)
56 memcpy (q, p, MIN ((size_t) (&numbuf[21] - p), buflen - unklen));
58 /* Terminate the string in any case. */
59 if (buflen > 0)
60 buf[buflen - 1] = '\0';
62 return buf;
65 return (char *) _(_sys_errlist_internal[errnum]);
67 weak_alias (__strerror_r, strerror_r)
68 libc_hidden_def (__strerror_r)