From 985a62d11802b44d1d1049ddd692038923da6f0b Mon Sep 17 00:00:00 2001 From: Mark Adler Date: Thu, 8 Feb 2024 17:35:23 -0800 Subject: [PATCH] Address Microsoft deprecation warnings. --- contrib/puff/pufftest.c | 2 +- examples/gznorm.c | 2 +- examples/zpipe.c | 2 +- gzguts.h | 9 +++++++-- gzlib.c | 17 ++++++++++------- test/minigzip.c | 6 +++++- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/contrib/puff/pufftest.c b/contrib/puff/pufftest.c index 5f72ecc..328cb9f 100644 --- a/contrib/puff/pufftest.c +++ b/contrib/puff/pufftest.c @@ -23,7 +23,7 @@ #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) # include # include -# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) +# define SET_BINARY_MODE(file) _setmode(_fileno(file), O_BINARY) #else # define SET_BINARY_MODE(file) #endif diff --git a/examples/gznorm.c b/examples/gznorm.c index 68e0a0f..2a1dda8 100644 --- a/examples/gznorm.c +++ b/examples/gznorm.c @@ -24,7 +24,7 @@ #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) # include # include -# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) +# define SET_BINARY_MODE(file) _setmode(_fileno(file), O_BINARY) #else # define SET_BINARY_MODE(file) #endif diff --git a/examples/zpipe.c b/examples/zpipe.c index 83535d1..184fce5 100644 --- a/examples/zpipe.c +++ b/examples/zpipe.c @@ -20,7 +20,7 @@ #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) # include # include -# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) +# define SET_BINARY_MODE(file) _setmode(_fileno(file), O_BINARY) #else # define SET_BINARY_MODE(file) #endif diff --git a/gzguts.h b/gzguts.h index 074ec84..a909e33 100644 --- a/gzguts.h +++ b/gzguts.h @@ -17,6 +17,10 @@ # define ZLIB_INTERNAL #endif +#if defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS) +# define _CRT_SECURE_NO_WARNINGS +#endif + #include #include "zlib.h" #ifdef STDC @@ -36,13 +40,14 @@ #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) # include +# include #endif -#if defined(_WIN32) +#if defined(_WIN32) && !defined(WIDECHAR) # define WIDECHAR #endif -#ifdef WINAPI_FAMILY +#if defined(_WIN32) || defined(WINAPI_FAMILY) # define open _open # define read _read # define write _write diff --git a/gzlib.c b/gzlib.c index 983153c..05a9ae5 100644 --- a/gzlib.c +++ b/gzlib.c @@ -52,7 +52,8 @@ char ZLIB_INTERNAL *gz_strwinerror(DWORD error) { msgbuf[chars] = 0; } - wcstombs(buf, msgbuf, chars + 1); + z_size_t len; + wcstombs_s(&len, buf, sizeof(buf), msgbuf, chars + 1); LocalFree(msgbuf); } else { @@ -180,8 +181,7 @@ local gzFile gz_open(const void *path, int fd, const char *mode) { /* save the path name for error messages */ #ifdef WIDECHAR if (fd == -2) { - len = wcstombs(NULL, path, 0); - if (len == (z_size_t)-1) + if (wcstombs_s(&len, NULL, 0, path, 0) != 0) len = 0; } else @@ -195,7 +195,7 @@ local gzFile gz_open(const void *path, int fd, const char *mode) { #ifdef WIDECHAR if (fd == -2) if (len) - wcstombs(state->path, path, len + 1); + wcstombs_s(&len, state->path, len + 1, path, len + 1); else *(state->path) = 0; else @@ -228,11 +228,14 @@ local gzFile gz_open(const void *path, int fd, const char *mode) { O_APPEND))); /* open the file with the appropriate flags (or just use fd) */ - state->fd = fd > -1 ? fd : ( + if (fd == -1) + state->fd = open((const char *)path, oflag, 0666); #ifdef WIDECHAR - fd == -2 ? _wopen(path, oflag, 0666) : + else if (fd == -2) + _wsopen_s(&state->fd, path, oflag, _SH_DENYNO, _S_IREAD | _S_IWRITE); #endif - open((const char *)path, oflag, 0666)); + else + state->fd = fd; if (state->fd == -1) { free(state->path); free(state); diff --git a/test/minigzip.c b/test/minigzip.c index aa787fc..d5173b5 100644 --- a/test/minigzip.c +++ b/test/minigzip.c @@ -39,7 +39,7 @@ # ifdef UNDER_CE # include # endif -# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) +# define SET_BINARY_MODE(file) _setmode(_fileno(file), O_BINARY) #else # define SET_BINARY_MODE(file) #endif @@ -58,6 +58,10 @@ #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os # include /* for fileno */ #endif +#ifdef WIN32 +# define fileno _fileno +# define unlink _unlink +#endif #if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE) #ifndef WIN32 /* unlink already in stdio.h for WIN32 */