delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/04/25/19:28:08

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-Id: <3.0.5.32.20030425192850.007f96b0@mail.attbi.com>
X-Sender: phumblet AT mail DOT attbi DOT com (Unverified)
Date: Fri, 25 Apr 2003 19:28:50 -0400
To: cygwin AT cygwin DOT com
From: "Pierre A. Humblet" <Pierre DOT Humblet AT ieee DOT org>
Subject: cat stories
Mime-Version: 1.0

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019