From 02bb2616470766a71d8caea2ae61cdd5f96bb3f1 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 17 Jun 2012 12:36:30 +0000 Subject: [PATCH] win32: define a custom sprintf_s() weak symbol. The VS2010 runtime does not provide the deprecated snprintf(). The mingw32 runtime does not provide the MS-specific sprintf_s(). Mingw-w64 copes with both. So we switch to sprintf_s but also provide it as a weak symbol so that mingw32 does not complain. --- caca/caca.h | 6 ++++++ caca/canvas.c | 4 +++- caca/figfont.c | 20 ++++++++++++++++++++ caca/string.c | 16 ++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/caca/caca.h b/caca/caca.h index 689afde..0410897 100644 --- a/caca/caca.h +++ b/caca/caca.h @@ -689,6 +689,12 @@ typedef struct cucul_buffer cucul_buffer_t; # define CACA_ALIAS(x) # endif +# if defined __GNUC__ && __GNUC__ > 3 +# define CACA_WEAK __attribute__ ((weak)) +# else +# define CACA_WEAK +# endif + /* Aliases from old libcaca and libcucul functions */ __extern int cucul_putchar(caca_canvas_t *, int, int, diff --git a/caca/canvas.c b/caca/canvas.c index 50e2806..f8974e5 100644 --- a/caca/canvas.c +++ b/caca/canvas.c @@ -341,13 +341,15 @@ int caca_free_canvas(caca_canvas_t *cv) * \return A random integer comprised between \p min and \p max - 1 * (inclusive). */ +static caca_timer_t timer = {0, 0}; + int caca_rand(int min, int max) { static int need_init = 1; if(need_init) { - srand(getpid() + time(NULL)); + srand(getpid() + _caca_getticks(&timer)); need_init = 0; } diff --git a/caca/figfont.c b/caca/figfont.c index 7903aee..8b12185 100644 --- a/caca/figfont.c +++ b/caca/figfont.c @@ -29,6 +29,10 @@ #include "caca.h" #include "caca_internals.h" +#if defined _WIN32 && defined __GNUC__ && __GNUC__ >= 3 +int sprintf_s(char *s, size_t n, const char *fmt, ...) CACA_WEAK; +#endif + struct caca_charfont { int term_width; @@ -622,6 +626,22 @@ static uint32_t hsmush(uint32_t ch1, uint32_t ch2, int rule) return 0; } +/* + * Functions for the mingw32 runtime + */ + +#if defined _WIN32 && defined __GNUC__ && __GNUC__ >= 3 +int sprintf_s(char *s, size_t n, const char *fmt, ...) +{ + va_list args; + int ret; + va_start(args, fmt); + ret = vsnprintf(s, n, fmt, args); + va_end(args); + return ret; +} +#endif + /* * XXX: The following functions are aliases. */ diff --git a/caca/string.c b/caca/string.c index fd333dc..023f0fa 100644 --- a/caca/string.c +++ b/caca/string.c @@ -36,6 +36,11 @@ #include "caca.h" #include "caca_internals.h" +#if defined _WIN32 && defined __GNUC__ && __GNUC__ >= 3 +int vsnprintf_s(char *s, size_t n, size_t c, + const char *fmt, va_list args) CACA_WEAK; +#endif + /** \brief Set cursor position. * * Put the cursor at the given coordinates. Functions making use of the @@ -597,6 +602,17 @@ int caca_set_canvas_boundaries(caca_canvas_t *cv, int x, int y, int w, int h) return 0; } +/* + * Functions for the mingw32 runtime + */ + +#if defined _WIN32 && defined __GNUC__ && __GNUC__ >= 3 +int vsnprintf_s(char *s, size_t n, size_t c, const char *fmt, va_list args) +{ + return vsnprintf(s, n, fmt, args); +} +#endif + /* * XXX: The following functions are aliases. */