Back to index

glibc  2.9
_strerror.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993,95,96,97,98,2000,02 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <libintl.h>
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <mach/error.h>
00023 #include <errorlib.h>
00024 #include <sys/param.h>
00025 #include <stdio-common/_itoa.h>
00026 
00027 /* It is critical here that we always use the `dcgettext' function for
00028    the message translation.  Since <libintl.h> only defines the macro
00029    `dgettext' to use `dcgettext' for optimizing programs this is not
00030    always guaranteed.  */
00031 #ifndef dgettext
00032 # include <locale.h>        /* We need LC_MESSAGES.  */
00033 # define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
00034 #endif
00035 
00036 /* Return a string describing the errno code in ERRNUM.  */
00037 char *
00038 __strerror_r (int errnum, char *buf, size_t buflen)
00039 {
00040   int system;
00041   int sub;
00042   int code;
00043   const struct error_system *es;
00044   extern void __mach_error_map_compat (int *);
00045 
00046   __mach_error_map_compat (&errnum);
00047 
00048   system = err_get_system (errnum);
00049   sub = err_get_sub (errnum);
00050   code = err_get_code (errnum);
00051 
00052   if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
00053     {
00054       /* Buffer we use to print the number in.  For a maximum size for
00055         `int' of 8 bytes we never need more than 20 digits.  */
00056       char numbuf[21];
00057       const char *unk = _("Error in unknown error system: ");
00058       const size_t unklen = strlen (unk);
00059       char *p, *q;
00060 
00061       numbuf[20] = '\0';
00062       p = _itoa_word (errnum, &numbuf[20], 16, 1);
00063 
00064       /* Now construct the result while taking care for the destination
00065         buffer size.  */
00066       q = __mempcpy (buf, unk, MIN (unklen, buflen));
00067       if (unklen < buflen)
00068        memcpy (q, p, MIN (&numbuf[21] - p, buflen - unklen));
00069 
00070       /* Terminate the string in any case.  */
00071       if (buflen > 0)
00072        buf[buflen - 1] = '\0';
00073 
00074       return buf;
00075     }
00076 
00077   es = &__mach_error_systems[system];
00078 
00079   if (sub >= es->max_sub)
00080     return (char *) es->bad_sub;
00081 
00082   if (code >= es->subsystem[sub].max_code)
00083     {
00084       /* Buffer we use to print the number in.  For a maximum size for
00085         `int' of 8 bytes we never need more than 20 digits.  */
00086       char numbuf[21];
00087       const char *unk = _("Unknown error ");
00088       const size_t unklen = strlen (unk);
00089       char *p, *q;
00090       size_t len = strlen (es->subsystem[sub].subsys_name);
00091 
00092       numbuf[20] = '\0';
00093       p = _itoa_word (errnum, &numbuf[20], 10, 0);
00094 
00095       /* Now construct the result while taking care for the destination
00096         buffer size.  */
00097       q = __mempcpy (buf, unk, MIN (unklen, buflen));
00098       if (unklen < buflen)
00099        {
00100          q = __mempcpy (q, es->subsystem[sub].subsys_name,
00101                       MIN (len, buflen - unklen));
00102          if (unklen + len < buflen)
00103            {
00104              *q++ = ' ';
00105              if (unklen + len + 1 < buflen)
00106               memcpy (q, p,
00107                      MIN (&numbuf[21] - p, buflen - unklen - len - 1));
00108            }
00109        }
00110 
00111        /* Terminate the string in any case.  */
00112       if (buflen > 0)
00113        buf[buflen - 1] = '\0';
00114 
00115       return buf;
00116     }
00117 
00118   return (char *) _(es->subsystem[sub].codes[code]);
00119 }
00120 libc_hidden_def (__strerror_r)
00121 weak_alias (__strerror_r, strerror_r)