isatty gives wrong result via ssh

Konstantin Kouptsov konstantin@kouptsov.com
Mon Nov 26 15:08:00 GMT 2012


Hi,

I have a program, which I reduced to the following short code, which is intended to be compiled both on Linux and Windows. It is run either locally, from a local terminal prompt, or remotely, through ssh. It should detect whether it runs in an interactive session (with a command line) or in a pipe, and behave differently.

--- checktty.c ---
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

int main()
{
	if(isatty(0))
		printf("tty\n");
	else
		printf("not a tty\n");
}
---------

When I compile it on Linux, and run from the local terminal, it performs as expected:

$ gcc -o checktty checktty.c
$ ./checktty
tty
$ ./checktty < checktty.c
not a tty

Same happens when I connect to the Linux computer via ssh either from another Linux computer or from Windows (via PuTTY or Cygwin ssh).


On Windows, if I compile it using a Microsoft compiler:

C: > cl /out:checktty.exe checktty.c

the program behaves correctly when I run it from a DOS prompt or from a Cywin's bash prompt. However, if I connect to the Windows computer running Cygwin's sshd service from another Linux or Windows computer, it always gives the same result:

$ ./checktty.exe
not a tty

$ ./checktty.exe < checktty.c
not a tty

(When I compile with Cygwin's gcc, everything is fine)

What happens here?

Given that I must compile the program using Microsoft's compiler on Windows, how this can be worked around?

Thanks,
Konstantin.

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list