LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-02-2004, 12:26 PM   #1
reishin
LQ Newbie
 
Registered: Oct 2004
Posts: 2

Rep: Reputation: 0
linux C syscall to get number of columns of current terminal?


I've been trying to search for a way to get the number of columns of the current terminal in my C program but I can't seem to find a c function that would get me the number of columns. Does anyone know of a built in c function that would get me the number of columns in the current terminal window?
 
Old 11-02-2004, 12:59 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
The environment variable COLUMNS is defined at login by stty.
Code:
#include <stdlib.h>

int tty_cols(void)
{
   char *tmp=getenv("COLUMNS");
   if(tmp==NULL) return (-1);
   return(atoi(tmp));
}
 
Old 11-02-2004, 01:05 PM   #3
reishin
LQ Newbie
 
Registered: Oct 2004
Posts: 2

Original Poster
Rep: Reputation: 0
this way works if the COLUMNS enviroment variable is set but on certain linux distros the COLUMNS environment variable isn't set so I won't be able to get the number of columns that way.
 
Old 11-02-2004, 03:53 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Heavily inspired (not to say copied) by kev82's code in this thread:
http://www.linuxquestions.org/questi...threadid=81570

Code:
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>

int main()
{
    struct winsize ws;

    ioctl(1, TIOCGWINSZ, &ws);
    printf("Columns: %d\tRows: %d\n", ws.ws_col, ws.ws_row);
    return 0;
}

Last edited by Hko; 11-02-2004 at 03:54 PM.
 
Old 07-15-2009, 02:59 AM   #5
sd9
Member
 
Registered: Apr 2009
Posts: 53

Rep: Reputation: 15
Quote:
Originally Posted by Hko View Post
Heavily inspired (not to say copied) by kev82's code in this thread:
http://www.linuxquestions.org/questi...threadid=81570

Code:
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>

int main()
{
    struct winsize ws;

    ioctl(1, TIOCGWINSZ, &ws);
    printf("Columns: %d\tRows: %d\n", ws.ws_col, ws.ws_row);
    return 0;
}
Works like a charm! Love it! Thanks a lot!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is difference between syscall of linux and Unix sdamo Linux - General 1 06-12-2005 02:07 AM
Keyboard Number Lock not working and a terminal emualtion program needed Peter M-M DamnSmallLinux 2 03-17-2005 09:04 AM
Linux syscall 11 (execve) Genjix Programming 1 12-30-2004 12:38 AM
how to get Terminal to start in current folder woodywellhung Linux - Software 1 04-28-2004 10:13 AM
Lock the current terminal session? kipbenson4 Linux - Newbie 1 08-14-2003 10:04 AM


All times are GMT -5. The time now is 06:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration