UNIX & GNU/Linux - System calls - Using tgetnum()

The tgetnum() function is a Linux system call function. It is designed to be used with the getenv() and tgetent() functions.
If the description of the terminal is not found, most probably because there is no result in the tgetent() function, the tgetnum() one will return -1.

With the tgetnum() function, we have to put an only one string parameter. You have to use the two letters of the appropriate termcaps. You can find all of them in the Numeric capabilities of the termcap manual.

Let's see an example of the tgetnum() function with the "TERM" variable and its height and width:

** Made by BadproG.com                                                     

#include        <stdio.h>
#include        <curses.h>
#include        <term.h>
#include        <stdlib.h>

int     main()
  const char *name;
  char  *bp;
  char  *term;
  int   height;
  int   width;

  bp = malloc(sizeof(*bp));
  name = "TERM";
  if ((term = getenv(name)) == NULL)
    return (1);
  printf("My terminal is %s.\n", term);
  tgetent(bp, term);
  height = tgetnum("li");
  width = tgetnum("co");
  printf("H : %d\nL : %d\n", height, width);
  return (0);

Do not forget to use the -lncurses library when you compile. Note that you can use the -ltermcap library as well:

cc main.c -lncurses




Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.