+ +
a18b27f fix(windows): resolve Win32 Winsock, signal, and CLI test compatibility
saifmukhtar · 2026-11-24
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * include/compat/win32/unistd.h - POSIX unistd.h emulation for Windows MSVC |
| 7 | */ |
| 8 | |
| 9 | #ifndef OPENAVX_COMPAT_WIN32_UNISTD_H |
| 10 | #define OPENAVX_COMPAT_WIN32_UNISTD_H |
| 11 | |
| 12 | #if defined(__MINGW32__) || defined(__MINGW64__) |
| 13 | #include_next <unistd.h> |
| 14 | #endif |
| 15 | |
| 16 | #if defined(_WIN32) || defined(_MSC_VER) |
| 17 | |
| 18 | #ifndef WIN32_LEAN_AND_MEAN |
| 19 | #define WIN32_LEAN_AND_MEAN |
| 20 | #endif |
| 21 | #include <winsock2.h> |
| 22 | #include <windows.h> |
| 23 | #include <io.h> |
| 24 | #include <process.h> |
| 25 | #include <direct.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <time.h> |
| 29 | #include "compat/win32/pthread.h" |
| 30 | |
| 31 | #ifndef STDIN_FILENO |
| 32 | #define STDIN_FILENO 0 |
| 33 | #endif |
| 34 | #ifndef STDOUT_FILENO |
| 35 | #define STDOUT_FILENO 1 |
| 36 | #endif |
| 37 | #ifndef STDERR_FILENO |
| 38 | #define STDERR_FILENO 2 |
| 39 | #endif |
| 40 | |
| 41 | #ifndef R_OK |
| 42 | #define R_OK 4 |
| 43 | #endif |
| 44 | #ifndef W_OK |
| 45 | #define W_OK 2 |
| 46 | #endif |
| 47 | #ifndef X_OK |
| 48 | #define X_OK 0 |
| 49 | #endif |
| 50 | #ifndef F_OK |
| 51 | #define F_OK 0 |
| 52 | #endif |
| 53 | |
| 54 | #ifndef M_PI |
| 55 | #define M_PI 3.14159265358979323846 |
| 56 | #endif |
| 57 | |
| 58 | #ifndef MSG_DONTWAIT |
| 59 | #define MSG_DONTWAIT 0 |
| 60 | #endif |
| 61 | |
| 62 | #ifndef MSG_NOSIGNAL |
| 63 | #define MSG_NOSIGNAL 0 |
| 64 | #endif |
| 65 | |
| 66 | #if defined(_MSC_VER) |
| 67 | typedef unsigned int useconds_t; |
| 68 | typedef int pid_t; |
| 69 | #endif |
| 70 | |
| 71 | #ifndef WNOHANG |
| 72 | #define WNOHANG 1 |
| 73 | #endif |
| 74 | |
| 75 | #ifndef SIGKILL |
| 76 | #define SIGKILL 9 |
| 77 | #endif |
| 78 | |
| 79 | #ifndef WIFEXITED |
| 80 | #define WIFEXITED(s) 1 |
| 81 | #endif |
| 82 | #ifndef WEXITSTATUS |
| 83 | #define WEXITSTATUS(s) (s) |
| 84 | #endif |
| 85 | #ifndef WIFSIGNALED |
| 86 | #define WIFSIGNALED(s) 0 |
| 87 | #endif |
| 88 | #ifndef WTERMSIG |
| 89 | #define WTERMSIG(s) 0 |
| 90 | #endif |
| 91 | |
| 92 | #if defined(_MSC_VER) |
| 93 | #ifndef CLOCK_MONOTONIC |
| 94 | #define CLOCK_MONOTONIC 1 |
| 95 | #endif |
| 96 | #ifndef CLOCK_REALTIME |
| 97 | #define CLOCK_REALTIME 0 |
| 98 | #endif |
| 99 | |
| 100 | static inline int clock_gettime(int clk_id, struct timespec *tp) { |
| 101 | (void)clk_id; |
| 102 | if (!tp) return -1; |
| 103 | LARGE_INTEGER freq, count; |
| 104 | QueryPerformanceFrequency(&freq); |
| 105 | QueryPerformanceCounter(&count); |
| 106 | tp->tv_sec = (time_t)(count.QuadPart / freq.QuadPart); |
| 107 | tp->tv_nsec = (long)(((count.QuadPart % freq.QuadPart) * 1000000000ULL) / freq.QuadPart); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static inline int nanosleep(const struct timespec *req, struct timespec *rem) { |
| 112 | (void)rem; |
| 113 | if (!req) return -1; |
| 114 | DWORD ms = (DWORD)(req->tv_sec * 1000 + (req->tv_nsec + 999999) / 1000000); |
| 115 | Sleep(ms); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static inline int usleep(useconds_t us) { |
| 120 | Sleep((DWORD)((us + 999) / 1000)); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static inline unsigned int sleep(unsigned int sec) { |
| 125 | Sleep((DWORD)(sec * 1000)); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static inline int kill(pid_t pid, int sig) { |
| 130 | (void)pid; |
| 131 | (void)sig; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static inline pid_t waitpid(pid_t pid, int *status, int options) { |
| 136 | (void)status; |
| 137 | (void)options; |
| 138 | return pid; |
| 139 | } |
| 140 | |
| 141 | static inline unsigned int alarm(unsigned int sec) { |
| 142 | (void)sec; |
| 143 | return 0; |
| 144 | } |
| 145 | #endif /* _MSC_VER */ |
| 146 | |
| 147 | static inline char* mkdtemp(char *tmpl) { |
| 148 | if (!tmpl) return NULL; |
| 149 | char *p = _mktemp(tmpl); |
| 150 | if (!p) return NULL; |
| 151 | if (_mkdir(p) != 0) return NULL; |
| 152 | return p; |
| 153 | } |
| 154 | |
| 155 | static inline int setenv(const char *name, const char *value, int overwrite) { |
| 156 | if (!overwrite && getenv(name)) return 0; |
| 157 | return _putenv_s(name, value); |
| 158 | } |
| 159 | |
| 160 | static inline int unsetenv(const char *name) { |
| 161 | return _putenv_s(name, ""); |
| 162 | } |
| 163 | |
| 164 | #ifndef isatty |
| 165 | #define isatty _isatty |
| 166 | #endif |
| 167 | |
| 168 | #ifndef popen |
| 169 | #define popen _popen |
| 170 | #endif |
| 171 | #ifndef pclose |
| 172 | #define pclose _pclose |
| 173 | #endif |
| 174 | |
| 175 | #ifndef rmdir |
| 176 | #define rmdir _rmdir |
| 177 | #endif |
| 178 | #ifndef unlink |
| 179 | #define unlink _unlink |
| 180 | #endif |
| 181 | |
| 182 | static inline int win32_close_fd_or_socket(int fd) { |
| 183 | if (closesocket((SOCKET)fd) == 0) { |
| 184 | return 0; |
| 185 | } |
| 186 | return _close(fd); |
| 187 | } |
| 188 | #undef close |
| 189 | #define close(fd) win32_close_fd_or_socket(fd) |
| 190 | |
| 191 | #endif /* _WIN32 */ |
| 192 | |
| 193 | #endif /* OPENAVX_COMPAT_WIN32_UNISTD_H */ |