Uzyskiwanie szerokości terminala w C?
Szukałem sposobu na uzyskanie szerokości terminala z poziomu mojego programu C. To, co wymyślam, to coś w rodzaju:
<code>#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); } </code>
Ale za każdym razem, gdy próbuję, dostaję
<code>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.) </code>
Czy jest to najlepszy sposób na to, czy jest lepszy sposób? Jeśli nie, jak mogę to zrobić?
EDYCJA: poprawiony kod to
<code>#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; } </code>