summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-time
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2017-04-25 10:39:11 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2017-04-25 10:41:50 +0900
commit88cd807379152ea3fec5f534e5f4d6ebebd53982 (patch)
treef6b27a76821b8b92f6fc474ce56aab5795afb51b /mrbgems/mruby-time
parent03cdb8e9dd3447115530418c2b8183c94dee2a53 (diff)
downloadmruby-88cd807379152ea3fec5f534e5f4d6ebebd53982.tar.gz
mruby-88cd807379152ea3fec5f534e5f4d6ebebd53982.zip
Avoid use of `snprintf()` when DISABLE_STDIO is set; fix #3632
ref #3492 #3515 #3517
Diffstat (limited to 'mrbgems/mruby-time')
-rw-r--r--mrbgems/mruby-time/src/time.c36
1 files changed, 30 insertions, 6 deletions
diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c
index ce99a5a65..e367ddb64 100644
--- a/mrbgems/mruby-time/src/time.c
+++ b/mrbgems/mruby-time/src/time.c
@@ -5,12 +5,17 @@
*/
#include <math.h>
-#include <stdio.h>
#include <time.h>
#include <mruby.h>
#include <mruby/class.h>
#include <mruby/data.h>
+#ifndef DISABLE_STDIO
+#include <stdio.h>
+#else
+#include <string.h>
+#endif
+
#define NDIV(x,y) (-(-((x)+1)/(y))-1)
#if _MSC_VER < 1800
@@ -55,6 +60,13 @@ double round(double x) {
#endif
#endif
+/* asctime(3) */
+/* mruby usually use its own implementation of struct tm to string conversion */
+/* except when DISABLE_STDIO is set. In that case, it uses asctime() or asctime_r(). */
+/* By default mruby tries to use asctime_r() which is reentrant. */
+/* Undef following macro on platforms that does not have asctime_r(). */
+/* #define NO_ASCTIME_R */
+
/* timegm(3) */
/* mktime() creates tm structure for localtime; timegm() is for UTC time */
/* define following macro to use probably faster timegm() on the platform */
@@ -166,6 +178,7 @@ static const mrb_timezone_name timezone_names[] = {
{ "LOCAL", sizeof("LOCAL") - 1 },
};
+#ifndef DISABLE_STDIO
static const char mon_names[12][4] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
};
@@ -173,6 +186,7 @@ static const char mon_names[12][4] = {
static const char wday_names[7][4] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
};
+#endif
struct mrb_time {
time_t sec;
@@ -528,18 +542,28 @@ mrb_time_zone(mrb_state *mrb, mrb_value self)
static mrb_value
mrb_time_asctime(mrb_state *mrb, mrb_value self)
{
- struct mrb_time *tm;
- struct tm *d;
- char buf[256];
+ struct mrb_time *tm = time_get_ptr(mrb, self);
+ struct tm *d = &tm->datetime;
int len;
- tm = time_get_ptr(mrb, self);
- d = &tm->datetime;
+#if defined(DISABLE_STDIO)
+ char *s;
+# ifdef NO_ASCTIME_R
+ s = asctime(d);
+# else
+ char buf[32];
+ s = asctime_r(d, buf);
+# endif
+ len = strlen(s)-1; /* truncate the last newline */
+#else
+ char buf[256];
+
len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d",
wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
d->tm_hour, d->tm_min, d->tm_sec,
tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "",
d->tm_year + 1900);
+#endif
return mrb_str_new(mrb, buf, len);
}