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

@ -41,7 +41,7 @@
static char *parse_filename(const char *ptr, size_t len);
#ifdef WIN32
#ifdef _WIN32
#define BOLD "\x1b[1m"
#define BOLDOFF "\x1b[22m"
#else
@ -87,7 +87,7 @@ size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
}
#endif
#ifdef WIN32
#ifdef _WIN32
/* Discard incomplete UTF-8 sequence buffered from body */
if(outs->utf8seq[0])
memset(outs->utf8seq, 0, sizeof(outs->utf8seq));
@ -213,7 +213,7 @@ size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
return CURL_WRITEFUNC_ERROR;
if(hdrcbdata->global->isatty &&
#ifdef WIN32
#ifdef _WIN32
tool_term_has_bold &&
#endif
hdrcbdata->global->styled_output)
@ -304,7 +304,7 @@ static char *parse_filename(const char *ptr, size_t len)
if(copy != p)
memmove(copy, p, strlen(p) + 1);
#if defined(MSDOS) || defined(WIN32)
#if defined(_WIN32) || defined(MSDOS)
{
char *sanitized;
SANITIZEcode sc = sanitize_file_name(&sanitized, copy, 0);
@ -313,7 +313,7 @@ static char *parse_filename(const char *ptr, size_t len)
return NULL;
copy = sanitized;
}
#endif /* MSDOS || WIN32 */
#endif /* _WIN32 || MSDOS */
/* in case we built debug enabled, we allow an environment variable
* named CURL_TESTDIR to prefix the given file name to put it into a

View file

@ -249,7 +249,7 @@ void progressbarinit(struct ProgressData *bar,
struct winsize ts;
if(!ioctl(STDIN_FILENO, TIOCGWINSZ, &ts))
cols = ts.ws_col;
#elif defined(WIN32)
#elif defined(_WIN32)
{
HANDLE stderr_hnd = GetStdHandle(STD_ERROR_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO console_info;

View file

@ -62,7 +62,7 @@ size_t tool_read_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
if(msdelta > config->timeout_ms)
/* timeout */
return 0;
#ifndef WIN32
#ifndef _WIN32
/* this logic waits on read activity on a file descriptor that is not a
socket which makes it not work with select() on Windows */
else {

View file

@ -25,7 +25,7 @@
***************************************************************************/
#include "tool_setup.h"
#if defined(WIN32) && !defined(HAVE_FTRUNCATE)
#if defined(_WIN32) && !defined(HAVE_FTRUNCATE)
int tool_ftruncate64(int fd, curl_off_t where);
@ -35,7 +35,7 @@ int tool_ftruncate64(int fd, curl_off_t where);
#define HAVE_FTRUNCATE 1
#define USE_TOOL_FTRUNCATE 1
#endif /* WIN32 && ! HAVE_FTRUNCATE */
#endif /* _WIN32 && ! HAVE_FTRUNCATE */
/*
** callback for CURLOPT_SEEKFUNCTION

View file

@ -44,7 +44,7 @@
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifdef WIN32
#ifdef _WIN32
#define OPENMODE S_IREAD | S_IWRITE
#else
#define OPENMODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
@ -159,7 +159,7 @@ size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
struct OperationConfig *config = per->config;
size_t bytes = sz * nmemb;
bool is_tty = config->global->isatty;
#ifdef WIN32
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO console_info;
intptr_t fhnd;
#endif
@ -231,7 +231,7 @@ size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
}
}
#ifdef WIN32
#ifdef _WIN32
fhnd = _get_osfhandle(fileno(outs->stream));
/* if windows console then UTF-8 must be converted to UTF-16 */
if(isatty(fileno(outs->stream)) &&

View file

@ -25,7 +25,7 @@
#include <sys/stat.h>
#ifdef WIN32
#ifdef _WIN32
# include <direct.h>
#endif
@ -38,7 +38,7 @@
#include "memdebug.h" /* keep this as LAST include */
#if defined(WIN32) || (defined(MSDOS) && !defined(__DJGPP__))
#if defined(_WIN32) || (defined(MSDOS) && !defined(__DJGPP__))
# define mkdir(x,y) (mkdir)((x))
# ifndef F_OK
# define F_OK 0
@ -88,7 +88,7 @@ static void show_dir_errno(struct GlobalConfig *global, const char *name)
* should create all the dir* automagically
*/
#if defined(WIN32) || defined(__DJGPP__)
#if defined(_WIN32) || defined(__DJGPP__)
/* systems that may use either or when specifying a path */
#define PATH_DELIMITERS "\\/"
#else
@ -132,7 +132,7 @@ CURLcode create_dir_hierarchy(const char *outfile, struct GlobalConfig *global)
msnprintf(&dirbuildup[dlen], outlen - dlen, "%s%s", DIR_CHAR, tempdir);
else {
if(outdup == tempdir) {
#if defined(MSDOS) || defined(WIN32)
#if defined(_WIN32) || defined(MSDOS)
/* Skip creating a drive's current directory.
It may seem as though that would harmlessly fail but it could be
a corner case if X: did not exist, since we would be creating it

View file

@ -23,13 +23,13 @@
***************************************************************************/
#include "tool_setup.h"
#if defined(MSDOS) || defined(WIN32)
#if defined(_WIN32) || defined(MSDOS)
#if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
# include <libgen.h>
#endif
#ifdef WIN32
#ifdef _WIN32
# include <stdlib.h>
# include <tlhelp32.h>
# include "tool_cfgable.h"
@ -42,7 +42,7 @@
#include "curlx.h"
#include "memdebug.h" /* keep this as LAST include */
#ifdef WIN32
#ifdef _WIN32
# undef PATH_MAX
# define PATH_MAX MAX_PATH
#endif
@ -55,7 +55,7 @@
# endif
#endif
#ifdef WIN32
#ifdef _WIN32
# define _use_lfn(f) (1) /* long file names always available */
#elif !defined(__DJGPP__) || (__DJGPP__ < 2) /* DJGPP 2.0 has _use_lfn() */
# define _use_lfn(f) (0) /* long file names never available */
@ -597,7 +597,7 @@ char **__crt0_glob_function(char *arg)
#endif /* MSDOS && (__DJGPP__ || __GO32__) */
#ifdef WIN32
#ifdef _WIN32
/*
* Function to find CACert bundle on a Win32 platform using SearchPath.
@ -791,6 +791,6 @@ CURLcode win32_init(void)
return CURLE_OK;
}
#endif /* WIN32 */
#endif /* _WIN32 */
#endif /* MSDOS || WIN32 */
#endif /* _WIN32 || MSDOS */

View file

@ -25,7 +25,7 @@
***************************************************************************/
#include "tool_setup.h"
#if defined(MSDOS) || defined(WIN32)
#if defined(_WIN32) || defined(MSDOS)
#define SANITIZE_ALLOW_COLONS (1<<0) /* Allow colons */
#define SANITIZE_ALLOW_PATH (1<<1) /* Allow path separators and colons */
@ -57,7 +57,7 @@ char **__crt0_glob_function(char *arg);
#endif /* MSDOS && (__DJGPP__ || __GO32__) */
#ifdef WIN32
#ifdef _WIN32
CURLcode FindWin32CACert(struct OperationConfig *config,
curl_sslbackend backend,
@ -65,8 +65,8 @@ CURLcode FindWin32CACert(struct OperationConfig *config,
struct curl_slist *GetLoadedModulePaths(void);
CURLcode win32_init(void);
#endif /* WIN32 */
#endif /* _WIN32 */
#endif /* MSDOS || WIN32 */
#endif /* _WIN32 || MSDOS */
#endif /* HEADER_CURL_TOOL_DOSWIN_H */

View file

@ -41,7 +41,7 @@ int getfiletime(const char *filename, struct GlobalConfig *global,
/* Windows stat() may attempt to adjust the unix GMT file time by a daylight
saving time offset and since it's GMT that is bad behavior. When we have
access to a 64-bit type we can bypass stat and get the times directly. */
#if defined(WIN32)
#if defined(_WIN32)
HANDLE hfile;
TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
@ -87,7 +87,7 @@ int getfiletime(const char *filename, struct GlobalConfig *global,
return rc;
}
#if defined(HAVE_UTIME) || defined(HAVE_UTIMES) || defined(WIN32)
#if defined(HAVE_UTIME) || defined(HAVE_UTIMES) || defined(_WIN32)
void setfiletime(curl_off_t filetime, const char *filename,
struct GlobalConfig *global)
{
@ -95,7 +95,7 @@ void setfiletime(curl_off_t filetime, const char *filename,
/* Windows utime() may attempt to adjust the unix GMT file time by a daylight
saving time offset and since it's GMT that is bad behavior. When we have
access to a 64-bit type we can bypass utime and set the times directly. */
#if defined(WIN32)
#if defined(_WIN32)
HANDLE hfile;
TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
@ -153,4 +153,4 @@ void setfiletime(curl_off_t filetime, const char *filename,
}
}
#endif /* defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
defined(WIN32) */
defined(_WIN32) */

View file

@ -31,12 +31,12 @@ int getfiletime(const char *filename, struct GlobalConfig *global,
curl_off_t *stamp);
#if defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
(defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8))
(defined(_WIN32) && (SIZEOF_CURL_OFF_T >= 8))
void setfiletime(curl_off_t filetime, const char *filename,
struct GlobalConfig *global);
#else
#define setfiletime(a,b,c) Curl_nop_stmt
#endif /* defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
(defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8)) */
(defined(_WIN32) && (SIZEOF_CURL_OFF_T >= 8)) */
#endif /* HEADER_CURL_TOOL_FILETIME_H */

View file

@ -53,7 +53,7 @@ static const struct finder conf_list[] = {
{ "CURL_HOME", NULL, FALSE },
{ "XDG_CONFIG_HOME", NULL, FALSE }, /* index == 1, used in the code */
{ "HOME", NULL, FALSE },
#ifdef WIN32
#ifdef _WIN32
{ "USERPROFILE", NULL, FALSE },
{ "APPDATA", NULL, FALSE },
{ "USERPROFILE", "\\Application Data", FALSE},

View file

@ -25,7 +25,7 @@
***************************************************************************/
#include "tool_setup.h"
#ifdef WIN32
#ifdef _WIN32
#define CURLRC_DOTSCORE 2 /* look for underscore-prefixed name too */
#else
#define CURLRC_DOTSCORE 1 /* regular .curlrc check */

View file

@ -437,7 +437,7 @@ void parse_cert_parameter(const char *cert_parameter,
needs to work. In order not to break compatibility, we still use : as
separator, but we try to detect when it is used for a file name! On
windows. */
#ifdef WIN32
#ifdef _WIN32
if((param_place == &cert_parameter[1]) &&
(cert_parameter[2] == '\\' || cert_parameter[2] == '/') &&
(ISALPHA(cert_parameter[0])) ) {

View file

@ -46,7 +46,7 @@
# include iodef
#endif
#ifdef WIN32
#ifdef _WIN32
# include <conio.h>
#endif
@ -94,7 +94,7 @@ char *getpass_r(const char *prompt, char *buffer, size_t buflen)
#define DONE
#endif /* __VMS */
#if defined(WIN32)
#if defined(_WIN32)
char *getpass_r(const char *prompt, char *buffer, size_t buflen)
{
@ -122,7 +122,7 @@ char *getpass_r(const char *prompt, char *buffer, size_t buflen)
return buffer; /* we always return success */
}
#define DONE
#endif /* WIN32 */
#endif /* _WIN32 */
#ifndef DONE /* not previously provided */

View file

@ -25,7 +25,7 @@
#include <sys/stat.h>
#ifdef WIN32
#ifdef _WIN32
#include <tchar.h>
#endif
@ -235,7 +235,7 @@ int main(int argc, char *argv[])
tool_init_stderr();
#ifdef WIN32
#ifdef _WIN32
/* Undocumented diagnostic option to list the full paths of all loaded
modules. This is purposely pre-init. */
if(argc == 2 && !_tcscmp(argv[1], _T("--dump-module-paths"))) {
@ -276,7 +276,7 @@ int main(int argc, char *argv[])
main_free(&global);
}
#ifdef WIN32
#ifdef _WIN32
/* Flush buffers of all streams opened in write or update mode */
fflush(NULL);
#endif

View file

@ -463,7 +463,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
}
}
#ifdef WIN32
#ifdef _WIN32
/* Discard incomplete UTF-8 sequence buffered from body */
if(outs->utf8seq[0])
memset(outs->utf8seq, 0, sizeof(outs->utf8seq));
@ -2636,7 +2636,7 @@ static CURLcode transfer_per_config(struct GlobalConfig *global,
if(env)
curl_free(env);
#ifdef WIN32
#ifdef _WIN32
else {
result = FindWin32CACert(config, tls_backend_info->backend,
TEXT("curl-ca-bundle.crt"));

View file

@ -215,7 +215,7 @@ CURLcode get_url_file_name(char **filename, const char *url)
if(!*filename)
return CURLE_OUT_OF_MEMORY;
#if defined(MSDOS) || defined(WIN32)
#if defined(_WIN32) || defined(MSDOS)
{
char *sanitized;
SANITIZEcode sc = sanitize_file_name(&sanitized, *filename, 0);
@ -227,7 +227,7 @@ CURLcode get_url_file_name(char **filename, const char *url)
}
*filename = sanitized;
}
#endif /* MSDOS || WIN32 */
#endif /* _WIN32 || MSDOS */
/* in case we built debug enabled, we allow an environment variable
* named CURL_TESTDIR to prefix the given file name to put it into a

View file

@ -46,7 +46,7 @@ static const char *unslashquote(const char *line, char *param);
#define MAX_CONFIG_LINE_LENGTH (10*1024*1024)
static bool my_get_line(FILE *fp, struct curlx_dynbuf *, bool *error);
#ifdef WIN32
#ifdef _WIN32
static FILE *execpath(const char *filename, char **pathp)
{
static char filebuffer[512];
@ -98,7 +98,7 @@ int parseconfig(const char *filename, struct GlobalConfig *global)
}
filename = pathalloc = curlrc;
}
#ifdef WIN32 /* Windows */
#ifdef _WIN32 /* Windows */
else {
char *fullp;
/* check for .curlrc then _curlrc in the dir of the executable */

View file

@ -71,7 +71,7 @@ struct OutStruct {
FILE *stream;
curl_off_t bytes;
curl_off_t init;
#ifdef WIN32
#ifdef _WIN32
unsigned char utf8seq[5];
#endif
};

View file

@ -66,7 +66,7 @@ extern FILE *tool_stderr;
# include "tool_strdup.h"
#endif
#if defined(WIN32) && !defined(MSDOS)
#if defined(_WIN32)
/* set in win32_init() */
extern LARGE_INTEGER tool_freq;
extern bool tool_isVistaOrGreater;

View file

@ -47,7 +47,7 @@ void tool_go_sleep(long ms)
{
#if defined(MSDOS)
delay(ms);
#elif defined(WIN32)
#elif defined(_WIN32)
Sleep(ms);
#elif defined(HAVE_POLL_FINE)
(void)poll((void *)0, 0, (int)ms);

View file

@ -702,7 +702,7 @@ CURLcode glob_match_url(char **result, char *filename, struct URLGlob *glob)
if(curlx_dyn_addn(&dyn, "", 0))
return CURLE_OUT_OF_MEMORY;
#if defined(MSDOS) || defined(WIN32)
#if defined(_WIN32) || defined(MSDOS)
{
char *sanitized;
SANITIZEcode sc = sanitize_file_name(&sanitized, curlx_dyn_ptr(&dyn),
@ -717,5 +717,5 @@ CURLcode glob_match_url(char **result, char *filename, struct URLGlob *glob)
#else
*result = curlx_dyn_ptr(&dyn);
return CURLE_OK;
#endif /* MSDOS || WIN32 */
#endif /* _WIN32 || MSDOS */
}

View file

@ -31,7 +31,7 @@
#include "memdebug.h" /* keep this as LAST include */
#if defined(WIN32) && !defined(MSDOS)
#if defined(_WIN32)
/* In case of bug fix this function has a counterpart in timeval.c */
struct timeval tvnow(void)