Back to index

glibc  2.9
_strerror.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,93,95,96,97,98,2000,2002,2006
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <libintl.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <sys/param.h>
00024 #include <stdio-common/_itoa.h>
00025 
00026 /* It is critical here that we always use the `dcgettext' function for
00027    the message translation.  Since <libintl.h> only defines the macro
00028    `dgettext' to use `dcgettext' for optimizing programs this is not
00029    always guaranteed.  */
00030 #ifndef dgettext
00031 # include <locale.h>        /* We need LC_MESSAGES.  */
00032 # define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
00033 #endif
00034 
00035 /* Return a string describing the errno code in ERRNUM.  */
00036 char *
00037 __strerror_r (int errnum, char *buf, size_t buflen)
00038 {
00039   if (__builtin_expect (errnum < 0 || errnum >= _sys_nerr_internal
00040                      || _sys_errlist_internal[errnum] == NULL, 0))
00041     {
00042       /* Buffer we use to print the number in.  For a maximum size for
00043         `int' of 8 bytes we never need more than 20 digits.  */
00044       char numbuf[21];
00045       const char *unk = _("Unknown error ");
00046       const size_t unklen = strlen (unk);
00047       char *p, *q;
00048 
00049       numbuf[20] = '\0';
00050       p = _itoa_word (errnum, &numbuf[20], 10, 0);
00051 
00052       /* Now construct the result while taking care for the destination
00053         buffer size.  */
00054       q = __mempcpy (buf, unk, MIN (unklen, buflen));
00055       if (unklen < buflen)
00056        memcpy (q, p, MIN ((size_t) (&numbuf[21] - p), buflen - unklen));
00057 
00058       /* Terminate the string in any case.  */
00059       if (buflen > 0)
00060        buf[buflen - 1] = '\0';
00061 
00062       return buf;
00063     }
00064 
00065   return (char *) _(_sys_errlist_internal[errnum]);
00066 }
00067 weak_alias (__strerror_r, strerror_r)
00068 libc_hidden_def (__strerror_r)