From 58a2480d52ce643698f56232dd32ea4527d3c56a Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Tue, 13 Sep 2016 21:39:44 +0000 Subject: [PATCH] Port: Add time.h compat for mingw MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- include/Makefile.am | 1 + include/babeltrace/compat/time-internal.h | 100 ++++++++++++++++++++++ plugins/text/pretty/print.c | 5 +- plugins/utils/trimmer/iterator.c | 9 +- 4 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 include/babeltrace/compat/time-internal.h diff --git a/include/Makefile.am b/include/Makefile.am index bf0ca69c..f00544c0 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -86,6 +86,7 @@ noinst_HEADERS = \ babeltrace/compat/stdio-internal.h \ babeltrace/compat/stdlib-internal.h \ babeltrace/compat/string-internal.h \ + babeltrace/compat/time-internal.h \ babeltrace/compat/unistd-internal.h \ babeltrace/compat/utc-internal.h \ babeltrace/compat/uuid-internal.h \ diff --git a/include/babeltrace/compat/time-internal.h b/include/babeltrace/compat/time-internal.h new file mode 100644 index 00000000..c7ce44c5 --- /dev/null +++ b/include/babeltrace/compat/time-internal.h @@ -0,0 +1,100 @@ +#ifndef _BABELTRACE_INCLUDE_COMPAT_TIME_H +#define _BABELTRACE_INCLUDE_COMPAT_TIME_H + +/* + * babeltrace/compat/time.h + * + * Copyright (C) 2013 JP Ikaheimonen + * 2016 Michael Jeanson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + + +#include +#include + +#ifdef __MINGW32__ + +#include + +/* + * The Windows version of the time functions use one common tm structure per + * thread which makes them thread-safe. Implement the POSIX _r variants by + * copying this to a user supplied struct. + */ + +static inline +struct tm *bt_gmtime_r(const time_t *timep, struct tm *result) +{ + struct tm *local_res; + + if (!result) { + goto error; + } + + local_res = gmtime(timep); + if (!local_res) { + result = NULL; + goto error; + } + + memcpy(result, local_res, sizeof(struct tm)); + +error: + return result; +} + +static inline +struct tm *bt_localtime_r(const time_t *timep, struct tm *result) +{ + struct tm *local_res; + + if (!result) { + goto error; + } + + local_res = localtime(timep); + if (!local_res) { + result = NULL; + goto error; + } + + memcpy(result, local_res, sizeof(struct tm)); + +error: + return result; +} + +#else /* __MINGW32__ */ + +static inline +struct tm *bt_gmtime_r(const time_t *timep, struct tm *result) +{ + return gmtime_r(timep, result); +} + +static inline +struct tm *bt_localtime_r(const time_t *timep, struct tm *result) +{ + return localtime_r(timep, result); +} + +#endif /* __MINGW32__ */ +#endif /* _BABELTRACE_INCLUDE_COMPAT_TIME_H */ diff --git a/plugins/text/pretty/print.c b/plugins/text/pretty/print.c index 6cba7ce0..0545178e 100644 --- a/plugins/text/pretty/print.c +++ b/plugins/text/pretty/print.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include "pretty.h" @@ -201,7 +202,7 @@ void print_timestamp_wall(struct pretty_component *pretty, if (!pretty->options.clock_gmt) { struct tm *res; - res = localtime_r(&time_s, &tm); + res = bt_localtime_r(&time_s, &tm); if (!res) { // TODO: log instead fprintf(stderr, "[warning] Unable to get localtime.\n"); @@ -210,7 +211,7 @@ void print_timestamp_wall(struct pretty_component *pretty, } else { struct tm *res; - res = gmtime_r(&time_s, &tm); + res = bt_gmtime_r(&time_s, &tm); if (!res) { // TODO: log instead fprintf(stderr, "[warning] Unable to get gmtime.\n"); diff --git a/plugins/utils/trimmer/iterator.c b/plugins/utils/trimmer/iterator.c index dcff7eca..b726c695 100644 --- a/plugins/utils/trimmer/iterator.c +++ b/plugins/utils/trimmer/iterator.c @@ -26,6 +26,7 @@ * SOFTWARE. */ +#include #include #include #include @@ -150,8 +151,8 @@ int update_lazy_bound(struct trimmer_bound *bound, const char *name, if (bound->lazy_values.gmt) { /* Get day, month, year. */ - if (!gmtime_r(&timeval, &tm)) { - printf_error("Failure in gmtime_r()"); + if (!bt_gmtime_r(&timeval, &tm)) { + printf_error("Failure in bt_gmtime_r()"); goto error; } tm.tm_sec = bound->lazy_values.ss; @@ -165,8 +166,8 @@ int update_lazy_bound(struct trimmer_bound *bound, const char *name, } } else { /* Get day, month, year. */ - if (!localtime_r(&timeval, &tm)) { - printf_error("Failure in localtime_r()"); + if (!bt_localtime_r(&timeval, &tm)) { + printf_error("Failure in bt_localtime_r()"); goto error; } tm.tm_sec = bound->lazy_values.ss; -- 2.34.1