You can just use the box()
function to draw a border around the edge of your window, you don't need to know the height and width to do it. Here's a simple example, with a white border on a blue background:
#include <stdlib.h>
#include <curses.h>
#define MAIN_WIN_COLOR 1
int main(void) {
/* Create and initialize window */
WINDOW * win;
if ( (win = initscr()) == NULL ) {
fputs("Could not initialize screen.", stderr);
exit(EXIT_FAILURE);
}
/* Initialize colors */
if ( start_color() == ERR || !has_colors() || !can_change_color() ) {
delwin(win);
endwin();
refresh();
fputs("Could not use colors.", stderr);
exit(EXIT_FAILURE);
}
init_pair(MAIN_WIN_COLOR, COLOR_WHITE, COLOR_BLUE);
wbkgd(win, COLOR_PAIR(MAIN_WIN_COLOR));
/* Draw box */
box(win, 0, 0);
wrefresh(win);
/* Wait for keypress before ending */
getch();
/* Clean up and exit */
delwin(win);
endwin();
refresh();
return EXIT_SUCCESS;
}
If you want to know the dimensions of the window anyway, you can use ioctl()
like so:
#include <sys/ioctl.h>
void get_term_size(unsigned short * height, unsigned short * width) {
struct winsize ws = {0, 0, 0, 0};
if ( ioctl(0, TIOCGWINSZ, &ws) < 0 ) {
exit(EXIT_FAILURE);
}
*height = ws.ws_row;
*width = ws.ws_col;
}