From 22a8f1164897132f22644864d179313f4e353502 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 10 Nov 2015 00:36:14 +0100 Subject: add ability to select clock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- DEPENDENCIES | 3 +++ Makefile | 11 +++++++++-- src/sleep-until.c | 28 +++++++++++++++++++++++----- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/DEPENDENCIES b/DEPENDENCIES index cad26b4..ff2cda9 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -8,6 +8,9 @@ BUILD DEPENDENCIES: libc (glibc>=2.8) cc make + coreutils + grep + gpp (https://github.com/maandree/gpp) texinfo (opt-out, for info, pdf, ps and dvi manuals) texlive-core (opt-in, for pdf, ps and dvi manuals) diff --git a/Makefile b/Makefile index d1ac213..1accbd7 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,10 @@ PKGNAME = sleep-until COMMAND = sleep-until +# The https://github.com/maandree/gpp command. +GPP = gpp + + WARN = -Wall -Wextra -pedantic -Wdouble-promotion -Wformat=2 -Winit-self -Wmissing-include-dirs \ -Wtrampolines -Wmissing-prototypes -Wmissing-declarations -Wnested-externs \ -Wno-variadic-macros -Wsync-nand -Wunsafe-loop-optimizations -Wcast-align \ @@ -62,10 +66,13 @@ bin/sleep-until: obj/sleep-until.o @mkdir -p bin $(CC) $(FLAGS) -o $@ $^ $(LDFLAGS) -obj/sleep-until.o: src/sleep-until.c - @mkdir -p obj +obj/sleep-until.o: obj/sleep-until.c $(CC) $(FLAGS) -c -o $@ $< $(CFLAGS) $(CPPFLAGS) +obj/sleep-until.c: src/sleep-until.c + @mkdir -p obj + gpp -s '$$' -i $< -o $@ + # Build rules for documentation. .PHONY: doc diff --git a/src/sleep-until.c b/src/sleep-until.c index dd2a055..176b24d 100644 --- a/src/sleep-until.c +++ b/src/sleep-until.c @@ -31,8 +31,9 @@ int main(int argc, char* argv[]) char float_part[10]; struct itimerspec value; struct itimerspec largest_value; - int i, fd = -1; + int i, fd = -1, clocks = 0; uint64_t _expirations; + int clockid = CLOCK_REALTIME; if (argc < 2) return 0; @@ -45,10 +46,24 @@ int main(int argc, char* argv[]) for (i = 0; i < argc; i++) { char* p1 = argv[i]; - char* p2 = strchr(p1, '.'); + char* p2; char excess; size_t len; - if (p2) + + if (strstr(p1, "CLOCK_") != p1) + goto parse_time; +$>cat /usr/include/bits/time.h | grep '^ *# *define *CLOCK_' | grep -Po 'CLOCK_[^[:blank:]]*' | +$>while read c; do + else if (!strcmp(p1, "${c}")) + clockid = ${c}; +$>done + else + clockid = -1; + clocks++; + continue; + + parse_time: + if ((p2 = strchr(p1, '.'))) *p2++ = '\0'; value.it_value.tv_sec = (time_t)atoll(p1); @@ -74,7 +89,10 @@ int main(int argc, char* argv[]) largest_value = value; } - fd = timerfd_create(CLOCK_REALTIME, 0); + if (clocks == argc) + return 0; + + fd = timerfd_create(clockid, 0); if (fd < 0) goto fail; if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &largest_value, NULL)) @@ -82,7 +100,7 @@ int main(int argc, char* argv[]) for (;;) { - if (clock_gettime(CLOCK_REALTIME, &(value.it_value))) + if (clock_gettime(clockid, &(value.it_value))) goto fail; if (value.it_value.tv_sec > largest_value.it_value.tv_sec) break; -- cgit v1.2.3-70-g09d2