Next | Unix Command Internals | 16 |
ls is a little tricky
When it runs, it makes an fstat call on its standard output
Among other things, this tells ls whether stdout is a file or a terminal
If it's a terminal, ls enables the column-formatting and color options
If not, it doesn't
In Perl, the code might look something like this:
@statinfo = stat STDOUT; if ( $statinfo[2] & 0020000 && ($statinfo[6] & 0xff) == 5) { print "Terminal\n" } else { print "Not a terminal\n" }
Or more likely, for portability reasons, you'd use isatty:
use POSIX 'isatty'; if (isatty(STDOUT)) { print "Terminal\n" } else { print "Not a terminal\n" }
Or even more likely, you'd use Perl's built-in -t operator
if (-t STDOUT) { print "Terminal\n" } else { print "Not a terminal\n" }
Internally, -t just uses isatty
Next | Copyright © 2003 M. J. Dominus |