Link to home
Start Free TrialLog in
Avatar of brycen
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);
        }
}

Open in new window

Avatar of brycen
brycen

ASKER

FreeBSD Result
-------------------
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.
Avatar of brycen

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.
Avatar of brycen

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.
SOLUTION
Avatar of florin_ganea
florin_ganea
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Hanno P.S.
Hanno P.S.
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brycen

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 ;-)
Avatar of brycen

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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brycen

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).