문제
C 프로그램 내에서 터미널 너비를 얻는 방법을 찾고 있습니다. 내가 계속 생각해 낸 것은 다음과 같은 선을 따라야합니다.
#include <sys/ioctl.h>
#include <stdio.h>
int main (void)
{
struct ttysize ts;
ioctl(0, TIOCGSIZE, &ts);
printf ("lines %d\n", ts.ts_lines);
printf ("columns %d\n", ts.ts_cols);
}
그러나 내가 시도 할 때마다 나는 얻는다
austin@:~$ gcc test.c -o test
test.c: In function ‘main’:
test.c:6: error: storage size of ‘ts’ isn’t known
test.c:7: error: ‘TIOCGSIZE’ undeclared (first use in this function)
test.c:7: error: (Each undeclared identifier is reported only once
test.c:7: error: for each function it appears in.)
이것이 이것을하는 가장 좋은 방법입니까, 아니면 더 좋은 방법이 있습니까? 그렇지 않다면 어떻게이 일을 할 수 있습니까?
편집 : 고정 코드입니다
#include <sys/ioctl.h>
#include <stdio.h>
int main (void)
{
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
printf ("lines %d\n", w.ws_row);
printf ("columns %d\n", w.ws_col);
return 0;
}
해결책
사용을 고려 했습니까? getenv () ? 터미널 열과 라인을 포함하는 시스템의 환경 변수를 얻을 수 있습니다.
또는 방법을 사용하여, 커널이 터미널 크기로 보는 것을 보려면 (터미널이 크기를 조정할 경우 더 나은 경우) Tiocgwinsz를 사용해야합니다.
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
그리고 전체 코드 :
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
int main (int argc, char **argv)
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
printf ("lines %d\n", w.ws_row);
printf ("columns %d\n", w.ws_col);
return 0; // make sure your main returns int
}
다른 팁
이 예제는 긴 측면에서 약간 있지만 말단 치수를 감지하는 가장 휴대 방식이라고 생각합니다. 이것은 또한 크기 조정 이벤트를 처리합니다.
Tim과 Rlbond가 제안한 것처럼 NCURSES를 사용하고 있습니다. 읽기 환경 변수와 비교할 때 터미널 호환성이 크게 향상됩니다.
#include <ncurses.h>
#include <string.h>
#include <signal.h>
// SIGWINCH is called when the window is resized.
void handle_winch(int sig){
signal(SIGWINCH, SIG_IGN);
// Reinitialize the window to update data structures.
endwin();
initscr();
refresh();
clear();
char tmp[128];
sprintf(tmp, "%dx%d", COLS, LINES);
// Approximate the center
int x = COLS / 2 - strlen(tmp) / 2;
int y = LINES / 2 - 1;
mvaddstr(y, x, tmp);
refresh();
signal(SIGWINCH, handle_winch);
}
int main(int argc, char *argv[]){
initscr();
// COLS/LINES are now set
signal(SIGWINCH, handle_winch);
while(getch() != 27){
/* Nada */
}
endwin();
return(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <termcap.h>
#include <error.h>
static char termbuf[2048];
int main(void)
{
char *termtype = getenv("TERM");
if (tgetent(termbuf, termtype) < 0) {
error(EXIT_FAILURE, 0, "Could not access the termcap data base.\n");
}
int lines = tgetnum("li");
int columns = tgetnum("co");
printf("lines = %d; columns = %d.\n", lines, columns);
return 0;
}
컴파일해야합니다 -ltermcap
. TermCap을 사용하여 얻을 수있는 다른 유용한 정보가 많이 있습니다. 사용 설명서 용어 매뉴얼을 확인하십시오 info termcap
자세한 사항은.
NCURSE가 설치되어 있고 사용중인 경우 사용할 수 있습니다. getmaxyx()
터미널의 치수를 찾습니다.
당신이 Linux에 있다고 가정하면, 나는 당신이 사용하고 싶다고 생각합니다. ncurses 대신 도서관. 나는 당신이 가진 ttysize 물건이 stdlib에 있지 않다고 확신합니다.
따라서 여기서 답을 제안하는 것이 아니라 :
linux-pc:~/scratch$ echo $LINES
49
linux-pc:~/scratch$ printenv | grep LINES
linux-pc:~/scratch$
자, 그놈 터미널 크기를 조정하면 선과 열 변수가 다음을 따릅니다.
Gnome 터미널이 이러한 환경 변수 자체를 생성하는 것처럼 보입니까?
다음은 이미 제안 된 환경 변수에 대한 기능이 있습니다.
int lines = atoi(getenv("LINES"));
int columns = atoi(getenv("COLUMNS"));