Articles
Software
Gallery
Other works
Contacts

#Getting the TTY size in C without ncurses.html



        >*NIX only
        >it is baffling how everyone only ever says "uh oh use ncurses or something"
        {@begin=c@
            // @BAKE gcc -o $*.out $@
            #include 
            #include 
            #include 

            int get_tty_size(int * width_out, int * height_out) {
                struct winsize ws;

                int fd = open("/dev/tty", O_WRONLY);
                if (fd == -1) { return 1; }

                ioctl(fd, TIOCGWINSZ, &ws);
                close(fd);

                *width_out  = ws.ws_col;
                *height_out = ws.ws_row;

                return 0;
            }

            #include 

            signed main(void) {
                int width, height;
                get_tty_size(&width, &height);

                printf("Your terminal size is %dx%d\n", width, height);

                return 0;
            }
        @end=c@}