]> git.sesse.net Git - vlc/blob - include/vlc_fixups.h
Revert "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 vlc_vsnprintf (char *str, size_t size, const char *format, va_list ap)
127 {
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);
135     return ret;
136 }
137 # define vsnprintf vlc_vsnprintf
138
139 static inline int vlc_printf (const char *format, ...)
140 {
141     va_list ap;
142     int ret;
143     va_start (ap, format);
144     ret = vlc_vprintf (format, ap);
145     va_end (ap);
146     return ret;
147 }
148 # define printf(...) vlc_printf(__VA_ARGS__)
149
150 static inline int vlc_fprintf (FILE *stream, const char *format, ...)
151 {
152     va_list ap;
153     int ret;
154     va_start (ap, format);
155     ret = vlc_vfprintf (stream, format, ap);
156     va_end (ap);
157     return ret;
158 }
159 # define fprintf vlc_fprintf
160
161 #if 0
162 static inline int vlc_sprintf (char *str, const char *format, ...)
163 {
164     va_list ap;
165     int ret;
166     va_start (ap, format);
167     ret = vlc_vsprintf (str, format, ap);
168     va_end (ap);
169     return ret;
170 }
171 # define sprintf vlc_sprintf
172 #endif
173
174 static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
175 {
176     va_list ap;
177     int ret;
178     va_start (ap, format);
179     ret = vlc_vsnprintf (str, size, format, ap);
180     va_end (ap);
181     return ret;
182 }
183 /* win32: snprintf must always be vlc_snprintf or _snprintf,
184  * see comment in vlc_vsnprintf */
185 # define snprintf vlc_snprintf
186
187 /* Make sure we don't use flawed vasprintf or asprintf either */
188 # undef HAVE_VASPRINTF
189 # undef HAVE_ASPRINTF
190 #endif
191
192 #ifndef HAVE_VASPRINTF
193 # include <stdio.h>
194 # include <stdlib.h>
195 # include <stdarg.h>
196 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
197 {
198 #ifndef UNDER_CE
199     int len = vsnprintf (NULL, 0, fmt, ap) + 1;
200     char *res = (char *)malloc (len);
201     if (res == NULL)
202         return -1;
203     *strp = res;
204     return vsnprintf (res, len, fmt, ap);
205 #else
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
211      *
212      Guess we need no more than 50 bytes. */
213     int n, size = 50;
214     char *res, *np;
215
216     if ((res = (char *) malloc (size)) == NULL)
217         return -1;
218
219     while (1)
220     {
221         n = vsnprintf (res, size, fmt, ap);
222
223         /* If that worked, return the string. */
224         if (n > -1 && n < size)
225         {
226             *strp = res;
227             return n;
228         }
229
230         /* Else try again with more space. */
231         size *= 2;  /* twice the old size */
232
233         if ((np = (char *) realloc (res, size)) == NULL)
234         {
235             free(res);
236             return -1;
237         }
238         else
239         {
240             res = np;
241         }
242
243     }
244 #endif /* UNDER_CE */
245 }
246 #endif
247
248 #ifndef HAVE_ASPRINTF
249 # include <stdio.h>
250 # include <stdarg.h>
251 static inline int asprintf (char **strp, const char *fmt, ...)
252 {
253     va_list ap;
254     int ret;
255     va_start (ap, fmt);
256     ret = vasprintf (strp, fmt, ap);
257     va_end (ap);
258     return ret;
259 }
260 #endif
261
262 #ifndef HAVE_STRNLEN
263 # include <string.h>
264 static inline size_t strnlen (const char *str, size_t max)
265 {
266     const char *end = (const char *) memchr (str, 0, max);
267     return end ? (size_t)(end - str) : max;
268 }
269 #endif
270
271 #ifndef HAVE_STRNDUP
272 # include <string.h>
273 # include <stdlib.h>
274 static inline char *strndup (const char *str, size_t max)
275 {
276     size_t len = strnlen (str, max);
277     char *res = (char *) malloc (len + 1);
278     if (res)
279     {
280         memcpy (res, str, len);
281         res[len] = '\0';
282     }
283     return res;
284 }
285 #endif
286
287 #ifndef HAVE_STRLCPY
288 # define strlcpy vlc_strlcpy
289 #endif
290
291 #ifndef HAVE_STRTOF
292 # define strtof( a, b ) ((float)strtod (a, b))
293 #endif
294
295 #ifndef HAVE_ATOF
296 # define atof( str ) (strtod ((str), (char **)NULL, 10))
297 #endif
298
299 #ifndef HAVE_STRTOLL
300 # define strtoll vlc_strtoll
301 #endif
302
303 #ifndef HAVE_STRSEP
304 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
305 {
306     char *psz_string = *ppsz_string;
307     if( !psz_string )
308         return NULL;
309
310     char *p = strpbrk( psz_string, psz_delimiters );
311     if( !p )
312     {
313         *ppsz_string = NULL;
314         return psz_string;
315     }
316     *p++ = '\0';
317
318     *ppsz_string = p;
319     return psz_string;
320 }
321 #endif
322
323 #ifndef HAVE_ATOLL
324 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
325 #endif
326
327 #ifndef HAVE_LLDIV
328 typedef struct {
329     long long quot; /* Quotient. */
330     long long rem;  /* Remainder. */
331 } lldiv_t;
332
333 static inline lldiv_t lldiv (long long numer, long long denom)
334 {
335     lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
336     return d;
337 }
338 #endif
339
340 #ifndef HAVE_SCANDIR
341 # define scandir vlc_scandir
342 # define alphasort vlc_alphasort
343 #endif
344
345 #ifndef HAVE_GETENV
346 static inline char *getenv (const char *name)
347 {
348     (void)name;
349     return NULL;
350 }
351 #endif
352
353 #ifndef HAVE_STRCASECMP
354 # ifndef HAVE_STRICMP
355 #  include <ctype.h>
356 static inline int strcasecmp (const char *s1, const char *s2)
357 {
358     for (size_t i = 0;; i++)
359     {
360         int d = tolower (s1[i]) - tolower (s2[i]);
361         if (d || !s1[i]) return d;
362     }
363     return 0;
364 }
365 # else
366 #  define strcasecmp stricmp
367 # endif
368 #endif
369
370 #ifndef HAVE_STRNCASECMP
371 # ifndef HAVE_STRNICMP
372 #  include <ctype.h>
373 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
374 {
375     for (size_t i = 0; i < n; 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 strncasecmp strnicmp
384 # endif
385 #endif
386
387 #ifndef HAVE_STRCASESTR
388 # ifndef HAVE_STRISTR
389 #  define strcasestr vlc_strcasestr
390 # else
391 #  define strcasestr stristr
392 # endif
393 #endif
394
395 #ifndef HAVE_LOCALTIME_R
396 /* If localtime_r() is not provided, we assume localtime() uses
397  * thread-specific storage. */
398 # include <time.h>
399 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
400 {
401     struct tm *s = localtime (timep);
402     if (s == NULL)
403         return NULL;
404
405     *result = *s;
406     return result;
407 }
408 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
409 {
410     struct tm *s = gmtime (timep);
411     if (s == NULL)
412         return NULL;
413
414     *result = *s;
415     return result;
416 }
417 #endif
418
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)))
422 #else
423 #   define ATTR_ALIGN(align)
424 #endif
425
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
431 #endif
432
433 #ifdef WIN32
434 # include <dirent.h>
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
438 #endif
439
440 /* libintl support */
441 #define _(str) vlc_gettext (str)
442
443 #if defined (ENABLE_NLS)
444 # include <libintl.h>
445 #endif
446
447 #define N_(str) gettext_noop (str)
448 #define gettext_noop(str) (str)
449
450 #ifdef UNDER_CE
451 static inline void rewind ( FILE *stream )
452 {
453     fseek(stream, 0L, SEEK_SET);
454     clearerr(stream);
455 }
456 #endif
457
458 #endif /* !LIBVLC_FIXUPS_H */