1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <pulse/utf8.h>
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/thread.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/log.h>
36
37 #include "core-error.h"
38
39 PA_STATIC_TLS_DECLARE(cstrerror, pa_xfree);
40
pa_cstrerror(int errnum)41 const char* pa_cstrerror(int errnum) {
42 const char *original = NULL;
43 char *translated, *t;
44 char errbuf[128];
45
46 if (errnum < 0)
47 errnum = -errnum;
48
49 if ((t = PA_STATIC_TLS_GET(cstrerror)))
50 pa_xfree(t);
51
52 #if defined(HAVE_STRERROR_R) && defined(__GLIBC__)
53 original = strerror_r(errnum, errbuf, sizeof(errbuf));
54 #elif defined(HAVE_STRERROR_R)
55 if (strerror_r(errnum, errbuf, sizeof(errbuf)) == 0) {
56 errbuf[sizeof(errbuf) - 1] = 0;
57 original = errbuf;
58 }
59 #else
60 /* This might not be thread safe, but we hope for the best */
61 original = strerror(errnum);
62 #endif
63
64 /* The second condition is a Windows-ism */
65 if (!original || !strcasecmp(original, "Unknown error")) {
66 pa_snprintf(errbuf, sizeof(errbuf), "Unknown error %d", errnum);
67 original = errbuf;
68 }
69
70 if (!(translated = pa_locale_to_utf8(original))) {
71 pa_log_warn("Unable to convert error string to locale, filtering.");
72 translated = pa_utf8_filter(original);
73 }
74
75 PA_STATIC_TLS_SET(cstrerror, translated);
76
77 return translated;
78 }
79