1 /*****************************************************************************
2 * fixups.h: portability fixups included from config.h
3 *****************************************************************************
4 * Copyright © 1998-2008 the VideoLAN project
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 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 General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
23 * This file is a collection of portability fixes
26 #ifndef LIBVLC_FIXUPS_H
27 # define LIBVLC_FIXUPS_H 1
29 #ifdef __MINGW32_VERSION
30 # if __MINGW32_MAJOR_VERSION == 3 && __MINGW32_MINOR_VERSION < 14
31 # error This mingw-runtime is too old, it has a broken vsnprintf
33 /* mingw-runtime provides the whole printf family in a c99 compliant way. */
34 /* the way to enable this is to define __USE_MINGW_ANSI_STDIO, or something
35 * such as _ISOC99_SOURCE; the former is done by configure.ac */
36 /* This isn't done here, since some modules don't include config.h and
37 * therefore this as the first include file */
38 #elif defined UNDER_CE
39 # error Window CE support for *printf needs fixing.
45 static inline char *strdup (const char *str)
47 size_t len = strlen (str) + 1;
48 char *res = (char *)malloc (len);
49 if (res) memcpy (res, str, len);
55 /* Windows' printf doesn't support %z modifiers, thus we need to rewrite
56 * the format string in a wrapper. */
59 static inline char *vlc_fix_format_string (const char *format)
63 const char *src = format, *tmp;
66 while ((tmp = strstr (src, "%z")) != NULL)
74 fmt = (char*)malloc (strlen (format) + n + 1);
80 while ((tmp = strstr (src, "%z")) != NULL)
85 memcpy (dst, "%ll", 3);
92 if (strstr (format, "%z") == NULL)
95 fmt = strdup (format);
99 while ((f = strstr (fmt, "%z")) != NULL)
110 static inline int vlc_vprintf (const char *format, va_list ap)
112 char *fmt = vlc_fix_format_string (format);
113 int ret = vprintf (fmt ? fmt : format, ap);
117 # define vprintf vlc_vprintf
119 static inline int vlc_vfprintf (FILE *stream, const char *format, va_list ap)
121 char *fmt = vlc_fix_format_string (format);
122 int ret = vfprintf (stream, fmt ? fmt : format, ap);
126 # define vfprintf vlc_vfprintf
128 static inline int vlc_vsprintf (char *str, const char *format, va_list ap)
130 char *fmt = vlc_fix_format_string (format);
131 int ret = vsprintf (str, fmt ? fmt : format, ap);
135 # define vsprintf vlc_vsprintf
137 static inline int vlc_vsnprintf (char *str, size_t size, const char *format, va_list ap)
139 char *fmt = vlc_fix_format_string (format);
140 int ret = vsnprintf (str, size, fmt ? fmt : format, ap);
144 # define vsnprintf vlc_vsnprintf
146 static inline int vlc_printf (const char *format, ...)
150 va_start (ap, format);
151 ret = vprintf (format, ap);
155 # define printf(...) vlc_printf(__VA_ARGS__)
157 static inline int vlc_fprintf (FILE *stream, const char *format, ...)
161 va_start (ap, format);
162 ret = vfprintf (stream, format, ap);
166 # define fprintf vlc_fprintf
169 static inline int vlc_sprintf (char *str, const char *format, ...)
173 va_start (ap, format);
174 ret = vsprintf (str, format, ap);
178 # define sprintf vlc_sprintf
181 static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
185 va_start (ap, format);
186 ret = vsnprintf (str, size, format, ap);
190 # define snprintf vlc_snprintf
192 /* Make sure we don't use flawed vasprintf or asprintf either */
193 # undef HAVE_VASPRINTF
194 # undef HAVE_ASPRINTF
197 #ifndef HAVE_VASPRINTF
201 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
204 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
205 char *res = (char *)malloc (len);
209 return vsnprintf (res, len, fmt, ap);
211 /* HACK: vsnprintf in the WinCE API behaves like
212 * the one in glibc 2.0 and doesn't return the number of characters
213 * it needed to copy the string.
214 * cf http://msdn.microsoft.com/en-us/library/1kt27hek.aspx
215 * and cf the man page of vsnprintf
217 Guess we need no more than 50 bytes. */
221 if ((res = (char *) malloc (size)) == NULL)
226 n = vsnprintf (res, size, fmt, ap);
228 /* If that worked, return the string. */
229 if (n > -1 && n < size)
235 /* Else try again with more space. */
236 size *= 2; /* twice the old size */
238 if ((np = (char *) realloc (res, size)) == NULL)
249 #endif /* UNDER_CE */
253 #ifndef HAVE_ASPRINTF
256 static inline int asprintf (char **strp, const char *fmt, ...)
261 ret = vasprintf (strp, fmt, ap);
269 static inline size_t strnlen (const char *str, size_t max)
271 const char *end = (const char *) memchr (str, 0, max);
272 return end ? (size_t)(end - str) : max;
279 static inline char *strndup (const char *str, size_t max)
281 size_t len = strnlen (str, max);
282 char *res = (char *) malloc (len + 1);
285 memcpy (res, str, len);
293 # define strlcpy vlc_strlcpy
297 # define strtof( a, b ) ((float)strtod (a, b))
301 # define atof( str ) (strtod ((str), (char **)NULL, 10))
305 # define strtoll vlc_strtoll
309 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
311 char *psz_string = *ppsz_string;
315 char *p = strpbrk( psz_string, psz_delimiters );
329 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
334 long long quot; /* Quotient. */
335 long long rem; /* Remainder. */
338 static inline lldiv_t lldiv (long long numer, long long denom)
340 lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
346 # define scandir vlc_scandir
347 # define alphasort vlc_alphasort
351 static inline char *getenv (const char *name)
358 #ifndef HAVE_STRCASECMP
359 # ifndef HAVE_STRICMP
361 static inline int strcasecmp (const char *s1, const char *s2)
363 for (size_t i = 0;; i++)
365 int d = tolower (s1[i]) - tolower (s2[i]);
366 if (d || !s1[i]) return d;
371 # define strcasecmp stricmp
375 #ifndef HAVE_STRNCASECMP
376 # ifndef HAVE_STRNICMP
378 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
380 for (size_t i = 0; i < n; i++)
382 int d = tolower (s1[i]) - tolower (s2[i]);
383 if (d || !s1[i]) return d;
388 # define strncasecmp strnicmp
392 #ifndef HAVE_STRCASESTR
393 # ifndef HAVE_STRISTR
394 # define strcasestr vlc_strcasestr
396 # define strcasestr stristr
400 #ifndef HAVE_LOCALTIME_R
401 /* If localtime_r() is not provided, we assume localtime() uses
402 * thread-specific storage. */
404 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
406 struct tm *s = localtime (timep);
413 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
415 struct tm *s = gmtime (timep);
424 /* Alignment of critical static data structures */
425 #ifdef ATTRIBUTE_ALIGNED_MAX
426 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
428 # define ATTR_ALIGN(align)
431 #ifndef HAVE_USELOCALE
432 typedef void *locale_t;
433 # define newlocale( a, b, c ) ((locale_t)0)
434 # define uselocale( a ) ((locale_t)0)
435 # define freelocale( a ) (void)0
440 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
441 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
442 # define closedir vlc_wclosedir
445 /* libintl support */
446 #define _(str) vlc_gettext (str)
448 #if defined (ENABLE_NLS)
449 # include <libintl.h>
452 #define N_(str) gettext_noop (str)
453 #define gettext_noop(str) (str)
456 static inline void rewind ( FILE *stream )
458 fseek(stream, 0L, SEEK_SET);
463 #endif /* !LIBVLC_FIXUPS_H */