From c8c692f4ba3cd6a1103827b9a31eba4d63921223 Mon Sep 17 00:00:00 2001 From: mglae Date: Sat, 26 Dec 2020 19:11:12 +0100 Subject: [PATCH] wait-time-sync: add optional -t/--timeout parameter --- .../wait-time-sync/sources/wait-time-sync.c | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/sysutils/wait-time-sync/sources/wait-time-sync.c b/packages/sysutils/wait-time-sync/sources/wait-time-sync.c index 52cd5d780a..da7cff182d 100644 --- a/packages/sysutils/wait-time-sync/sources/wait-time-sync.c +++ b/packages/sysutils/wait-time-sync/sources/wait-time-sync.c @@ -2,14 +2,43 @@ /* Copyright (C) 2020-present Team LibreELEC (https://libreelec.tv) */ #include +#include +#include #include +#include #include +#include -int main() +void usage(char *name) { - int rc; + if (!name) + name = "wait-time-sync"; - for (;;) + printf("Usage: %s [-t seconds | --timeout seconds]\n", name); + exit(2); +} + +int main(int argc, char** argv) +{ + unsigned timeout = UINT_MAX; + int rc = 0; + + if (argc == 3) + { + unsigned long val; + char *p; + + if (strcmp(argv[1], "-t") && strcmp(argv[1], "--timeout")) + usage(argv[0]); + val = strtoul(argv[2], &p, 0); + if (*p || val == 0 || val >= UINT_MAX/3) + usage(argv[0]); + timeout = (unsigned)val * 3; + } + else if (argc != 1) + usage(argv[0]); + + for ( ; timeout; --timeout) { struct timex tx = {}; @@ -19,5 +48,5 @@ int main() usleep(1000000U/3); } - return rc == -1 ? errno : 0; + return rc == -1 ? errno : !timeout; }