Scripting question: the width of the terminal window
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Thanks! But this is odd... I can echo the variables from the command line and get the proper results, but in my script(s) the $LINES and $COLUMNS are empty... This quick and silly test gives nothing for me:
Well, well, I haven't done very much with this, but since yesterday I have been looking into kev82's "badly written c program" I have now two very short programs that give me the terminal width and height, respectively. It would be nice if someone could have a look and comment. I had some big problems with finding out how to treat STDIN_FILENO, but it seems to be enough just to put a 0 there. Is this the way to do it? It works, but that doesn't necessarily mean its right, right?
OK, here is the one that gives me the terminal width, i.e. the number of columns:
I had some big problems with finding out how to treat STDIN_FILENO, but it seems to be enough just to put a 0 there. Is this the way to do it?
It actually is (a little) better to use STDIN_FILENO instead of 0. Suppuse you compile it on a system where stdin is not on filedescriptor 0, but say 5, then the system will #define STDIN_FILENO as being 5. Well, in this case of stdin, this is very unlikely to happen, so it's a non-issue. But making a habit of using symbols #defined by the system header files is a good thing in general as there are many examples where it would cause a problem when compiled on another system, or when some library/kernel-headers are update (with possibly changed #defined symbols).
STDIN_FILENO is #defined in unistd.h, so you need to include that header. Also, it's better to #include stdio.h when you use printf() in the .c file. When you compile your program with all warnings turned on you get a warning about this:
gcc -Wall -pedantic -o winsize winsize.c
winsize.c: In function `main':
winsize.c:9: warning: implicit declaration of function `printf'
So, actually there's nothing really wrong with your program, but as I understand you are learning C, and are asking for comment, I figured you might wanted to know.