Flash Colors on Terminal Window in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
int main()
{
        int i;
        struct winsize w;
        char *spaces;
        /* Get terminal window size: */
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        /* Allocate enough spaces to fill the whole
* whole window (rows * cols):*/
        spaces = (char *)malloc(w.ws_row * w.ws_col);
        memset(spaces, ' ', w.ws_row * w.ws_col);
        /* Change background color, then print
* rows * cols of white spaces: */
        for(i = 0; 1; i++) {
                printf("\e[7;%dm\n", 31 + i%6);
                write(1, spaces, w.ws_row * w.ws_col);
                usleep(1000 * 1000);
        }
        return 0;
}