commit 86bf168eeae15a5a59464feb361cb3dc222eb168 Author: Emil Date: Sun Sep 24 04:39:25 2023 +0000 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9777346 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +clock +clock~ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5d2e605 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +#!/bin/make -f + +CFLAGS := -std=c99 -Os -s -Wall -Wextra -Wpedantic +CPPFLAGS := +INSTALL := /usr/local +PROGN := clock + +$(PROGN): clock.c + $(CC) $(CFLAGS) -pipe -o $@ $< + gzexe $@ + +clean: + -rm $(PROGN) $(PROGN)~ + +install: $(PROGN) + install -m 755 $< $(INSTALL)/bin/ + +.PHONY: clean install all diff --git a/README b/README new file mode 100644 index 0000000..bc7bd72 --- /dev/null +++ b/README @@ -0,0 +1,25 @@ +/-------------------------------\ +| Clock | +\-------------------------------/ + +It's just a clock. + +The executable is compressed, if you compile it and it fails to run, run `mv clock~ clock` and try again. + +Building: make or baked + + CPPFLAGS='... + PERIOD - Inteval which the clock will update by. + CLEAR - The control code for clearing the terminal. + CLEAR_DUMB - How it clears on a dumb terminal, 127 newlines is enough. + ' + + These definitions may be omitted without issue. + +Installing: make install INSTALL=/usr/local PROGN=clock + INSTALL - Installation location, should be somewhere within the PATH. + PROGN - A overridable program name, change it if you want to. + +Licensed under the public domain. + +This line makes the README as exactly as long as the code. diff --git a/clock.c b/clock.c new file mode 100644 index 0000000..270e806 --- /dev/null +++ b/clock.c @@ -0,0 +1,49 @@ +/* clock.c - Public Domain - It's a digital clock +EXEC: + cc -O1 -s -pipe -Wall -Wextra -Wpedantic $CPPFLAGS -o clock clock.c && gzexe clock +:STOP */ +#include +#include +#include + +#ifndef CLEAR +# define CLEAR "\033[2J" +#endif + +#ifndef CLEAR_DUMB +# define CLEAR_DUMB "\n" +#endif + +#ifndef PERIOD +# define PERIOD 5 +#endif + +char * hmstime(struct tm * tm) +{ + static char time[9]; + sprintf(time, "%.2d:%.2d:%.2d", + tm->tm_hour, tm->tm_min, tm->tm_sec); + return time; +} + +int +main(void) +{ + struct tm * tm; + time_t now; + int sec; + + puts("Type C-c to exit.\n"); + + for (;;) + { + time(&now); + tm = gmtime(&now); + /* Junk character may be printed, this is the time to get a new terminal. */ + printf(CLEAR " %s\n" CLEAR_DUMB, hmstime(tm)); + sec = tm->tm_sec % PERIOD; + sleep(!sec ? PERIOD : PERIOD - sec); + } + + return 0; +}