brycen
asked on
Small test function, please run on multiple systems, is it portable? Terminal $COLUMNS
I'm building a grid of OS support for telling an application what the terminal width is. Could you run this small test C program, and report results on various platforms? Is there a universal way to write a C program, to correctly get the terminal width, that will work across all environments that have consoles (e.g. Windows, cygwin, unix, linux, os x, BeOS, etc.)?
/*
** Terminal size parameters test program.
** cc -l readline -o col_test col_test.c
** Run this, resize window, run again.
**
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
main()
{
struct winsize win;
int temp;
printf("$COLUMNS=%s\n",getenv("COLUMNS"));
if( temp = ioctl(STDOUT_FILENO, TIOCGWINSZ, (void *)&win) ) {
printf("TIOCGWINSZ failed %d\n",temp);
} else {
printf("TIOCGWINSZ win.ws_col=%d\n",win.ws_col);
}
readline("What OS are you running this on? ");
readline("What OS are you typing on? ");
readline("What terminal emulator are you using? ");
printf("$COLUMNS=%s\n",getenv("COLUMNS"));
if( temp = ioctl(STDOUT_FILENO, TIOCGWINSZ, (void *)&win) ) {
printf("TIOCGWINSZ failed %d\n",temp);
} else {
printf("TIOCGWINSZ win.ws_col=%d\n",win.ws_col);
}
}
ASKER
Linux SUSE 10.3 Result
-------------------------- --
temp> ./col_test
$COLUMNS=(null)
TIOCGWINSZ win.ws_col=80
What OS are you running this on? Linux SUSE 10.3
What OS are you typing on? Linux SUSE 10.3
What terminal emulator are you using? Xfce Terminal
$COLUMNS=80
TIOCGWINSZ win.ws_col=80
temp> echo $COLUMNS
80
Resizing the window DOES change all of the results above correctly.
--------------------------
temp> ./col_test
$COLUMNS=(null)
TIOCGWINSZ win.ws_col=80
What OS are you running this on? Linux SUSE 10.3
What OS are you typing on? Linux SUSE 10.3
What terminal emulator are you using? Xfce Terminal
$COLUMNS=80
TIOCGWINSZ win.ws_col=80
temp> echo $COLUMNS
80
Resizing the window DOES change all of the results above correctly.
ASKER
OS X
----------------------
it% ./col_test
$COLUMNS=(null)
TIOCGWINSZ win.ws_col=73
What OS are you running this on? OS X
What OS are you typing on? Linux SUSE 10.3
What terminal emulator are you using? os-cillation terminal
$COLUMNS=(null)
TIOCGWINSZ win.ws_col=73
Resizing the window DOES change all of the results above correctly.
----------------------
it% ./col_test
$COLUMNS=(null)
TIOCGWINSZ win.ws_col=73
What OS are you running this on? OS X
What OS are you typing on? Linux SUSE 10.3
What terminal emulator are you using? os-cillation terminal
$COLUMNS=(null)
TIOCGWINSZ win.ws_col=73
Resizing the window DOES change all of the results above correctly.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome. So TIOCGWINSZ win.ws_col is looking pretty good, in terms of tracking the terminal width.
Now it's up to you to dispense your points awared ;-)
ASKER
Is there a way to slide the question over to a Windows Zone, so I can get a answer from someone without cygwin? Is there a windows way to do this?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'll have to ask the windows question elsewhere then... it's got a console: printf works, which is all I'll do (just being careful not to exceed window width).
ASKER
-------------------
bryce> ./col_test
$COLUMNS=(null)
TIOCGWINSZ win.ws_col=72
What OS are you running this on? FreeBSD 4.10
What OS are you typing on? Linux SUSE 10.3
What terminal emulator are you using? os-cillation terminal
$COLUMNS=72
TIOCGWINSZ win.ws_col=72
bryce> echo $COLUMNS
COLUMNS: Undefined variable.
Resizing the window DOES change all of the results above correctly.