From 037532d73b9e2763b57fb5e079b09e8f4ef404f5 Mon Sep 17 00:00:00 2001 From: Klaas Freitag Date: Wed, 20 Jun 2012 10:40:40 +0200 Subject: [PATCH] win32: Fix random file name generation, init random generator once. --- src/csync.c | 3 +++ src/std/c_path.c | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/csync.c b/src/csync.c index d6b4f84068..1c0bf09e76 100644 --- a/src/csync.c +++ b/src/csync.c @@ -351,6 +351,9 @@ retry_vio_init: ctx->status = CSYNC_STATUS_INIT; + /* initialize random generator */ + srand(time(NULL)); + rc = 0; out: diff --git a/src/std/c_path.c b/src/std/c_path.c index 732a208501..b32838f0d4 100644 --- a/src/std/c_path.c +++ b/src/std/c_path.c @@ -132,10 +132,15 @@ int c_tmpname(char *template) { } } - srand(time(NULL)); - for (i = 0; i < 6; ++i) { +#ifdef _WIN32 + /* in win32 MAX_RAND is 32767, thus we can not shift that far, + * otherwise the last three chars are 0 + */ + int hexdigit = (rand() >> (i * 2)) & 0x1f; +#else int hexdigit = (rand() >> (i * 5)) & 0x1f; +#endif tmp[i] = hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0'; }