Update.
[glibc.git] / sysdeps / mach / _strerror.c
blob7b1599492a89c4c3280640c2078ae9d63e13a675
1 /* Copyright (C) 1993, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
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 <stdio-common/_itoa.h>
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
36 /* Return a string describing the errno code in ERRNUM. */
37 char *
38 __strerror_r (int errnum, char *buf, size_t buflen)
40 int system;
41 int sub;
42 int code;
43 const struct error_system *es;
44 extern void __mach_error_map_compat (int *);
46 __mach_error_map_compat (&errnum);
48 system = err_get_system (errnum);
49 sub = err_get_sub (errnum);
50 code = err_get_code (errnum);
52 if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
54 /* Buffer we use to print the number in. For a maximum size for
55 `int' of 8 bytes we never need more than 20 digits. */
56 char numbuf[21];
57 const char *unk = _("Error in unknown error system: ");
58 const size_t unklen = strlen (unk);
59 char *p, *q;
61 numbuf[20] = '\0';
62 p = _itoa_word (errnum, &numbuf[20], 16, 1);
64 /* Now construct the result while taking care for the destination
65 buffer size. */
66 q = __mempcpy (buf, unk, MIN (unklen, buflen));
67 if (unklen < buflen)
68 __stpncpy (q, p, buflen - unklen);
70 /* Terminate the string in any case. */
71 if (buflen > 0)
72 buf[buflen - 1] = '\0';
74 return buf;
77 es = &__mach_error_systems[system];
79 if (sub >= es->max_sub)
80 return (char *) es->bad_sub;
82 if (code >= es->subsystem[sub].max_code)
84 /* Buffer we use to print the number in. For a maximum size for
85 `int' of 8 bytes we never need more than 20 digits. */
86 char numbuf[21];
87 const char *unk = _("Unknown error ");
88 const size_t unklen = strlen (unk);
89 char *p, *q;
90 size_t len = strlen (es->subsystem[sub].subsys_name);
92 numbuf[20] = '\0';
93 p = _itoa_word (errnum, &numbuf[20], 10, 0);
95 /* Now construct the result while taking care for the destination
96 buffer size. */
97 q = __mempcpy (buf, unk, MIN (unklen, buflen));
98 if (unklen < buflen)
100 q = __mempcpy (q, es->subsystem[sub].subsys_name,
101 MIN (len, buflen - unklen));
102 if (unklen + len < buflen)
104 *q++ = ' ';
105 if (unklen + len + 1 < buflen)
106 __stpncpy (q, p, buflen - unklen - len - 1);
110 /* Terminate the string in any case. */
111 if (buflen > 0)
112 buf[buflen - 1] = '\0';
114 return buf;
117 return (char *) _(es->subsystem[sub].codes[code]);
119 weak_alias (__strerror_r, strerror_r)