我正在使用 C 来获取终端大小。该函数将在主函数中调用。然后我希望再次运行它以检查终端大小是否已更改或保持不变。这次该函数在其他函数 run_menu 中调用。有关其他信息,运行菜单也在主函数中调用。我将在代码中进行更多解释。错误是“函数'get_terminal_size'的参数太少。
//this function is to get the terminal size
//my idea is to use pointer as it will be use again in other function
void get_terminal_size(int *x, int* y)
{
int cols,lines;
assert(x);
assert(y);
#ifdef TIOCGSIZE
ioctl(0,TIOCGSIZE, &ts);
lines = ts.ts_lines;
cols = ts.ts_cols;
#elif defined(TIOCGWINSZ)
struct winsize ts;
ioctl(0,TIOCGWINSZ, &ts);
lines = ts.ws_row;
cols = ts.ws_cols;
#endif
*x = cols;
*y = lines;
}
//this function is to see either the size has changed or not
int change_terminal_size()
{
int new_cols, new_lines;
int x, y;
get_terminal_size(&x, &y);
#ifdef TIOCGSIZE
ioctl(0,TIOCGSIZE, &ts);
new_lines = ts.ts_lines;
new_cols = ts.ts_cols;
#elif defined(TIOCGWINSZ)
struct winsize ts;
ioctl(0,TIOCGWINSZ, &ts);
new_lines = ts.ws_row;
new_cols = ts.ws_cols;
#endif
log_debug("new lines=%d,new cols =%d",new_lines,new_cols);
if((new_cols !=x)||(new_lines != y)){
return 1;
}
return 0;
}
//this function is to run the menu.
static void run_menu()
{
//bla bla bla with other declaration and function
//i will not write it because its not related
while(1){
if (change_terminal_size()){
log_debug("the terminal has change size");
}
//and it will continue with other things
//this is the main function
int main()
{
//again with other declaration and function not related
get_terminal_size();
run_menu();
//continue on with other thing, then return 0
}
如你看到的。我两次调用“get_terminal_size”函数。它与我遇到的问题有关吗?据我所知,如果我使用的是指针,那么它应该没有任何问题。