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);
42 /* Windows' printf doesn't support %z modifiers, thus we need to rewrite
43 * the format string in a wrapper. */
45 static inline char *vlc_fix_format_string (const char *format)
48 if (strstr (format, "%z") == NULL)
51 fmt = strdup (format);
55 while ((f = strstr (fmt, "%z")) != NULL)
66 static inline int vlc_vprintf (const char *format, va_list ap)
68 char *fmt = vlc_fix_format_string (format);
69 int ret = vprintf (fmt ? fmt : format, ap);
73 # define vprintf vlc_vprintf
75 static inline int vlc_vfprintf (FILE *stream, const char *format, va_list ap)
77 char *fmt = vlc_fix_format_string (format);
78 int ret = vfprintf (stream, fmt ? fmt : format, ap);
82 # define vfprintf vlc_vfprintf
84 static inline int vlc_vsprintf (char *str, const char *format, va_list ap)
86 char *fmt = vlc_fix_format_string (format);
87 int ret = vsprintf (str, fmt ? fmt : format, ap);
91 # define vsprintf vlc_vsprintf
93 static inline int vlc_vsnprintf (char *str, size_t size, const char *format, va_list ap)
95 char *fmt = vlc_fix_format_string (format);
96 int ret = vsnprintf (str, size, fmt ? fmt : format, ap);
100 # define vsnprintf vlc_vsnprintf
102 static inline int vlc_printf (const char *format, ...)
106 va_start (ap, format);
107 ret = vprintf (format, ap);
111 # define printf(...) vlc_printf(__VA_ARGS__)
113 static inline int vlc_fprintf (FILE *stream, const char *format, ...)
117 va_start (ap, format);
118 ret = vfprintf (stream, format, ap);
122 # define fprintf vlc_fprintf
124 static inline int vlc_sprintf (char *str, const char *format, ...)
128 va_start (ap, format);
129 ret = vsprintf (str, format, ap);
133 # define sprintf vlc_sprintf
135 static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
139 va_start (ap, format);
140 ret = vsnprintf (str, size, format, ap);
144 # define snprintf vlc_snprintf
146 /* Make sure we don't use flawed vasprintf or asprintf either */
147 # undef HAVE_VASPRINTF
148 # undef HAVE_ASPRINTF
151 #ifndef HAVE_VASPRINTF
155 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
158 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
159 char *res = (char *)malloc (len);
163 return vsnprintf (res, len, fmt, ap);
165 /* HACK: vsnprintf in the WinCE API behaves like
166 * the one in glibc 2.0 and doesn't return the number of characters
167 * it needed to copy the string.
168 * cf http://msdn.microsoft.com/en-us/library/1kt27hek.aspx
169 * and cf the man page of vsnprintf
171 Guess we need no more than 50 bytes. */
175 if ((res = (char *) malloc (size)) == NULL)
180 n = vsnprintf (res, size, fmt, ap);
182 /* If that worked, return the string. */
183 if (n > -1 && n < size)
189 /* Else try again with more space. */
190 size *= 2; /* twice the old size */
192 if ((np = (char *) realloc (res, size)) == NULL)
203 #endif /* UNDER_CE */
207 #ifndef HAVE_ASPRINTF
210 static inline int asprintf (char **strp, const char *fmt, ...)
215 ret = vasprintf (strp, fmt, ap);
223 static inline size_t strnlen (const char *str, size_t max)
225 const char *end = (const char *) memchr (str, 0, max);
226 return end ? (size_t)(end - str) : max;
233 static inline char *strndup (const char *str, size_t max)
235 size_t len = strnlen (str, max);
236 char *res = (char *) malloc (len + 1);
239 memcpy (res, str, len);
247 # define strlcpy vlc_strlcpy
251 # define strtof( a, b ) ((float)strtod (a, b))
255 # define atof( str ) (strtod ((str), (char **)NULL, 10))
259 # define strtoll vlc_strtoll
263 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
265 char *psz_string = *ppsz_string;
269 char *p = strpbrk( psz_string, psz_delimiters );
283 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
288 long long quot; /* Quotient. */
289 long long rem; /* Remainder. */
292 static inline lldiv_t lldiv (long long numer, long long denom)
294 lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
300 # define scandir vlc_scandir
301 # define alphasort vlc_alphasort
305 static inline char *getenv (const char *name)
312 #ifndef HAVE_STRCASECMP
313 # ifndef HAVE_STRICMP
315 static inline int strcasecmp (const char *s1, const char *s2)
317 for (size_t i = 0;; i++)
319 int d = tolower (s1[i]) - tolower (s2[i]);
320 if (d || !s1[i]) return d;
325 # define strcasecmp stricmp
329 #ifndef HAVE_STRNCASECMP
330 # ifndef HAVE_STRNICMP
332 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
334 for (size_t i = 0; i < n; i++)
336 int d = tolower (s1[i]) - tolower (s2[i]);
337 if (d || !s1[i]) return d;
342 # define strncasecmp strnicmp
346 #ifndef HAVE_STRCASESTR
347 # ifndef HAVE_STRISTR
348 # define strcasestr vlc_strcasestr
350 # define strcasestr stristr
354 #ifndef HAVE_LOCALTIME_R
355 /* If localtime_r() is not provided, we assume localtime() uses
356 * thread-specific storage. */
358 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
360 struct tm *s = localtime (timep);
367 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
369 struct tm *s = gmtime (timep);
378 /* Alignment of critical static data structures */
379 #ifdef ATTRIBUTE_ALIGNED_MAX
380 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
382 # define ATTR_ALIGN(align)
385 #ifndef HAVE_USELOCALE
386 typedef void *locale_t;
387 # define newlocale( a, b, c ) ((locale_t)0)
388 # define uselocale( a ) ((locale_t)0)
389 # define freelocale( a ) (void)0
394 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
395 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
396 # define closedir vlc_wclosedir
399 /* libintl support */
400 #define _(str) vlc_gettext (str)
402 #if defined (ENABLE_NLS)
403 # include <libintl.h>
406 #define N_(str) gettext_noop (str)
407 #define gettext_noop(str) (str)
410 static inline void rewind ( FILE *stream )
412 fseek(stream, 0L, SEEK_SET);
417 #endif /* !LIBVLC_FIXUPS_H */