Have some improvements on Windows: isatty

187 views
Skip to first unread message

Mihai Nita

unread,
Jan 3, 2012, 6:21:44 PM1/3/12
to jansi-dev

It looks like org.fusesource.jansi.internal.CLibrary.HAVE_ISATTY is
false on Windows.
But there is an isatty, only that it is called _isatty.

It is in io.h (see http://msdn.microsoft.com/en-us/library/f4s0ddew%28v=VS.100%29.aspx)

This is something I have tried in my code, but I would rather not add
my own DLL to the mix :-)

Hiram Chirino

unread,
Jan 4, 2012, 5:01:41 AM1/4/12
to jans...@googlegroups.com
Awesome.

Perhaps you can update https://github.com/fusesource/jansi-native to
use that _isatty :)

Regards,
Hiram

FuseSource
Web: http://fusesource.com/

Mihai Nita

unread,
Jan 4, 2012, 6:16:36 PM1/4/12
to jansi-dev
Ok, here are my changes
(sorry, I am still trying to figure out how maven works, not too
familiar with it :-)

=====================
====== jansi.h ======
=====================

=== In the #if defined(_WIN32) || defined(_WIN64) section added this:

#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
#define HAVE_ISATTY

#include <io.h>

Got the numbers with the following code:
printf("#define STDIN_FILENO %d\n", fileno(stdin) );
printf("#define STDOUT_FILENO %d\n", fileno(stdout) );
printf("#define STDERR_FILENO %d\n", fileno(stderr) );

These numbers have been the same since forever (since DOS times).
So they are probably safe to hardcode.
(currently jansi returns 0 for all of them)

=====================
====== jansi.c ======
=====================

=== In CLibrary_NATIVE(init) changed
#if defined(STDIN_FILENO)
(*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env,
that, "STDOUT_FILENO", "I"), (jint)STDOUT_FILENO);
#endif
#if defined(STDIN_FILENO)
(*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env,
that, "STDERR_FILENO", "I"), (jint)STDERR_FILENO);
#endif
to
#if defined(STDOUT_FILENO)
(*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env,
that, "STDOUT_FILENO", "I"), (jint)STDOUT_FILENO);
#endif
#if defined(STDERR_FILENO)
(*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env,
that, "STDERR_FILENO", "I"), (jint)STDERR_FILENO);
#endif


=== In CLibrary_NATIVE(isatty) changed
rc = (jint)isatty(arg0);
to
rc = (jint)(isatty(arg0) ? 1 : 0);

Because Windows returns 0 and 64 instead of 0 and 1:

"_isatty returns a nonzero value if the descriptor is associated with
a character device. Otherwise, _isatty returns 0."
http://msdn.microsoft.com/en-us/library/f4s0ddew%28v=vs.80%29.aspx

"The isatty() function shall return 1 if fildes is associated with a
terminal; otherwise, it shall return 0 and may set errno to indicate
the error."
http://pubs.opengroup.org/onlinepubs/007904875/functions/isatty.html
"isatty() returns 1 if fd is an open file descriptor referring to a
terminal; otherwise 0 is returned, and errno is set to indicate the
error."
http://linux.die.net/man/3/isatty

==========================================

This is the diff, if you preffer that:
====
diff original/src/jansi.c updated/src/jansi.c
29c29
< #if defined(STDIN_FILENO)
---
> #if defined(STDOUT_FILENO)
32c32
< #if defined(STDIN_FILENO)
---
> #if defined(STDERR_FILENO)
46c46
< rc = (jint)isatty(arg0);
---
> rc = (jint)(isatty(arg0) ? 1 : 0);
diff original/src/jansi.h updated/src/jansi.h
27a28,33
>
> #define STDIN_FILENO 0
> #define STDOUT_FILENO 1
> #define STDERR_FILENO 2
> #define HAVE_ISATTY
>
29a36
> #include <io.h>
====
Reply all
Reply to author
Forward
0 new messages