[tz] [PATCH] Allow use of <direct.h> from Microsoft Windows to fix mkdir() parameters

Ian Abbott abbotti at mev.co.uk
Thu Feb 25 15:39:01 UTC 2016


This is an alternative to a patch by Esben Haabendal, titled "Fix MinGW 
builds by adding MKDIR_TAKES_ONE_ARG", posted on Feb 23.

Esben's patch made "private.h" optionally include <sys/stat.h> to get at 
Microsoft's mkdir function, but I think that only works in MinGW, and 
relies on MinGW's <io.h> header (included by <sys/stat.h>) optionally 
declaring the mkdir function.  The mkdir function is also deprecated by 
Microsoft.  New code should call _mkdir instead.  Both functions only 
take a single parameter.

This patch makes "private.h" optionally include <direct.h> (and also 
<io.h>, due to MinGW's behavior), controlled by the HAVE_DIRECT_H macro, 
and defines mkdir(path, mode) as macro calling _mkdir(path).

With this patch, tz builds successfully in a MSYS2 MingW-w64 Win32 or 
Win64 environment using the following command line:

make CFLAGS="-DHAVE_SYS_WAIT_H=0 -DHAVE_DIRECT_H=1 -DHAVE_LINK=0 
-DHAVE_SYMLINK=0 -DHAVE_LOCALTIME_R=0"

There are some warnings about 'tzname' being redeclared without 
dllimport attribute, when compiling zdump.c and localtime.c, but the 
resulting zdump executable seems to work.

-- 
-=( Ian Abbott @ MEV Ltd.    E-mail: <abbotti at mev.co.uk> )=-
-=(                          Web: http://www.mev.co.uk/  )=-
-------------- next part --------------
From bc21adb17966b29615ecc771af9ec775cb5d4cc5 Mon Sep 17 00:00:00 2001
From: Ian Abbott <abbotti at mev.co.uk>
Date: Thu, 25 Feb 2016 13:17:29 +0000
Subject: [PATCH] Allow use of <direct.h> from Microsoft Windows to fix mkdir()
 parameters

The mkdir function in Microsoft Windows C runtime library only takes a
single parameter.  It is also deprecated in favor of _mkdir, which also
takes a single parameter.

Add an optional CFLAG '-DHAVE_DIRECT_H' to conditionally include
<direct.h> and redefine mkdir as a macro that calls _mkdir, ignoring the
second parameter.

In MinGW, which uses the Microsoft C run-time library, mkdir may also be
declared as a function in <io.h>, so include that as well before
redefining mkdir.

Signed-off-by: Ian Abbott <abbotti at mev.co.uk>
---
 Makefile  |  1 +
 private.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/Makefile b/Makefile
index 568f7f6..5633953 100644
--- a/Makefile
+++ b/Makefile
@@ -106,6 +106,7 @@ LDLIBS=
 
 # Add the following to the end of the "CFLAGS=" line as needed.
 #  -DBIG_BANG=-9999999LL if the Big Bang occurred at time -9999999 (see zic.c)
+#  -DHAVE_DIRECT_H=1 if you have <direct.h> (MS-Windows C runtime library)
 #  -DHAVE_DOS_FILE_NAMES if file names have drive specifiers etc. (MS-DOS)
 #  -DHAVE_GETTEXT=1 if 'gettext' works (GNU, Linux, Solaris); also see LDLIBS
 #  -DHAVE_INCOMPATIBLE_CTIME_R=1 if your system's time.h declares
diff --git a/private.h b/private.h
index 1c176e6..153f82e 100644
--- a/private.h
+++ b/private.h
@@ -58,6 +58,10 @@
 #define HAVE_UTMPX_H		1
 #endif /* !defined HAVE_UTMPX_H */
 
+#ifndef HAVE_DIRECT_H
+#define HAVE_DIRECT_H		0
+#endif /* !defined HAVE_DIRECT_H */
+
 #ifndef NETBSD_INSPIRED
 # define NETBSD_INSPIRED 1
 #endif
@@ -125,6 +129,31 @@
 #define WEXITSTATUS(status)	(((status) >> 8) & 0xff)
 #endif /* !defined WEXITSTATUS */
 
+#if HAVE_DIRECT_H
+/*
+** For Microsoft Windows, include <direct.h> for access to functions such
+** as mkdir, but redefine them as macros to use the functions prefixed with
+** an underscore such as _mkdir, as the ones without an underscore are
+** deprecated.
+**
+** In MinGW, which targets Microsoft's C run-time libraries, some of the
+** deprecated functions without the underscores may also be declared in <io.h>,
+** so include that as well before our workarounds.
+*/
+# include <direct.h>
+# include <io.h>
+
+/* Only need mkdir for now. */
+
+/*
+** Microsoft's mkdir and _mkdir only take a single parameter, so ignore the
+** mode set by the second parameter.
+*/
+# undef mkdir
+# define mkdir(path, mode) _mkdir(path)
+
+#endif /* HAVE_DIRECT_H */
+
 #if HAVE_UNISTD_H
 #include "unistd.h"	/* for F_OK, R_OK, and other POSIX goodness */
 #endif /* HAVE_UNISTD_H */
-- 
2.7.1


More information about the tz mailing list