demux: avi: PTSToByte remove useless casts and change type
[vlc.git] / src / posix / error.c
blobdb51004601906825dd15654838909a7cf45013b3
1 /*****************************************************************************
2 * error.c: POSIX error messages formatting
3 *****************************************************************************
4 * Copyright © 2013 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <string.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <assert.h>
30 #include <vlc_common.h>
32 static const char *vlc_strerror_l(int errnum, const char *lname)
34 int saved_errno = errno;
35 locale_t loc = newlocale(LC_MESSAGES_MASK, lname, (locale_t)0);
37 if (unlikely(loc == (locale_t)0))
39 if (errno == ENOENT) /* fallback to POSIX locale */
40 loc = newlocale(LC_MESSAGES_MASK, "C", (locale_t)0);
42 if (unlikely(loc == (locale_t)0))
44 assert(errno != EINVAL && errno != ENOENT);
45 errno = saved_errno;
46 return "Error message unavailable";
48 errno = saved_errno;
51 const char *buf = strerror_l(errnum, loc);
53 freelocale(loc);
54 return buf;
57 /**
58 * Formats an error message in the current locale.
59 * @param errnum error number (as in errno.h)
60 * @return A string pointer, valid until the next call to a function of the
61 * strerror() family in the same thread. This function cannot fail.
63 const char *vlc_strerror(int errnum)
65 /* We cannot simply use strerror() here, since it is not thread-safe. */
66 return vlc_strerror_l(errnum, "");
69 /**
70 * Formats an error message in the POSIX/C locale (i.e. American English).
71 * @param errnum error number (as in errno.h)
72 * @return A string pointer, valid until the next call to a function of the
73 * strerror() family in the same thread. This function cannot fail.
75 const char *vlc_strerror_c(int errnum)
77 return vlc_strerror_l(errnum, "C");