From 20011808a19ebb63334612b7576ff09d3ee8ff98 Mon Sep 17 00:00:00 2001 From: KO Myung-Hun Date: Mon, 24 Nov 2014 12:01:23 +0900 Subject: [PATCH] tar: set stdin/stdout to binary mode correctly * src/buffer.c (_open_archive): Ensure that stdin is binary mode before check_compressed_archive(). Set stdout to binary mode if writing archives to stdout. * src/extract.c (extract_file): Set stdout to binary mode if writing to stdout. * src/misc.c (xpipe): Set pipes to binary mode * src/system.c (sys_child_open_for_compress): Set stdout to binary mode. (sys_child_open_for_uncompress): Set stdin to binary mode. --- src/buffer.c | 2 ++ src/extract.c | 5 ++++- src/misc.c | 3 +++ src/system.c | 10 ++++++++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/buffer.c b/src/buffer.c index a7d8971..171c872 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -748,6 +748,7 @@ _open_archive (enum access_mode wanted_access) enum compress_type type; archive = STDIN_FILENO; + SET_BINARY_MODE (archive); type = check_compressed_archive (&shortfile); if (type != ct_tar && type != ct_none) FATAL_ERROR ((0, 0, @@ -767,6 +768,7 @@ _open_archive (enum access_mode wanted_access) case ACCESS_UPDATE: archive = STDIN_FILENO; write_archive_to_stdout = true; + SET_BINARY_MODE (STDOUT_FILENO); record_end = record_start; /* set up for 1st record = # 0 */ if (!index_file_name) stdlis = stderr; diff --git a/src/extract.c b/src/extract.c index ca25603..91b1989 100644 --- a/src/extract.c +++ b/src/extract.c @@ -1076,7 +1076,10 @@ extract_file (char *file_name, int typeflag) mode_t current_mode_mask = 0; if (to_stdout_option) - fd = STDOUT_FILENO; + { + fd = STDOUT_FILENO; + SET_BINARY_MODE (fd); + } else if (to_command_option) { fd = sys_exec_command (file_name, 'f', ¤t_stat_info); diff --git a/src/misc.c b/src/misc.c index 8e66643..7f89871 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1129,6 +1129,9 @@ xpipe (int fd[2]) { if (pipe (fd) < 0) call_arg_fatal ("pipe", _("interprocess channel")); + + SET_BINARY_MODE (fd[0]); + SET_BINARY_MODE (fd[1]); } /* Return PTR, aligned upward to the next multiple of ALIGNMENT. diff --git a/src/system.c b/src/system.c index 9414233..7a03748 100644 --- a/src/system.c +++ b/src/system.c @@ -405,7 +405,10 @@ sys_child_open_for_compress (void) xclose (child_pipe[PWRITE]); if (strcmp (archive_name_array[0], "-") == 0) - archive = STDOUT_FILENO; + { + archive = STDOUT_FILENO; + SET_BINARY_MODE (archive); + } else { archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option); @@ -575,7 +578,10 @@ sys_child_open_for_uncompress (void) xclose (child_pipe[PREAD]); if (strcmp (archive_name_array[0], "-") == 0) - archive = STDIN_FILENO; + { + archive = STDIN_FILENO; + SET_BINARY_MODE (archive); + } else archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW, rsh_command_option); -- 1.8.5.2