Type of fake output

By default, certain programs format their output according to the type of stream they write. For example, the output of ls and ls > file looks different. I would like to know how this is achieved by the program. Besides, is there a way by which we can trick such programs as if the output stream was a terminal, where it is actually a file (especially when they do not have any parameters affecting the formatting of the output)?

+4
source share
2 answers

Via isatty :

 if (!isatty(fileno(stdout)) { // redirected to a file or piped to a process } 

One way to cheat is, instead of doing a redirect, run a script . Now everything that goes into 'tty' (including what you type in stdin and what is sent to the output) is sent to a file called typescript.

+5
source

These programs use isatty(fileno(stdout)) to check if they are writing TTY (terminal) or something else (like a channel).

About tty fake, check fool the application into thinking that its stdin is interactive, not a protocol

+2
source

Source: https://habr.com/ru/post/1310331/


All Articles