Add install target to makefile.
[show-time.git] / show-time.c
1
2 #include <stdio.h>
3 #include <sys/ioctl.h>
4 #include <time.h>
5 #include <string.h>
6 #include <unistd.h>
7
8 #define false 0
9 #define true (!false)
10 #define null 0
11
12 int getColumns() {
13         struct winsize terminalSize;
14         int error;
15
16         error = ioctl(1, TIOCGWINSZ, &terminalSize);
17
18         if (!error) {
19                 return terminalSize.ws_col;
20         }
21         return 80;
22 }
23
24 int main(int argc, char** argv) {
25         
26         struct tm *localTime;
27         time_t currentTime;
28         char buffer[80];
29
30         if (fork() != 0) {
31                 return 0;
32         }
33
34         while (true) {
35                 currentTime = time(null);
36                 localTime = localtime(&currentTime);
37                 strftime(buffer, 80, "%d.%m.%Y %H:%M:%S", localTime);
38                 printf("%c[s", 27); /* save cursor position */
39                 printf("%c[%d;%dH", 27, 1, (int) (getColumns() - strlen(buffer))); /* set cursor */
40                 printf("%s", buffer);
41                 printf("%c[u", 27); /* restore cursor position */
42                 fflush(stdout);
43                 sleep(1);
44         }
45
46 }
47