Adjusted.
[glibc.git] / sysdeps / mach / _strerror.c
blob0dcf264075900efea5250ab86e32e1e5f639246a
1 /* Copyright (C) 1993, 1995, 1996, 1997, 1998, 2000
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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <libintl.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <mach/error.h>
24 #include <errorlib.h>
25 #include <sys/param.h>
26 #include <stdio-common/_itoa.h>
28 /* It is critical here that we always use the `dcgettext' function for
29 the message translation. Since <libintl.h> only defines the macro
30 `dgettext' to use `dcgettext' for optimizing programs this is not
31 always guaranteed. */
32 #ifndef dgettext
33 # include <locale.h> /* We need LC_MESSAGES. */
34 # define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
35 #endif
37 /* Return a string describing the errno code in ERRNUM. */
38 char *
39 __strerror_r (int errnum, char *buf, size_t buflen)
41 int system;
42 int sub;
43 int code;
44 const struct error_system *es;
45 extern void __mach_error_map_compat (int *);
47 __mach_error_map_compat (&errnum);
49 system = err_get_system (errnum);
50 sub = err_get_sub (errnum);
51 code = err_get_code (errnum);
53 if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
55 /* Buffer we use to print the number in. For a maximum size for
56 `int' of 8 bytes we never need more than 20 digits. */
57 char numbuf[21];
58 const char *unk = _("Error in unknown error system: ");
59 const size_t unklen = strlen (unk);
60 char *p, *q;
62 numbuf[20] = '\0';
63 p = _itoa_word (errnum, &numbuf[20], 16, 1);
65 /* Now construct the result while taking care for the destination
66 buffer size. */
67 q = __mempcpy (buf, unk, MIN (unklen, buflen));
68 if (unklen < buflen)
69 memcpy (q, p, MIN (&numbuf[21] - p, buflen - unklen));
71 /* Terminate the string in any case. */
72 if (buflen > 0)
73 buf[buflen - 1] = '\0';
75 return buf;
78 es = &__mach_error_systems[system];
80 if (sub >= es->max_sub)
81 return (char *) es->bad_sub;
83 if (code >= es->subsystem[sub].max_code)
85 /* Buffer we use to print the number in. For a maximum size for
86 `int' of 8 bytes we never need more than 20 digits. */
87 char numbuf[21];
88 const char *unk = _("Unknown error ");
89 const size_t unklen = strlen (unk);
90 char *p, *q;
91 size_t len = strlen (es->subsystem[sub].subsys_name);
93 numbuf[20] = '\0';
94 p = _itoa_word (errnum, &numbuf[20], 10, 0);
96 /* Now construct the result while taking care for the destination
97 buffer size. */
98 q = __mempcpy (buf, unk, MIN (unklen, buflen));
99 if (unklen < buflen)
101 q = __mempcpy (q, es->subsystem[sub].subsys_name,
102 MIN (len, buflen - unklen));
103 if (unklen + len < buflen)
105 *q++ = ' ';
106 if (unklen + len + 1 < buflen)
107 memcpy (q, p,
108 MIN (&numbuf[21] - p, buflen - unklen - len - 1));
112 /* Terminate the string in any case. */
113 if (buflen > 0)
114 buf[buflen - 1] = '\0';
116 return buf;
119 return (char *) _(es->subsystem[sub].codes[code]);
121 weak_alias (__strerror_r, strerror_r)