Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / September 2004



Tip: Looking for answers? Try searching our database.

shell variable COLUMNS

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jøhnny Fävòrítê (it means "halo, then resonate") - 26 Sep 2004 02:43 GMT
i've noticed that the shell variable COLUMNS get set correctly in the
macosx shell.  if i set a terminal window to 120 columns, then type
"echo $COLUMNS" at the prompt, it will print 120, just as i'd expect.

so now i'd like to make use of that value in my command-line programs,
so i can know how many columns of output to print before word-wrapping.
alas, getenv("COLUMNS") returns a null pointer, and so does
getenv("columns").  i know i'm using the function right, because
getenv("PATH") returns the string i'd expect.  what the heck?  i fooled
around a little, and the environment passed to a command-line program
seems to be much reduced from the variables available to an interactive
shell.

does anybody know how i can get the COLUMNS value associated with the
shell that a command-line program is running in?
Jøhnny Fävòrítê (it means "halo, then resonate") - 26 Sep 2004 04:24 GMT
> does anybody know how i can get the COLUMNS value associated with the
> shell that a command-line program is running in?

answered my own question, via a half-hour of googling.  man, how did
anybody *ever* program before the web?  this code will do it:

 winsize  size;
 int      stat;

 memset(&size, 0, sizeof(size));
 stat = ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);

 if (stat == 0)
   {
     // make use of size.ws_col, size.ws_row, etc.
   }

the ioctl() call will fail if your program isn't connected to a live
terminal, which can happen if it's run by being double-clicked in a
finder window for instance, so testing 'stat' before using the 'size'
struct is important.  you'll have to include <sys/ioctl.h> and possibly
other headers.
Mike Hall - 26 Sep 2004 06:43 GMT
Jøhnny Fävòrítê it means halo, then resonate"  wrote:

>J¿hnny FŠv˜r’t (it means "halo, then resonate") wrote:
>> does anybody know how i can get the COLUMNS value associated with the
>> shell that a command-line program is running in?

Yes, just 'export' it, so child processes can see it.

$ echo $COLUMNS
80

$ set  |  grep COL
COLUMNS=80

$ env | grep COL
$

The shell variable is 'set' for the current shell, but it's not in the exported
environment available to the children.  'export COLUMNS' (or whatever
for csh, tcsh), and the children will see it, and 'getenv' will be happy to
give you the value.

Or, if you really like 'ioctl's, that's the other way to go.
Can also use 'signal' to catch SIGWINCH signals.
Jøhnny Fävòrítê (it means "halo, then resonate") - 26 Sep 2004 10:07 GMT
> Yes, just 'export' it, so child processes can see it.

that would require everybody who uses the program to have a special bash
start-up script though, right?  yucko.  at least i know it's possible to
do it that way, though.

> Can also use 'signal' to catch SIGWINCH signals.

do those signals fire whenever the terminal size changes?  that would be
handy.
Miro Jurisic - 26 Sep 2004 17:19 GMT
> > Can also use 'signal' to catch SIGWINCH signals.
>
> do those signals fire whenever the terminal size changes?  that would be
> handy.

Yes.

meeroh

Signature

If this message helped you, consider buying an item
from my wish list: <http://web.meeroh.org/wishlist>

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.