get terminal width and height


 
Thread Tools Search this Thread
Top Forums Programming get terminal width and height
# 1  
Old 10-13-2011
get terminal width and height

I was reading Advanced Programming in the Unix Environment and it says to use ioctl() to get the terminal's width and height. I wrote this program:

Code:
// print width and height

#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>

int main( int argc, char **argv ){
	struct winsize size;
	ioctl( 0, TIOCSWINSZ, (char *) &size );
	printf( "Rows: %u\nCols: %u\n", size.ws_row, size.ws_col );
	return 0;
}

When I ran it, the numbers it outputted were in the thousands, and when I started Vim up again, the terminal got overloaded. Everything turned into static and the activity monitor showed Vim occupying 1 GB of memory. Is there a way to get the right terminal dimensions and avoid this problem?

---------- Post updated at 12:23 PM ---------- Previous update was at 11:46 AM ----------

Never mind. Fixed the problem.
# 2  
Old 10-13-2011
How was the problem fixed, for the benefit of future struggling googlers?
# 3  
Old 10-13-2011
I changed TIOCSWINSZ to TIOCGWINSZ. When you use an S, it sets the terminal dimensions, and when you use a G, it gets the dimensions.

My problem was that I set the terminal dimensions to fields in an uninitialized structure, which caused my terminal program to overload as it tried to make an arbitrarily large window.
# 4  
Old 10-13-2011
Thank you for telling us the solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

How to know full width blade or half width blade?

HI, How do we figure out if the server is half blade server or full blade server? Anything we need to look at to know on this? thanks in advance (9 Replies)
Discussion started by: snchaudhari2
9 Replies

2. Shell Programming and Scripting

Print Terminal Output Exactly how it Appears in the Terminal to a New Text File

Hello All, I have a text file containing output from a command that contains lots of escape/control characters that when viewed using vi or view, looks like jibberish. But when viewed using the cat command the output is formatted properly. Is there any way to take the output from the cat... (7 Replies)
Discussion started by: mrm5102
7 Replies

3. Shell Programming and Scripting

Cannot get terminal application to launch with a graphical launcher when successful in terminal

I have been having an extremely annoying problem. For the record, I am relatively new at this. I've only been working with unix-based OS's for roughly two years, mostly Xubuntu and some Kali. I am pretty familiar with the BASH language, as that's the default shell for debian. Now, I've made this... (16 Replies)
Discussion started by: Huitzilopochtli
16 Replies

4. Shell Programming and Scripting

give all images same height

How do I resize all images in a directory to the same pixel height? (1 Reply)
Discussion started by: locoroco
1 Replies

5. Shell Programming and Scripting

Set terminal width inside a shell script

Hi all, I have a shell script which uses "mailx -H" to get the subject of a email in a Linux system. However, the subject is truncated, and I think it has something to do with the terminal width because it only returns the first 80 characters of each line. I have tried "stty columns"... (7 Replies)
Discussion started by: mezzo
7 Replies

6. Programming

terminal Width/height change

Hi, i am very new to unix/linux programming. for one of the application i have to change the Terminal width and height. i did try this if (ioctl (fd, TIOCGWINSZ, &win)) return; if (y && y >24) win.ws_row = y; else win.ws_row = 24; if (x && x>80)... (2 Replies)
Discussion started by: bgsunny
2 Replies

7. Programming

get terminal width and cursor position

I want to get the screen width and cursor positions. When I used curses, all the screen content was cleared. So Can I use curses to get the screen size without clearing anything in the window? Or is there any other alternative??? I can use only C or C++. (0 Replies)
Discussion started by: Sreejesh.S
0 Replies

8. UNIX for Advanced & Expert Users

return image height and width

Hi all. I need to be able to get image (gif/jpg) height and widths from the server so I can size pop-up windows to fit the images. Is there a built in utility that accomplishes this on Unix or Linux? (1 Reply)
Discussion started by: Jabba
1 Replies

9. UNIX for Advanced & Expert Users

connecting to unix through hyper terminal - as a dumb terminal

I just changed from windows NT to XP and I am no longer able to connect to my unix system. I used to use hyper terminal -- which acts as dumb terminal to my main frame unix system. I think one of the options used to be "direct to comX". This option isn't listed now. I use a serial port and the... (2 Replies)
Discussion started by: michelle
2 Replies
Login or Register to Ask a Question