Browse Source

Fix platform_compat.h - remove duplicate definitions for MSYS2

- Remove duplicate struct pollfd (already in winsock2.h)
- Remove duplicate struct timezone (already in time.h)
- Remove duplicate nanosleep (already in pthread_time.h)
- Add fcntl.h include for _O_BINARY
- Use platform_pipe/fcntl to avoid naming conflicts
- All 22 tests pass on Linux
nodeinfo-routing-update
Evgeny 8 months ago
parent
commit
0688efcd0e
  1. 85
      lib/platform_compat.h

85
lib/platform_compat.h

@ -11,12 +11,15 @@
#include <windows.h> #include <windows.h>
#include <io.h> #include <io.h>
#include <time.h> #include <time.h>
#include <fcntl.h>
#include <stdint.h>
// POSIX functions missing on Windows // POSIX functions missing on Windows
#define strcasecmp _stricmp #define strcasecmp _stricmp
#define strncasecmp _strnicmp #define strncasecmp _strnicmp
// poll() implementation for Windows // poll() and pollfd are already defined in winsock2.h on MSYS2 UCRT64
// Only define if not already present
#ifndef POLLIN #ifndef POLLIN
#define POLLIN 0x0001 #define POLLIN 0x0001
#endif #endif
@ -36,19 +39,9 @@
#define POLLPRI 0x0002 #define POLLPRI 0x0002
#endif #endif
struct pollfd { // gettimeofday for Windows (timezone already defined in time.h)
int fd; #ifndef HAVE_GETTIMEOFDAY
short events; static inline int gettimeofday(struct timeval *tv, void *tz) {
short revents;
};
// gettimeofday for Windows
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
static inline int gettimeofday(struct timeval *tv, struct timezone *tz) {
(void)tz; (void)tz;
if (tv) { if (tv) {
FILETIME ft; FILETIME ft;
@ -62,56 +55,28 @@
} }
return 0; return 0;
} }
#endif
// Pipe creation for Windows // Pipe creation for Windows
static inline int pipe(int pipefd[2]) { static inline int platform_pipe(int pipefd[2]) {
return _pipe(pipefd, 4096, _O_BINARY); return _pipe(pipefd, 4096, _O_BINARY);
} }
#define pipe platform_pipe
// poll() using select() for Windows // nfds_t already defined in winsock2.h on MSYS2
static inline int poll(struct pollfd *fds, nfds_t nfds, int timeout) {
fd_set readfds, writefds, exceptfds;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
int max_fd = -1;
for (nfds_t i = 0; i < nfds; i++) {
if (fds[i].fd < 0) continue;
if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &readfds);
if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &writefds);
FD_SET(fds[i].fd, &exceptfds);
if (fds[i].fd > max_fd) max_fd = fds[i].fd;
}
struct timeval tv, *ptv = NULL;
if (timeout >= 0) {
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
ptv = &tv;
}
int ret = select(max_fd + 1, &readfds, &writefds, &exceptfds, ptv);
if (ret < 0) return ret;
// Set revents
for (nfds_t i = 0; i < nfds; i++) {
fds[i].revents = 0;
if (fds[i].fd < 0) continue;
if (FD_ISSET(fds[i].fd, &readfds)) fds[i].revents |= POLLIN;
if (FD_ISSET(fds[i].fd, &writefds)) fds[i].revents |= POLLOUT;
if (FD_ISSET(fds[i].fd, &exceptfds)) fds[i].revents |= POLLERR;
}
return ret;
}
// fcntl replacement for Windows (simplified - only supports F_GETFL/F_SETFL with O_NONBLOCK) // fcntl replacement for Windows (simplified - only supports F_GETFL/F_SETFL with O_NONBLOCK)
#define F_GETFL 3 #ifndef F_GETFL
#define F_SETFL 4 #define F_GETFL 3
#define O_NONBLOCK 0x4000 #endif
#ifndef F_SETFL
#define F_SETFL 4
#endif
#ifndef O_NONBLOCK
#define O_NONBLOCK 0x4000
#endif
static inline int fcntl(int fd, int cmd, ... /* arg */ ) { static inline int platform_fcntl(int fd, int cmd, ... /* arg */ ) {
va_list ap; va_list ap;
va_start(ap, cmd); va_start(ap, cmd);
@ -134,13 +99,9 @@
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
#define fcntl platform_fcntl
// nanosleep replacement // nanosleep already defined in pthread_time.h on MSYS2
static inline int nanosleep(const struct timespec *req, struct timespec *rem) {
(void)rem;
Sleep((DWORD)(req->tv_sec * 1000 + req->tv_nsec / 1000000));
return 0;
}
#else #else
// POSIX - include standard headers // POSIX - include standard headers

Loading…
Cancel
Save