When we using the ls, when can see some output has different colors not the default black colors, so i believe the output should add some escape sequence to adjust the color of the terminal, but when using ls > ls.log, in the ls.log we can not see the escape sequence,so the program need to decide whether the output is a file or terminal if it is the terminal it will the using terminfo to print out the result otherwise it only print out the real result without the escape sequence! are there any APIs we can use to decide the things i mentioned before the STD out is terminal or normal files. if there are no APIs, what should we do to find the truth!
Asked
Active
Viewed 53 times
0
-
[man isatty](http://linux.die.net/man/3/isatty) – Paul R Sep 12 '14 at 09:46
-
1@PaulR Yes, it's a duplicate. Wasn't aware of this while answering. – hek2mgl Sep 12 '14 at 09:49
1 Answers
1
On Linux, programs are using the glibc function isatty()
to decide whether stdout is a terminal or not:
if(isatty(1)) {
printf("stdout is a terminal");
}
See man 3 isatty
.

hek2mgl
- 152,036
- 28
- 249
- 266