Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Testing if a file is a terminal?



On Tue, Dec 9, 2008 at 12:06 PM, Stephane Chazelas
<Stephane_Chazelas@xxxxxxxx> wrote:
> On Tue, Dec 09, 2008 at 11:47:51AM -0500, Rocky Bernstein wrote:
>> First, many thanks to Peter and Stéphane for the very informative
>> answers on terminal redirection.
>>
>> Before allowing a terminal to be set, the zshdb debugger uses this test:
>>
>>     [[ -r "$1" && -w "$1" ]] && return 0 # Ok
>>
>> $1 is a string --  the device name (e.g. /dev/pts/1) -- not a file descriptor.
>>
>> But is this really the best one can do? I suppose on could also add a
>> test if the device is a character device but I just wonder if there
>> isn't a better way.
>>
>> isatty()  works on a file descriptor, but we don't have that yet. I
>> suppose one could try to open that and then test but then care needs
>> to be made to use that opened file descriptor and not say close and
>> reopen that.
>>
>> Any other thoughts or suggestions?
> [...]
>
> You could compare the device major and type to that of $TTY
> (using zstat).

Ok - that seems like a reasonable thing to do.


> [ -t <fd> ] tests whether a fd is a terminal (in
> any shell, not only zsh).
>
> But why would you want to check for that?

Just because it is a reliable way to test for terminal-ness. So if I
can open a tty, see if that works, and if so get the associated file
descriptor for that then it would be a way to see if the device name
is a terminal.

But of course this is a bit involved and I'd only contemplate that if
there were nothing better.

>
> --
> Stéphane
>



Messages sorted by: Reverse Date, Date, Thread, Author