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
32 static inline char *strdup (const char *str)
34 size_t len = strlen (str) + 1;
35 char *res = (char *)malloc (len);
36 if (res) memcpy (res, str, len);
45 * vlc_fix_format_string:
46 * @format: address of format string to fix (format string is not modified)
48 * Windows' printf doesn't support %z size modifiers.
49 * Fix a *printf format string to make it safe for mingw/MSVCRT run times:
50 * %z* (not supported in MSVCRT) -> either %I64* or %I32.
52 * Returns: 1 if *format must be free()d; 0 otherwise
54 static inline int vlc_fix_format_string (const char **format)
57 const char *tmp = *format;
58 while ((tmp = strstr (tmp, "%z")) != NULL)
66 char *dst = (char*)malloc (strlen (*format) + 2*n + 1);
69 *format = "vlc_fix_format_string: due to malloc failure, unable to fix unsafe string";
73 const char *src = *format;
75 while ((tmp = strstr (src, "%z")) != NULL)
77 /* NB, don't use %l*, as this is buggy in mingw*/
86 # else /* ie: WIN32 */
87 /* on win32, since the default size is 32bit, dont specify
88 * a modifer. (I32 isn't on wince, l doesn't work on mingw) */
99 static inline int vlc_vprintf (const char *format, va_list ap)
101 int must_free = vlc_fix_format_string (&format);
102 int ret = vprintf (format, ap);
103 if (must_free) free ((char *)format);
106 # define vprintf vlc_vprintf
108 static inline int vlc_vfprintf (FILE *stream, const char *format, va_list ap)
110 int must_free = vlc_fix_format_string (&format);
111 int ret = vfprintf (stream, format, ap);
112 if (must_free) free ((char *)format);
115 # define vfprintf vlc_vfprintf
117 static inline int vlc_vsprintf (char *str, const char *format, va_list ap)
119 int must_free = vlc_fix_format_string (&format);
120 int ret = vsprintf (str, format, ap);
121 if (must_free) free ((char *)format);
124 # define vsprintf vlc_vsprintf
126 static inline int vlc_vsnprintf (char *str, size_t size, const char *format, va_list ap)
128 int must_free = vlc_fix_format_string (&format);
129 /* traditionally, MSVCRT has provided vsnprintf as _vsnprintf;
130 * to 'aid' portability/standards compliance, mingw provides a
131 * static version of vsnprintf that is buggy. Be sure to use
132 * MSVCRT version, at least it behaves as expected */
133 int ret = _vsnprintf (str, size, format, ap);
134 if (must_free) free ((char *)format);
137 # define vsnprintf vlc_vsnprintf
139 static inline int vlc_printf (const char *format, ...)
143 va_start (ap, format);
144 ret = vlc_vprintf (format, ap);
148 # define printf(...) vlc_printf(__VA_ARGS__)
150 static inline int vlc_fprintf (FILE *stream, const char *format, ...)
154 va_start (ap, format);
155 ret = vlc_vfprintf (stream, format, ap);
159 # define fprintf vlc_fprintf
162 static inline int vlc_sprintf (char *str, const char *format, ...)
166 va_start (ap, format);
167 ret = vlc_vsprintf (str, format, ap);
171 # define sprintf vlc_sprintf
174 static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
178 va_start (ap, format);
179 ret = vlc_vsnprintf (str, size, format, ap);
183 /* win32: snprintf must always be vlc_snprintf or _snprintf,
184 * see comment in vlc_vsnprintf */
185 # define snprintf vlc_snprintf
187 /* Make sure we don't use flawed vasprintf or asprintf either */
188 # undef HAVE_VASPRINTF
189 # undef HAVE_ASPRINTF
192 #ifndef HAVE_VASPRINTF
196 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
199 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
200 char *res = (char *)malloc (len);
204 return vsnprintf (res, len, fmt, ap);
206 /* HACK: vsnprintf in the WinCE API behaves like
207 * the one in glibc 2.0 and doesn't return the number of characters
208 * it needed to copy the string.
209 * cf http://msdn.microsoft.com/en-us/library/1kt27hek.aspx
210 * and cf the man page of vsnprintf
212 Guess we need no more than 50 bytes. */
216 if ((res = (char *) malloc (size)) == NULL)
221 n = vsnprintf (res, size, fmt, ap);
223 /* If that worked, return the string. */
224 if (n > -1 && n < size)
230 /* Else try again with more space. */
231 size *= 2; /* twice the old size */
233 if ((np = (char *) realloc (res, size)) == NULL)
244 #endif /* UNDER_CE */
248 #ifndef HAVE_ASPRINTF
251 static inline int asprintf (char **strp, const char *fmt, ...)
256 ret = vasprintf (strp, fmt, ap);
264 static inline size_t strnlen (const char *str, size_t max)
266 const char *end = (const char *) memchr (str, 0, max);
267 return end ? (size_t)(end - str) : max;
274 static inline char *strndup (const char *str, size_t max)
276 size_t len = strnlen (str, max);
277 char *res = (char *) malloc (len + 1);
280 memcpy (res, str, len);
288 # define strlcpy vlc_strlcpy
292 # define strtof( a, b ) ((float)strtod (a, b))
296 # define atof( str ) (strtod ((str), (char **)NULL, 10))
300 # define strtoll vlc_strtoll
304 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
306 char *psz_string = *ppsz_string;
310 char *p = strpbrk( psz_string, psz_delimiters );
324 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
329 long long quot; /* Quotient. */
330 long long rem; /* Remainder. */
333 static inline lldiv_t lldiv (long long numer, long long denom)
335 lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
341 # define scandir vlc_scandir
342 # define alphasort vlc_alphasort
346 static inline char *getenv (const char *name)
353 #ifndef HAVE_STRCASECMP
354 # ifndef HAVE_STRICMP
356 static inline int strcasecmp (const char *s1, const char *s2)
358 for (size_t i = 0;; i++)
360 int d = tolower (s1[i]) - tolower (s2[i]);
361 if (d || !s1[i]) return d;
366 # define strcasecmp stricmp
370 #ifndef HAVE_STRNCASECMP
371 # ifndef HAVE_STRNICMP
373 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
375 for (size_t i = 0; i < n; i++)
377 int d = tolower (s1[i]) - tolower (s2[i]);
378 if (d || !s1[i]) return d;
383 # define strncasecmp strnicmp
387 #ifndef HAVE_STRCASESTR
388 # ifndef HAVE_STRISTR
389 # define strcasestr vlc_strcasestr
391 # define strcasestr stristr
395 #ifndef HAVE_LOCALTIME_R
396 /* If localtime_r() is not provided, we assume localtime() uses
397 * thread-specific storage. */
399 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
401 struct tm *s = localtime (timep);
408 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
410 struct tm *s = gmtime (timep);
419 /* Alignment of critical static data structures */
420 #ifdef ATTRIBUTE_ALIGNED_MAX
421 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
423 # define ATTR_ALIGN(align)
426 #ifndef HAVE_USELOCALE
427 typedef void *locale_t;
428 # define newlocale( a, b, c ) ((locale_t)0)
429 # define uselocale( a ) ((locale_t)0)
430 # define freelocale( a ) (void)0
435 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
436 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
437 # define closedir vlc_wclosedir
440 /* libintl support */
441 #define _(str) vlc_gettext (str)
443 #if defined (ENABLE_NLS)
444 # include <libintl.h>
447 #define N_(str) gettext_noop (str)
448 #define gettext_noop(str) (str)
451 static inline void rewind ( FILE *stream )
453 fseek(stream, 0L, SEEK_SET);
458 #endif /* !LIBVLC_FIXUPS_H */