1

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!

1ShotSniper
  • 63
  • 1
  • 7
  • The tgetstr() function returns NULL if the capability was not found. Question now is - why can't it find it... – favoretti Aug 10 '18 at 12:56
  • According to the GNU manual for termcap, the `cl` capability is available on all systems. Hmm... – 1ShotSniper Aug 10 '18 at 13:29
  • Your `ft_puts` function is super broken, and you didn't call `setupterm`. This can't work. – melpomene Aug 10 '18 at 13:35
  • ... actually, let me amend that. Your `ft_puts` might work on little-endian platforms. – melpomene Aug 10 '18 at 13:36
  • To expand on melpomene's point, your function takes an `int` which is at least two bytes and writes one byte of it. What is in that byte? Who knows! – Zan Lynx Aug 11 '18 at 06:48

1 Answers1

3

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

David Ranieri
  • 39,972
  • 7
  • 52
  • 94