Skip to content

Commit

Permalink
Fix tar -w on Windows (libarchive#2219)
Browse files Browse the repository at this point in the history
The tar utility reads from stderr to receive user input even when stdin
is a pipe. That is unfortunately unsupported on Windows.

The nearest equivalent is to reopen and read from the console input
handle.

Closes libarchive#2215
  • Loading branch information
DHowett authored Jun 4, 2024
1 parent f3a5248 commit 6c467b7
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tar/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ yes(const char *fmt, ...)
char buff[32];
char *p;
ssize_t l;
int read_fd = 2; /* stderr */

va_list ap;
va_start(ap, fmt);
Expand All @@ -242,7 +243,24 @@ yes(const char *fmt, ...)
fprintf(stderr, " (y/N)? ");
fflush(stderr);

l = read(2, buff, sizeof(buff) - 1);
#if defined(_WIN32) && !defined(__CYGWIN__)
/* To be resilient when stdin is a pipe, bsdtar prefers to read from
* stderr. On Windows, stderr cannot be read. The nearest "piping
* resilient" equivalent is reopening the console input handle.
*/
read_fd = _open("CONIN$", O_RDONLY);
if (read_fd < 0) {
fprintf(stderr, "Keyboard read failed\n");
exit(1);
}
#endif

l = read(read_fd, buff, sizeof(buff) - 1);

#if defined(_WIN32) && !defined(__CYGWIN__)
_close(read_fd);
#endif

if (l < 0) {
fprintf(stderr, "Keyboard read failed\n");
exit(1);
Expand Down

0 comments on commit 6c467b7

Please sign in to comment.