This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

cat stories


There have been recurrent questions about why "cat" 
sometimes introduces carriage returns (\r) in its output, 
e.g. http://cygwin.com/ml/cygwin/2003-04/msg01887.html

I looked up the code and found that cat's behavior depends on
- the input being both "stdin and a tty" or not
- stdout being a tty or not.
If no special switches are given, cat sets its Input/Output
as follows, where D = Default, B = Binary mode, T = Text mode.

 input: "stdin + tty"    else
stdout: 
notty       I: D         I: B
            O: T         O: B

tty         I: D         I: D
            O: D         O: D

The main surprise is that when the input is "stdin + tyy", the
non-tty output is always placed in text mode.
That's why "cat -" and "cat /dev/tty" differ. 

The code contains the following interesting comment:

  /* 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.  */

I don't belong to the people expecting that behavior!
I'd rather have the result depend on the default output mount mode. 

Also the implementation does not follow the comment.
It turns on text output whenever "stdin + tty" becomes an input.
Below xyz is a file containing "xyz\n". 

> cat xyz - xyz | od -c
abc
0000000   x   y   z  \n   a   b   c  \r  \n   x   y   z  \r  \n
0000016


The -B switch only has minor effects:
 input:  "stdin + tty"   else
stdout:
notty       I: D         I: B
            O: T         O: B

tty         I: D         I: B
            O: T         O: B

Pierre


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]