From: David ‘Bombe’ Roden Date: Fri, 17 Jun 2011 07:27:18 +0000 (+0200) Subject: First version. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=85524745e24cdf1ea20ddc927855f6a120c30218;p=show-time.git First version. --- 85524745e24cdf1ea20ddc927855f6a120c30218 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1fd9fb2 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ + +show-time: show-time.o + $(CC) -o $@ $< diff --git a/show-time.c b/show-time.c new file mode 100644 index 0000000..4e4369f --- /dev/null +++ b/show-time.c @@ -0,0 +1,47 @@ + +#include +#include +#include +#include +#include + +#define false 0 +#define true (!false) +#define null 0 + +int getColumns() { + struct winsize terminalSize; + int error; + + error = ioctl(1, TIOCGWINSZ, &terminalSize); + + if (!error) { + return terminalSize.ws_col; + } + return 80; +} + +int main(int argc, char** argv) { + + struct tm *localTime; + time_t currentTime; + char buffer[80]; + + if (fork() != 0) { + return 0; + } + + while (true) { + currentTime = time(null); + localTime = localtime(¤tTime); + strftime(buffer, 80, "%d.%m.%Y %H:%M:%S", localTime); + printf("%c[s", 27); /* save cursor position */ + printf("%c[%d;%dH", 27, 1, (int) (getColumns() - strlen(buffer))); /* set cursor */ + printf("%s", buffer); + printf("%c[u", 27); /* restore cursor position */ + fflush(stdout); + sleep(1); + } + +} +