From 871b91b7c378b9936c1e84bc1fa9232ced959ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pankowski?= Date: Thu, 15 Oct 2026 23:56:01 +0200 Subject: [PATCH] stdout-clock: add option -p for prefix --- lupan-clock/stdout-clock.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lupan-clock/stdout-clock.c b/lupan-clock/stdout-clock.c index dd34ae4..13f36e2 100644 --- a/lupan-clock/stdout-clock.c +++ b/lupan-clock/stdout-clock.c @@ -1,27 +1,40 @@ #include #include +#include #include #include #include -void show(struct tm *tm) +void show(char *prefix, struct tm *tm) { char s[6]; strftime(s, sizeof(s), "%H:%M", tm); - printf("%s\n", s); + printf("%s%s\n", prefix, s); fflush(stdout); } -int main() { - int h, m, init = 60; +int main(int argc, char **argv) { + int h, m, init = 60, opt; time_t t; struct tm *tm; + char *prefix = ""; + + while ((opt = getopt(argc, argv, "p:")) != -1) { + switch (opt) { + case 'p': + prefix = optarg; + break; + default: + fprintf(stderr, "Usage: %s [-p PREFIX]\n", argv[0]); + exit(EXIT_FAILURE); + } + } while (1) { t = time(NULL); tm = localtime(&t); if (tm->tm_hour != h || tm->tm_min != m || init > 0) { - show(tm); + show(prefix, tm); h = tm->tm_hour; m = tm->tm_min; init = (init > 0) ? init - 1 : 0;