windows: use built-in _WIN32 macro to detect Windows

Windows compilers define `_WIN32` automatically. Windows SDK headers
or build env defines `WIN32`, or we have to take care of it. The
agreement seems to be that `_WIN32` is the preferred practice here.
Make the source code rely on that to detect we're building for Windows.

Public `curl.h` was using `WIN32`, `__WIN32__` and `CURL_WIN32` for
Windows detection, next to the official `_WIN32`. After this patch it
only uses `_WIN32` for this. Also, make it stop defining `CURL_WIN32`.

There is a slight chance these break compatibility with Windows
compilers that fail to define `_WIN32`. I'm not aware of any obsolete
or modern compiler affected, but in case there is one, one possible
solution is to define this macro manually.

grepping for `WIN32` remains useful to discover Windows-specific code.

Also:

- extend `checksrc` to ensure we're not using `WIN32` anymore.

- apply minor formatting here and there.

- delete unnecessary checks for `!MSDOS` when `_WIN32` is present.

Co-authored-by: Jay Satiro
Reviewed-by: Daniel Stenberg

Closes #12376
This commit is contained in:
Viktor Szakats 2023-11-21 16:54:49 +00:00
parent 1c37d472a0
commit e9a7d4a1c8
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
99 changed files with 223 additions and 227 deletions

View file

@ -60,7 +60,7 @@ curl_free_callback Curl_cfree = (curl_free_callback)free;
curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)strdup;
curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
#if defined(WIN32) && defined(UNICODE)
#if defined(_WIN32) && defined(UNICODE)
curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
#endif

View file

@ -1016,7 +1016,7 @@ int main(int argc, char *argv[])
msnprintf(loglockfile, sizeof(loglockfile), "%s/%s/mqtt-%s.lock",
logdir, SERVERLOGS_LOCKDIR, ipv_inuse);
#ifdef WIN32
#ifdef _WIN32
win32_init();
atexit(win32_cleanup);

View file

@ -102,7 +102,7 @@ int main(int argc, char *argv[])
return 1;
}
#ifdef WIN32
#ifdef _WIN32
win32_init();
atexit(win32_cleanup);
#endif

View file

@ -1150,7 +1150,7 @@ int main(int argc, char *argv[])
msnprintf(loglockfile, sizeof(loglockfile), "%s/%s/rtsp-%s.lock",
logdir, SERVERLOGS_LOCKDIR, ipv_inuse);
#ifdef WIN32
#ifdef _WIN32
win32_init();
atexit(win32_cleanup);
#endif

View file

@ -152,7 +152,7 @@ enum sockmode {
ACTIVE_DISCONNECT /* as a client, disconnected from server */
};
#ifdef WIN32
#ifdef _WIN32
/*
* read-wrapper to support reading from stdin on Windows.
*/
@ -1461,7 +1461,7 @@ int main(int argc, char *argv[])
}
}
#ifdef WIN32
#ifdef _WIN32
win32_init();
atexit(win32_cleanup);

View file

@ -1076,7 +1076,7 @@ int main(int argc, char *argv[])
}
}
#ifdef WIN32
#ifdef _WIN32
win32_init();
atexit(win32_cleanup);

View file

@ -2119,7 +2119,7 @@ int main(int argc, char *argv[])
logdir, SERVERLOGS_LOCKDIR, protocol_type,
is_proxy ? "-proxy" : "", socket_type);
#ifdef WIN32
#ifdef _WIN32
win32_init();
atexit(win32_cleanup);
#endif

View file

@ -453,7 +453,7 @@ static ssize_t write_behind(struct testcase *test, int convert)
if(!test->ofile) {
char outfile[256];
msnprintf(outfile, sizeof(outfile), "%s/upload.%ld", logdir, test->testno);
#ifdef WIN32
#ifdef _WIN32
test->ofile = open(outfile, O_CREAT|O_RDWR|O_BINARY, 0777);
#else
test->ofile = open(outfile, O_CREAT|O_RDWR, 0777);
@ -642,7 +642,7 @@ int main(int argc, char **argv)
msnprintf(loglockfile, sizeof(loglockfile), "%s/%s/tftp-%s.lock",
logdir, SERVERLOGS_LOCKDIR, ipv_inuse);
#ifdef WIN32
#ifdef _WIN32
win32_init();
atexit(win32_cleanup);
#endif

View file

@ -144,7 +144,7 @@ void logmsg(const char *msg, ...)
}
}
#ifdef WIN32
#ifdef _WIN32
/* use instead of strerror() on generic Windows */
static const char *win32_strerror(int err, char *buf, size_t buflen)
{
@ -208,7 +208,7 @@ const char *sstrerror(int err)
static char buf[512];
return win32_strerror(err, buf, sizeof(buf));
}
#endif /* WIN32 */
#endif /* _WIN32 */
/* set by the main code to point to where the test dir is */
const char *path = ".";
@ -292,7 +292,7 @@ curl_off_t our_getpid(void)
curl_off_t pid;
pid = (curl_off_t)getpid();
#if defined(WIN32) || defined(_WIN32)
#if defined(_WIN32) || defined(_WIN32)
/* store pid + 65536 to avoid conflict with Cygwin/msys PIDs, see also:
* - https://cygwin.com/git/?p=newlib-cygwin.git;a=commit; ↵
* h=b5e1003722cb14235c4f166be72c09acdffc62ea
@ -378,7 +378,7 @@ void clear_advisor_read_lock(const char *filename)
}
#if defined(WIN32) && !defined(MSDOS)
#if defined(_WIN32) && !defined(MSDOS)
static struct timeval tvnow(void)
{
@ -501,11 +501,11 @@ static SIGHANDLER_T old_sigint_handler = SIG_ERR;
static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
#endif
#if defined(SIGBREAK) && defined(WIN32)
#if defined(SIGBREAK) && defined(_WIN32)
static SIGHANDLER_T old_sigbreak_handler = SIG_ERR;
#endif
#ifdef WIN32
#ifdef _WIN32
#ifdef _WIN32_WCE
static DWORD thread_main_id = 0;
#else
@ -521,7 +521,7 @@ volatile int got_exit_signal = 0;
/* if next is set indicates the first signal handled in exit_signal_handler */
volatile int exit_signal = 0;
#ifdef WIN32
#ifdef _WIN32
/* event which if set indicates that the program should finish */
HANDLE exit_event = NULL;
#endif
@ -538,7 +538,7 @@ static void exit_signal_handler(int signum)
if(got_exit_signal == 0) {
got_exit_signal = 1;
exit_signal = signum;
#ifdef WIN32
#ifdef _WIN32
if(exit_event)
(void)SetEvent(exit_event);
#endif
@ -547,7 +547,7 @@ static void exit_signal_handler(int signum)
errno = old_errno;
}
#ifdef WIN32
#ifdef _WIN32
/* CTRL event handler for Windows Console applications to simulate
* SIGINT, SIGTERM and SIGBREAK on CTRL events and trigger signal handler.
*
@ -698,7 +698,7 @@ static SIGHANDLER_T set_signal(int signum, SIGHANDLER_T handler,
void install_signal_handlers(bool keep_sigalrm)
{
#ifdef WIN32
#ifdef _WIN32
#ifdef _WIN32_WCE
typedef HANDLE curl_win_thread_handle_t;
#else
@ -744,13 +744,13 @@ void install_signal_handlers(bool keep_sigalrm)
if(old_sigterm_handler == SIG_ERR)
logmsg("cannot install SIGTERM handler: %s", strerror(errno));
#endif
#if defined(SIGBREAK) && defined(WIN32)
#if defined(SIGBREAK) && defined(_WIN32)
/* handle SIGBREAK signal with our exit_signal_handler */
old_sigbreak_handler = set_signal(SIGBREAK, exit_signal_handler, TRUE);
if(old_sigbreak_handler == SIG_ERR)
logmsg("cannot install SIGBREAK handler: %s", strerror(errno));
#endif
#ifdef WIN32
#ifdef _WIN32
if(!SetConsoleCtrlHandler(ctrl_event_handler, TRUE))
logmsg("cannot install CTRL event handler");
#ifdef _WIN32_WCE
@ -792,11 +792,11 @@ void restore_signal_handlers(bool keep_sigalrm)
if(SIG_ERR != old_sigterm_handler)
(void) set_signal(SIGTERM, old_sigterm_handler, FALSE);
#endif
#if defined(SIGBREAK) && defined(WIN32)
#if defined(SIGBREAK) && defined(_WIN32)
if(SIG_ERR != old_sigbreak_handler)
(void) set_signal(SIGBREAK, old_sigbreak_handler, FALSE);
#endif
#ifdef WIN32
#ifdef _WIN32
(void)SetConsoleCtrlHandler(ctrl_event_handler, FALSE);
if(thread_main_window && thread_main_id) {
if(PostThreadMessage(thread_main_id, WM_APP, 0, 0)) {
@ -846,7 +846,7 @@ int bind_unix_socket(curl_socket_t sock, const char *unix_socket,
return rc;
}
/* socket server is not alive, now check if it was actually a socket. */
#ifdef WIN32
#ifdef _WIN32
/* Windows does not have lstat function. */
rc = curlx_win32_stat(unix_socket, &statbuf);
#else

View file

@ -41,7 +41,7 @@ extern const char *serverlogfile;
extern const char *cmdfile;
#ifdef WIN32
#ifdef _WIN32
#include <process.h>
#include <fcntl.h>
@ -54,10 +54,10 @@ void win32_perror(const char *msg);
void win32_init(void);
void win32_cleanup(void);
const char *sstrerror(int err);
#else /* WIN32 */
#else /* _WIN32 */
#define sstrerror(e) strerror(e)
#endif /* WIN32 */
#endif /* _WIN32 */
/* fopens the test case file */
FILE *test2fopen(long testno, const char *logdir);
@ -75,7 +75,7 @@ extern volatile int got_exit_signal;
/* global variable which if set indicates the first signal handled */
extern volatile int exit_signal;
#ifdef WIN32
#ifdef _WIN32
/* global event which if set indicates that the program should finish */
extern HANDLE exit_event;
#endif