Skip to content

Commit 514d654

Browse files
committed
Ensure O_BINARY is used in the cli tool for Windows
1 parent 5c83b74 commit 514d654

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/main.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ extern int deflateEnd(z_stream *strm);
4646
#define O_NOFOLLOW 0
4747
#endif
4848

49+
/* Ensure O_BINARY is available for binary-safe extraction on Windows. */
50+
#ifndef O_BINARY
51+
#ifdef _O_BINARY
52+
#define O_BINARY _O_BINARY
53+
#else
54+
#define O_BINARY 0
55+
#endif
56+
#endif
57+
4958
#ifdef _WIN32
5059
#include <direct.h>
5160
#include <io.h>
@@ -490,7 +499,7 @@ static int extract_all(const char *path) {
490499
* stdio buffering issues. */
491500
int fd = -1;
492501
int open_flags = O_WRONLY | O_CREAT | O_EXCL;
493-
fd = open (fname_sanitized, open_flags, desired_mode);
502+
fd = open (fname_sanitized, open_flags | O_BINARY, desired_mode);
494503
if (fd < 0) {
495504
if (errno == EEXIST) {
496505
if (!g_force) {
@@ -504,7 +513,7 @@ static int extract_all(const char *path) {
504513
zip_fclose (zf);
505514
continue;
506515
}
507-
fd = open (fname_sanitized, O_WRONLY | O_TRUNC | O_NOFOLLOW);
516+
fd = open (fname_sanitized, O_WRONLY | O_TRUNC | O_NOFOLLOW | O_BINARY);
508517
if (fd < 0) {
509518
fprintf (stderr, "Cannot open for overwrite %s: %s\n", fname_sanitized, strerror (errno));
510519
zip_fclose (zf);

0 commit comments

Comments
 (0)