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 vasprintf (char **strp, const char *fmt, va_list ap);
127 static inline int vlc_vsnprintf (char *str, size_t size, const char *format, va_list ap)
129 int must_free = vlc_fix_format_string (&format);
130 /* traditionally, MSVCRT has provided vsnprintf as _vsnprintf;
131 * to 'aid' portability/standards compliance, mingw provides a
132 * static version of vsnprintf that is buggy. Be sure to use
133 * MSVCRT version, at least it behaves as expected */
134 /* MSVCRT _vsnprintf does not:
135 * - null terminate string if insufficient storage
136 * - return the number of characters that would've been written
138 int ret = _vsnprintf (str, size-1, format, ap);
139 str[size-1] = 0; /* ensure the null gets written */
142 /* work out the number of chars that should've been written */
143 ret = vasprintf (&str, format, ap);
144 if (ret >= 0 && str) free (str);
146 if (must_free) free ((char *)format);
149 # define vsnprintf vlc_vsnprintf
151 static inline int vlc_printf (const char *format, ...)
155 va_start (ap, format);
156 ret = vlc_vprintf (format, ap);
160 # define printf(...) vlc_printf(__VA_ARGS__)
162 static inline int vlc_fprintf (FILE *stream, const char *format, ...)
166 va_start (ap, format);
167 ret = vlc_vfprintf (stream, format, ap);
171 # define fprintf vlc_fprintf
174 static inline int vlc_sprintf (char *str, const char *format, ...)
178 va_start (ap, format);
179 ret = vlc_vsprintf (str, format, ap);
183 # define sprintf vlc_sprintf
186 static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
190 va_start (ap, format);
191 ret = vlc_vsnprintf (str, size, format, ap);
195 /* win32: snprintf must always be vlc_snprintf or _snprintf,
196 * see comment in vlc_vsnprintf */
197 # define snprintf vlc_snprintf
199 /* Make sure we don't use flawed vasprintf or asprintf either */
200 # undef HAVE_VASPRINTF
201 # undef HAVE_ASPRINTF
204 #ifndef HAVE_VASPRINTF
208 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
211 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
212 char *res = (char *)malloc (len);
216 return vsnprintf (res, len, fmt, ap);
218 /* HACK: vsnprintf in the Win32 API behaves like
219 * the one in glibc 2.0 and doesn't return the number of characters
220 * it needed to copy the string.
221 * cf http://msdn.microsoft.com/en-us/library/1kt27hek.aspx
222 * and cf the man page of vsnprintf
224 int must_free = vlc_fix_format_string (&fmt);
225 int n, size = 2 * strlen (fmt);
228 if ((res = (char *) malloc (size)) == NULL)
230 if (must_free) free ((char *)fmt);
236 n = _vsnprintf (res, size, fmt, ap);
238 /* If that worked, return the string. */
239 if (n > -1 && n < size)
242 if (must_free) free ((char *)fmt);
246 /* Else try again with more space. */
247 size *= 2; /* twice the old size */
249 if ((np = (char *) realloc (res, size)) == NULL)
252 if (must_free) free ((char *)fmt);
265 #ifndef HAVE_ASPRINTF
268 static inline int asprintf (char **strp, const char *fmt, ...)
273 ret = vasprintf (strp, fmt, ap);
281 static inline size_t strnlen (const char *str, size_t max)
283 const char *end = (const char *) memchr (str, 0, max);
284 return end ? (size_t)(end - str) : max;
291 static inline char *strndup (const char *str, size_t max)
293 size_t len = strnlen (str, max);
294 char *res = (char *) malloc (len + 1);
297 memcpy (res, str, len);
305 # define strlcpy vlc_strlcpy
309 # define strtof( a, b ) ((float)strtod (a, b))
313 # define atof( str ) (strtod ((str), (char **)NULL, 10))
317 # define strtoll vlc_strtoll
321 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
323 char *psz_string = *ppsz_string;
327 char *p = strpbrk( psz_string, psz_delimiters );
341 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
346 long long quot; /* Quotient. */
347 long long rem; /* Remainder. */
350 static inline lldiv_t lldiv (long long numer, long long denom)
352 lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
358 # define scandir vlc_scandir
359 # define alphasort vlc_alphasort
363 static inline char *getenv (const char *name)
370 #ifndef HAVE_STRCASECMP
371 # ifndef HAVE_STRICMP
373 static inline int strcasecmp (const char *s1, const char *s2)
375 for (size_t i = 0;; i++)
377 int d = tolower (s1[i]) - tolower (s2[i]);
378 if (d || !s1[i]) return d;
383 # define strcasecmp stricmp
387 #ifndef HAVE_STRNCASECMP
388 # ifndef HAVE_STRNICMP
390 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
392 for (size_t i = 0; i < n; i++)
394 int d = tolower (s1[i]) - tolower (s2[i]);
395 if (d || !s1[i]) return d;
400 # define strncasecmp strnicmp
404 #ifndef HAVE_STRCASESTR
405 # ifndef HAVE_STRISTR
406 # define strcasestr vlc_strcasestr
408 # define strcasestr stristr
412 #ifndef HAVE_LOCALTIME_R
413 /* If localtime_r() is not provided, we assume localtime() uses
414 * thread-specific storage. */
416 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
418 struct tm *s = localtime (timep);
425 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
427 struct tm *s = gmtime (timep);
436 /* Alignment of critical static data structures */
437 #ifdef ATTRIBUTE_ALIGNED_MAX
438 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
440 # define ATTR_ALIGN(align)
443 #ifndef HAVE_USELOCALE
444 typedef void *locale_t;
445 # define newlocale( a, b, c ) ((locale_t)0)
446 # define uselocale( a ) ((locale_t)0)
447 # define freelocale( a ) (void)0
452 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
453 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
454 # define closedir vlc_wclosedir
457 /* libintl support */
458 #define _(str) vlc_gettext (str)
460 #if defined (ENABLE_NLS)
461 # include <libintl.h>
464 #define N_(str) gettext_noop (str)
465 #define gettext_noop(str) (str)
468 static inline void rewind ( FILE *stream )
470 fseek(stream, 0L, SEEK_SET);
475 #endif /* !LIBVLC_FIXUPS_H */