Search code examples
cterminaltermcap

Termcap "cl" command doesn't clear screen


I can't seem to get termcap's "cl" command to work, but the terminal escape code does.

For example:

#include <termcap.h>
#include <stdio.h>

int main()
{
    tputs(tgetstr("cl", NULL), 1, putchar);
}

This doesn't change the terminal. But when I run:

#include <stdio.h>

int main()
{
    printf("\e[2J");
}

or if I call echo `tput cl` The terminal is cleared.

Why does this happen? Shouldn't termcap give that same escape code?

EDIT: Fixed writing characters

EDIT2: It's because i didn't call tgetent() before calling tgetstr(). Thanks guys!


Solution

  • Before interrogating with tgetstr(), you need to find the description of the user's terminal with tgetent():

    #include <stdio.h>
    #include <stdlib.h>   // getenv
    #include <termcap.h>  // tgetent tgetstr
    
    int main(void)
    {
        char buf[1024];
        char *str;
    
        tgetent(buf, getenv("TERM"));
        str = tgetstr("cl", NULL);
        fputs(str, stdout);
        return 0;
    }
    

    Compile with -ltermcap