From 201b4751332f7b1b75c5864698b311beb7501ddd Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Fri, 8 Nov 2019 04:11:57 +0000 Subject: [PATCH] some win32 tweaks --- moo/lib/std.c | 31 +++++++++++++++++++++++++------ moo/mod/io-file.c | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/moo/lib/std.c b/moo/lib/std.c index 34140cf..ba00729 100644 --- a/moo/lib/std.c +++ b/moo/lib/std.c @@ -956,24 +956,41 @@ static void log_write (moo_t* moo, moo_bitmask_t mask, const moo_ooch_t* msg, mo * do classification based on mask. */ if (!(mask & (MOO_LOG_STDOUT | MOO_LOG_STDERR))) { + #if defined(_WIN32) + TIME_ZONE_INFORMATION tzi; + SYSTEMTIME now; + #else time_t now; + struct tm tm, *tmp; + #endif #if defined(MOO_OOCH_IS_UCH) char ts[32 * MOO_BCSIZE_MAX]; #else char ts[32]; #endif moo_oow_t tslen; - struct tm tm, *tmp; - now = time(MOO_NULL); #if defined(_WIN32) - tmp = localtime(&now); - tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp); - if (tslen == 0) + /* %z for strftime() in win32 seems to produce a long non-numeric timezone name. + * i don't use strftime() for time formatting. */ + GetLocalTime (&now); + if (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_INVALID) { - tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d ", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); + tzi.Bias = -tzi.Bias; + tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d %+02.2d%02.2d ", + (int)now.wYear, (int)now.wMonth, (int)now.wDay, + (int)now.wHour, (int)now.wMinute, (int)now.wSecond, + (int)(tzi.Bias / 60), (int)(tzi.Bias % 60)); + } + else + { + tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d ", + (int)now.wYear, (int)now.wMonth, (int)now.wDay, + (int)now.wHour, (int)now.wMinute, (int)now.wSecond); } #elif defined(__OS2__) + now = time(MOO_NULL); + #if defined(__WATCOMC__) tmp = _localtime(&now, &tm); #else @@ -992,10 +1009,12 @@ static void log_write (moo_t* moo, moo_bitmask_t mask, const moo_ooch_t* msg, mo } #elif defined(__DOS__) + now = time(MOO_NULL); tmp = localtime(&now); /* since i know that %z/%Z is not available in strftime, i switch to sprintf immediately */ tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d ", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); #else + now = time(MOO_NULL); #if defined(HAVE_LOCALTIME_R) tmp = localtime_r(&now, &tm); #else diff --git a/moo/mod/io-file.c b/moo/mod/io-file.c index 1f9f653..75110f2 100644 --- a/moo/mod/io-file.c +++ b/moo/mod/io-file.c @@ -91,6 +91,7 @@ static moo_pfrc_t pf_open_file (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) goto oops; } +#if defined(O_NONBLOCK) && defined(O_CLOEXEC) fl = fcntl(fd, F_GETFL, 0); if (fl == -1) { @@ -102,6 +103,7 @@ static moo_pfrc_t pf_open_file (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) fl |= O_NONBLOCK | O_CLOEXEC; if (fcntl(fd, F_SETFL, fl) == -1) goto fcntl_failure; +#endif io->handle = MOO_SMOOI_TO_OOP(fd); MOO_STACK_SETRETTORCV (moo, nargs); @@ -146,6 +148,10 @@ oops: static moo_pfrc_t pf_chmod_file (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) { +#if defined(_WIN32) + moo_seterrnum (moo, MOO_ENOIMPL); + return MOO_PF_FAILURE; +#else moo_oop_t tmp; int fd; moo_oow_t mode; @@ -163,11 +169,16 @@ static moo_pfrc_t pf_chmod_file (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) SETRET_WITH_SYSCALL (moo, nargs, fchmod(fd, mode)); return MOO_PF_SUCCESS; +#endif } static moo_pfrc_t pf_chown_file (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) { +#if defined(_WIN32) + moo_seterrnum (moo, MOO_ENOIMPL); + return MOO_PF_FAILURE; +#else moo_oop_t tmp; int fd; moo_ooi_t uid, gid; @@ -196,10 +207,15 @@ static moo_pfrc_t pf_chown_file (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) SETRET_WITH_SYSCALL (moo, nargs, fchown(fd, uid, gid)); return MOO_PF_SUCCESS; +#endif } static moo_pfrc_t pf_lock_file (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) { +#if defined(_WIN32) + moo_seterrnum (moo, MOO_ENOIMPL); + return MOO_PF_FAILURE; +#else moo_oop_t tmp; int fd; @@ -214,6 +230,7 @@ static moo_pfrc_t pf_lock_file (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) SETRET_WITH_SYSCALL (moo, nargs, flock(fd, MOO_OOP_TO_SMOOI(tmp))); return MOO_PF_SUCCESS; +#endif } static moo_pfrc_t pf_seek_file (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) @@ -293,6 +310,14 @@ static moo_pfinfo_t pfinfos[] = { I, "truncate:", { pf_truncate_file, 1, 1 } } }; +#if defined(_WIN32) +# define LOCK_EX 0 +# define LOCK_NB 0 +# define LOCK_SH 0 +# define LOCK_UN 0 +# define O_NOFOLLOW 0 +# define O_NONBLOCK 0 +#endif static moo_pvinfo_t pvinfos[] = { /*