]> git.sesse.net Git - vlc/blob - include/vlc_fixups.h
win32: make vlc_vsnprintf more like c99 vsnprintf
[vlc] / include / vlc_fixups.h
1 /*****************************************************************************
2  * fixups.h: portability fixups included from config.h
3  *****************************************************************************
4  * Copyright © 1998-2008 the VideoLAN project
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 /**
22  * \file
23  * This file is a collection of portability fixes
24  */
25
26 #ifndef LIBVLC_FIXUPS_H
27 # define LIBVLC_FIXUPS_H 1
28
29 #ifndef HAVE_STRDUP
30 # include <string.h>
31 # include <stdlib.h>
32 static inline char *strdup (const char *str)
33 {
34     size_t len = strlen (str) + 1;
35     char *res = (char *)malloc (len);
36     if (res) memcpy (res, str, len);
37     return res;
38 }
39 #endif
40
41 #ifdef WIN32
42 # include <string.h>
43 # include <stdlib.h>
44 /**
45  * vlc_fix_format_string:
46  * @format: address of format string to fix (format string is not modified)
47  *
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.
51  *
52  * Returns: 1 if *format must be free()d; 0 otherwise
53  */
54 static inline int vlc_fix_format_string (const char **format)
55 {
56     int n = 0;
57     const char *tmp = *format;
58     while ((tmp = strstr (tmp, "%z")) != NULL)
59     {
60         n++;
61         tmp += 2;
62     }
63     if (!n)
64         return 0;
65
66     char *dst = (char*)malloc (strlen (*format) + 2*n + 1);
67     if (!dst)
68     {
69         *format = "vlc_fix_format_string: due to malloc failure, unable to fix unsafe string";
70         return 0;
71     }
72
73     const char *src = *format;
74     *format = dst;
75     while ((tmp = strstr (src, "%z")) != NULL)
76     {
77         /* NB, don't use %l*, as this is buggy in mingw*/
78         size_t d = tmp - src;
79         memcpy (dst, src, d);
80         dst += d;
81         *dst++ = '%';
82 # ifdef WIN64
83         *dst++ = 'I';
84         *dst++ = '6';
85         *dst++ = '4';
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) */
89 # endif
90         src = tmp + 2;
91     }
92     strcpy (dst, src);
93     return 1;
94 }
95
96 # include <stdio.h>
97 # include <stdarg.h>
98
99 static inline int vlc_vprintf (const char *format, va_list ap)
100 {
101     int must_free = vlc_fix_format_string (&format);
102     int ret = vprintf (format, ap);
103     if (must_free) free ((char *)format);
104     return ret;
105 }
106 # define vprintf vlc_vprintf
107
108 static inline int vlc_vfprintf (FILE *stream, const char *format, va_list ap)
109 {
110     int must_free = vlc_fix_format_string (&format);
111     int ret = vfprintf (stream, format, ap);
112     if (must_free) free ((char *)format);
113     return ret;
114 }
115 # define vfprintf vlc_vfprintf
116
117 static inline int vlc_vsprintf (char *str, const char *format, va_list ap)
118 {
119     int must_free = vlc_fix_format_string (&format);
120     int ret = vsprintf (str, format, ap);
121     if (must_free) free ((char *)format);
122     return ret;
123 }
124 # define vsprintf vlc_vsprintf
125
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)
128 {
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
137      */
138     int ret = _vsnprintf (str, size-1, format, ap);
139     str[size-1] = 0; /* ensure the null gets written */
140     if (ret == -1)
141     {
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);
145     }
146     if (must_free) free ((char *)format);
147     return ret;
148 }
149 # define vsnprintf vlc_vsnprintf
150
151 static inline int vlc_printf (const char *format, ...)
152 {
153     va_list ap;
154     int ret;
155     va_start (ap, format);
156     ret = vlc_vprintf (format, ap);
157     va_end (ap);
158     return ret;
159 }
160 # define printf(...) vlc_printf(__VA_ARGS__)
161
162 static inline int vlc_fprintf (FILE *stream, const char *format, ...)
163 {
164     va_list ap;
165     int ret;
166     va_start (ap, format);
167     ret = vlc_vfprintf (stream, format, ap);
168     va_end (ap);
169     return ret;
170 }
171 # define fprintf vlc_fprintf
172
173 #if 0
174 static inline int vlc_sprintf (char *str, const char *format, ...)
175 {
176     va_list ap;
177     int ret;
178     va_start (ap, format);
179     ret = vlc_vsprintf (str, format, ap);
180     va_end (ap);
181     return ret;
182 }
183 # define sprintf vlc_sprintf
184 #endif
185
186 static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
187 {
188     va_list ap;
189     int ret;
190     va_start (ap, format);
191     ret = vlc_vsnprintf (str, size, format, ap);
192     va_end (ap);
193     return ret;
194 }
195 /* win32: snprintf must always be vlc_snprintf or _snprintf,
196  * see comment in vlc_vsnprintf */
197 # define snprintf vlc_snprintf
198
199 /* Make sure we don't use flawed vasprintf or asprintf either */
200 # undef HAVE_VASPRINTF
201 # undef HAVE_ASPRINTF
202 #endif
203
204 #ifndef HAVE_VASPRINTF
205 # include <stdio.h>
206 # include <stdlib.h>
207 # include <stdarg.h>
208 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
209 {
210 #ifndef WIN32
211     int len = vsnprintf (NULL, 0, fmt, ap) + 1;
212     char *res = (char *)malloc (len);
213     if (res == NULL)
214         return -1;
215     *strp = res;
216     return vsnprintf (res, len, fmt, ap);
217 #else
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
223      */
224     int must_free = vlc_fix_format_string (&fmt);
225     int n, size = 2 * strlen (fmt);
226     char *res, *np;
227
228     if ((res = (char *) malloc (size)) == NULL)
229     {
230         if (must_free) free ((char *)fmt);
231         return -1;
232     }
233
234     while (1)
235     {
236         n = _vsnprintf (res, size, fmt, ap);
237
238         /* If that worked, return the string. */
239         if (n > -1 && n < size)
240         {
241             *strp = res;
242             if (must_free) free ((char *)fmt);
243             return n;
244         }
245
246         /* Else try again with more space. */
247         size *= 2;  /* twice the old size */
248
249         if ((np = (char *) realloc (res, size)) == NULL)
250         {
251             free(res);
252             if (must_free) free ((char *)fmt);
253             return -1;
254         }
255         else
256         {
257             res = np;
258         }
259
260     }
261 #endif /* WIN32 */
262 }
263 #endif
264
265 #ifndef HAVE_ASPRINTF
266 # include <stdio.h>
267 # include <stdarg.h>
268 static inline int asprintf (char **strp, const char *fmt, ...)
269 {
270     va_list ap;
271     int ret;
272     va_start (ap, fmt);
273     ret = vasprintf (strp, fmt, ap);
274     va_end (ap);
275     return ret;
276 }
277 #endif
278
279 #ifndef HAVE_STRNLEN
280 # include <string.h>
281 static inline size_t strnlen (const char *str, size_t max)
282 {
283     const char *end = (const char *) memchr (str, 0, max);
284     return end ? (size_t)(end - str) : max;
285 }
286 #endif
287
288 #ifndef HAVE_STRNDUP
289 # include <string.h>
290 # include <stdlib.h>
291 static inline char *strndup (const char *str, size_t max)
292 {
293     size_t len = strnlen (str, max);
294     char *res = (char *) malloc (len + 1);
295     if (res)
296     {
297         memcpy (res, str, len);
298         res[len] = '\0';
299     }
300     return res;
301 }
302 #endif
303
304 #ifndef HAVE_STRLCPY
305 # define strlcpy vlc_strlcpy
306 #endif
307
308 #ifndef HAVE_STRTOF
309 # define strtof( a, b ) ((float)strtod (a, b))
310 #endif
311
312 #ifndef HAVE_ATOF
313 # define atof( str ) (strtod ((str), (char **)NULL, 10))
314 #endif
315
316 #ifndef HAVE_STRTOLL
317 # define strtoll vlc_strtoll
318 #endif
319
320 #ifndef HAVE_STRSEP
321 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
322 {
323     char *psz_string = *ppsz_string;
324     if( !psz_string )
325         return NULL;
326
327     char *p = strpbrk( psz_string, psz_delimiters );
328     if( !p )
329     {
330         *ppsz_string = NULL;
331         return psz_string;
332     }
333     *p++ = '\0';
334
335     *ppsz_string = p;
336     return psz_string;
337 }
338 #endif
339
340 #ifndef HAVE_ATOLL
341 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
342 #endif
343
344 #ifndef HAVE_LLDIV
345 typedef struct {
346     long long quot; /* Quotient. */
347     long long rem;  /* Remainder. */
348 } lldiv_t;
349
350 static inline lldiv_t lldiv (long long numer, long long denom)
351 {
352     lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
353     return d;
354 }
355 #endif
356
357 #ifndef HAVE_SCANDIR
358 # define scandir vlc_scandir
359 # define alphasort vlc_alphasort
360 #endif
361
362 #ifndef HAVE_GETENV
363 static inline char *getenv (const char *name)
364 {
365     (void)name;
366     return NULL;
367 }
368 #endif
369
370 #ifndef HAVE_STRCASECMP
371 # ifndef HAVE_STRICMP
372 #  include <ctype.h>
373 static inline int strcasecmp (const char *s1, const char *s2)
374 {
375     for (size_t i = 0;; i++)
376     {
377         int d = tolower (s1[i]) - tolower (s2[i]);
378         if (d || !s1[i]) return d;
379     }
380     return 0;
381 }
382 # else
383 #  define strcasecmp stricmp
384 # endif
385 #endif
386
387 #ifndef HAVE_STRNCASECMP
388 # ifndef HAVE_STRNICMP
389 #  include <ctype.h>
390 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
391 {
392     for (size_t i = 0; i < n; i++)
393     {
394         int d = tolower (s1[i]) - tolower (s2[i]);
395         if (d || !s1[i]) return d;
396     }
397     return 0;
398 }
399 # else
400 #  define strncasecmp strnicmp
401 # endif
402 #endif
403
404 #ifndef HAVE_STRCASESTR
405 # ifndef HAVE_STRISTR
406 #  define strcasestr vlc_strcasestr
407 # else
408 #  define strcasestr stristr
409 # endif
410 #endif
411
412 #ifndef HAVE_LOCALTIME_R
413 /* If localtime_r() is not provided, we assume localtime() uses
414  * thread-specific storage. */
415 # include <time.h>
416 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
417 {
418     struct tm *s = localtime (timep);
419     if (s == NULL)
420         return NULL;
421
422     *result = *s;
423     return result;
424 }
425 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
426 {
427     struct tm *s = gmtime (timep);
428     if (s == NULL)
429         return NULL;
430
431     *result = *s;
432     return result;
433 }
434 #endif
435
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)))
439 #else
440 #   define ATTR_ALIGN(align)
441 #endif
442
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
448 #endif
449
450 #ifdef WIN32
451 # include <dirent.h>
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
455 #endif
456
457 /* libintl support */
458 #define _(str) vlc_gettext (str)
459
460 #if defined (ENABLE_NLS)
461 # include <libintl.h>
462 #endif
463
464 #define N_(str) gettext_noop (str)
465 #define gettext_noop(str) (str)
466
467 #ifdef UNDER_CE
468 static inline void rewind ( FILE *stream )
469 {
470     fseek(stream, 0L, SEEK_SET);
471     clearerr(stream);
472 }
473 #endif
474
475 #endif /* !LIBVLC_FIXUPS_H */