By default certain programs format their output according to the type of the stream they write to. For example, the output of ls
and ls > file
looks differently. I'd like to know how this is achieved by a program. Additionally, is there a way by which we can trick such programs as if the output stream is a terminal where it actually is a file (especially when they don't have any options that affect output formatting)?
Asked
Active
Viewed 1,474 times
4

Jeenu
- 2,109
- 2
- 20
- 27
2 Answers
5
Via isatty
:
if (!isatty(fileno(stdout))
{
// redirected to a file or piped to a process
}
One way to trick is instead of doing redirect, start script
. Now everything that goes to the 'tty' (including what you type into stdin and what is sent to output) is sent to a file called typescript.

R Samuel Klatchko
- 74,869
- 16
- 134
- 187
-
Better check stdout, not stdin. – ThiefMaster May 20 '10 at 06:32
2
Those programs use isatty(fileno(stdout))
to check if they are writing to a TTY (terminal) or something else (e.g. a pipe).
About faking a tty, check Trick an application into thinking its stdin is interactive, not a pipe

Community
- 1
- 1

ThiefMaster
- 310,957
- 84
- 592
- 636