Fix whitespace issue.
[glibc.git] / sysdeps / mach / strerror_l.c
blobccd3bad7edf98ccc90a776956e3634e7430f60f0
1 /* strerror_l - Get errno description string in given locale. Mach version.
2 Copyright (C) 2007, 2008 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 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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <libintl.h>
21 #include <locale.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <mach/error.h>
26 #include <errorlib.h>
27 #include <sys/param.h>
30 static __thread char *last_value;
33 static const char *
34 translate (const char *str, locale_t loc)
36 locale_t oldloc = __uselocale (loc);
37 const char *res = _(str);
38 __uselocale (oldloc);
39 return res;
43 /* Return a string describing the errno code in ERRNUM. */
44 char *
45 strerror_l (int errnum, locale_t loc)
47 int system;
48 int sub;
49 int code;
50 const struct error_system *es;
51 extern void __mach_error_map_compat (int *);
53 __mach_error_map_compat (&errnum);
55 system = err_get_system (errnum);
56 sub = err_get_sub (errnum);
57 code = err_get_code (errnum);
59 if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
61 free (last_value);
62 if (__asprintf (&last_value, "%s%X",
63 translate ("Error in unknown error system: ", loc),
64 errnum) == -1)
65 last_value = NULL;
67 return last_value;
70 es = &__mach_error_systems[system];
72 if (sub >= es->max_sub)
73 return (char *) translate (es->bad_sub, loc);
75 if (code >= es->subsystem[sub].max_code)
77 free (last_value);
78 if (__asprintf (&last_value, "%s%s %d",
79 translate ("Unknown error ", loc),
80 translate (es->subsystem[sub].subsys_name, loc),
81 errnum) == -1)
82 last_value = NULL;
84 return last_value;
87 return (char *) translate (es->subsystem[sub].codes[code], loc);
91 #ifdef _LIBC
92 # ifdef _LIBC_REENTRANT
93 /* This is called when a thread is exiting to free the last_value string. */
94 static void __attribute__ ((section ("__libc_thread_freeres_fn")))
95 strerror_thread_freeres (void)
97 free (last_value);
99 text_set_element (__libc_thread_subfreeres, strerror_thread_freeres);
100 text_set_element (__libc_subfreeres, strerror_thread_freeres);
101 # endif
102 #endif