Clear() function in curses.h library
Question
I'm doing school assignments. Writing a simple command line interpreter. One of the functions is to clear screen. Its is called cmd_clr. For that, I'm trying to use clear() function from curses.h library. The problem is clear() returns -1 value, for some reason. Here is the code:
#include <stdio.h> /* Standard IO functions */
#include <string.h> /* String library, might be useful */
#include <dirent.h> /* Directory entry API */
#include <curses.h> /* Useful for screen control */
#include <unistd.h> /* Process management API */
/* Global constants */
#define true 1
#define false 0
/* Global variables, structures */
char pwd[512];
char shell[512];
void cmd_dir(char []);
void cmd_cd(char []);
void cmd_clr(void);
int main (void)
{
char prompt[512] = {":#"};
char command[512];
char temp[512];
char* token;
while (true)
{
/* Print command prompot, including PWD */
printf("%s%s ", pwd, prompt);
/* Get command input */
gets(command);
/* Isolate the command token */
strcpy(temp, command);
token = strtok (temp, " ");
void cmd_dir(char directory[])
{
printf("shell command: dir\n");
token = strtok(NULL, " "); //to get the directory
execlp("/bin/ls","ls", "-l", token, NULL);
}
void cmd_cd(char directory[])
{
printf("shell command: cd\n");
token = strtok(NULL, " "); //to get the directory
chdir(token);
system("pwd");//to print the current directory
}
void cmd_clr(void)
{
printf("shell command: clr\n");
int tv = clear();
printf("%d", tv);
}
if (strcmp(token, "dir") == 0)
{
cmd_dir(command);
}
else if (strcmp(token, "cd") == 0)
{
cmd_cd(command);
}
else if (strcmp(token, "clr") == 0)
{
cmd_clr();
}
}
}
The output is:
mekhron@ubuntu:~/folder4$ gcc -o test test.c -lncurses
mekhron@ubuntu:~/folder4$ ./test
:# clr
shell command: clr
-1:# ^C
mekhron@ubuntu:~/folder4$
Solution
The curses clear()
function, like most curses
functions, cannot be used without first calling initscr()
.
Judging by the rest of your code, you probably don't want to be using curses
or ncurses
anyway. curses
is designed to manage your entire screen. It's incompatible with the other I/O you're doing. The curses
clear()
function doesn't just clear the screen; it clears the curses
internal representation of the state of your screen. Your actual screen won't be cleared until you call refresh()
.
If you just want to clear the screen immediately, you should find another way to do it. The clear
command should do it; just call system("clear");
.
One more thing I need to point out: You're using the gets()
function. Don't. gets()
cannot be used safely; since it doesn't let you specify the size of the array into which you're reading, it cannot prevent a long input line from overflowing your array and clobbering other memory. The fgets()
function is a little more difficult to use (in particular, it stores the trailing '\n'
in your array), but it can be used safely.
And assuming your compiler supports it, you can drop the definitions of false
and true
and just add #include <stdbool.h>
to the top of your program.