Update copyright notices with scripts/update-copyrights.
[jlayton/glibc.git] / sysdeps / mach / strerror_l.c
1 /* strerror_l - Get errno description string in given locale.  Mach version.
2    Copyright (C) 2007-2013 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <libintl.h>
20 #include <locale.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <mach/error.h>
25 #include <errorlib.h>
26 #include <sys/param.h>
27
28
29 static __thread char *last_value;
30
31
32 static const char *
33 translate (const char *str, locale_t loc)
34 {
35   locale_t oldloc = __uselocale (loc);
36   const char *res = _(str);
37   __uselocale (oldloc);
38   return res;
39 }
40
41
42 /* Return a string describing the errno code in ERRNUM.  */
43 char *
44 strerror_l (int errnum, locale_t loc)
45 {
46   int system;
47   int sub;
48   int code;
49   const struct error_system *es;
50   extern void __mach_error_map_compat (int *);
51
52   __mach_error_map_compat (&errnum);
53
54   system = err_get_system (errnum);
55   sub = err_get_sub (errnum);
56   code = err_get_code (errnum);
57
58   if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
59     {
60       free (last_value);
61       if (__asprintf (&last_value, "%s%X",
62                       translate ("Error in unknown error system: ", loc),
63                       errnum) == -1)
64         last_value = NULL;
65
66       return last_value;
67     }
68
69   es = &__mach_error_systems[system];
70
71   if (sub >= es->max_sub)
72     return (char *) translate (es->bad_sub, loc);
73
74   if (code >= es->subsystem[sub].max_code)
75     {
76       free (last_value);
77       if (__asprintf (&last_value, "%s%s %d",
78                       translate ("Unknown error ", loc),
79                       translate (es->subsystem[sub].subsys_name, loc),
80                       errnum) == -1)
81         last_value = NULL;
82
83       return last_value;
84     }
85
86   return (char *) translate (es->subsystem[sub].codes[code], loc);
87 }
88
89
90 #ifdef _LIBC
91 # ifdef _LIBC_REENTRANT
92 /* This is called when a thread is exiting to free the last_value string.  */
93 static void __attribute__ ((section ("__libc_thread_freeres_fn")))
94 strerror_thread_freeres (void)
95 {
96   free (last_value);
97 }
98 text_set_element (__libc_thread_subfreeres, strerror_thread_freeres);
99 text_set_element (__libc_subfreeres, strerror_thread_freeres);
100 # endif
101 #endif