(root)/
glibc-2.38/
sysdeps/
mach/
xpg-strerror.c
       1  /* Copyright (C) 1993-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       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.
       8  
       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.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <errno.h>
      19  #include <libintl.h>
      20  #include <stdio.h>
      21  #include <string.h>
      22  #include <mach/error.h>
      23  #include <errorlib.h>
      24  #include <sys/param.h>
      25  #include <_itoa.h>
      26  
      27  /* It is critical here that we always use the `dcgettext' function for
      28     the message translation.  Since <libintl.h> only defines the macro
      29     `dgettext' to use `dcgettext' for optimizing programs this is not
      30     always guaranteed.  */
      31  #ifndef dgettext
      32  # include <locale.h>		/* We need LC_MESSAGES.  */
      33  # define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
      34  #endif
      35  
      36  /* Fill buf with a string describing the errno code in ERRNUM.  */
      37  int
      38  __xpg_strerror_r (int errnum, char *buf, size_t buflen)
      39  {
      40    int system;
      41    int sub;
      42    int code;
      43    const struct error_system *es;
      44    extern void __mach_error_map_compat (int *);
      45    const char *estr;
      46  
      47    __mach_error_map_compat (&errnum);
      48  
      49    system = err_get_system (errnum);
      50    sub = err_get_sub (errnum);
      51    code = err_get_code (errnum);
      52  
      53    if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
      54      {
      55        __snprintf (buf, buflen, "%s%X", _("Error in unknown error system: "),
      56  		  errnum);
      57        return EINVAL;
      58      }
      59  
      60    es = &__mach_error_systems[system];
      61  
      62    if (sub >= es->max_sub)
      63      estr = (const char *) es->bad_sub;
      64    else if (code >= es->subsystem[sub].max_code)
      65      return EINVAL;
      66    else
      67      estr = (const char *) _(es->subsystem[sub].codes[code]);
      68  
      69    size_t estrlen = strlen (estr);
      70  
      71    /* Terminate the string in any case.  */
      72    if (buflen > 0)
      73      *((char *) __mempcpy (buf, estr, MIN (buflen - 1, estrlen))) = '\0';
      74  
      75    return buflen <= estrlen ? ERANGE : 0;
      76  }