--- cat.c.orig 2001-12-01 12:29:26.000000000 -0500 +++ cat.c 2004-08-23 19:28:54.000000000 -0400 @@ -618,7 +618,6 @@ main (int argc, char **argv) #if O_BINARY case 'B': - ++options; binary_files = 1; break; #endif @@ -676,9 +675,18 @@ main (int argc, char **argv) -b, -s and -E would surprise users on DOS/Windows where a line with only CR-LF is an empty line. (Besides, if they ask for one of these options, they don't care much about the original - file contents anyway). */ + file contents anyway). + Another exception is that we don't set the output to binary + when the only input is a tty on stdin. + This is so that "cat > xyzzy" can create a DOS-style text file, + like people expect if it is the default. + */ + if ((!isatty (STDOUT_FILENO) - && !(numbers || squeeze_empty_lines || mark_line_ends)) + && !(isatty (STDIN_FILENO) + && (optind == argc + || (optind == (argc - 1) && !strcmp (argv[optind], "-"))) + && !(numbers || squeeze_empty_lines || mark_line_ends))) || binary_files) { /* Switch stdout to BINARY mode. */ @@ -695,12 +703,6 @@ main (int argc, char **argv) But keep console output in text mode, so that LF causes both CR and LF on output, and the output is readable. */ file_open_mode |= O_BINARY; - SET_BINARY (0); - - /* Setting stdin to binary switches the console device to - raw I/O, which also affects stdout to console. Undo that. */ - if (isatty (STDOUT_FILENO)) - setmode (STDOUT_FILENO, O_TEXT); } #endif @@ -723,28 +725,25 @@ main (int argc, char **argv) #if O_BINARY /* Switch stdin to BINARY mode if needed. */ - if (binary_output) - { - int tty_in = isatty (input_desc); - /* If stdin is a terminal device, and it is the ONLY - input file (i.e. we didn't write anything to the - output yet), switch the output back to TEXT mode. - This is so "cat > xyzzy" creates a DOS-style text - file, like people expect. */ - if (tty_in && optind <= argc) - setmode (STDOUT_FILENO, O_TEXT); - else - { - SET_BINARY (input_desc); + int tty_in = isatty (input_desc); + + if ((binary_output && !tty_in) || quote) + { + SET_BINARY (input_desc); # ifdef __DJGPP__ - /* This is DJGPP-specific. By default, switching console - to binary mode disables SIGINT. But we want terminal - reads to be interruptible. */ - if (tty_in) - __djgpp_set_ctrl_c (1); + /* This is DJGPP-specific. By default, switching console + to binary mode disables SIGINT. But we want terminal + reads to be interruptible. */ + if (tty_in) + __djgpp_set_ctrl_c (1); +# endif +# if !(defined __CYGWIN__) + /* Setting stdin to binary switches the console device to + raw I/O, which also affects stdout to console. Undo that. */ + if (tty_in && isatty (STDOUT_FILENO)) + setmode (STDOUT_FILENO, O_TEXT); # endif - } } #endif }