Skip to content

Commit

Permalink
Address Microsoft deprecation warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
madler committed Feb 9, 2024
1 parent 504403f commit 985a62d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion contrib/puff/pufftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# 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
Expand Down
2 changes: 1 addition & 1 deletion examples/gznorm.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# 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
Expand Down
2 changes: 1 addition & 1 deletion examples/zpipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# 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
Expand Down
9 changes: 7 additions & 2 deletions gzguts.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
# define ZLIB_INTERNAL
#endif

#if defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS)
# define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include "zlib.h"
#ifdef STDC
Expand All @@ -36,13 +40,14 @@

#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
# include <io.h>
# include <sys/stat.h>
#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
Expand Down
17 changes: 10 additions & 7 deletions gzlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion test/minigzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# ifdef UNDER_CE
# include <stdlib.h>
# 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
Expand All @@ -58,6 +58,10 @@
#if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
# include <unix.h> /* 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 */
Expand Down

0 comments on commit 985a62d

Please sign in to comment.