w32: Modernize for MinGW 6.0.0, and clean up

We provide gettimeofday() only #ifdef _MSC_VER, but declare it
unconditionally in our own sys/time.h.  This is asking for trouble.
Declare it only #ifdef _MSC_VER, else pull in MinGW's sys/time.h with
#include_next.

Likewise, define macros ftruncate(), S_IRUSR & friends in our own
unistd.h only #ifdef _MSC_VER, else pull in MinGW's unistd.h with
#include_next.  The #include <getopt.h> is now useless, drop.

src/lib/commands/info.c relies on sys/time.h including windows.h.
Unclean, and no longer the case with MinGW.  Include it directly.

MinGW provides EWOULDBLOCK.  Drop our replacement.

MinGW provides inet_ntop() in ws2tcpip.h, but only if feature test
macro _WIN32_WINNT >= 0x600, i.e. Windows Vista or later.  It defaults
to 0x0502 (Windows Server 2003).  Make configure #define _WIN32_WINNT
0x0601 in config.h.  This requests Windows 7.  Trying to support Vista
feels unwise.  Include ws2tcpip.h, and drop our replacement.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2021-01-27 08:20:05 +01:00
parent 598669673e
commit a164390d1f
8 changed files with 26 additions and 53 deletions

View file

@ -12,6 +12,8 @@ AC_DEFUN([MY_WINDOWS_API],
AC_DEFINE([WINDOWS], 1, [Define if compiling for Windows API]) AC_DEFINE([WINDOWS], 1, [Define if compiling for Windows API])
AC_DEFINE([WIN32_LEAN_AND_MEAN], 1, AC_DEFINE([WIN32_LEAN_AND_MEAN], 1,
[Define to make Windows includes pull in less junk]) [Define to make Windows includes pull in less junk])
AC_DEFINE([_WIN32_WINNT], 0x0601,
[Request Windows 7])
LIBS_SOCKETS="-lws2_32" LIBS_SOCKETS="-lws2_32"
else else
LIBS_SOCKETS= LIBS_SOCKETS=

View file

@ -40,7 +40,9 @@
#include <errno.h> #include <errno.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdio.h> #include <stdio.h>
#if !defined(_WIN32) #ifdef _WIN32
#include <windows.h>
#else
#include <strings.h> #include <strings.h>
#include <dirent.h> #include <dirent.h>
#endif #endif

View file

@ -33,9 +33,6 @@
#ifndef ARPA_INET_H #ifndef ARPA_INET_H
#define ARPA_INET_H #define ARPA_INET_H
#include "sys/socket.h" #include "ws2tcpip.h"
extern const char *inet_ntop(int af, const void *src, char *dst,
socklen_t cnt);
#endif /* ARPA_INET_H */ #endif /* ARPA_INET_H */

View file

@ -33,7 +33,7 @@
#include <config.h> #include <config.h>
#include <errno.h> #include <windows.h>
#include "sys/resource.h" #include "sys/resource.h"
int int

View file

@ -33,9 +33,15 @@
#ifndef SYS_TIME_H #ifndef SYS_TIME_H
#define SYS_TIME_H #define SYS_TIME_H
#ifdef _MSC_VER
/* include winsock2.h thru sys/socket.h to get struct timeval */ /* include winsock2.h thru sys/socket.h to get struct timeval */
#include "sys/socket.h" #include "sys/socket.h"
extern int gettimeofday(struct timeval *tv, void *tz); extern int gettimeofday(struct timeval *tv, void *tz);
#else /* !_MSC_VER */
#include_next <sys/time.h>
#endif
#endif /* SYS_TIME_H */ #endif /* SYS_TIME_H */

View file

@ -46,7 +46,6 @@
* here. Major name space pollution, can't be helped. * here. Major name space pollution, can't be helped.
*/ */
#include <direct.h> #include <direct.h>
#include <getopt.h>
#include <io.h> #include <io.h>
#include <process.h> #include <process.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -60,6 +59,7 @@
extern int w32_mkdir(const char *dirname, mode_t perm); extern int w32_mkdir(const char *dirname, mode_t perm);
/* Should be in sys/stat.h */ /* Should be in sys/stat.h */
#ifdef _MSC_VER
#ifndef S_IRUSR #ifndef S_IRUSR
#define S_IRUSR _S_IREAD #define S_IRUSR _S_IREAD
#define S_IWUSR _S_IWRITE #define S_IWUSR _S_IWRITE
@ -78,22 +78,28 @@ extern int w32_mkdir(const char *dirname, mode_t perm);
#define S_IXOTH 0 #define S_IXOTH 0
#define S_IRWXO S_IROTH | S_IWOTH | S_IXOTH #define S_IRWXO S_IROTH | S_IWOTH | S_IXOTH
#endif #endif
#endif
/* Should be in fcntl.h */ /* Should be in fcntl.h */
/* HACK, for use with fcntl() on a socket only, see w32sockets.c */
#define O_NONBLOCK 1 #define O_NONBLOCK 1
#define F_GETFL 1 #define F_GETFL 1
#define F_SETFL 2 #define F_SETFL 2
extern int fcntl(int fd, int cmd, ...); extern int fcntl(int fd, int cmd, ...);
/* Stuff that actually belongs here */ /* Stuff that actually belongs here */
#define close(fd) w32_close_function((fd)) #define close(fd) w32_close_function((fd))
extern int (*w32_close_function)(int); extern int (*w32_close_function)(int);
#define ftruncate(fd, length) _chsize((fd), (length))
#define read(fd, buf, sz) w32_read_function((fd), (buf), (sz)) #define read(fd, buf, sz) w32_read_function((fd), (buf), (sz))
extern int (*w32_read_function)(int, void *, unsigned); extern int (*w32_read_function)(int, void *, unsigned);
#define write(fd, buf, sz) w32_write_function((fd), (buf), (sz)) #define write(fd, buf, sz) w32_write_function((fd), (buf), (sz))
extern int (*w32_write_function)(int, const void *, unsigned); extern int (*w32_write_function)(int, const void *, unsigned);
#ifdef _MSC_VER
#define ftruncate(fd, length) _chsize((fd), (length))
#endif
#ifndef _MSC_VER
#include_next <unistd.h>
#endif
#endif /* UNISTD_H */ #endif /* UNISTD_H */

View file

@ -28,7 +28,7 @@
* *
* Known contributors to this file: * Known contributors to this file:
* Ron Koenderink, 2007 * Ron Koenderink, 2007
* Markus Armbruster, 2007-2013 * Markus Armbruster, 2007-2021
*/ */
/* /*
@ -44,16 +44,13 @@
#ifdef _MSC_VER #ifdef _MSC_VER
/* integral mismatch, due to misuse of sector short */ /* integral mismatch, due to misuse of sector short */
#pragma warning (disable : 4761 ) #pragma warning (disable : 4761 )
#endif
/* strings.h */ /* strings.h */
#ifdef _MSC_VER
#define strncasecmp(s1, s2, s3) _strnicmp((s1), (s2), (s3)) #define strncasecmp(s1, s2, s3) _strnicmp((s1), (s2), (s3))
#endif /* _MSC_VER */ #endif /* _MSC_VER */
/* errno.h */
#ifndef EWOULDBLOCK
#define EWOULDBLOCK EAGAIN
#endif
/* fcntl.h */ /* fcntl.h */
#ifdef _MSC_VER #ifdef _MSC_VER
#define O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR) #define O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)

View file

@ -232,43 +232,6 @@ w32_socket(int domain, int type, int protocol)
return W32_SOCKET_TO_FD(sock); return W32_SOCKET_TO_FD(sock);
} }
#ifdef HAVE_GETADDRINFO
const char *
inet_ntop(int af, const void *src, char *dst, socklen_t len)
{
struct sockaddr *sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
size_t salen;
if (af == AF_INET) {
memset(&sin, 0, sizeof(sin));
sin.sin_family = af;
memcpy(&sin.sin_addr, src, sizeof(sin.sin_addr));
sa = (struct sockaddr *)&sin;
salen = sizeof(sin);
} else if (af == AF_INET6) {
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = af;
memcpy(&sin6.sin6_addr, src, sizeof(sin6.sin6_addr));
sa = (struct sockaddr *)&sin6;
salen = sizeof(sin6);
} else {
WSASetLastError(WSAEAFNOSUPPORT);
w32_set_winsock_errno();
return NULL;
}
if (getnameinfo(sa, salen, dst, len, NULL, 0, NI_NUMERICHOST)) {
WSASetLastError(WSAEAFNOSUPPORT);
w32_set_winsock_errno();
return NULL;
}
return dst;
}
#endif
/* /*
* POSIX equivalent for fcntl(). * POSIX equivalent for fcntl().
* Horrible hacks, just good enough support Empire's use of fcntl(). * Horrible hacks, just good enough support Empire's use of fcntl().