|
Linux Forum Index » Linux Development - Applications » Where output is going and number of columns in xterm...
Page 1 of 1
|
| Author |
Message |
| Igor... |
Posted: Mon Aug 18, 2008 6:24 am |
|
|
|
Guest
|
Hello
Could anybody answer the following 2 questions:
If I run a program (written in C or C++) from shell running in xterm
window or similar other window,
how can my program determine whether its output goes to xterm screen
or it goes by redirection ">" to a file?
That is if a program just says "blu-blu", how can it determine from
itsef whether this "blu-blu" will appear
at screen or it will be redirected to a file?
So whether this is this case:
shell> executable_program_name
blu-blu
shell>
or that one:
shell> executable_program_name > file_name
shell>
And the next question (related to the first): how can program
determine the number of columns in currectly
opened xterm (or other similar terminal), if its output goes to
xterm?
Thanks in advance
Igor |
|
|
| Back to top |
|
| Igor... |
Posted: Mon Aug 18, 2008 7:02 am |
|
|
|
Guest
|
On Aug 18, 8:40 pm, j... at (no spam) toerring.de (Jens Thoms Toerring) wrote:
Quote: Igor <Igor.Smir... at (no spam) cern.ch> wrote:
It can use the isatty() function (declared in <unistd.h>) to
figure out what kind of output it's connected to (but that's
not 100% safe - if the caller uses a pseudo-terminal it looks
as if output goes to a terminal event though it really goes
to a pipe).
And the next question (related to the first): how can program
determine the number of columns in currectly
opened xterm (or other similar terminal), if its output goes to
xterm?
That's tricky. Your best bet is probably using the ncurses
library. How ncurses does it I never did care to find out...
Regards, Jens
Thanks a lot. Absolutely helpful and fast answer.
Since I have tried ncurses before writting my questions, I can publish
the recipe
with it (and already with your isatty):
#include <curses.h>
#include <term.h>
#include <unistd.h>
....
int is_a_term = isatty(STDOUT_FILENO);
if(is_a_term == 1)
{
initscr(); // initialization of ncurses
char id[10] = "co"; // command to output number of columns
int num = tgetnum(id); // output number of columns
endwin();
That's all. The problem was that in the case of rediretion to file
ncurses at the call of initscr() wrote to file some unreadable shit.
the condiion which I included according to your advice allows to avoid
this. So now the problem is solved
But still if somebody knows more "standard" way to determine the
number of columns
without link to this probably-too-large ncurses library, please let me
know.
Thanks
Igor |
|
|
| Back to top |
|
| Jens Thoms Toerring... |
Posted: Mon Aug 18, 2008 11:40 am |
|
|
|
Guest
|
Igor <Igor.Smirnov at (no spam) cern.ch> wrote:
Quote: If I run a program (written in C or C++) from shell running in xterm
window or similar other window,
how can my program determine whether its output goes to xterm screen
or it goes by redirection ">" to a file?
That is if a program just says "blu-blu", how can it determine from
itsef whether this "blu-blu" will appear
at screen or it will be redirected to a file?
So whether this is this case:
shell> executable_program_name
blu-blu
shell
or that one:
shell> executable_program_name > file_name
shell
It can use the isatty() function (declared in <unistd.h>) to
figure out what kind of output it's connected to (but that's
not 100% safe - if the caller uses a pseudo-terminal it looks
as if output goes to a terminal event though it really goes
to a pipe).
Quote: And the next question (related to the first): how can program
determine the number of columns in currectly
opened xterm (or other similar terminal), if its output goes to
xterm?
That's tricky. Your best bet is probably using the ncurses
library. How ncurses does it I never did care to find out...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt at (no spam) toerring.de
\__________________________ http://toerring.de |
|
|
| Back to top |
|
| Joe Pfeiffer... |
Posted: Mon Aug 18, 2008 1:00 pm |
|
|
|
Guest
|
jt at (no spam) toerring.de (Jens Thoms Toerring) writes:
Quote: Igor <Igor.Smirnov at (no spam) cern.ch> wrote:
And the next question (related to the first): how can program
determine the number of columns in currectly
opened xterm (or other similar terminal), if its output goes to
xterm?
That's tricky. Your best bet is probably using the ncurses
library. How ncurses does it I never did care to find out...
There is an ioctl that will give you the window geometry.
man tty_ioctl |
|
|
| Back to top |
|
| Igor... |
Posted: Tue Aug 19, 2008 3:09 am |
|
|
|
Guest
|
On Aug 18, 10:00 pm, Joe Pfeiffer <pfeif... at (no spam) cs.nmsu.edu> wrote:
Quote: j... at (no spam) toerring.de (Jens Thoms Toerring) writes:
There is an ioctl that will give you the window geometry.
man tty_ioctl
This works too. Thanks. |
|
|
| Back to top |
|
| Joe Beanfish... |
Posted: Tue Aug 19, 2008 12:29 pm |
|
|
|
Guest
|
Joe Pfeiffer wrote:
Quote: jt at (no spam) toerring.de (Jens Thoms Toerring) writes:
Igor <Igor.Smirnov at (no spam) cern.ch> wrote:
And the next question (related to the first): how can program
determine the number of columns in currectly
opened xterm (or other similar terminal), if its output goes to
xterm?
That's tricky. Your best bet is probably using the ncurses
library. How ncurses does it I never did care to find out...
There is an ioctl that will give you the window geometry.
man tty_ioctl
There's also a signal you can receive when the window size changes.
man signal
man -s 7 signal
SIGWINCH |
|
|
| Back to top |
|
| |